skip to Main Content

When I try to send a Poll to a chat I receive the following error:

{'ok': False, 'error_code': 400, 'description': "Bad Request: can't parse options JSON object"}

Regarding to https://core.telegram.org/bots/api#sendpoll the “options” have to be an array out of strings, which obviously doesn’t work.

This is the script I built:

import json, requests

telegram_poll_url = 'https://api.telegram.org/botXXXX:YYYY/sendPoll'
telegram_poll_data = {'chat_id': XXXX, 'options':  ["5 Minuten", "10 Minuten"], 'question': "Wann bist hier?", 'is_anonymous': False}

response = requests.post(telegram_poll_url, telegram_poll_data).json()
print(response)

Edit: This also doesnt work

options = ["5 Minuten", "10 Minuten"]
telegram_poll_data = {
    'chat_id': -321158590, 
    'options':  options, 
    'question': "Wann bist du im FF Haus?", 
    'is_anonymous': False
}

response = requests.post(telegram_poll_url, telegram_poll_data).json()

3

Answers


  1. Parse your options list to JSON.

    GET

    options = ['bla', 'bla2', 'bla3']
    uri = f'https://api.telegram.org/bot{_TELEGRAM_BOT_TOKEN}/sendPoll?chat_id={chat_id}&' 
          f'question={question}&options={json.dumps(options)}&type={type_}&correct_option_id={correct_option_id}' 
          f'&is_anonymous=false'
    get(uri)
    
    Login or Signup to reply.
  2. I had the same problem until I realized I was supposed to send serialized JSON data through the json requests.post() parameter instead of the default data:

    requests.post(telegram_poll_url, json=telegram_poll_data)
    

    This way it would actually use the application/json Content-Type and allow the API server to properly decode the sendPoll fields.

    Login or Signup to reply.
  3. Use the following

    import json
    
    
    options = ["5 Minuten", "10 Minuten"]
    telegram_poll_data = {
        'chat_id': -321158590, 
        'options':  json.dumps(options), 
        'question': "Wann bist du im FF Haus?", 
        'is_anonymous': False
    }
    
    response = requests.post(telegram_poll_url, json=telegram_poll_data)
    

    i.e. json.dumps(options)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search