refactor: modularize Telegram bot and add RabbitMQ client foundation
Build and Push Docker Images / build (push) Successful in 1m28s

- Рефакторинг Telegram бота на модульную структуру для удобства поддержки и расширения
- Создан общий RabbitMQ клиент для Flask и Telegram компонентов
- Подготовлена базовая архитектура для будущего масштабирования и новых функций

Signed-off-by: UdoChudo <stream@udochudo.ru>
This commit is contained in:
2025-06-16 09:08:46 +05:00
parent 52e31864b3
commit ccb47d527f
72 changed files with 1677 additions and 1675 deletions
+25
View File
@@ -0,0 +1,25 @@
import json
import requests
from config import TOKEN
TELEGRAM_API_URL = "https://api.telegram.org/bot{}/sendMessage".format(TOKEN)
def bot_send_message(chat_id: int, text: str):
keyboard = {
"keyboard": [
[{"text": "Продолжить"}]
],
"resize_keyboard": True,
"one_time_keyboard": True,
}
payload = {'chat_id': chat_id, 'text': text, 'reply_markup': json.dumps(keyboard)}
try:
response = requests.get(TELEGRAM_API_URL, json=payload)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
pass
+1
View File
@@ -1,3 +1,4 @@
#app/extensions/db.py
from flask_sqlalchemy import SQLAlchemy
db: SQLAlchemy = SQLAlchemy()
+63
View File
@@ -0,0 +1,63 @@
import pika
from config import RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_LOGIN, RABBITMQ_PASS, RABBITMQ_QUEUE
import time
class RabbitMQClient:
def __init__(self):
self._connect()
def _connect(self):
credentials = pika.PlainCredentials(RABBITMQ_LOGIN, RABBITMQ_PASS)
parameters = pika.ConnectionParameters(
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=600,
blocked_connection_timeout=300
)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True)
def publish_message(self, message: str, retry=1):
try:
if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed:
self._connect()
self.channel.basic_publish(
exchange='',
routing_key=RABBITMQ_QUEUE,
body=message,
properties=pika.BasicProperties(
delivery_mode=2,
)
)
except (pika.exceptions.ChannelClosedByBroker,
pika.exceptions.ConnectionClosed,
pika.exceptions.AMQPConnectionError) as e:
if retry > 0:
# Короткая пауза перед переподключением
time.sleep(1)
self._connect()
self.publish_message(message, retry=retry-1)
else:
raise e
def close(self):
if self.connection and self.connection.is_open:
self.connection.close()
def close(self):
if self.connection and self.connection.is_open:
self.connection.close()
# Глобальная переменная
rabbitmq_client = None
def get_rabbitmq_client():
global rabbitmq_client
if rabbitmq_client is None:
rabbitmq_client = RabbitMQClient()
return rabbitmq_client