-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactions.py
269 lines (237 loc) · 11.2 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
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from rasa_core.actions.action import Action
from rasa_core.events import SlotSet
import zomatopy
import json
import pandas as pd
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json
from rasa_core.events import AllSlotsReset
from rasa_core.events import Restarted
from rasa_core.actions.forms import (
BooleanFormField,
EntityFormField,
FormAction,
FreeTextFormField
)
class GetAllSearchResults(FormAction):
RANDOMIZE = False
@staticmethod
def required_fields():
return [
EntityFormField("location", "location"),
EntityFormField("cuisine", "cuisine"),
EntityFormField("price", "price")
]
def name(self):
return 'get_all_search_results'
def submit(self, dispatcher, tracker, domain):
config={ "user_key":"2ee4b992c32c9d3fba974f2345ceb640"}
zomato = zomatopy.initialize_app(config)
loc = tracker.get_slot('location')
cuisine = tracker.get_slot('cuisine')
price_range = tracker.get_slot('price')
location_detail=zomato.get_location(loc, 1)
d1 = json.loads(location_detail)
lat=d1["location_suggestions"][0]["latitude"]
lon=d1["location_suggestions"][0]["longitude"]
cuisines_dict={'mexican':73,'chinese':25,'italian':55,'american':1,'north indian':50,'south indian':85}
dispatcher.utter_message("Hold on while we fetch the restaurants ..............")
fetchIndex=0
search_results = pd.DataFrame(columns=['Restaurant Name','Restaurant locality address','Average budget for two people','Zomato user rating'])
while (fetchIndex!=80):
results=zomato.restaurant_search("", lat, lon, str(cuisines_dict.get(cuisine)), 20,fetchIndex)
d = json.loads(results)
response=""
if d['results_found'] == 0:
break
else:
for restaurant in d['restaurants']:
search_results = search_results.append({'Restaurant Name':restaurant['restaurant']['name'],
'Restaurant locality address': restaurant['restaurant']['location']['address'],
'Average budget for two people':restaurant['restaurant']['average_cost_for_two'],
'Zomato user rating':restaurant['restaurant']['user_rating']['aggregate_rating']},ignore_index=True)
fetchIndex = fetchIndex+20
if(len(search_results)==0):
dispatcher.utter_template("utter_no_restaurants_found")
SlotSet("price", None)
return[SlotSet("cuisine", None)]
#response= "no results"
else:
return[SlotSet("full_restaurant_search",search_results.to_json())]
class ActionSearchRestaurants(Action):
def name(self):
return 'action_restaurant'
def run(self, dispatcher, tracker, domain):
response=""
loc = tracker.get_slot('location')
cuisine = tracker.get_slot('cuisine')
price_range = tracker.get_slot('price')
search_results = pd.read_json(tracker.get_slot('full_restaurant_search'))
if(price_range=="1"):
filterd_results = search_results[search_results['Average budget for two people'] <=300]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
SlotSet("price", None)
dispatcher.utter_template("utter_no_match_price_range", tracker)
return[SlotSet("query_restaurant_search", None)]
#response=response+" Sorry couldn't find any restaurants in price range. Please re enter different price range "
else:
for index, row in filterd_results.head(5).iterrows():
response = response+row['Restaurant Name']+" in "+ row['Restaurant locality address']+" has been rated "+str(row['Zomato user rating'])+"\n"
return[SlotSet("query_restaurant_search", response)]
elif(price_range=="2"):
filterd_results = search_results[(search_results['Average budget for two people'] > 300) & (search_results['Average budget for two people'] <= 700)]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
SlotSet("price", None)
dispatcher.utter_template("utter_no_match_price_range", tracker)
return[SlotSet("query_restaurant_search", None)]
else:
for index, row in filterd_results.head(5).iterrows():
response = response+row['Restaurant Name']+" in "+ row['Restaurant locality address']+" has been rated "+str(row['Zomato user rating'])+"\n"
return[SlotSet("query_restaurant_search", response)]
elif(price_range=="3"):
filterd_results = search_results[search_results['Average budget for two people'] >700]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
#response=response+" Sorry couldn't find any restaurants in price range. Please re enter different price range "
SlotSet("price", None)
dispatcher.utter_template("utter_no_match_price_range", tracker)
return[SlotSet("query_restaurant_search", None)]
else:
for index, row in filterd_results.head(5).iterrows():
response = response+row['Restaurant Name']+" in "+ row['Restaurant locality address']+" has been rated "+str(row['Zomato user rating'])+"\n"
return[SlotSet("query_restaurant_search", response)]
class ActionValidateLocation(Action):
def name(self):
return 'validate_location'
def run(self, dispatcher, tracker, domain):
city_list = ['ahmedabad', 'bangalore', 'chennai', 'delhi', 'hyderabad', 'kolkata', 'mumbai', 'pune',
'agra', 'ajmer', 'aligarh', 'allahabad', 'amravati', 'amritsar', 'asansol', 'aurangabad', 'bareilly', 'belgaum',
'bhavnagar', 'bhiwandi', 'bhopal', 'bhubaneswar', 'bikaner', 'bokaro steel city', 'chandigarh', 'coimbatore',
'cuttack', 'dehradun', 'dhanbad', 'durg-bhilai nagar', 'durgapur', 'erode', 'faridabad', 'firozabad', 'ghaziabad',
'gorakhpur', 'gulbarga', 'guntur', 'gurgaon', 'guwahati', 'gwalior', 'hubli-dharwad', 'indore', 'jabalpur', 'jaipur',
'jalandhar', 'jammu', 'jamnagar', 'jamshedpur', 'jhansi', 'jodhpur', 'kannur', 'kanpur', 'kakinada', 'kochi', 'kottayam',
'kolhapur', 'kollam', 'kota', 'kozhikode', 'kurnool', 'lucknow', 'ludhiana', 'madurai', 'malappuram', 'mathura', 'goa',
'mangalore', 'meerut', 'moradabad', 'mysore', 'nagpur', 'nanded', 'nashik', 'nellore', 'noida', 'palakkad', 'patna',
'pondicherry', 'raipur', 'rajkot', 'rajahmundry', 'ranchi', 'rourkela', 'salem', 'sangli', 'siliguri', 'solapur',
'srinagar', 'sultanpur', 'surat', 'thiruvananthapuram', 'thrissur', 'tiruchirappalli', 'tirunelveli', 'tiruppur',
'ujjain', 'vijayapura', 'vadodara', 'varanasi', 'vasai-virar city', 'vijayawada', 'visakhapatnam', 'warangal']
loc = tracker.get_slot('location')
response = ""
if(loc == None):
dispatcher.utter_template("utter_invalid_location",tracker)
return [SlotSet('location',None)]
elif(loc.lower() not in city_list):
dispatcher.utter_template("utter_invalid_location",tracker)
return [SlotSet('location',None)]
else:
return [SlotSet('location',loc)]
class ActionValidateCuisine(Action):
def name(self):
return 'validate_cuisine'
def run(self, dispatcher, tracker, domain):
cuisines=['mexican','chinese','italian','american','north indian','south indian']
cuisine = tracker.get_slot('cuisine')
#response = ""
if(cuisine == None):
dispatcher.utter_template("utter_invalid_cuisine",tracker)
return [SlotSet('cuisine',None)]
elif(cuisine.lower() not in cuisines):
dispatcher.utter_template("utter_invalid_cuisine",tracker)
return [SlotSet('cuisine',None)]
else:
return [SlotSet('cuisine',cuisine)]
import re
class ActionSendEmail(Action):
def name(self):
return 'action_email'
def run(self, dispatcher, tracker, domain):
price_range = tracker.get_slot('price')
email = tracker.get_slot('email')
if(email==None):
dispatcher.utter_template("utter_email_not_recognized", tracker)
return[SlotSet('email',None)]
list_email = re.findall('([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)',email)
msg = MIMEMultipart('alternative')
search_results = pd.read_json(tracker.get_slot('full_restaurant_search'))
if(len(list_email)==0):
dispatcher.utter_template("utter_email_not_recognized", tracker)
return[SlotSet('email',email)]
elif(len(search_results)==0):
msg.attach(MIMEText("Sorry no results found",'html'))
else:
email = list_email[0]
if(price_range=="1"):
filterd_results = search_results[search_results['Average budget for two people'] <=300]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
msg.attach(MIMEText("Sorry couldn't find any restaurants in price range. Please choose different price range",'html'))
else:
msg.attach(MIMEText(filterd_results.head(10).to_html(),'html'))
elif(price_range=="2"):
filterd_results = search_results[(search_results['Average budget for two people'] > 300) & (search_results['Average budget for two people'] <= 700)]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
msg.attach(MIMEText("Sorry couldn't find any restaurants in price range. Please choose different price range",'html'))
else:
msg.attach(MIMEText(filterd_results.head(10).to_html(),'html'))
elif(price_range=="3"):
filterd_results = search_results[search_results['Average budget for two people'] >700]
filterd_results.sort_values('Zomato user rating',ascending=False,inplace=True)
if(len(filterd_results)==0):
msg.attach(MIMEText("Sorry couldn't find any restaurants in price range. Please choose different price range",'html'))
else:
msg.attach(MIMEText(filterd_results.head(10).to_html(),'html'))
msg['Subject'] = 'Restaurant search results from Zomato'
msg['From'] = '[email protected]'
msg['To'] = email
# Send the message via our own SMTP server.
#server = smtplib.SMTP('smtp.gmail.com:587')
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
#server.starttls()
server.login('[email protected]','IIITBtest2018')
server.send_message(msg)
server.quit()
dispatcher.utter_template("utter_email_sent_successfully", tracker)
return[SlotSet('email',email)]
class ActionValidatePrice(Action):
def name(self):
return 'validate_price'
def run(self, dispatcher, tracker, domain):
prices=['1','2','3']
price = tracker.get_slot('price')
#response = ""
if(price == None):
dispatcher.utter_template("utter_invalid_price_range",tracker)
return [SlotSet('price',None)]
elif(price.lower() not in prices):
dispatcher.utter_template("utter_invalid_price_range",tracker)
return [SlotSet('price',None)]
else:
return [SlotSet('price',price)]
class ActionGoodBye(Action):
def name(self):
return 'action_bye'
def run(self, dispatcher, tracker, domain):
dispatcher.utter_template("utter_goodbye",tracker)
return[AllSlotsReset()]
class ActionRestarted(Action):
def name(self):
return 'action_restarted'
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()]