【问题标题】:Bad Request: can't parse reply keyboard markup JSON object Python错误请求:无法解析回复键盘标记 JSON 对象 Python
【发布时间】:2022-11-04 19:53:16
【问题描述】:

如果在邮递员中复制/粘贴打印,则有效!但在 Python 中给出错误

这是我的代码

array = '{"chat_id": "' + chat_id + '", "text": "Test Buttons", "reply_markup" : { "inline_keyboard" : [[ { "text" : "web", "url" :"google.es"}]]}}'    
data3  = json.loads(array)
    
print(array)
url = f'https://api.telegram.org/bot{token}/sendMessage'
response = requests.get(url, params=data3)
print(response.json())

错误是

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

印刷品是

{"chat_id": "123XXX", "text": "Test Buttons", "reply_markup" : { "inline_keyboard" : [[ { "text" : "web", "url" :"google.es"}]]}}

解决 Send Telegram keyboard with Python

【问题讨论】:

  • 您可能希望使用三个反引号“ ``` ”以便以易于理解的方式表示您的代码。

标签: python telegram


【解决方案1】:

您似乎为第二个按钮的文本使用了错误的键,您应该使用text

{
  "chat_id": -123456,
  "text": "Test",
  "reply_markup": {
    "inline_keyboard": [
      [
        {
          "text": "Button2",
          "url": "https://example.com"
        },
        {
          "text": "Button2",
          "url": "https://example2.com"
        }
      ]
    ]
  }
}

【讨论】:

  • 是的,当我写消息时,现在是正确的,但错误仍在继续
  • 什么错误继续?你能具体点吗?
  • 错误请求:无法解析回复键盘标记 JSON 对象。我编辑主要帖子
  • 你解决了吗?我有同样的问题
【解决方案2】:

看起来 Telegram API 接受键 reply_markup 中的字符串,因此您可以在发出发布请求之前对对象进行字符串化。

{
  "chat_id": -123456,
  "text": "Test",
  "reply_markup": JSON.stringify({
    "inline_keyboard": [
      [
        {
          "text": "Button2",
          "url": "https://www.example.com"
        },
        {
          "text": "Button2",
          "url": "https://www.example2.com"
        }
      ]
    ]
  })
}

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多