diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 94c6e29..0e9f032 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -10,6 +10,8 @@ services: TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV MONGO_HOST: "mongo.develop.sprinthub.ru" MONGO_PASSWORD: $MONGO_PASSWORD_DEV + MINIO_HOST: "minio.develop.sprinthub.ru" + MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV networks: - net deploy: diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index 66924a5..21ea660 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -13,6 +13,8 @@ services: TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD MONGO_HOST: "mongo.sprinthub.ru" MONGO_PASSWORD: $MONGO_PASSWORD_PROD + MINIO_HOST: "minio.sprinthub.ru" + MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD deploy: mode: replicated restart_policy: diff --git a/bot.py b/bot.py index b983e61..d59e98e 100644 --- a/bot.py +++ b/bot.py @@ -1,9 +1,11 @@ import os +import requests import telebot from telebot.types import Message, ReplyKeyboardRemove -from mongo import mongo +from tools.minio import minio_client as minio +from tools.mongo import mongo bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN")) @@ -12,7 +14,7 @@ class Core: def __init__(self, message: Message): self.message = message self.chat_id = message.chat.id - self.message_text = message.text + self.message_text = message.text or message.caption or "" print(f'Handled message from {self.chat_id}: {self.message_text}') user = mongo.chats_collection.find_one({"chat_id": message.chat.id}) if user is None: @@ -64,6 +66,8 @@ class Core: self.send_message('πŸ€– Поиски собСсСдника ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°ΡŽΡ‚ΡΡ') def send_message(self, text, chat_id=None, reply_markup=None, remove_keyboard=True, **kwargs): + if not text: + return if reply_markup is None and remove_keyboard: reply_markup = ReplyKeyboardRemove() bot.send_message(chat_id or self.chat_id, text, reply_markup=reply_markup, **kwargs) @@ -79,11 +83,25 @@ class Core: def handle_state_dialog(self): current_dialog = mongo.get_current_dialog(self.chat_id) - mongo.create_message(self.message_text, current_dialog['_id'], self.chat_id) - if current_dialog['chat_id_1'] == self.chat_id: - self.send_message(self.message_text, current_dialog['chat_id_2']) - else: - self.send_message(self.message_text, current_dialog['chat_id_1']) + chat_to_send = current_dialog['chat_id_2'] if current_dialog['chat_id_1'] == self.chat_id else current_dialog['chat_id_1'] + res = mongo.create_message(self.message.content_type, self.message_text, current_dialog['_id'], self.chat_id) + if self.message.photo: + photo = requests.get(bot.get_file_url(self.message.photo[-1].file_id)).content + minio.put_object(f"photos/{res.inserted_id}.jpg", photo) + bot.send_photo(chat_to_send, self.message.photo[-1].file_id) + if self.message.sticker: + sticker = requests.get(bot.get_file_url(self.message.sticker.file_id)).content + minio.put_object(f"stickers/{res.inserted_id}.jpg", sticker) + bot.send_sticker(chat_to_send, self.message.sticker.file_id) + if self.message.voice: + voice = requests.get(bot.get_file_url(self.message.voice.file_id)).content + minio.put_object(f"voices/{res.inserted_id}.jpg", voice) + bot.send_voice(chat_to_send, self.message.voice.file_id) + if self.message.video_note: + video_note = requests.get(bot.get_file_url(self.message.video_note.file_id)).content + minio.put_object(f"video_notes/{res.inserted_id}.jpg", video_note) + bot.send_video_note(chat_to_send, self.message.video_note.file_id) + self.send_message(self.message_text, chat_to_send) def start_new_dialog(self, chat_ids): self.set_state('search', chat_ids) @@ -99,8 +117,23 @@ class Core: def run_bot(): - @bot.message_handler() + @bot.message_handler(content_types=[ + # 'audio', + 'photo', + 'voice', + 'video_note', + # 'document', + 'text', + # 'location', + # 'contact', + 'sticker' + ] + ) def do_action(message: Message): - Core(message).process() + try: + Core(message).process() + except Exception as e: + print(e) + print('bot is starting') bot.polling() diff --git a/requirements.txt b/requirements.txt index 1850856..539e7bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,15 +1,20 @@ +async-timeout==4.0.3 certifi==2022.12.7 charset-normalizer==3.0.1 click==8.1.3 dnspython==2.3.0 Flask==2.2.3 idna==3.4 +importlib-metadata==6.7.0 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.2 minio==7.1.13 pymongo==4.3.3 pyTelegramBotAPI==4.1.1 +redis==5.0.4 requests==2.28.2 +typing_extensions==4.7.1 urllib3==1.26.14 Werkzeug==2.2.3 +zipp==3.15.0 diff --git a/settings.py b/settings.py index 6eef607..3ecd81b 100644 --- a/settings.py +++ b/settings.py @@ -3,3 +3,8 @@ import os MONGO_USER = os.getenv("MONGO_USER", "mongo") MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password") MONGO_HOST = os.getenv("MONGO_HOST", "localhost") + +MINIO_HOST = os.getenv("MINIO_HOST", "localhost") + ":9000" +MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin") +MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin") +MINIO_BUCKET_NAME = 'ruletka' diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/minio.py b/tools/minio.py new file mode 100644 index 0000000..1498e29 --- /dev/null +++ b/tools/minio.py @@ -0,0 +1,45 @@ +import io + +from minio import Minio +from minio.error import MinioException + +import settings + + +class Client: + + def __init__(self, host: str, access_key: str, secret_key: str, bucket_name: str): + self.bucket_name = bucket_name + self.cli = Minio( + host, + access_key=access_key, + secret_key=secret_key, + secure=False + ) + try: + self.cli.make_bucket(bucket_name) + except MinioException: + pass + + def put_object(self, name: str, data: bytes): + self.cli.put_object(self.bucket_name, name, io.BytesIO(data), len(data)) + + def get_object(self, name: str) -> bytes: + try: + return self.cli.get_object(self.bucket_name, name).data + except MinioException: + return b"" + + def delete_object(self, name: str): + try: + self.cli.remove_object(self.bucket_name, name) + except MinioException: + pass + + +minio_client = Client( + settings.MINIO_HOST, + settings.MINIO_ACCESS_KEY, + settings.MINIO_SECRET_KEY, + settings.MINIO_BUCKET_NAME +) diff --git a/mongo.py b/tools/mongo.py similarity index 91% rename from mongo.py rename to tools/mongo.py index c129a4d..58c46fc 100644 --- a/mongo.py +++ b/tools/mongo.py @@ -44,8 +44,9 @@ class Mongo: def get_current_dialog(self, chat_id): return self.dialogs_collection.find_one({'$or': [{'chat_id_1': chat_id}, {'chat_id_2': chat_id}], 'finished_at': None}) - def create_message(self, text, dialog_id, sender): - self.messages_collection.insert_one({ + def create_message(self, message_type, text, dialog_id, sender): + return self.messages_collection.insert_one({ + 'message_type': message_type, 'dialog_id': dialog_id, 'text': text, 'sender': sender,