Telezab/app/bot/processors/subscribe_processor.py
UdoChudo ccb47d527f
All checks were successful
Build and Push Docker Images / build (push) Successful in 1m28s
refactor: modularize Telegram bot and add RabbitMQ client foundation
- Рефакторинг Telegram бота на модульную структуру для удобства поддержки и расширения
- Создан общий RabbitMQ клиент для Flask и Telegram компонентов
- Подготовлена базовая архитектура для будущего масштабирования и новых функций

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-16 09:08:46 +05:00

76 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from telebot.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, MessageID
from app.bot.keyboards.settings_menu import get_settings_menu
from app.extensions.db import db
from app import Regions, Subscriptions
from app.bot.utils.tg_audit import log_user_event
from app.bot.constants import UserStates
def process_subscription_button(message: Message, app, bot, chat_id: int, state_manager, bot_message: MessageID):
parts = [part.strip() for part in message.text.split(',')]
if not parts or not all(part.isdigit() for part in parts):
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton(text="Отмена", callback_data="cancel_input"))
bot.send_message(chat_id,
"❌ Неверный ввод, введите число(а) через запятую, либо нажмите отмена.",
reply_markup=markup)
def delayed_handler(msg):
process_subscription_button(msg, app, bot, chat_id, state_manager)
bot.register_next_step_handler(message, delayed_handler)
return
bot.delete_message(chat_id, bot_message)
region_ids = [int(part) for part in parts]
try:
with app.app_context():
valid_region_ids = [r.region_id for r in Regions.query.filter(Regions.active == True).all()]
subbed_regions = []
invalid_regions = []
for region_id in region_ids:
if region_id not in valid_region_ids:
invalid_regions.append(str(region_id))
continue
subscription = Subscriptions.query.filter_by(chat_id=chat_id, region_id=region_id).first()
if subscription:
if not subscription.active:
subscription.active = True
db.session.add(subscription)
subbed_regions.append(str(region_id))
else:
new_sub = Subscriptions(chat_id=chat_id, region_id=region_id, active=True)
db.session.add(new_sub)
subbed_regions.append(str(region_id))
db.session.commit()
except Exception as e:
bot.send_message(chat_id, "⚠️ Произошла ошибка при обработке запроса. Попробуйте позже.")
return
if invalid_regions:
bot.send_message(chat_id,
f"⚠️ Регион с ID {', '.join(invalid_regions)} не существует. Введите корректные номера или 'отмена'.")
# Можно не менять состояние, чтобы пользователь мог повторить ввод
# И снова ждём ввод:
bot.register_next_step_handler(message, process_subscription_button, bot, chat_id, state_manager)
return
if subbed_regions:
username = f"{message.from_user.username}" if message.from_user.username else "N/A"
log_user_event(chat_id, app, username, f"Подписался на регионы: {', '.join(subbed_regions)}")
# Сбрасываем состояние, чтобы не продолжать ждать ввод
state_manager.set_state(chat_id, UserStates.SETTINGS_MENU)
# Показываем меню
bot.send_message(chat_id, f"✅ Подписка на регионы: {', '.join(subbed_regions)} оформлена.", reply_markup=get_settings_menu())