diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 707038a2..ff1d9972 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -4,5 +4,6 @@ 'help', 'joke', 'movie', - 'request' + 'request', + 'weather' ] diff --git a/modules/src/weather.py b/modules/src/weather.py new file mode 100644 index 00000000..66294f4f --- /dev/null +++ b/modules/src/weather.py @@ -0,0 +1,20 @@ +import re +import config +import os +import requests + +OPEN_WEATHER_MAP_ACCESS_TOKEN = os.environ.get('OPEN_WEATHER_MAP_ACCESS_TOKEN', config.OPEN_WEATHER_MAP_ACCESS_TOKEN) +def process(input, entities=None): + output = {} + + try: + location = entities['location'][0]['value'] + r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=' + OPEN_WEATHER_MAP_ACCESS_TOKEN) + data = r.json() + output['input'] = input + output['output'] = 'Current weather: ' + data['main']['temp'] + data['weather'][0]['description'] + \ + " information provided by OpenWeatherMap" + output['success'] = True + except: + output['success'] = False + return output \ No newline at end of file diff --git a/modules/tests/test_weather.py b/modules/tests/test_weather.py new file mode 100644 index 00000000..8fd8ffbd --- /dev/null +++ b/modules/tests/test_weather.py @@ -0,0 +1,7 @@ +import modules + +def test_weather(): + assert('weather' == modules.process_query('tell me the weather in London')[0]) + assert('weather' == modules.process_query('weather London')[0]) + assert('weather' == modules.process_query('whats the weather in London')[0]) + assert('weather' != modules.process_query('something random')[0])