Skip to content

Commit fd4ed7f

Browse files
committed
Updated decisions to be a flow instead of random
1 parent 836167f commit fd4ed7f

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

backend/PyMatcha/utils/bot_actions.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import logging
33
from random import choice
4+
from random import choices
5+
from random import randrange
46

57
from chatterbot import ChatBot
68
from chatterbot.trainers import ChatterBotCorpusTrainer
@@ -18,7 +20,7 @@
1820
# TODO: DO superlikes
1921

2022

21-
def _bot_response(bot_name, user_input):
23+
def _prepare_chatbot(bot_name):
2224
logging.debug(f"Starting chatbot with name {bot_name}")
2325
chatbot = ChatBot(
2426
bot_name,
@@ -34,7 +36,7 @@ def _bot_response(bot_name, user_input):
3436
"chatterbot.corpus.english.humor",
3537
"PyMatcha.utils.dating",
3638
)
37-
return chatbot.get_response(user_input)
39+
return chatbot
3840

3941

4042
def _get_recommendations(bot_user: User):
@@ -47,8 +49,7 @@ def _get_recommendations(bot_user: User):
4749
return json.loads(recommendations)
4850

4951

50-
def botaction_like(bot_user: User):
51-
recommendations = _get_recommendations(bot_user)
52+
def _botaction_like(bot_user: User, recommendations):
5253
liked_ids = [like.liked_id for like in bot_user.get_likes_sent()]
5354
for user in recommendations:
5455
if user["id"] in liked_ids:
@@ -74,16 +75,15 @@ def botaction_unlike(bot_user: User):
7475
Like.get_multi(liker_id=bot_user.id, liked_id=id_to_unlike).delete()
7576

7677

77-
def botaction_view(bot_user: User):
78-
recommendations = _get_recommendations(bot_user)
78+
def _botaction_view(bot_user: User, recommendations):
7979
try:
8080
user_to_view = choice(recommendations)
8181
except IndexError:
8282
return
8383
View.create(profile_id=user_to_view["id"], viewer_id=bot_user.id)
8484

8585

86-
def botaction_message_new_conversation(bot_user: User):
86+
def _botaction_message_new_conversation(bot_user: User):
8787
matches = bot_user.get_matches()
8888
unopened_matches = []
8989
for match in matches:
@@ -105,7 +105,7 @@ def botaction_message_new_conversation(bot_user: User):
105105
bot_user.send_message(to_id=other_user, content=choice(BOT_CONV_OPENERS))
106106

107107

108-
def botaction_respond_to_unread(bot_user: User):
108+
def _botaction_respond_to_unread(bot_user: User, chatbot):
109109
last_message_list = bot_user.get_conversation_list()
110110

111111
unread_last_messages = []
@@ -116,11 +116,11 @@ def botaction_respond_to_unread(bot_user: User):
116116
message_to_reply = choice(unread_last_messages)
117117
except IndexError:
118118
return
119-
bot_reply = _bot_response(bot_user.username, message_to_reply.content)
119+
bot_reply = chatbot.get_response(message_to_reply.content)
120120
bot_user.send_message(to_id=message_to_reply.from_id, content=bot_reply)
121121

122122

123-
def botaction_send_message_over_old_one(bot_user: User):
123+
def _botaction_send_message_over_old_one(bot_user: User, chatbot):
124124
last_message_list = bot_user.get_conversation_list()
125125
try:
126126
message_to_reply = choice(last_message_list)
@@ -131,5 +131,41 @@ def botaction_send_message_over_old_one(bot_user: User):
131131
else:
132132
other_user = message_to_reply.to_id
133133

134-
bot_reply = _bot_response(bot_user.username, ".")
134+
bot_reply = chatbot.get_response(".")
135135
bot_user.send_message(to_id=other_user, content=bot_reply)
136+
137+
138+
def decide_bot_action(bot_user: User):
139+
recommendations = _get_recommendations(bot_user)
140+
141+
# The bot will first view 0 to 10 profiles
142+
for _ in range(0, randrange(0, 10)):
143+
_botaction_view(bot_user, recommendations)
144+
145+
# The bot will then like 0 to 3 profiles
146+
for _ in range(0, randrange(0, 3)):
147+
_botaction_like(bot_user, recommendations)
148+
149+
matches = bot_user.get_matches()
150+
if not matches:
151+
# No matches so far, no more actions to be done
152+
# TODO: Add unlike
153+
return
154+
155+
for match in matches:
156+
# if match.user_1 == bot_user.id:
157+
# other_user_id = match.user_2
158+
# else:
159+
# other_user_id = match.user_1
160+
161+
chatbot = _prepare_chatbot(bot_user.username)
162+
163+
message_actions = [
164+
_botaction_respond_to_unread,
165+
_botaction_message_new_conversation,
166+
_botaction_send_message_over_old_one,
167+
]
168+
selected_action = choices(message_actions, weights=[10, 9, 5], k=1)
169+
if selected_action[0]:
170+
# Has to have the [0] because random.choices return a list
171+
selected_action[0](bot_user, chatbot)

backend/PyMatcha/utils/tasks.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import datetime
22
import json
3-
import random
43
from math import ceil
54

65
from PyMatcha import celery
76
from PyMatcha import redis
87
from PyMatcha.models.message import Message
98
from PyMatcha.models.user import User
10-
from PyMatcha.utils.bot_actions import botaction_like
11-
from PyMatcha.utils.bot_actions import botaction_message_new_conversation
12-
from PyMatcha.utils.bot_actions import botaction_respond_to_unread
13-
from PyMatcha.utils.bot_actions import botaction_send_message_over_old_one
14-
from PyMatcha.utils.bot_actions import botaction_unlike
15-
from PyMatcha.utils.bot_actions import botaction_view
9+
from PyMatcha.utils.bot_actions import decide_bot_action
1610
from PyMatcha.utils.recommendations import create_user_recommendations
1711

12+
1813
BASE_HEAT_SCORE = 30
1914
LIKES_MULTIPLIER = 2
2015
SUPERLIKES_MULTIPLIER = 10
@@ -35,7 +30,7 @@ def setup_periodic_tasks(sender, **kwargs):
3530
sender.add_periodic_task(
3631
600, calc_search_min_max.s(), name="Update Minimum and Maximum scores and ages for search every 10 minutes"
3732
)
38-
sender.add_periodic_task(30, random_bot_action.s(), name="Have the only bots do an action")
33+
sender.add_periodic_task(30, random_bot_action.s(), name="200 random bots actions")
3934

4035

4136
@celery.task
@@ -153,17 +148,9 @@ def take_random_users_online():
153148

154149
@celery.task
155150
def random_bot_action():
156-
for user in User.get_multis(skip_recommendations=True, is_online=True):
157-
bot_actions = [
158-
None,
159-
botaction_view,
160-
botaction_like,
161-
botaction_respond_to_unread,
162-
botaction_message_new_conversation,
163-
botaction_send_message_over_old_one,
164-
botaction_unlike,
165-
]
166-
selected_action = random.choices(bot_actions, cum_weights=(100, 100, 95, 90, 90, 80, 50), k=1)
167-
if selected_action[0]:
168-
# Has to have the [0] because random.choices return a list
169-
selected_action[0](user)
151+
for user in User.select_random(200):
152+
if not user.is_online and not user.skip_recommendations:
153+
continue
154+
else:
155+
decide_bot_action(user)
156+
return "200 bots done actions"

0 commit comments

Comments
 (0)