skip to Main Content

If copy/paste the print in the postman, works! but in Python give error

This is my code

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

The error is

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

The print is

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

Resolved
Send Telegram keyboard with Python

2

Answers


  1. You seem to have used the wrong key for your second button’s text, you should use text.

    {
      "chat_id": -123456,
      "text": "Test",
      "reply_markup": {
        "inline_keyboard": [
          [
            {
              "text": "Button2",
              "url": "https://example.com"
            },
            {
              "text": "Button2",
              "url": "https://example2.com"
            }
          ]
        ]
      }
    }
    
    Login or Signup to reply.
  2. Looks like Telegram API accepts string in the key reply_markup, so you can stringify the object before making a post request.

    {
      "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"
            }
          ]
        ]
      })
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search