1
votes

I am trying to send a Telegram keyboard with a bot to my account. I have followed TelegramAPI documentation but I am unable. I am using Python 2.7.

So far, this is what I have tried:

import requests

bot_token = 'xxxxxxxxxxxxxxxxxxxxxxxx'
chat_id = 'xxxxxxxxxxxx'
text = "Choose:"
reply_markup={"keyboard":[["Yes","No"],["Maybe"],["1","2","3"]],"one_time_keyboard":True}
data = {'chat_id': chat_id, 'text': text, 'reply_markup': reply_markup}
url ="https://api.telegram.org/bot" + bot_token + "/sendMessage"

r = requests.get(url, data = data)
results = r.json()
print (results)

Python does not crash, but I get a fail response from Telegram API and I don't know why:

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

Any idea or suggestion is highly appreciated.

Thanks, Ander.

1
Have you tried this: data = {'chat_id': chat_id, 'text': text, 'reply_markup': str(reply_markup)} - tashakori
@tashakori Thank you. I have tried it just know and I get the same Telegram error... - Andermutu
Or if there is any other method via requests to send keyboard please share... - Andermutu
Did you try "one_time_keyboard":"true" ? (Instead of "one_time_keyboard":True) - newsha
why don't you use well-implemented libraries like github.com/python-telegram-bot/python-telegram-bot ? - tashakori

1 Answers

1
votes

utilizing json library you can dump your keyboard object:

import requests, json

bot_token = 'xxxxxxxxxxxxxxxxxxxxxxxx'
chat_id = 'xxxxxxxxxxxx'
text = "Choose:"
reply_markup={"keyboard":[["Yes","No"],["Maybe"],["1","2","3"]],"one_time_keyboard":True}
data = {'chat_id': chat_id, 'text': text, 'reply_markup': json.dumps(reply_markup)}
url ="https://api.telegram.org/bot" + bot_token + "/sendMessage"

r = requests.get(url, data = data)
results = r.json()
print (results)