-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
483 lines (364 loc) · 12.2 KB
/
main.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# import libraries
import os
import logging
from dotenv import load_dotenv
from datetime import datetime
# aiogram module
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.utils import executor
# keyboards.py
from keyboards import (
MAIN_MENU_INLINE_KEYBOARD,
VEHICLE_TYPE_KEYBOARD,
PAYMENT_MODE_KEYBOARD,
REMOVE_KEYBOARD,
)
# messages.py
from messages import (
MSG_MAIN_MENU,
MSG_HELP_MENU,
MSG_BOOKING_CONFIRMED,
MSG_BOOKING_STARTED,
MSG_BOOKING_CANCELLED,
MSG_FEATURE_COMING_SOON,
)
# database.py
from database import getUserBookingHistory, insertIntoDataBase, logIntoDataBase
# logger
logging.basicConfig(level=logging.INFO)
# bot token
load_dotenv()
BOT_TOKEN = os.environ.get("BOT_TOKEN")
# bot setup
bot = Bot(token=BOT_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
# states
class Form(StatesGroup):
booking_time = State()
pickup = State()
destination = State()
vehicle = State()
payment = State()
fare = State()
name = State()
age = State()
gender = State()
# global variables
USER_ID = ""
USER_FULL_NAME = ""
# /start
@dp.message_handler(commands="start")
async def cmd_start(message: types.Message):
global USER_ID, USER_FULL_NAME
USER_ID = message.from_id
USER_FULL_NAME = message.from_user.full_name
LOG_ACTION = "chat started by user"
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info("%d - %s - %s", USER_ID, USER_FULL_NAME, LOG_ACTION)
await message.reply(MSG_MAIN_MENU, reply_markup=MAIN_MENU_INLINE_KEYBOARD)
# /cancel
@dp.message_handler(state="*", commands="cancel")
@dp.message_handler(Text(equals="cancel", ignore_case=True), state="*")
async def cancel_handler(message: types.Message, state: FSMContext):
current_state = await state.get_state()
if current_state == None:
return
USER_ID = message.from_id
USER_FULL_NAME = message.from_user.full_name
LOG_ACTION = "booking process cancelled by user from state"
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info(
"%d - %s - %s - %r",
USER_ID,
USER_FULL_NAME,
LOG_ACTION,
current_state,
)
await message.reply(MSG_BOOKING_CANCELLED, reply_markup=MAIN_MENU_INLINE_KEYBOARD)
await state.finish()
@dp.callback_query_handler(
text=["book_ride", "booking_history", "settings", "get_help"]
)
async def main_menu_callback(call: types.CallbackQuery, state: FSMContext):
global USER_ID, USER_FULL_NAME
USER_ID = call.message.chat.id
USER_FULL_NAME = ""
# set USER_FULL_NAME
if call.message.chat.first_name and call.message.chat.last_name:
USER_FULL_NAME = (
call.message.chat.first_name + " " + call.message.chat.last_name
)
elif call.message.chat.first_name:
USER_FULL_NAME += call.message.chat.first_name
elif call.message.chat.first_name:
USER_FULL_NAME += call.message.chat.last_name
# book_ride
if call.data == "book_ride":
now = datetime.now()
curr_time = now.strftime("%d/%m/%Y %H:%M:%S")
async with state.proxy() as data:
data["booking_time"] = curr_time
LOG_ACTION = "chat started by user"
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info("%d - %s - %s", USER_ID, USER_FULL_NAME, LOG_ACTION)
await call.message.answer(MSG_BOOKING_STARTED, reply_markup=REMOVE_KEYBOARD)
await Form.pickup.set()
# booking_history
if call.data == "booking_history":
BOOKING_HISTORY = getUserBookingHistory(USER_ID)
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
LOG_ACTION = "booking history viewed by user"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info("%d - %s - %s", USER_ID, USER_FULL_NAME, LOG_ACTION)
await call.message.answer(
BOOKING_HISTORY, reply_markup=MAIN_MENU_INLINE_KEYBOARD
)
# settings
if call.data == "settings":
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
LOG_ACTION = "settings changed by user"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info("%d - %s - %s", USER_ID, USER_FULL_NAME, LOG_ACTION)
await call.message.answer(
MSG_FEATURE_COMING_SOON, reply_markup=MAIN_MENU_INLINE_KEYBOARD
)
# get_help
if call.data == "get_help":
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
LOG_ACTION = "help taken by user"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info("%d - %s - %s", USER_ID, USER_FULL_NAME, LOG_ACTION)
await call.message.answer(MSG_HELP_MENU, reply_markup=MAIN_MENU_INLINE_KEYBOARD)
await call.answer()
# pickup location
@dp.message_handler(state=Form.pickup, content_types=["location"])
async def pickup(message: types.Message, state: FSMContext):
pickup_latitude = message.location.latitude
pickup_longitude = message.location.longitude
async with state.proxy() as data:
data["pickup"] = [pickup_latitude, pickup_longitude]
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
USER_ID = message.from_id
USER_FULL_NAME = message.from_user.full_name
LOG_ACTION = "user pickup location"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"pickup_latitude": pickup_latitude,
"pickup_longitude": pickup_longitude,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info(
"%d - %s - %s - latitute: %f - longitude: %f",
USER_ID,
USER_FULL_NAME,
LOG_ACTION,
pickup_latitude,
pickup_longitude,
)
await message.answer("Great. Now send your Destination")
await Form.destination.set()
# destination location
@dp.message_handler(state=Form.destination, content_types=["location"])
async def destination(message: types.Message, state: FSMContext):
destination_latitude = message.location.latitude
destination_longitude = message.location.longitude
async with state.proxy() as data:
data["destination"] = [destination_latitude, destination_longitude]
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
USER_ID = message.from_id
USER_FULL_NAME = message.from_user.full_name
LOG_ACTION = "user destination"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"destination_latitude": destination_latitude,
"destination_longitude": destination_longitude,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info(
"%d - %s - %s - latitute: %f - longitude: %f",
USER_ID,
USER_FULL_NAME,
LOG_ACTION,
destination_latitude,
destination_longitude,
)
await message.answer("Select your vehicle type", reply_markup=VEHICLE_TYPE_KEYBOARD)
await Form.vehicle.set()
# invalid input for vehicle type
@dp.message_handler(
lambda message: message.text not in ["Auto", "Mini", "Sedan", "SUV"],
state=Form.vehicle,
)
async def vehicle_invalid(message: types.Message):
return await message.reply(
"Invalid vehicle type. Choose your vehicle type from the keyboard."
)
# vehicle type
@dp.message_handler(state=Form.vehicle)
async def vehicle(message: types.Message, state: FSMContext):
async with state.proxy() as data:
generated_fare = round(
1000
* (
abs(data["pickup"][1] - data["destination"][1])
+ abs(data["pickup"][0] - data["destination"][0])
),
2,
)
data["vehicle"] = message.text
data["fare"] = generated_fare
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
vehicle_type=message.text
USER_ID = message.from_id
USER_FULL_NAME = message.from_user.full_name
LOG_ACTION = "user vehicle type"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"vehicle_type": vehicle_type,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info(
"%d - %s - %s - %s",
message.from_id,
message.from_user.full_name,
LOG_ACTION,
vehicle_type,
)
text = (
"Your generated fare is Rs. " + str(generated_fare) + "\n\nSelect payment mode"
)
await message.answer(text, reply_markup=PAYMENT_MODE_KEYBOARD)
await Form.payment.set()
# invalid input for payment mode
@dp.message_handler(
lambda message: message.text not in ["Cash", "Online"], state=Form.payment
)
async def payment_invalid(message: types.Message):
return await message.reply(
"Invalid payment mode. Choose your payment mode from the keyboard."
)
# payment mode
@dp.message_handler(state=Form.payment)
async def payment(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data["payment"] = message.text
now = datetime.now()
curr_time = now.strftime("%Y/%m/%d %H:%M:%S")
payment_mode= message.text
LOG_ACTION="user payment mode"
logData = {
"user_id": USER_ID,
"user_full_name": USER_FULL_NAME,
"action": LOG_ACTION,
"payment_mode": payment_mode,
"createdAt": curr_time,
}
logIntoDataBase(logData)
logging.info(
"%d - %s - %s - %s",
message.from_id,
message.from_user.full_name,
LOG_ACTION,
payment_mode,
)
await message.answer(MSG_BOOKING_CONFIRMED, reply_markup=REMOVE_KEYBOARD)
BOOKING_SUMMARY = (
"Your booking summary\n"
+ "\nBooking Time: \t "
+ data["booking_time"]
+ "\nDestination: \n "
+ "\t\t\t\tlat: "
+ str(round(data["destination"][0], 4))
+ "\t\t\t\tlong: "
+ str(round(data["destination"][1], 4))
+ "\nPickup: \n "
+ "\t\t\t\tlat: "
+ str(round(data["pickup"][0], 4))
+ "\t\t\t\tlong: "
+ str(round(data["pickup"][1], 4))
+ "\nVehicle: \t "
+ data["vehicle"]
+ "\nPayment Mode: \t "
+ data["payment"]
+ "\nFare: \t "
+ str(data["fare"])
)
data = {
"USER_ID": USER_ID,
"USER_FULL_NAME": USER_FULL_NAME,
"PICKUP_LATITUDE": data["pickup"][0],
"PICKUP_LONGITUDE": data["pickup"][1],
"DESTINATION_LATITUDE": data["destination"][0],
"DESTINATION_LONGITUDE": data["destination"][1],
"BOOKING_TIME": data["booking_time"],
"VEHICLE_TYPE": data["vehicle"],
"PAYMENT_MODE": data["payment"],
"FARE": data["fare"],
}
insertIntoDataBase(data)
await message.answer(BOOKING_SUMMARY, reply_markup=MAIN_MENU_INLINE_KEYBOARD)
await state.finish()
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)