11import json
22import logging
33from random import choice
4+ from random import choices
5+ from random import randrange
46
57from chatterbot import ChatBot
68from chatterbot .trainers import ChatterBotCorpusTrainer
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
4042def _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 )
0 commit comments