forked from ruijietay/Dabao4Me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRatings.py
134 lines (100 loc) · 5.31 KB
/
UserRatings.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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler
import DynamoDB
import MainMenu
import logging
import FulfillerDetails
####################################### Parameters ###########################################
# Enable logging
logging.basicConfig(
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# Define GOOD and BAD for easier readability
GOOD = 1
BAD = 0
# Define the options using a 2D array.
userRatingOptions = [
[InlineKeyboardButton("\U0001F44D", callback_data = GOOD)],
[InlineKeyboardButton("\U0001F44E", callback_data = BAD)]
]
# Transform the 2D array into an actual inline keyboard (IK) that can be interpreted by Telegram.
userRatingOptionsIK = InlineKeyboardMarkup(userRatingOptions)
####################################### Helper Functions #####################################
def updateRatingTable(giver_chat_id, receiver_chat_id, rating):
# If GOOD rating given
if (int(rating) == GOOD):
# Increment giver's good_given
response = DynamoDB.userRatingsTable.update_item(
Key = {"user_chat_id": str(giver_chat_id)},
ExpressionAttributeValues = {":inc": 1},
UpdateExpression = "ADD good_given :inc"
)
logger.info("DynamoDB update_item response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
# Increment receiver's good_received
response = DynamoDB.userRatingsTable.update_item(
Key = {"user_chat_id": str(receiver_chat_id)},
ExpressionAttributeValues = {":inc": 1},
UpdateExpression = "ADD good_received :inc"
)
logger.info("DynamoDB update_item response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
if (int(rating) == BAD):
# Increment giver's bad_given
response = DynamoDB.userRatingsTable.update_item(
Key = {"user_chat_id": str(giver_chat_id)},
ExpressionAttributeValues = {":inc": 1},
UpdateExpression = "ADD bad_given :inc"
)
logger.info("DynamoDB update_item response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
# Increment receiver's bad_received
response = DynamoDB.userRatingsTable.update_item(
Key = {"user_chat_id": str(receiver_chat_id)},
ExpressionAttributeValues = {":inc": 1},
UpdateExpression = "ADD bad_received :inc"
)
logger.info("DynamoDB update_item response: %s", response["ResponseMetadata"]["HTTPStatusCode"])
####################################### Main Functions #######################################
async def inputUserRating(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Define the options using a 2D array.
userRatingOptions = [
[InlineKeyboardButton("\U0001F44D", callback_data = GOOD)],
[InlineKeyboardButton("\U0001F44E", callback_data = BAD)]
]
# Transform the 2D array into an actual inline keyboard (IK) that can be interpreted by Telegram.
userRatingOptionsIK = InlineKeyboardMarkup(userRatingOptions)
# Prompt user to select a rating option
await update.callback_query.message.reply_text("How would you rate your interaction with this user?", reply_markup = userRatingOptionsIK)
return MainMenu.UPDATE_RATINGS
async def updateUserRatings(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.callback_query.answer()
# Get the rating input by the user
ratingInput = update.callback_query.data
# Query the DB for the request made.
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"]
# Save the chat_id of the user talking to the bot
user_chat_id = update.effective_chat.id
# Get chat_id's of requester and fulfiller according to the database
requester_chat_id = request['requester_chat_id']
fulfiller_chat_id = request['fulfiller_chat_id']
# The person giving the rating is the one talking to the bot
# We do not know if the receiver is the fulfiller or requester at this stage.
giver_chat_id = user_chat_id
receiver_chat_id = "PLACEHOLDER"
# If the person giving the rating is fulfiller, the person receiving must be the requester, and vice versa
if (int(giver_chat_id) == int(fulfiller_chat_id)):
receiver_chat_id = requester_chat_id
else:
receiver_chat_id = fulfiller_chat_id
# Updates the table that stores user ratings
updateRatingTable(giver_chat_id, receiver_chat_id, ratingInput)
if (int(ratingInput) == GOOD):
logger.info(f"{giver_chat_id} gave {receiver_chat_id} a GOOD review.")
else:
logger.info(f"{giver_chat_id} gave {receiver_chat_id} a BAD review.")
# Sends rating confirmation message to the user
await context.bot.send_message(chat_id = user_chat_id, text = "Rating submitted! Use /start to use Dabao4Me again.")
return ConversationHandler.END