Skip to content

Commit 55e0fb6

Browse files
committed
Added action to reply to unread message
1 parent 37e950c commit 55e0fb6

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

backend/PyMatcha/utils/bot_actions.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,28 @@ def botaction_like(bot_user: User, is_superlike: bool):
5151
for user in recommendations:
5252
if user["id"] in liked_ids:
5353
recommendations.remove(user)
54-
user_to_like = choice(recommendations)
54+
try:
55+
user_to_like = choice(recommendations)
56+
except IndexError:
57+
return
5558
Like.create(liker_id=bot_user.id, liked_id=user_to_like["id"], is_superlike=is_superlike)
5659

5760

5861
def botaction_unlike(bot_user: User):
5962
liked_ids = [like.liked_id for like in bot_user.get_likes_sent()]
60-
id_to_unlike = choice(liked_ids)
63+
try:
64+
id_to_unlike = choice(liked_ids)
65+
except IndexError:
66+
return
6167
Like.get_multi(liker_id=bot_user.id, liked_id=id_to_unlike).delete()
6268

6369

6470
def botaction_view(bot_user: User):
6571
recommendations = _get_recommendations(bot_user)
66-
user_to_view = choice(recommendations)
72+
try:
73+
user_to_view = choice(recommendations)
74+
except IndexError:
75+
return
6776
View.create(profile_id=user_to_view["id"], viewer_id=bot_user.id)
6877

6978

@@ -75,11 +84,30 @@ def botaction_message_new_conversation(bot_user: User):
7584
msg_2 = Message.get_multi(from_id=match.user_2, to_id=match.user_1)
7685
if not msg_1 and not msg_2:
7786
unopened_matches.append(match)
78-
match_to_open_conv = choice(unopened_matches)
87+
88+
try:
89+
match_to_open_conv = choice(unopened_matches)
90+
except IndexError:
91+
return
7992

8093
if match_to_open_conv.user_1 == bot_user.id:
8194
other_user = match_to_open_conv.user_2
8295
else:
8396
other_user = match_to_open_conv.user_1
8497

85-
Message.create(from_id=bot_user.id, to_id=other_user, content=choice(BOT_CONV_OPENERS))
98+
bot_user.send_message(to_id=other_user, content=choice(BOT_CONV_OPENERS))
99+
100+
101+
def botaction_respond_to_unread(bot_user: User):
102+
last_message_list = bot_user.get_conversation_list()
103+
104+
unread_last_messages = []
105+
for last_message in last_message_list:
106+
if not last_message.is_seen and last_message.to_id == bot_user.id:
107+
unread_last_messages.append(last_message)
108+
try:
109+
message_to_reply = choice(unread_last_messages)
110+
except IndexError:
111+
return
112+
bot_reply = _bot_response(bot_user.username, message_to_reply.content)
113+
bot_user.send_message(to_id=message_to_reply.from_id, content=bot_reply)

0 commit comments

Comments
 (0)