Skip to content

Commit e206bc0

Browse files
committed
Added notifications for bot actions
1 parent eba6077 commit e206bc0

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

backend/PyMatcha/utils/bot_actions.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from PyMatcha.models.like import Like
1111
from PyMatcha.models.match import Match
1212
from PyMatcha.models.message import Message
13+
from PyMatcha.models.notification import Notification
1314
from PyMatcha.models.user import User
1415
from PyMatcha.models.view import View
1516
from PyMatcha.utils.recommendations import create_user_recommendations
@@ -60,10 +61,30 @@ def _botaction_like(bot_user: User, recommendations):
6061
return
6162
user_to_like = User.get(id=user_to_like["id"])
6263
View.create(profile_id=user_to_like.id, viewer_id=bot_user.id)
64+
Notification.create(
65+
trigger_id=bot_user.id,
66+
user_id=user_to_like["id"],
67+
content=f"{bot_user.first_name} just viewed your profile! Go check their profile out!",
68+
type="view",
69+
link_to=f"users/{bot_user.id}",
70+
)
6371
Like.create(liker_id=bot_user.id, liked_id=user_to_like.id)
64-
72+
Notification.create(
73+
trigger_id=bot_user.id,
74+
user_id=user_to_like.id,
75+
content=f"{bot_user.first_name} liked you! Go check them out!",
76+
type="like",
77+
link_to=f"users/{bot_user.id}",
78+
)
6579
if user_to_like.already_likes(bot_user.id):
6680
Match.create(user_1=bot_user.id, user_2=user_to_like.id)
81+
Notification.create(
82+
trigger_id=bot_user.id,
83+
user_id=user_to_like.id,
84+
content=f"You and {bot_user.first_name} matched!",
85+
type="match",
86+
link_to=f"conversation/{bot_user.id}",
87+
)
6788

6889

6990
def botaction_unlike(bot_user: User):
@@ -81,6 +102,13 @@ def _botaction_view(bot_user: User, recommendations):
81102
except IndexError:
82103
return
83104
View.create(profile_id=user_to_view["id"], viewer_id=bot_user.id)
105+
Notification.create(
106+
trigger_id=bot_user.id,
107+
user_id=user_to_view["id"],
108+
content=f"{bot_user.first_name} just viewed your profile! Go check their profile out!",
109+
type="view",
110+
link_to=f"users/{bot_user.id}",
111+
)
84112

85113

86114
def _botaction_message_new_conversation(bot_user: User):
@@ -102,7 +130,15 @@ def _botaction_message_new_conversation(bot_user: User):
102130
else:
103131
other_user = match_to_open_conv.user_1
104132

105-
bot_user.send_message(to_id=other_user, content=choice(BOT_CONV_OPENERS))
133+
content = choice(BOT_CONV_OPENERS)
134+
bot_user.send_message(to_id=other_user, content=content)
135+
Notification.create(
136+
trigger_id=bot_user.id,
137+
user_id=other_user,
138+
content=f"{bot_user.first_name} said: {content}",
139+
type="message",
140+
link_to=f"conversation/{bot_user.id}",
141+
)
106142

107143

108144
def _botaction_respond_to_unread(bot_user: User, chatbot):
@@ -118,6 +154,13 @@ def _botaction_respond_to_unread(bot_user: User, chatbot):
118154
return
119155
bot_reply = chatbot.get_response(message_to_reply.content)
120156
bot_user.send_message(to_id=message_to_reply.from_id, content=bot_reply.text)
157+
Notification.create(
158+
trigger_id=bot_user.id,
159+
user_id=message_to_reply.from_id,
160+
content=f"{bot_user.first_name} said: {bot_reply.text}",
161+
type="message",
162+
link_to=f"conversation/{bot_user.id}",
163+
)
121164

122165

123166
def _botaction_send_message_over_old_one(bot_user: User, chatbot):
@@ -133,6 +176,13 @@ def _botaction_send_message_over_old_one(bot_user: User, chatbot):
133176

134177
bot_reply = chatbot.get_response(".")
135178
bot_user.send_message(to_id=other_user, content=bot_reply.text)
179+
Notification.create(
180+
trigger_id=bot_user.id,
181+
user_id=message_to_reply.from_id,
182+
content=f"{bot_user.first_name} said: {bot_reply.text}",
183+
type="message",
184+
link_to=f"conversation/{bot_user.id}",
185+
)
136186

137187

138188
def decide_bot_action(bot_user: User):

backend/PyMatcha/utils/tasks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from PyMatcha import celery
66
from PyMatcha import redis
77
from PyMatcha.models.message import Message
8+
from PyMatcha.models.notification import Notification
89
from PyMatcha.models.user import User
910
from PyMatcha.utils.bot_actions import _prepare_chatbot
1011
from PyMatcha.utils.bot_actions import decide_bot_action
@@ -161,5 +162,12 @@ def bot_respond_to_message(bot_id: int, from_id: int, message_content: str):
161162
chatbot = _prepare_chatbot(bot_user.username)
162163
reply = chatbot.get_response(message_content)
163164
bot_user.send_message(from_user.id, reply.text)
165+
Notification.create(
166+
trigger_id=bot_id,
167+
user_id=from_id,
168+
content=f"{bot_user.first_name} said: {reply.text}",
169+
type="message",
170+
link_to=f"conversation/{bot_user.id}",
171+
)
164172

165173
return f"Bot {bot_id} successfully replied to {from_id}"

0 commit comments

Comments
 (0)