-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.py
338 lines (288 loc) · 13.3 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
from typing import Any, Text, Dict, List, Union
from rasa_sdk import Action, Tracker,events
from rasa_sdk.events import FollowupAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import SlotSet
import json
from datetime import datetime
from PIL import Image, ImageDraw
import pyqrcode
import png
import random
class AskDate(Action):
def name(self) -> Text:
return "ask_date"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
dateList = []
buttons = []
dateList = movieSchedule.keys()
today = datetime.now().strftime("%d-%m-%Y")
today = datetime.strptime(today,"%d-%m-%Y")
for i in dateList:
temp_date = datetime.strptime(i,"%d-%m-%Y")
if (temp_date < today):
continue
else:
payload = str(i)
buttons.append(
{"title": "{}".format(i), "payload": payload})
message = "Please select a date"
dispatcher.utter_button_message(message, buttons)
return []
class AskChoice(Action):
def name(selfself) -> Text:
return "ask_movieChoice"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
buttons = []
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
with open("data/movieTime.json", "r") as MT:
movieTime = json.load(MT)
movieDate = str(tracker.get_slot("movieDate"))
today_time = datetime.now().strftime("%I %p")
today_time = datetime.strptime(today_time,"%I %p")
today = datetime.now().strftime("%d-%m-%Y")
for i in movieSchedule[movieDate].keys():
temp_time = datetime.strptime(movieTime[i],"%I %p")
if(movieDate == today):
if( temp_time > today_time):
payload = "I choose "+ i
buttons.append(
{"title": "{}".format(movieSchedule[movieDate][i]), "payload": payload})
else:
payload = "I choose " + i
buttons.append(
{"title": "{}".format(movieSchedule[movieDate][i]), "payload": payload})
message = "Please select a movie"
dispatcher.utter_button_message(message, buttons)
return[]
class PrintImdbMovieDetails(Action):
def name(selfself) -> Text:
return "print_imdb_movie_details"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
with open("data/movieScreen.json", "r") as MScreen:
movieScreen = json.load(MScreen)
with open("data/movieTime.json", "r") as MT:
movieTime = json.load(MT)
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
with open("data/movieDetails.json", "r") as MD:
movieDetails = json.load(MD)
buttons = []
movieDate = tracker.get_slot("movieDate")
movieChoice = tracker.get_slot("movieChoice")
movieName = movieSchedule[str(movieDate)][str(movieChoice)]
message = "Here are the details of the selected movie, if you care ;)\n"
imdbLink = "https://www.imdb.com/title/"+ movieDetails[str(movieDate)][str(movieChoice)]
message = message + "View details of " + movieName + " in IMDB\n"
message = message + imdbLink
dispatcher.utter_message(message)
return[]
class SlotDisplay(Action):
def name(selfself) -> Text:
return "slot_display"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
movieDate = tracker.get_slot("movieDate")
movieChoice = tracker.get_slot("movieChoice")
noTickets = tracker.get_slot("noTickets")
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
with open("data/movieSeatMap.json", "r") as SM:
movieSeatMap = json.load(SM)
# movieSeatMap = movieSeatMap[str(movieDate)][str(movieChoice)]
reply = ""
img = Image.new('RGB', (500, 525), color=(73, 109, 137))
d = ImageDraw.Draw(img)
r = 5
c = 25
for i in movieSeatMap[str(movieDate)][str(movieChoice)].keys():
for j in movieSeatMap[str(movieDate)][str(movieChoice)][i].keys():
if j == str(9):
c = c + 50
reply = ""
d.text((c, r), reply, fill=(255, 255, 0))
if movieSeatMap[str(movieDate)][str(movieChoice)][i][j] == "1":
reply = str.upper(str(i + j))
d.text((c, r), reply, fill=(255, 255, 0))
c = c + 25
else:
reply = "(X)"
d.text((c, r), reply, fill=(255, 0, 0))
c = c + 25
reply = reply + "\n "
r = r + 30
c = 25
r = r + 20
c = 175
reply = "------- SCREEN -------"
d.text((c, r), reply, fill=(0, 0, 0))
imgName = "data/available_seats-" + datetime.now().strftime("%H%M") + ".png"
img.save(imgName)
imgURL = "http://localhost:7000/" + imgName
text = "(X) represents that those seats are booked and are not available,Please select only " + str(noTickets) + " seats."
text = text + "\n Please input your seat selection with a comma separating each seat like in the example,\nEx: a2,b7,c8"
dispatcher.utter_message(image=imgURL)
dispatcher.utter_message(text=text)
return[]
class SlotCheck(Action):
def name(selfself) -> Text:
return "slot_check"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
movieDate = tracker.get_slot("movieDate")
movieChoice = tracker.get_slot("movieChoice")
noTickets = tracker.get_slot("noTickets")
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
with open("data/movieSeatMap.json", "r") as SM:
movieSeatMap = json.load(SM)
movieSeatMap = movieSeatMap[str(movieDate)][str(movieChoice)]
message = str((tracker.latest_message)['text'])
seats = str(message).split(",")
newSeats = []
flag = 0
for i in range(len(seats)):
for i1 in range(len(seats)):
if i != i1:
if seats[i] == seats[i1]:
flag = 1
if(flag == 1):
msg = "Invalid Seat selection detected !"
dispatcher.utter_message(msg)
return[FollowupAction("slot_display")]
if (str(len(seats)) != str(noTickets)):
msg = "Please select only ", str(noTickets), " seats..."
dispatcher.utter_message(msg)
return[FollowupAction("slot_display")]
else:
newSeats = seats
seatCount = 0
for seat in newSeats:
i = seat[0]
j = seat[1:]
try:
if ((movieSeatMap[str(i)][str(j)]) == "1"):
seatCount = seatCount + 1
else:
seatCount = seatCount + 0
except:
msg = "Invalid Seat selection detected !"
dispatcher.utter_message(msg)
return [FollowupAction("slot_display")]
if(seatCount == int(noTickets)):
return[FollowupAction("utter_seat_confirmation")]
else:
dispatcher.utter_message("One or More seats are booked.Please re-enter your selection!")
return[FollowupAction("slot_display")]
class PersonalDetailsCheck(Action):
def name(self):
return "personal_details_check"
def run(self,dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],) -> List[Dict]:
pdEmail = tracker.get_slot("pdEmail")
pdNumber = tracker.get_slot("pdNumber")
if "@" not in str(pdEmail):
dispatcher.utter_message("Please enter a valid Email address")
return [FollowupAction("utter_ask_pdEmail")]
if (len(pdNumber) != 10):
dispatcher.utter_message("Please enter a valid 10 digit phone number")
return [FollowupAction("utter_ask_pdNumber")]
message = ""
movieChoice = tracker.get_slot("movieChoice")
noTickets = tracker.get_slot("noTickets")
movieDate = tracker.get_slot("movieDate")
seats = tracker.get_slot("seats")
with open("data/movieSchedule.json", "r") as MS:
movieSchedule = json.load(MS)
with open("data/movieScreen.json", "r") as MScreen:
movieScreen = json.load(MScreen)
with open("data/movieTime.json", "r") as MT:
movieTime = json.load(MT)
movieName = movieSchedule[str(movieDate)][str(movieChoice)]
message = "SOKKALINGAM MOVIES PVT LTD.\nDubai Main Rd,\nDubai\n\nTICKET DETAILS :\n"
message += "Movie : " + str(movieName) + "\nShow Date : " + str(movieDate)
message += "\nShow time : " + str(movieTime[str(movieChoice)])
message += "\nScreen : " + str(movieScreen[str(movieChoice)]) + "\nNo. of Tickets : " + str(noTickets)
message += "\nSeats : " + str.upper(seats) + "\nTotal Amount : Rs." + str(int(noTickets)*250) +"/-\n"
pdName = tracker.get_slot("pdName")
pdEmail = tracker.get_slot("pdEmail")
pdNumber = tracker.get_slot("pdNumber")
message += "\nPERSONAL DETAILS : \nName : " + str(pdName) +"\nEmail address : " + str(pdEmail)
message += "\nPhone Number : "+ str(pdNumber)
dispatcher.utter_message(message)
return [SlotSet("ticketDetails", message)]
class TicketGeneration(Action):
def name(self) -> Text:
return "ticket_generation"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
ticket_details = tracker.get_slot("ticketDetails")
movieDate = tracker.get_slot("movieDate")
movieChoice = tracker.get_slot("movieChoice")
seats = str(tracker.get_slot("seats")).split(",")
ticketID = "SLM-" + str(random.choice(range(1000,10000)))
with open("data/movieSeatMap.json", "r") as SM:
movieSeatMap = json.load(SM)
for seat in seats:
i = seat[0]
j = seat[1:]
movieSeatMap[str(movieDate)][str(movieChoice)][str(i)][str(j)] = "0"
with open("data/movieSeatMap.json", "w") as SMw:
json.dump(movieSeatMap,SMw)
ticket_details = ticket_details + "\n\nTICKET ID : " + ticketID
qrData = pyqrcode.create(ticket_details)
ticketHistory = {}
ticketHistory[ticketID] = str(ticket_details)
with open("data/tickets/ticketHistory.json", "w") as outfile:
json.dump(ticketHistory, outfile)
ticket_file = "data/tickets/" + ticketID + ".png"
qrData.png(ticket_file, scale=6)
imgURL = "http://localhost:7000/" + ticket_file
dispatcher.utter_message(image=imgURL)
mesg = "TICKET ID : " + ticketID
dispatcher.utter_message(mesg)
dispatcher.utter_message("Your tickets have been booked successfully!\nSave the QRCode for easy check-in at the theatre\nPlease remember your TICKET ID in case of further reference")
return []
class HandlePayment(Action):
def name(self) -> Text:
return "handle_payment"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
dispatcher.utter_message("We are happy to announce that we are giving away tickets that are booked through our chatBot as a token of thanks for using our ChatBot.")
return [SlotSet("payment_flag", True)]
class TicketRetrieval(Action):
def name(self) -> Text:
return "ticket_retrieval"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
ticketID = tracker.get_slot("ticketID")
with open("data/tickets/ticketHistory.json", "r") as ticketfile:
tickets = json.load(ticketfile)
ticketID = "SLM-" + ticketID
if(ticketID in tickets.keys()):
dispatcher.utter_message("Ticket Found!")
qrData = pyqrcode.create(tickets[ticketID])
ticket_file = "data/tickets/" + ticketID + ".png"
qrData.png(ticket_file, scale=6)
imgURL = "http://localhost:7000/" + ticket_file
dispatcher.utter_message(image=imgURL)
mesg = "TICKET ID : " + ticketID
dispatcher.utter_message(mesg)
else:
dispatcher.utter_message("Sorry! I could not find any ticket with the given Ticket ID")
return []