1

It's a python code for a telegram bot to take a poll from the group chat. How can I get the final result of send_poll bot? I know the bot shows the result in the chat group but I need to save and use it for later. For example, here I need the output to be like a or b.

    def poll(bot, update):
        options=['a','b']  
        bot.send_poll(update.message.chat_id, "please choose option a or 
        b:",options)
    updater.dispatcher.add_handler(CommandHandler('set', poll))  
    updater.start_polling()

The following picture shows the output of send_poll bot in the group chat. I like to have the answer which here is 'b', not only in the group chat also as a returned answer.

user13253920
  • 21
  • 1
  • 3

1 Answers1

0

the response provided to your handler is something like:

{'update_id': 696992167, 
  'poll': {
    'id': '5920521737891479577', 
    'question': 'What is the capital of Ukraine?', 
    'options': [
      {'text': 'Rabat', 'voter_count': 0}, 
      {'text': 'Kyiv', 'voter_count': 1}, 
      {'text': 'Luxembourg', 'voter_count': 0}], 
    'total_voter_count': 1, 'is_closed': False, 'is_anonymous': True, 
    'type': 'quiz', 'allows_multiple_answers': False, 
    'correct_option_id': 1, 'explanation_entities': [], 'close_date': None}}

Notice the options are the answer you provided and voter_count=1 tells you the one picked by the user.

You can then process the request and use/save the value as you need

def get_answer(update):
answers = update.poll.options

ret = ""

for answer in answers:
    if answer.voter_count == 1:
        ret = answer.text

return ret

In order to implement the quiz you need to define how to send a question and how to process the answer:

# send question
context.bot.send_poll(chat_id=xxx, question='The question?',
                                options=[], type=Poll.QUIZ, correct_option_id=n, is_anonymous=True)

# define method 'main_handler' to process the answer
updater.dispatcher.add_handler(PollHandler(main_handler, pass_chat_data=True, pass_user_data=True))
Beppe C
  • 11,256
  • 2
  • 19
  • 41
  • Hi Beppe, that I think totally works, but how do you get that response? Because there's not a function like getPoll, there's only sendPoll so how do you get "the update"? Thanks! – Dani G. Sep 17 '20 at 22:09
  • 1
    Hi Dani, I have expanded the answer, hope it helps. – Beppe C Sep 18 '20 at 06:40
  • Hi Beppe, I have a similar problem in the question here: https://stackoverflow.com/questions/66360862/how-to-identify-the-user-answer-from-a-quiz-using-python-telegram-bot-module?noredirect=1#comment117343476_66360862 , but when I click in the answer in the chat group, the voter_count doesn't change. I tried to use telegram.PollAnswer but it doesn't seem to work. Thanks in advance. – minmax Feb 25 '21 at 18:45