skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. I am getting the correct response after doing some POC. I am using java com.github.pengrad.

    Below the code.

    GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates());
    List updates = updatesResponse.updates();
    for(Update update:updates){
        InlineQuery inlineQuery = update.inlineQuery();
        System.out.println(update);
        System.out.println(inlineQuery);
        System.out.println("----------------");
        if(inlineQuery!=null) {
            InlineQueryResult r1 = new InlineQueryResultPhoto("AgADBQADrqcxG5q8tQ0EKSz5JaZjzDWgvzIABL0Neit4ar9MsXYBAAEC", "https://api.telegram.org/file/bot230014106:AAGtWr8xUCqUy8HjSgSFrY3aCs4IZs00Omg/photo/file_1.jpg", "https://api.telegram.org/file/bot230014106:AAGtWr8xUCqUy8HjSgSFrY3aCs4IZs00Omg/photo/file_1.jpg");
            BaseResponse baseResponse = bot.execute(new AnswerInlineQuery(inlineQuery.id(), r1)
                .cacheTime(6000)
                .isPersonal(true)
                .nextOffset("offset")
                .switchPmParameter("pmParam")
                .switchPmText("pmText"));
            System.out.println(baseResponse.isOk());
            System.out.println(baseResponse.toString());
            System.out.println(baseResponse.description());
        }
    }
    

    Below the console output:

    Update{update_id=465103212, message=null, edited_message=null, inline_query=InlineQuery{id='995145139265927135', from=User{id=231700283, first_name='Manabendra', last_name='Maji', username='null'}, location=null, query='hi', offset=''}, chosen_inline_result=null, callback_query=null}
    
    InlineQuery{id='995145139265927135', from=User{id=231700283, first_name='Manabendra', last_name='Maji', username='null'}, location=null, query='hi', offset=''}
    
    true
    BaseResponse{ok=true, error_code=0, description='null'}
    null
    

    And I am getting proper response in my mobile telegram app also.

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