Files
myfirstprogram/addons/poll/handlers.py
T
2025-11-23 23:17:00 +03:00

93 lines
3.8 KiB
Python

from config import Config
from utils.antispam import admin_required
from aiogram import Dispatcher, Bot
from aiogram.types import Message, FSInputFile
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=["Я", "Я очень сильно опоздаю", "Я пиздец как опоздаю", "Наверное", "я ДОЛБОЯЩЕР и я к 2 паре", "Не опоздаю"],
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
#850906163
STARAST = 6394047531
if not option_ids:
msg = await bot.send_message(
chat_id=STARAST, text=f"{username} Отменил свой голос"
)
save_message(msg.chat.id, msg.message_id)
elif option_ids and option_ids[0] == 0:
msg = await bot.send_message(
chat_id=STARAST, 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=STARAST, 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=STARAST, text=f"{username} Пиздец опоздает"
)
save_message(msg.chat.id, msg.message_id)
elif option_ids[0] == 3:
msg = await bot.send_message(
chat_id=STARAST, text=f"{username} возможно опоздает"
)
save_message(msg.chat.id, msg.message_id)
elif option_ids[0] == 4:
photo = FSInputFile("/Users/mac/myfirstprogramm/addons/poll/img.png")
msg = await bot.send_photo(
chat_id=STARAST, photo=photo, caption=f"{username} ДОЛБОЯЩЕР"
)
save_message(msg.chat.id, msg.message_id)
elif option_ids[0] == 5:
msg = await bot.send_message(
chat_id=STARAST, text=f"{username} не опоздает"
)
save_message(msg.chat.id, msg.message_id)
else:
msg = await bot.send_message(
chat_id=STARAST, text=f"{username} выбрал вариант {option_ids}"
)
save_message(msg.chat.id, msg.message_id)