Skip to content

Commit 48919d9

Browse files
committed
Added action for bot to reply
1 parent 895edbf commit 48919d9

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

backend/PyMatcha/routes/api/messages.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from PyMatcha.utils.success import Success
3333
from PyMatcha.utils.success import SuccessOutput
3434
from PyMatcha.utils.success import SuccessOutputMessage
35+
from PyMatcha.utils.tasks import bot_respond_to_message
3536
from timeago import format as timeago_format
3637

3738

@@ -77,7 +78,7 @@ def send_message():
7778
current_user.send_message(to_id=to_user.id, content=content)
7879
current_app.logger.debug("/messages -> Message successfully sent to {}.".format(to_uid))
7980

80-
new_message = Message.get_multis(to_id=to_user.id, content=content, from_id=current_user.id)[-1]
81+
new_message = Message.get_multis(to_id=to_user.id, from_id=current_user.id)[-1]
8182

8283
Notification.create(
8384
trigger_id=current_user.id,
@@ -87,6 +88,9 @@ def send_message():
8788
link_to=f"conversation/{current_user.id}",
8889
)
8990

91+
if to_user.is_bot:
92+
bot_respond_to_message.delay(bot_id=to_user.id, from_id=current_user.id, message_id=new_message.id)
93+
9094
return SuccessOutputMessage("new_message", new_message.to_dict(), "Message successfully sent to {}.".format(to_uid))
9195

9296

backend/PyMatcha/utils/tasks.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import datetime
22
import json
3+
import random
4+
import time
35
from math import ceil
46

57
from PyMatcha import celery
68
from PyMatcha import redis
79
from PyMatcha.models.message import Message
810
from PyMatcha.models.user import User
9-
from PyMatcha.utils.bot_actions import decide_bot_action
11+
from PyMatcha.utils.bot_actions import _prepare_chatbot
1012
from PyMatcha.utils.recommendations import create_user_recommendations
1113

14+
# from PyMatcha.utils.bot_actions import decide_bot_action
1215

1316
BASE_HEAT_SCORE = 30
1417
LIKES_MULTIPLIER = 2
@@ -30,7 +33,7 @@ def setup_periodic_tasks(sender, **kwargs):
3033
sender.add_periodic_task(
3134
600, calc_search_min_max.s(), name="Update Minimum and Maximum scores and ages for search every 10 minutes"
3235
)
33-
sender.add_periodic_task(10, random_bot_action.s(), name="Bots will do random actions")
36+
# sender.add_periodic_task(10, random_bot_action.s(), name="Bots will do random actions")
3437

3538

3639
@celery.task
@@ -146,8 +149,21 @@ def take_random_users_online():
146149
return "Successfully set 250 users online"
147150

148151

152+
# @celery.task
153+
# def random_bot_action():
154+
# for user in User.select_random_multis(10, is_bot=True, is_online=True):
155+
# decide_bot_action(user)
156+
# return "Done 10 random actions"
157+
158+
149159
@celery.task
150-
def random_bot_action():
151-
for user in User.select_random_multis(10, is_bot=True, is_online=True):
152-
decide_bot_action(user)
153-
return "Done 10 random actions"
160+
def bot_respond_to_message(bot_id: int, from_id: int, message_id: int):
161+
delay = random.randint(1, 10)
162+
time.sleep(delay)
163+
bot_user = User.get(id=bot_id)
164+
from_user = User.get(id=from_id)
165+
message = Message.get(id=message_id)
166+
chatbot = _prepare_chatbot(bot_user.username)
167+
reply = chatbot.get_response(message.content)
168+
bot_user.send_message(from_user.id, reply)
169+
return f"Bot {bot_id} successfully replied to {from_id}"

backend/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ Pillow==8.0.1
4141

4242
timeago==1.0.14
4343

44-
chatterbot==1.1.0
44+
chatterbot==1.1.0
45+
chatterbot-corpus==1.2.0

0 commit comments

Comments
 (0)