add new features
This commit is contained in:
parent
1415c0209e
commit
5b8c334424
@ -10,6 +10,8 @@ services:
|
|||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
|
MINIO_HOST: "minio.develop.sprinthub.ru"
|
||||||
|
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
|
||||||
networks:
|
networks:
|
||||||
- net
|
- net
|
||||||
deploy:
|
deploy:
|
||||||
|
@ -13,6 +13,8 @@ services:
|
|||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
|
MINIO_HOST: "minio.sprinthub.ru"
|
||||||
|
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
|
51
bot.py
51
bot.py
@ -1,9 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
import requests
|
||||||
import telebot
|
import telebot
|
||||||
from telebot.types import Message, ReplyKeyboardRemove
|
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"))
|
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||||
|
|
||||||
@ -12,7 +14,7 @@ class Core:
|
|||||||
def __init__(self, message: Message):
|
def __init__(self, message: Message):
|
||||||
self.message = message
|
self.message = message
|
||||||
self.chat_id = message.chat.id
|
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}')
|
print(f'Handled message from {self.chat_id}: {self.message_text}')
|
||||||
user = mongo.chats_collection.find_one({"chat_id": message.chat.id})
|
user = mongo.chats_collection.find_one({"chat_id": message.chat.id})
|
||||||
if user is None:
|
if user is None:
|
||||||
@ -64,6 +66,8 @@ class Core:
|
|||||||
self.send_message('🤖 Поиски собеседника продолжаются')
|
self.send_message('🤖 Поиски собеседника продолжаются')
|
||||||
|
|
||||||
def send_message(self, text, chat_id=None, reply_markup=None, remove_keyboard=True, **kwargs):
|
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:
|
if reply_markup is None and remove_keyboard:
|
||||||
reply_markup = ReplyKeyboardRemove()
|
reply_markup = ReplyKeyboardRemove()
|
||||||
bot.send_message(chat_id or self.chat_id, text, reply_markup=reply_markup, **kwargs)
|
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):
|
def handle_state_dialog(self):
|
||||||
current_dialog = mongo.get_current_dialog(self.chat_id)
|
current_dialog = mongo.get_current_dialog(self.chat_id)
|
||||||
mongo.create_message(self.message_text, current_dialog['_id'], self.chat_id)
|
chat_to_send = current_dialog['chat_id_2'] if current_dialog['chat_id_1'] == self.chat_id else current_dialog['chat_id_1']
|
||||||
if current_dialog['chat_id_1'] == self.chat_id:
|
res = mongo.create_message(self.message.content_type, self.message_text, current_dialog['_id'], self.chat_id)
|
||||||
self.send_message(self.message_text, current_dialog['chat_id_2'])
|
if self.message.photo:
|
||||||
else:
|
photo = requests.get(bot.get_file_url(self.message.photo[-1].file_id)).content
|
||||||
self.send_message(self.message_text, current_dialog['chat_id_1'])
|
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):
|
def start_new_dialog(self, chat_ids):
|
||||||
self.set_state('search', chat_ids)
|
self.set_state('search', chat_ids)
|
||||||
@ -99,8 +117,23 @@ class Core:
|
|||||||
|
|
||||||
|
|
||||||
def run_bot():
|
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):
|
def do_action(message: Message):
|
||||||
Core(message).process()
|
try:
|
||||||
|
Core(message).process()
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
print('bot is starting')
|
||||||
bot.polling()
|
bot.polling()
|
||||||
|
@ -1,15 +1,20 @@
|
|||||||
|
async-timeout==4.0.3
|
||||||
certifi==2022.12.7
|
certifi==2022.12.7
|
||||||
charset-normalizer==3.0.1
|
charset-normalizer==3.0.1
|
||||||
click==8.1.3
|
click==8.1.3
|
||||||
dnspython==2.3.0
|
dnspython==2.3.0
|
||||||
Flask==2.2.3
|
Flask==2.2.3
|
||||||
idna==3.4
|
idna==3.4
|
||||||
|
importlib-metadata==6.7.0
|
||||||
itsdangerous==2.1.2
|
itsdangerous==2.1.2
|
||||||
Jinja2==3.1.2
|
Jinja2==3.1.2
|
||||||
MarkupSafe==2.1.2
|
MarkupSafe==2.1.2
|
||||||
minio==7.1.13
|
minio==7.1.13
|
||||||
pymongo==4.3.3
|
pymongo==4.3.3
|
||||||
pyTelegramBotAPI==4.1.1
|
pyTelegramBotAPI==4.1.1
|
||||||
|
redis==5.0.4
|
||||||
requests==2.28.2
|
requests==2.28.2
|
||||||
|
typing_extensions==4.7.1
|
||||||
urllib3==1.26.14
|
urllib3==1.26.14
|
||||||
Werkzeug==2.2.3
|
Werkzeug==2.2.3
|
||||||
|
zipp==3.15.0
|
||||||
|
@ -3,3 +3,8 @@ import os
|
|||||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||||
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
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'
|
||||||
|
0
tools/__init__.py
Normal file
0
tools/__init__.py
Normal file
45
tools/minio.py
Normal file
45
tools/minio.py
Normal file
@ -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
|
||||||
|
)
|
@ -44,8 +44,9 @@ class Mongo:
|
|||||||
def get_current_dialog(self, chat_id):
|
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})
|
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):
|
def create_message(self, message_type, text, dialog_id, sender):
|
||||||
self.messages_collection.insert_one({
|
return self.messages_collection.insert_one({
|
||||||
|
'message_type': message_type,
|
||||||
'dialog_id': dialog_id,
|
'dialog_id': dialog_id,
|
||||||
'text': text,
|
'text': text,
|
||||||
'sender': sender,
|
'sender': sender,
|
Loading…
Reference in New Issue
Block a user