-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.py
59 lines (51 loc) · 1.99 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/core/actions/#custom-actions/
# This is a simple example for a custom action which utters "Hello World!"
# from typing import Any, Text, Dict, List
#
# from rasa_sdk import Action, Tracker
# from rasa_sdk.executor import CollectingDispatcher
#
#
# class ActionHelloWorld(Action):
#
# def name(self) -> Text:
# return "action_hello_world"
#
# def run(self, dispatcher: CollectingDispatcher,
# tracker: Tracker,
# domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#
# dispatcher.utter_message("Hello World!")
#
# return []
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from rasa_core_sdk import Action
from rasa_core_sdk.events import SlotSet
import requests
class ActionWeather(Action):
def name(self):
return 'action_weather'
def run(self, dispatcher, tracker, domain):
#weatherstack api key
api_key = 'baec0948c69bdfe824247df8b2f8632f'
print(api_key)
loc = tracker.get_slot('location')
print(loc)
api_address='http://api.weatherstack.com/current?access_key={}&query={}'.format(api_key,loc) #for json data
current = requests.get(api_address).json()
print(current)
country = current['location']['country']
city = current['location']['name']
#condition = current['current']['condition']['text']
temperature_c = current['current']['temperature']
humidity = current['current']['humidity']
wind_mph = current['current']['wind_speed']
response = """The temperature in {} is {} degrees, the humidity is {}% and the wind speed is {} mph.""".format(city, temperature_c, humidity, wind_mph)
dispatcher.utter_message(response)
return [SlotSet('location',loc)]