Как телеграмм боту на Python реагировать на закрытие опроса?
пишу телеграмм бота опросника на языке программирования Python, я бы хотел при закрытие опроса подсчитать голоса. Закрываю опрос через send_poll(close_date=time()+ 5)
Вот код:
@dp.message_handler(commands=["vote"], commands_prefix="!")
async def vote(message: types.message):
global a
global b
m = await bot.send_poll(chat_id = message.chat.id, question='кикнуть', options=['Да', 'Нет'], is_anonymous=False, close_date = time()+5)
a = 0
b = 0
@dp.poll_answer_handler()
async def handle_poll_answer(quiz_answer:types.Pollanswer):
global a
global b
if quiz_answer.option_ids == [1]:
a += 1
print(a)
elif quiz_answer.option_ids ==[0]:
b += 1
print(b)
Ответы (1 шт):
Автор решения: oleksandrigo
→ Ссылка
Из того, что я понял, ловить момент автоматического закрытия невозможно.
Но можно сделать небольшой костыль с помощью asyncio.
@dp.message_handler(commands=["start"])
async def send_welcome(message: types.Message):
time = 5
msg = await bot.send_poll(chat_id=message.chat.id, question="Вопрос", options=["Вариант 1", "Вариант 2"])
await asyncio.sleep(time)
poll = await bot.stop_poll(chat_id=message.chat.id, message_id=msg.message_id)
print(poll)
В poll будет храниться данные о нем.
Пример
{"id": "...", "question": "Вопрос", "options": [{"text": "Вариант 1", "voter_count": 1}, {"text": "Вариант 2", "voter_count": 0}], "total_voter_count": 1, "is_closed": true, "is_anonymous": true, "type": "regular", "allows_multiple_answers": false}