from config import Config from utils.antispam import admin_required from aiogram import Dispatcher, Bot from aiogram.types import Message from models.state import BotState from aiogram.filters import Command from logging import getLogger from aiogram.types import PollAnswer from storage.message_storage import save_message logger = getLogger(__name__) def register_handlers(dp: Dispatcher, state: BotState, bot: Bot): @dp.message(Command("poll")) @admin_required(5) async def send_poll(message: Message): for chat_id in Config.CHAT_IDS: try: poll_msg = await bot.send_poll( chat_id=chat_id, question="Кто опоздает?", options=["Я", "Я очень сильно опоздаю", "Наверное"], is_anonymous=False, allows_multiple_answers=False, ) await bot.pin_chat_message( chat_id, poll_msg.message_id, disable_notification=False ) # сохраняем сам опрос save_message(poll_msg.chat.id, poll_msg.message_id) logger.info(f"Опрос отправлен в чат {chat_id}") except Exception as e: logger.error(f"Ошибка при отправке в чат {chat_id}: {e}") @dp.poll_answer() async def handle_poll_answer(poll_answer: PollAnswer): user = poll_answer.user option_ids = poll_answer.option_ids # username или fallback на имя username = f"@{user.username}" if user.username else user.first_name # всегда пишем в первый чат из Config.CHAT_IDS # 6394047531 if option_ids and option_ids[0] == 0: msg = await bot.send_message( chat_id=6394047531, text=f"{username} опоздает" ) save_message(msg.chat.id, msg.message_id) elif option_ids and option_ids[0] == 1: msg = await bot.send_message( chat_id=6394047531, text=f"{username} сильно опоздает" ) save_message(msg.chat.id, msg.message_id) elif option_ids and option_ids[0] == 2: msg = await bot.send_message( chat_id=6394047531, text=f"{username} возможно опоздает" ) save_message(msg.chat.id, msg.message_id) elif not option_ids: msg = await bot.send_message( chat_id=6394047531, text=f"{username} Отменил свой голос" ) save_message(msg.chat.id, msg.message_id) else: msg = await bot.send_message( chat_id=6394047531, text=f"{username} выбрал вариант {option_ids}" ) save_message(msg.chat.id, msg.message_id)