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

标签 python python-requests telegram-bot

我在使用 new inline mode 编写 Python 机器人时遇到问题.

机器人收到查询,并在尝试回答时收到错误 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 保存示例中描述的数据。

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

最佳答案

我怀疑这是因为您没有对 results 参数进行 JSON 序列化。

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 来提供数据。

关于python - Telegram bot API - 内联机器人在尝试回答内联查询时出现错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618784/

相关文章:

python - 如何解释链式比较操作的抽象语法树?

python - Many2one Odoo 11 中的域

python - 将列表转换为字符串三元组序列

telegram-bot - 从 Telegram bot 获取用户的 "Last Seen"?

python - python 范围如何在此代码段中工作?

python - 使用 Python Requests 和 BeautifulSoup 登录 Facebook

python - 使用 Python 请求发送 SOAP 请求

python - 如何使用请求测量下载速度和进度?

PHP telegram API 读取群聊消息

python - 如何在 Telepot 上获取用户输入的字符串?