|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from random import choice |
| 4 | +from random import choices |
| 5 | +from random import randrange |
| 6 | + |
| 7 | +from chatterbot import ChatBot |
| 8 | +from chatterbot.trainers import ChatterBotCorpusTrainer |
| 9 | +from PyMatcha import redis |
| 10 | +from PyMatcha.models.like import Like |
| 11 | +from PyMatcha.models.match import Match |
| 12 | +from PyMatcha.models.message import Message |
| 13 | +from PyMatcha.models.user import User |
| 14 | +from PyMatcha.models.view import View |
| 15 | +from PyMatcha.utils.recommendations import create_user_recommendations |
| 16 | +from PyMatcha.utils.static import BACKEND_ROOT |
| 17 | +from PyMatcha.utils.static import BOT_CONV_OPENERS |
| 18 | + |
| 19 | + |
| 20 | +# TODO: DO superlikes |
| 21 | + |
| 22 | + |
| 23 | +def _prepare_chatbot(bot_name): |
| 24 | + logging.debug(f"Starting chatbot with name {bot_name}") |
| 25 | + chatbot = ChatBot( |
| 26 | + bot_name, |
| 27 | + storage_adapter="chatterbot.storage.SQLStorageAdapter", |
| 28 | + database_uri=f"sqlite:///{BACKEND_ROOT}/../chatbot_database.sqlite3", |
| 29 | + ) |
| 30 | + |
| 31 | + trainer = ChatterBotCorpusTrainer(chatbot, show_training_progress=False) |
| 32 | + trainer.train( |
| 33 | + "chatterbot.corpus.english.conversations", |
| 34 | + "chatterbot.corpus.english.emotion", |
| 35 | + "chatterbot.corpus.english.greetings", |
| 36 | + "chatterbot.corpus.english.humor", |
| 37 | + "PyMatcha.utils.dating", |
| 38 | + ) |
| 39 | + return chatbot |
| 40 | + |
| 41 | + |
| 42 | +def _get_recommendations(bot_user: User, ignore_bots: bool): |
| 43 | + recommendations = redis.get(f"user_recommendations:{str(bot_user.id)}") |
| 44 | + if not recommendations: |
| 45 | + create_user_recommendations(bot_user, ignore_bots) |
| 46 | + recommendations = redis.get(f"user_recommendations:{str(bot_user.id)}") |
| 47 | + if not recommendations: |
| 48 | + raise ValueError("Recommendations could not be calculated") |
| 49 | + return json.loads(recommendations) |
| 50 | + |
| 51 | + |
| 52 | +def _botaction_like(bot_user: User, recommendations): |
| 53 | + liked_ids = [like.liked_id for like in bot_user.get_likes_sent()] |
| 54 | + for user in recommendations: |
| 55 | + if user["id"] in liked_ids: |
| 56 | + recommendations.remove(user) |
| 57 | + try: |
| 58 | + user_to_like = choice(recommendations) |
| 59 | + except IndexError: |
| 60 | + return |
| 61 | + user_to_like = User.get(id=user_to_like["id"]) |
| 62 | + View.create(profile_id=user_to_like.id, viewer_id=bot_user.id) |
| 63 | + Like.create(liker_id=bot_user.id, liked_id=user_to_like.id) |
| 64 | + |
| 65 | + if user_to_like.already_likes(bot_user.id): |
| 66 | + Match.create(user_1=bot_user.id, user_2=user_to_like.id) |
| 67 | + |
| 68 | + |
| 69 | +def botaction_unlike(bot_user: User): |
| 70 | + liked_ids = [like.liked_id for like in bot_user.get_likes_sent()] |
| 71 | + try: |
| 72 | + id_to_unlike = choice(liked_ids) |
| 73 | + except IndexError: |
| 74 | + return |
| 75 | + Like.get_multi(liker_id=bot_user.id, liked_id=id_to_unlike).delete() |
| 76 | + |
| 77 | + |
| 78 | +def _botaction_view(bot_user: User, recommendations): |
| 79 | + try: |
| 80 | + user_to_view = choice(recommendations) |
| 81 | + except IndexError: |
| 82 | + return |
| 83 | + View.create(profile_id=user_to_view["id"], viewer_id=bot_user.id) |
| 84 | + |
| 85 | + |
| 86 | +def _botaction_message_new_conversation(bot_user: User): |
| 87 | + matches = bot_user.get_matches() |
| 88 | + unopened_matches = [] |
| 89 | + for match in matches: |
| 90 | + msg_1 = Message.get_multi(from_id=match.user_1, to_id=match.user_2) |
| 91 | + msg_2 = Message.get_multi(from_id=match.user_2, to_id=match.user_1) |
| 92 | + if not msg_1 and not msg_2: |
| 93 | + unopened_matches.append(match) |
| 94 | + |
| 95 | + try: |
| 96 | + match_to_open_conv = choice(unopened_matches) |
| 97 | + except IndexError: |
| 98 | + return |
| 99 | + |
| 100 | + if match_to_open_conv.user_1 == bot_user.id: |
| 101 | + other_user = match_to_open_conv.user_2 |
| 102 | + else: |
| 103 | + other_user = match_to_open_conv.user_1 |
| 104 | + |
| 105 | + bot_user.send_message(to_id=other_user, content=choice(BOT_CONV_OPENERS)) |
| 106 | + |
| 107 | + |
| 108 | +def _botaction_respond_to_unread(bot_user: User, chatbot): |
| 109 | + last_message_list = bot_user.get_conversation_list() |
| 110 | + |
| 111 | + unread_last_messages = [] |
| 112 | + for last_message in last_message_list: |
| 113 | + if not last_message.is_seen and last_message.to_id == bot_user.id: |
| 114 | + unread_last_messages.append(last_message) |
| 115 | + try: |
| 116 | + message_to_reply = choice(unread_last_messages) |
| 117 | + except IndexError: |
| 118 | + return |
| 119 | + bot_reply = chatbot.get_response(message_to_reply.content) |
| 120 | + bot_user.send_message(to_id=message_to_reply.from_id, content=bot_reply.text) |
| 121 | + |
| 122 | + |
| 123 | +def _botaction_send_message_over_old_one(bot_user: User, chatbot): |
| 124 | + last_message_list = bot_user.get_conversation_list() |
| 125 | + try: |
| 126 | + message_to_reply = choice(last_message_list) |
| 127 | + except IndexError: |
| 128 | + return |
| 129 | + if message_to_reply.to_id == bot_user.id: |
| 130 | + other_user = message_to_reply.from_id |
| 131 | + else: |
| 132 | + other_user = message_to_reply.to_id |
| 133 | + |
| 134 | + bot_reply = chatbot.get_response(".") |
| 135 | + bot_user.send_message(to_id=other_user, content=bot_reply.text) |
| 136 | + |
| 137 | + |
| 138 | +def decide_bot_action(bot_user: User): |
| 139 | + recommendations = _get_recommendations(bot_user, ignore_bots=True) |
| 140 | + |
| 141 | + # The bot will first view 0 to 3 profiles |
| 142 | + for _ in range(0, randrange(0, 3)): |
| 143 | + _botaction_view(bot_user, recommendations) |
| 144 | + |
| 145 | + # The bot will then like 0 to 2 profiles |
| 146 | + for _ in range(0, randrange(0, 2)): |
| 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 | + chatbot = _prepare_chatbot(bot_user.username) |
| 157 | + message_actions = [ |
| 158 | + _botaction_respond_to_unread, |
| 159 | + _botaction_message_new_conversation, |
| 160 | + _botaction_send_message_over_old_one, |
| 161 | + ] |
| 162 | + selected_action = choices(message_actions, weights=[10, 9, 5], k=1) |
| 163 | + if selected_action[0]: |
| 164 | + # Has to have the [0] because random.choices return a list |
| 165 | + selected_action[0](bot_user, chatbot) |
0 commit comments