Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram bot API - Inline bot getting Error 400 while trying to answer inline query

I have a problem coding a bot in Python that works with the new inline mode.

The bot gets the query, and while trying to answer, it receives error 400.

Here is a sample of data sent by the bot at this time:

{
    'inline_query_id': '287878416582808857',
    'results': [
        {
            'type': 'article', 
            'title': 'Convertion', 
            'parse_mode': 'Markdown', 
            'id': '287878416582808857/0', 
            'message_text': 'blah blah'
        }
    ]
}

I use requests library in to make requests, and here is the line that does it in the code:

requests.post(url = "https://api.telegram.org/bot%s%s" % (telegram_bot_token, "/answerInlineQuery"), data = myData)

With myData holding the data described in the sample.

Can you help me solve this, please?

like image 358
Jahus Avatar asked Jun 12 '26 19:06

Jahus


1 Answers

I suspect it is because you haven't JSON-serialized the results parameter.

import json

results = [{'type': 'article', 
            'title': 'Convertion', 
            'parse_mode': 'Markdown', 
            'id': '287878416582808857/0', 
            'message_text': 'blah blah'}]

my_data = {
    'inline_query_id': '287878416582808857',
    'results': json.dumps(results),
}

requests.post(url="https://api.telegram.org/bot%s%s" % (telegram_bot_token, "/answerInlineQuery"), 
              params=my_data)

Note that I use params to supply the data.

like image 157
Nick Lee Avatar answered Jun 14 '26 08:06

Nick Lee