diff --git a/zulip_bots/zulip_bots/bots/weather/weather.py b/zulip_bots/zulip_bots/bots/weather/weather.py index af58642..4151d8c 100644 --- a/zulip_bots/zulip_bots/bots/weather/weather.py +++ b/zulip_bots/zulip_bots/bots/weather/weather.py @@ -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 '''