How to capture and store poll responses from Telegram bot using Python

I’m working on a Python Telegram bot that creates polls in group chats. My current setup can send polls successfully, but I’m struggling with retrieving the poll results programmatically.

Right now the bot displays poll outcomes in the chat, but I want to capture and store these results for future use. I need to access the winning option from the poll data.

Here’s my current implementation:

def create_survey(bot, update):
    choices = ['option_x', 'option_y']
    bot.send_poll(update.message.chat_id, "Select your preferred choice:", choices)

updater.dispatcher.add_handler(CommandHandler('poll', create_survey))
updater.start_polling()

The poll works fine and shows results in the group chat, but I can’t figure out how to programmatically access which option won. For instance, if ‘option_y’ gets the most votes, I want my code to capture that result and use it in other parts of my application.

What’s the proper way to handle poll result callbacks and extract the winning choice?

yea, just add a PollAnswerHandler to your updater. it lets you catch the votes as they’re cast, so you can save em in a dict or whatever. here’s a snippet: updater.dispatcher.add_handler(PollAnswerHandler(handle_poll_answer)) to get you started!

You need to handle poll answers with a dedicated handler function that processes the poll_answer update type. When users vote, Telegram sends these updates containing the poll ID and selected options. Here’s what worked for me:

def handle_poll_results(update, context):
    poll_answer = update.poll_answer
    poll_id = poll_answer.poll_id
    selected_options = poll_answer.option_ids
    
    # Store the vote data
    store_vote(poll_id, selected_options)

updater.dispatcher.add_handler(PollAnswerHandler(handle_poll_results))

The tricky part is determining the “winner” since votes come in individually. I maintain a running count for each poll ID and calculate the leading option whenever I need it. You’ll also want to store the poll ID when creating surveys so you can match results later.

The main issue you’re facing is that Telegram doesn’t provide a direct way to get final poll results, only individual vote updates. I had this same problem and ended up implementing a vote tracking system using a database table to store poll states.

What I did was create a polls table with columns for poll_id, options, and vote_counts. When creating the poll, I store the initial data with zero counts. Then in my PollAnswerHandler, I update the counts for each vote received. The challenging part was handling vote changes - users can modify their selections, so you need to track previous votes to avoid double counting.

For determining the winner, I wrote a simple function that queries the current vote totals for a given poll_id and returns the option with highest count. You could also set up a scheduled task to periodically check for completed polls if you need automated winner detection.