Reputation: 5598
I wrote a telegram bot. All things went well and it worked well. But when I want to use ReplyKeyboardMarkup
as it mentioned in its document, it did not work! I mean the keyboard doesn't show up.
This JSON object has a key keyboard
and its value ,according to its doc, is:
type: Array of Array of String.
description: Array of button rows, each represented by an Array of Strings
This is my code for sending the request:
reply_markup = {'keyboard': [['1'],['2']], 'resize_keyboard': True, 'one_time_keyboard': True}
params = urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'reply_markup': reply_markup,
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
})
resp = urllib2.urlopen(BASE_URL + 'sendMessage', params).read()
Upvotes: 2
Views: 6042
Reputation: 1
The following snippet will work:
reply_markup = {
"keyboard": [[{"text":"1"}], [{"text":"2"}]],
"resize_keyboard": True,
"one_time_keyboard": True
}
Upvotes: 0
Reputation: 11
If you like you can try this method
import telegram
from telegram.ext import Updater
updater = Updater(token='BOT_TOKEN')
dispatcher = updater.dispatcher
updater.start_polling()
def test(bot, update):
results = bot.sendMessage(chat_id=update.message.chat_id, text="Test", reply_markup={"keyboard":[["Test1"], ["Test2"], ["Test3"], ["Test4"]})
print results
dispatcher.addTelegramCommandHandler('test', test)
This import makes things a lot more short, I just started using it today python-telegram-bot 3.4
Upvotes: 1
Reputation: 1528
Because it still took me a bit of trial and error to get in right in Python even after reading the PHP answer linked to by Kostya, here is a the adapted Python code that works (you just need to add your bot's token and a chat ID to send the message to).
Note that I also had to update my Telegram client to the newest version (currently 3.1) to see the result.
import urllib
import urllib2
import json
TOKEN = "<your bot token>"
chat_id = <your chat id>
msg = "some string"
BASE_URL = "https://api.telegram.org/bot{}/".format(TOKEN)
reply_markup = {'keyboard': [['1'],['2']], 'resize_keyboard': True, 'one_time_keyboard': True}
reply_markup = json.dumps(reply_markup)
params = urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'reply_markup': reply_markup,
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
})
resp = urllib2.urlopen(BASE_URL + 'sendMessage', params).read()
Upvotes: 3
Reputation: 305
You have to serialize reply_markup to JSON string separately, like in this answer Telegram bot custom keyboard in PHP
Upvotes: 3