Created
August 8, 2018 18:37
-
-
Save MeetMartin/2e39cd0efdfcd14c279919a34ed58aa6 to your computer and use it in GitHub Desktop.
Weather Python action script for Rasa Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
from rasa_core.actions.action import Action | |
# https://pypi.org/project/weather-api/ | |
from weather import Weather, Unit | |
class ActionGetWeather(Action): | |
def name(self): | |
return 'action_get_weather' | |
def run(self, dispatcher, tracker, domain): | |
weather = Weather(unit=Unit.CELSIUS) | |
gpe = ('Auckland', tracker.get_slot('GPE'))[bool(tracker.get_slot('GPE'))] | |
result = weather.lookup_by_location(gpe) | |
if result: | |
condition = result.condition | |
city = result.location.city | |
country = result.location.country | |
dispatcher.utter_message('It\'s ' + condition.text + ' and ' + condition.temp + '°C in ' + | |
city + ', ' + country + '.') | |
else: | |
dispatcher.utter_message('We did not find any weather information for ' + gpe + '. Search by a city name.') | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment