Telegram 机器人 API - 内联机器人在尝试回答内联查询时出现错误 400

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

我在 Python 中编写与 new inline mode 一起使用的机器人时遇到问题。

机器人收到查询,并在尝试回答时收到 错误 400

这是此时机器人发送的数据样本:

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

我使用 requests 库来发出请求,这是在代码中执行此操作的行:

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

使用 myData 保存示例中描述的数据。

你能帮我解决这个问题吗?

我怀疑是因为你没有 JSON-serialized results 参数。

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)

请注意,我使用 params 来提供数据。

我在做了一些 POC 后得到了正确的响应。我正在使用 java com.github.pengrad.

代码下方。

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());
    }
}

下面控制台输出:

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

而且我的移动电报应用程序也得到了正确的响应。