-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
295 lines (220 loc) · 8.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# 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
import logging
from datetime import datetime
from typing import Text, Dict, Any, List
import json
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
from rasa_sdk.events import SlotSet, UserUtteranceReverted, ConversationPaused
import pymongo
#
#
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
api_link = "mongodb://localhost:27017/"
myclient = pymongo.MongoClient(api_link)
mydb = myclient["mydatabase"]
mycol1 = mydb["business"]
mycol2 = mydb["customer"]
class ActionInfoCity(Action):
def name(self) -> Text:
return "action_info_city"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#myclient = pymongo.MongoClient("mongodb://localhost:27017/")
#mydb = myclient["mydatabase"]
#mycol1 = mydb["business"]
#mycol2 = mydb["customer"]
myquery = { "city": { "$gt": "C" } } #Calgary,Charlotte
mydoc = mycol1.find(myquery)
temp = []
for x in mydoc:
temp.append(x)
city = []
for x in temp:
city.append(x['city'])
data = ','.join(list(set(city)))
dispatcher.utter_message("We are currently giving services in "+ data)
return
class ActionInfoCharlotte(Action):
def name(self) -> Text:
return "action_info_charlotte"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Charlotte", "name": "Queen City Plumbing"}
mydoc = mycol1.find(myquery)
#print(mydoc)
data = mydoc[0]['name']
dispatcher.utter_message("We are giving service in City Charlotte is Hotel " + str(data))
class ActionInfoCalgary(Action):
def name(self) -> Text:
return "action_info_calgary"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Calgary", "name": "Calgary Cycle"}
mydoc = mycol1.find(myquery)
data = mydoc[0]['name']
#print(data)
dispatcher.utter_message("We are giving service in City Calgary is Hotel "+ str(data))
class ActionTimeCharlotte(Action):
def name(self) -> Text:
return "action_time_charlotte"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Charlotte", "name": "Queen City Plumbing"}
mydoc = mycol1.find(myquery)
temp = []
for x in mydoc:
temp.append(x)
tre = temp[0]['hours']['Monday']
data = tre
dispatcher.utter_message("The working hours of Hotel Queen City Plumbing is :" + data)
class ActionTimeCalgary(Action):
def name(self) -> Text:
return "action_time_calgary"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Calgary", "name": "Calgary Cycle"}
mydoc = mycol1.find(myquery)
temp = []
for x in mydoc:
temp.append(x)
tre = temp[0]['hours']['Monday']
data = tre
dispatcher.utter_message("The working hours of Hotel Calgary Cycle is :" + data)
class ActionAddressCalgary(Action):
def name(self) -> Text:
return "action_address_calgary"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Calgary", "name": "Calgary Cycle"}
mydoc = mycol1.find(myquery)
temp = []
for x in mydoc:
temp.append(x)
tre = temp[0]['address']
data = tre
dispatcher.utter_message("The Address of Hotel Calgary Cycle is :" + data)
class ActionAddressCharlotte(Action):
def name(self) -> Text:
return "action_address_charlotte"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
myquery = {"city":"Charlotte", "name": "Queen City Plumbing"}
mydoc = mycol1.find(myquery)
temp = []
for x in mydoc:
temp.append(x)
tre = temp[0]['address']
data = tre
dispatcher.utter_message("The Address of Hotel Queen City Plumbing is :" + data)
class ActionChitchat(Action):
"""Returns the chitchat utterance dependent on the intent"""
def name(self):
return "action_chitchat"
def run(self, dispatcher, tracker, domain):
intent = tracker.latest_message["intent"].get("name")
# retrieve the correct chitchat utterance dependent on the intent
if intent in [
"ask_builder",
"ask_weather",
"ask_howdoing",
"ask_whatspossible",
"ask_whatisrasa",
"ask_isbot",
"ask_howold",
"ask_languagesbot",
"ask_restaurant",
"ask_time",
"ask_wherefrom",
"ask_whoami",
"handleinsult",
"nicetomeeyou",
"telljoke",
"ask_whatismyname",
"ask_howbuilt",
"ask_whoisit",
]:
dispatcher.utter_template("utter_" + intent, tracker)
return []
class ActionGreetUser(Action):
"""Greets the user with/without privacy policy"""
def name(self):
return "action_greet_user"
def run(self, dispatcher, tracker, domain):
intent = tracker.latest_message["intent"].get("name")
shown_privacy = tracker.get_slot("shown_privacy")
name_entity = next(tracker.get_latest_entity_values("name"), None)
if intent == "greet":
if shown_privacy and name_entity and name_entity.lower() != "sara":
dispatcher.utter_template("utter_greet_name", tracker, name=name_entity)
return []
elif shown_privacy:
dispatcher.utter_template("utter_greet_noname", tracker)
return []
else:
dispatcher.utter_template("utter_greet", tracker)
dispatcher.utter_template("utter_inform_privacypolicy", tracker)
dispatcher.utter_template("utter_ask_goal", tracker)
return [SlotSet("shown_privacy", True)]
elif intent[:-1] == "get_started_step" and not shown_privacy:
dispatcher.utter_template("utter_greet", tracker)
dispatcher.utter_template("utter_inform_privacypolicy", tracker)
dispatcher.utter_template("utter_" + intent, tracker)
return [SlotSet("shown_privacy", True), SlotSet("step", intent[-1])]
elif intent[:-1] == "get_started_step" and shown_privacy:
dispatcher.utter_template("utter_" + intent, tracker)
return [SlotSet("step", intent[-1])]
return []
class ActionDefaultFallback(Action):
def name(self) -> Text:
return "action_default_fallback"
def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List["Event"]:
intent = tracker.latest_message["intent"].get("name")
# retrieve the correct chitchat utterance dependent on the intent
if intent in [
"ask_builder",
"ask_weather",
"ask_howdoing",
"ask_whatspossible",
"ask_time",
"ask_wherefrom",
"ask_whoami",
"handleinsult",
"telljoke",
"ask_whatismyname",
"ask_howbuilt",
"ask_whoisit",
"out_of_scope"
]:
dispatcher.utter_template("utter_" + intent, tracker)
dispatcher.utter_template("utter_default", tracker)
return [UserUtteranceReverted()]