Files
BOTKlining/handlers/polls.py
2025-06-08 20:55:08 +09:00

23 lines
968 B
Python

from aiogram import types, Dispatcher
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from models.poll import Poll
from database import Session
async def create_poll(message: types.Message):
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton('Option 1', callback_data='vote_1'))
markup.add(InlineKeyboardButton('Option 2', callback_data='vote_2'))
await message.reply("Choose an option:", reply_markup=markup)
async def handle_vote(callback_query: types.CallbackQuery):
option = callback_query.data.split('_')[1]
with Session() as session:
poll = Poll(option=option)
session.add(poll)
session.commit()
await callback_query.answer(f"You voted for option {option}")
def register_handlers(dp: Dispatcher):
dp.register_message_handler(create_poll, commands=['poll'])
dp.register_callback_query_handler(handle_vote, lambda c: c.data.startswith('vote_'))