zulip_bots: Check API key on initialization for Weather bot.
This commit is contained in:
parent
524804fd2c
commit
9e73ddd292
|
@ -1,6 +1,7 @@
|
|||
# See readme.md for instructions on running this code.
|
||||
import requests
|
||||
import json
|
||||
import logging
|
||||
|
||||
from typing import Any, Dict
|
||||
|
||||
|
@ -8,6 +9,17 @@ class WeatherHandler(object):
|
|||
def initialize(self, bot_handler: Any) -> None:
|
||||
self.api_key = bot_handler.get_config_info('weather')['key']
|
||||
self.response_pattern = 'Weather in {}, {}:\n{:.2f} F / {:.2f} C\n{}'
|
||||
self.check_api_key()
|
||||
|
||||
def check_api_key(self) -> None:
|
||||
url = 'http://api.openweathermap.org/data/2.5/weather?q=nyc&APPID=' + self.api_key
|
||||
test_response = requests.get(url)
|
||||
try:
|
||||
test_response_data = test_response.json()
|
||||
if test_response_data['cod'] == 401:
|
||||
logging.error('API Key not valid. Please see doc.md to find out how to get it.')
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def usage(self) -> str:
|
||||
return '''
|
||||
|
|
Loading…
Reference in a new issue