-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactions.py
359 lines (287 loc) · 11.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import logging
import json
import smtplib
from email.message import EmailMessage
from rasa.constants import DEFAULT_DATA_PATH
from rasa_sdk import Action, Tracker
from rasa_sdk.events import AllSlotsReset, SlotSet, Restarted
from zomato.zomato_api import Zomato
import warnings
warnings.filterwarnings("ignore")
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("__name__")
user_key = "810872d4d561674a6f2d22162b7b2efb"
config = {"user_key": user_key}
_zomato = Zomato(config)
""" Custom action to fetch list of restaurants
"""
class ActionSearchRestaurants(Action):
def name(self):
return "action_restaurant"
def run(self, dispatcher, tracker, domain):
response_message = ""
email_message = ""
search_validity = "valid"
budget = tracker.get_slot("budget")
location = tracker.get_slot("location")
cuisine = tracker.get_slot("cuisine")
if not location:
search_validity = "invalid"
else:
"""
retrieve location details
"""
response = _zomato.get_location(location)
location_details = {}
if response is not None:
response_json = json.loads(response)
if response_json["status"] == "success":
"""
fetch location details and store 'city_id'
"""
location_details = response_json["location_suggestions"][0]
city_id = location_details["city_id"]
city_name = location_details["city_name"]
"""
Validate if the location details is of the requested location
"""
if location.lower() == city_name.lower():
"""
fetch all cuisines available in the location
"""
response_cuisine = _zomato.get_cuisines(city_id)
supported_cuisines = [
"American",
"Chinese",
"Italian",
"Mexican",
"North Indian",
"South Indian",
]
"""
filter only supported cuisines
"""
filtered_cuisine = {
key: value
for key, value in response_cuisine.items()
if value in supported_cuisines
}
if cuisine is not None:
cuisine_list = [
key
for key, value in filtered_cuisine.items()
if str(value).lower() == cuisine.lower()
]
else:
cuisine_list = [
key for key, value in filtered_cuisine.items()
]
restaurants_found = self.search_restaurant(
location, location_details, cuisine_list
)
if len(restaurants_found) > 0:
restaurant_filtered_budget = self.filter_restaurant_by_budget(budget, restaurants_found)
number_of_records = 10
if len(restaurant_filtered_budget) < 10:
number_of_records = len(restaurant_filtered_budget)
for index in range(0, number_of_records):
restaurant = restaurant_filtered_budget[index]
if index < 5:
response_message = (
response_message
+ "\n "
+ str(index + 1)
+ ". "
+ restaurant["name"]
+ " in "
+ restaurant["address"]
+ " has been rated "
+ restaurant["rating"]
+ " out of 5"
+ "\n"
)
email_message = (
email_message
+ "\n "
+ str(index + 1)
+ ". "
+ restaurant["name"]
+ " in "
+ restaurant["address"]
+ " has been rated "
+ restaurant["rating"]
+ " out of 5. "
+ "Average cost for 2 : "
+ str(restaurant["avg_cost_for_2"])
+ "\n"
)
else:
search_validity = "invalid"
else:
search_validity = "invalid"
else:
search_validity = "invalid"
else:
search_validity = "invalid"
if search_validity == "valid":
dispatcher.utter_message(response_message)
return [SlotSet("search_validity", search_validity), SlotSet("email_message", email_message)]
def search_restaurant(
self, location="", location_details={}, cuisine_list=[]
) -> list:
restaurants_found = []
"""
Search restaurants
"""
response = _zomato.restaurant_search(
location,
location_details["latitude"],
location_details["longitude"],
cuisine_list,
location_details["city_id"],
"city",
100
)
if response is not None:
response_json = json.loads(response)
if response_json["results_found"] > 0:
for restaurant in response_json["restaurants"]:
restaurants_found.append(
{
"name": restaurant["restaurant"]["name"],
"address": restaurant["restaurant"]["location"]["address"],
"avg_cost_for_2": restaurant["restaurant"]["average_cost_for_two"],
"rating": restaurant["restaurant"]["user_rating"]["aggregate_rating"],
}
)
return restaurants_found
def filter_restaurant_by_budget(self, budget, restaurant_list) -> list:
filtered_restaurant_list = []
"""
Set the budget range based on input
"""
rangeMin = 0
rangeMax = 999999
if budget == "299":
rangeMax = 299
elif budget == "700":
rangeMin = 300
rangeMax = 700
elif budget == "701":
rangeMin = 701
else:
"""
Default budget
"""
rangeMin = 0
rangeMax = 9999
for restaurant in restaurant_list:
avg_cost = int(restaurant["avg_cost_for_2"])
if avg_cost >= rangeMin and avg_cost <= rangeMax:
filtered_restaurant_list.append(restaurant)
return filtered_restaurant_list
""" Custom action to validate input location
"""
class ActionValidateLocation(Action):
def name(self):
return "action_location_valid"
def run(self, dispatcher, tracker, domain):
location = tracker.get_slot("location")
location_validity = "valid"
if not location:
location_validity = "invalid"
else:
filepath = DEFAULT_DATA_PATH + "/cities.json"
with open(filepath) as cities_file:
data = json.load(cities_file)
if data is not None:
tier1_cities = data["data"]["tier1"]
tier2_cities = data["data"]["tier2"]
tier1_cities_lower = [city.lower() for city in tier1_cities]
tier2_cities_lower = [city.lower() for city in tier2_cities]
location_validity = (
"invalid"
if location.lower() not in tier1_cities_lower
and location.lower() not in tier2_cities_lower
else "valid"
)
else:
location_validity = "invalid"
return [SlotSet("location_validity", location_validity)]
""" Custom action to validate input cuisine
"""
class ActionValidateCuisine(Action):
def name(self):
return "action_cuisine_valid"
def run(self, dispatcher, tracker, domain):
cuisine = tracker.get_slot("cuisine")
cuisine_check = "valid"
if not cuisine:
cuisine_check = "invalid"
else:
supported_cuisines = ["american","chinese","italian","mexican","north indian","south indian"]
cuisine_check = (
"invalid" if cuisine.lower() not in supported_cuisines else "valid")
return [SlotSet("cuisine_validity", cuisine_check)]
class ActionRestarted(Action):
def name(self):
return 'action_restart'
def run(self, dispatcher, tracker, domain):
return[Restarted()]
class ActionSlotReset(Action):
def name(self):
return 'action_slot_reset'
def run(self, dispatcher, tracker, domain):
return[AllSlotsReset()]
""" Defined Custom action to send an email
"""
class ActionSendEmail(Action):
def name(self):
return "action_send_email"
def run(self, dispatcher, tracker, domain):
location = tracker.get_slot("location")
cuisine = tracker.get_slot("cuisine")
email_id = tracker.get_slot("email")
email_message = tracker.get_slot("email_message")
"""
Parse email id
Required to handle email id sent from SLACK connector
"""
str_email_id = str(email_id)
if str_email_id.startswith("mailto"):
separator_index = str_email_id.index("|")
if separator_index > -1:
emails = str_email_id.split("|")
email_id = emails[1]
"""
Create an email message
"""
msg = EmailMessage()
msg.set_content(email_message)
msg['Subject'] = "Foodie restaurant Bot | List of {0} Restaurants in {1}".format(cuisine, location)
"""
Read SMTP configuration
"""
smtp_config = {}
filepath = DEFAULT_DATA_PATH + "/smtpconfig.txt"
with open(filepath) as mail_file:
for line in mail_file:
name, var = line.partition("=")[::2]
smtp_config[name.strip()] = var.strip()
"""
Send email to the user
"""
try:
s = smtplib.SMTP_SSL(host=smtp_config["smtpserver_host"], port=smtp_config["smtpserver_port"])
s.login(smtp_config["username"], smtp_config["password"])
msg['From'] = smtp_config["from_email"]
msg['To'] = email_id
s.send_message(msg)
s.quit()
except Exception as e:
print("failed to send an email")
print(e)
return [AllSlotsReset()]