Search code examples
pythonpython-3.xtelegram-botpy-telegram-bot-api

poll answer handler with pytelegrambotapi


I've sent a poll using this:

@bot.message_handler(commands=['start'])
def start(message):
    bot.send_poll(message.chat.id,'choose one',['a','b','c'])

but how could I get the answer back?

the documentation say's:

Handle poll answers @bot.poll_answer_handler() # <- passes a PollAnswer type object to your function.

but I didn't know how to use it.


Solution

  • You can access and print poll answers this way:

    @bot.poll_answer_handler()
    def handle_poll_answer(pollAnswer):
        print(pollAnswer)
    

    In python we can use the @ symbol along with the name of the decorator function and place it above the definition of the function to be decorated.

    So here we put @bot.poll_answer_handler() on top of our handle_poll_answer function for it to be registered as the bot's poll_answer_handler.

    Note that it also says the handler will receive a PollAnswer type, so we put that as a parameter to our function as pollAnswer.

    passes a PollAnswer type object to your function