-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMatchingUsers.py
497 lines (369 loc) · 24.3 KB
/
MatchingUsers.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler
from datetime import datetime
from decimal import Decimal
from time import sleep
import logging
import MainMenu
import configparser
import FulfillerDetails
import DynamoDB
import re
import RequesterDetails
import UserRatings
####################################### Parameters #######################################
# Create config parser and read config file
config = configparser.ConfigParser()
config.read("config.ini")
# Load bot token
bot_token = config["bot_keys"]["current_bot_token"]
# Define ConversationHandler.END in another variable for clarity.
ENDConv = ConversationHandler.END
# Enable logging
logging.basicConfig(
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
ENDRequesterConv = ConversationHandler.END
ENDFulfillerConv = ConversationHandler.END
####################################### Helper Functions #######################################
####################################### Main Functions #######################################
async def awaitFulfiller(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request made by the requester.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_MADE]["RequestID"])
# Check the status of the chat (i.e. finding fulfiller, in-convo, or cancelled). If fulfiller not found, return back to awaitFulfiller method.
if (response["Item"]["request_status"] == "Available"):
await update.message.reply_text("We are still trying to find a fulfiller. Please wait, or use /cancel to quit and remove your current request.")
return MainMenu.AWAIT_FULFILLER
elif (response["Item"]["request_status"] == "In Progress"):
# Get the message the requester is trying to send to the fulfiller
requesterMsg = update.message.text
# Get the request the requester has put out and update user_data
request = response["Item"]
context.user_data[MainMenu.REQUEST_MADE] = request
# Store information about the event.
logger.info("Requester '%s' (chat_id: '%s') sent message to fulfiller '%s' (chat_id: '%s'): %s", request["requester_user_name"], request["requester_chat_id"],
request["fulfiller_user_name"], request["fulfiller_chat_id"], requesterMsg)
in_chat_reminder = f"<b><u>Requester '{update.effective_user.name}' sent the following message:</u></b>\n\n"
# Send the message (with in-chat reminder) to the fulfiller via the fullfiler's chat id.
await context.bot.send_message(chat_id=request["fulfiller_chat_id"], text=in_chat_reminder + requesterMsg, parse_mode="HTML")
return MainMenu.REQUESTER_IN_CONVO
async def requesterCancelSearch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Get the request ID of the request to be deleted by the requester.
RequestID = context.user_data[MainMenu.REQUEST_MADE]["RequestID"]
response = DynamoDB.table.delete_item(Key = {"RequestID": RequestID})
logger.info(f"Requester {update.effective_user.name} (chat_id: {update.effective_user.id}) deleted their request (RequestID: {RequestID}) before a fulfiller was found")
logger.info("DynamoDB delete response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
await update.message.reply_text("Request cancelled! Use /start to use Dabao4Me again.")
return ConversationHandler.END
async def promptEditRequest(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Define the menu using a 2D array.
inlineMenu = [
[InlineKeyboardButton("Change Canteen Location", callback_data="editCanteen")],
[InlineKeyboardButton("Change Food Option", callback_data="editFood")],
[InlineKeyboardButton("Change Tip Amount", callback_data="editTip")]
]
# Transform the 2D array into an actual inline keyboard that can be interpreted by Telegram.
inlineMenuTG = InlineKeyboardMarkup(inlineMenu)
# Log event
logger.info(f"Requester {update.effective_user.name} (chat_id: {update.effective_user.id}) is modifying their request.")
await update.message.reply_text("Please select the part of the request you'd like to edit: ", reply_markup=inlineMenuTG)
return MainMenu.EDIT_ORDER
# When the fulfiller ends the conversation using the /end command.
async def fulfillerEndConv(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request selected by the fulfiller.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_CHOSEN]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_CHOSEN]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the request the fulfiller has chosen.
request = response["Item"]
# Update request_status in DynamoDB to "Closed".
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET request_status = :status",
ExpressionAttributeValues = {
":status" : "Closed"
}
)
# Update status for request variable before updating user_data
request['request_status'] = "Closed"
context.user_data[MainMenu.REQUEST_CHOSEN] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Prompt fulfiller to confirm that the conversation has ended.
await update.message.reply_text(f"You have ended the conversation with '{request['requester_user_name']}'. Use /start to request or fulfil an order again.")
# Prompt requester to notify them that the conversation has ended.
await context.bot.send_message(chat_id=request["requester_chat_id"], text=f"'{request['fulfiller_user_name']}' has ended the conversation. Use /start to request or fulfil an order again.")
# Store information about their name.
logger.info("Fulfiller '%s' (chat_id: '%s') ended a conversation with '%s' (chat_id: '%s').", request["fulfiller_user_name"], request["fulfiller_chat_id"],
request["requester_user_name"], request["requester_chat_id"])
return ENDConv
# When the requester ends the conversation using the /end command.
async def requesterEndConv(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request selected by the fulfiller.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_MADE]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_MADE]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the request the fulfiller has chosen.
request = response["Item"]
# Additional check needed here as this function might be called before the user is even connected to a user (while requester is in the midst of getting matched)
if (request["request_status"] == "Available"):
await update.message.reply_text("No conversation to end (we are still trying to find a fulfiller). Please wait, or use /cancel to quit and remove your current request.")
return MainMenu.AWAIT_FULFILLER
# Update request_status in DynamoDB to "Closed".
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET request_status = :status",
ExpressionAttributeValues = {
":status" : "Closed"
}
)
# Update status for request variable before updating user_data
request['request_status'] = "Closed"
context.user_data[MainMenu.REQUEST_MADE] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Prompt requester to confirm that the conversation has ended.
await update.message.reply_text(f"You have ended the conversation with '{request['fulfiller_user_name']}'. Use /start to request or fulfil an order again.")
# Prompt fulfiller to notify them that the conversation has ended.
await context.bot.send_message(chat_id=request["fulfiller_chat_id"], text=f"'{request['requester_user_name']}' has ended the conversation. Use /start to request or fulfil an order again.")
# Store information about their name.
logger.info("Requester '%s' (chat_id: '%s') ended a conversation with '%s' (chat_id: '%s').", request["requester_user_name"], ["requester_chat_id"],
request["fulfiller_user_name"], request["fulfiller_chat_id"])
return ENDConv
async def fulfilRequest(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Store user's argument for the /fulfil command
userInput = context.args[0]
# Get the canteen the fulfiller selected.
selectedCanteen = context.user_data[MainMenu.CANTEEN]
# Regex pattern for integers only
intPattern = r"^\d+$"
# Check that user input is an integer
if not re.match(intPattern, userInput):
await update.message.reply_text("Sorry, you have entered an invalid input (numbers only). Please try again.")
return MainMenu.FULFIL_REQUEST
# Input has been verified to be an integer
requestIndex = int(userInput) - 1
# Check that user input is not out of range
try:
selectedRequest = FulfillerDetails.filterRequests(selectedCanteen)[requestIndex]
except IndexError:
await update.message.reply_text("Invalid request number. Please try again.")
return MainMenu.FULFIL_REQUEST
# Get the specific request from the list of requests of the selected canteen via the requestIndex.
# 1. Via DynamoDB
selectedRequest = FulfillerDetails.filterRequests(selectedCanteen)[requestIndex]
response = DynamoDB.table.update_item(
Key = {
"RequestID": selectedRequest["RequestID"]
},
UpdateExpression = "SET fulfiller_chat_id = :chat_id, fulfiller_user_name = :user_name, request_status = :status",
ExpressionAttributeValues = {
":chat_id" : str(update.effective_user.id),
":user_name" : update.effective_user.name,
":status" : "In Progress"
}
)
# Update attributes for request variable before updating user_data
selectedRequest['fulfiller_chat_id'] = str(update.effective_user.id)
selectedRequest['fulfiller_user_name'] = update.effective_user.name
selectedRequest['request_status'] = "In Progress"
context.user_data[MainMenu.REQUEST_MADE] = selectedRequest
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", selectedRequest["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Store the selected request the fulfiller has chosen into user_data
context.user_data[MainMenu.REQUEST_CHOSEN] = FulfillerDetails.get_item(selectedRequest["RequestID"])["Item"]
# Send message to fulfiller to indicate connection to the requester.
await update.message.reply_text(f"You are now connected with {selectedRequest['requester_user_name']}! Use /end to end the conversation at any time, and /complete to mark the transaction as completed.")
# Get rating of fulfiller
response = DynamoDB.userRatingsTable.get_item(
Key = {"user_chat_id": str(update.effective_user.id)}
)
print()
logger.info("User Ratings Table get_item response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
# Initialize
good_received = 0
bad_received = 0
# If user has not received any ratings before, KeyError will occur during retrieval
try:
good_received = int(response["Item"]["good_received"])
except KeyError:
pass
try:
bad_received = int(response["Item"]["bad_received"])
except KeyError:
pass
total = int(good_received) + int(bad_received)
# Make sure total is not 0 to avoid dividing by 0
if (total != 0):
ratingPercent = float(good_received) / float(total)
# We only want the integer portion of the number
ratingPercent = int(ratingPercent * 100)
else:
ratingPercent = 0
# Send message to requester to indicate connection to the fulfiller.
await context.bot.send_message(chat_id=selectedRequest["requester_chat_id"], text=f"""Fulfiller found!. You are now connected with {selectedRequest["fulfiller_user_name"]} | {ratingPercent}% \U0001F44D out of {total} ratings.
Use /end to end the conversation at any time, and /complete to mark the transaction as completed.""")
# Store information about the event.
logger.info("Fulfiller '%s' (chat_id: '%s') started a conversation with '%s' (chat_id: '%s').", selectedRequest["fulfiller_user_name"], selectedRequest["fulfiller_chat_id"],
selectedRequest["requester_user_name"], selectedRequest["requester_chat_id"])
return MainMenu.FULFILLER_IN_CONVO
async def forwardFulfillerMsg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request selected by the fulfiller.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_CHOSEN]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_CHOSEN]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the message the fulfiller is trying to send to the requester
fulfillerMsg = update.message.text
try:
# Get the request the fulfiller has chosen.
request = response["Item"]
context.user_data[MainMenu.REQUEST_CHOSEN] = request
except KeyError:
# This triggers in the event where the requester does /cancel instead of /end to
# gracefully end the convo, and the fulfiller tries to send a message to the requester.
await update.message.reply_text(f"The requester has deleted their request. Use /start to request or fulfill an order again.")
return ENDConv
# Check if requester has ended the chat before sending the message to the requester.
if (request["request_status"] == "Closed"):
await update.message.reply_text(f"'{request['requester_user_name']}' has ended the conversation. Use /start to request or fulfill an order again.")
return ENDConv
elif (request["request_status"] == "Complete"):
await update.message.reply_text(f"The conversation has ended. Please rate '{request['requester_user_name']}' Use /start to request or fulfill an order again.")
return ENDConv
# Else, store information about the event.
logger.info("Fulfiller '%s' (chat_id: '%s') sent message to requester '%s' (chat_id: '%s'): %s", request["fulfiller_user_name"], request["fulfiller_chat_id"],
request["requester_user_name"], request["requester_chat_id"], fulfillerMsg)
in_chat_reminder = f"<b><u>Fulfiller '{request['fulfiller_user_name']}' sent the following message:</u></b>\n\n"
await context.bot.send_message(chat_id=request['requester_chat_id'], text=in_chat_reminder + fulfillerMsg, parse_mode="HTML")
return MainMenu.FULFILLER_IN_CONVO
async def forwardRequesterMsg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request made by the requester.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_MADE]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_MADE]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the message the requester is trying to send to the fulfiller
requesterMsg = update.message.text
# Get the request the requester has put out.
request = response["Item"]
context.user_data[MainMenu.REQUEST_MADE] = request
# Check valid convo status.
if (request["request_status"] == "Closed"):
await update.message.reply_text(f"'{request['fulfiller_user_name']}' has ended the conversation. Use /start to request or fulfill an order again.")
return ENDConv
elif (request["request_status"] == "Complete"):
await update.message.reply_text(f"The conversation has ended. Please rate '{request['fulfiller_user_name']}' Use /start to request or fulfill an order again.")
return ENDConv
# Else, store information about the event.
logger.info("Requester '%s' (chat_id: '%s') sent message to fulfiller '%s' (chat_id: '%s'): %s", request["requester_user_name"], request["requester_chat_id"],
request["fulfiller_user_name"], request["fulfiller_chat_id"], requesterMsg)
in_chat_reminder = f"<b><u>Requester '{request['requester_user_name']}' sent the following message:</u></b>\n\n"
await context.bot.send_message(chat_id=request['fulfiller_chat_id'], text=in_chat_reminder + requesterMsg, parse_mode="HTML")
return MainMenu.REQUESTER_IN_CONVO
async def requesterComplete(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request made by the requester.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_MADE]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_MADE]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the request the requester has put out.
request = response["Item"]
context.user_data[MainMenu.REQUEST_MADE] = request
# Check if the stauts of the request is "In progress" before allowing requester to confirm the order.
if (request["request_status"] == "In Progress"):
# Check if fulfiller has already sent the "/complete command"
if (request["fulfiller_complete"] == "true"):
# If fulfiller has already confirmed the fulfillment of the request, send the appropriate message to the requester
# Send the appropriate message to the fulfiller
await context.bot.send_message(chat_id=request["fulfiller_chat_id"], text=f"The order is now complete. \n\nHow would you rate your interaction with {request['requester_user_name']}?", reply_markup = UserRatings.userRatingOptionsIK)
# Update request_status in DynamoDB to "Complete".
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET request_status = :status",
ExpressionAttributeValues = {
":status" : "Complete"
}
)
# Update status for request variable before updating user_data
request['request_status'] = "Complete"
context.user_data[MainMenu.REQUEST_MADE] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Prompt user to select a rating option
await update.message.reply_text(f"The order is now complete. \n\nHow would you rate your interaction with {request['fulfiller_user_name']}?", reply_markup = UserRatings.userRatingOptionsIK)
return MainMenu.RATE_USER
else:
# Else, requester is the one that initiated the confirmation of the request, send the appropritate message to the requester
await update.message.reply_text(f"You have initiated to mark the order as complete. Please wait for '{request['fulfiller_user_name']}' to confirm before you can leave a rating for them.")
# Send the appropriate message to the fulfiller
await context.bot.send_message(chat_id=request["fulfiller_chat_id"], text=f"'{request['requester_user_name']}' has initiated to make the order as complete. To confirm and leave a review for them, please use the '/complete' command.")
# Update requester_complete to "true"
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET requester_complete = :complete",
ExpressionAttributeValues = {
":complete" : "true"
}
)
# Update status for request variable before updating user_data
request['requester_complete'] = "true"
context.user_data[MainMenu.REQUEST_MADE] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
return MainMenu.REQUESTER_IN_CONVO
else:
await update.message.reply_text(f"Fulfiller has yet to be found. Please use the command only when a fulfiller has been found, and you have agreed with them to mark the order as complete.")
return MainMenu.AWAIT_FULFILLER
async def fulfillerComplete(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Query the DB for the request made by the fulfiller.
response = FulfillerDetails.get_item(context.user_data[MainMenu.REQUEST_MADE]["RequestID"])
# Log DynamoDB response
logger.info("DynamoDB get_item response for RequestID '%s': '%s'", context.user_data[MainMenu.REQUEST_MADE]["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Get the request the fulfiller has put out.
request = response["Item"]
context.user_data[MainMenu.REQUEST_CHOSEN] = request
# Check if requester has already sent the "/complete command"
if (request["requester_complete"] == "true"):
# If requester has already confirmed the fulfillment of the request, send the appropriate message to the requester
# Send the appropriate message to the requester
await context.bot.send_message(chat_id=request["requester_chat_id"],
text=f"The order is now complete. \n\nHow would you rate your interaction with {request['fulfiller_user_name']}?", reply_markup = UserRatings.userRatingOptionsIK)
# Update request_status in DynamoDB to "Complete".
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET request_status = :status",
ExpressionAttributeValues = {
":status" : "Complete"
}
)
# Update status for request variable before updating user_data
request['request_status'] = "Complete"
context.user_data[MainMenu.REQUEST_MADE] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
# Prompt user to select a rating option
await update.message.reply_text(f"The order is now complete. \n\nHow would you rate your interaction with {request['requester_user_name']}?", reply_markup = UserRatings.userRatingOptionsIK)
return MainMenu.RATE_USER
else:
# Else, fulfiller is the one that initiated the confirmation of the request, send the appropritate message to the fulfiller
await update.message.reply_text(f"You have initiated to mark the order as complete. Please wait for '{request['requester_user_name']}' to confirm before you can leave a rating for them.")
# Send the appropriate message to the requester
await context.bot.send_message(chat_id=request["requester_chat_id"], text=f"'{request['fulfiller_user_name']}' has initiated to make the order as complete. To confirm and leave a review for them, please use the '/complete' command.")
# Update fulfiller_complete to "true"
response = DynamoDB.table.update_item(
Key = {
"RequestID": request["RequestID"]
},
UpdateExpression = "SET fulfiller_complete = :complete",
ExpressionAttributeValues = {
":complete" : "true"
}
)
# Update status for request variable before updating user_data
request['fulfiller_complete'] = "true"
context.user_data[MainMenu.REQUEST_MADE] = request
logger.info("DynamoDB update_item response for RequestID '%s': '%s'", request["RequestID"], response["ResponseMetadata"]["HTTPStatusCode"])
return MainMenu.FULFILLER_IN_CONVO