2

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

4 Answers 4

3

You have to serialize reply_markup to JSON string separately, like in this answer Telegram bot custom keyboard in PHP

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I realized it last night but I forgot to remove my question! +1 for your answer :)
3

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

1 Comment

You're a savior :)
1

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

Comments

0

The following snippet will work:

reply_markup = {
    "keyboard": [[{"text":"1"}], [{"text":"2"}]], 
    "resize_keyboard": True, 
    "one_time_keyboard": True
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.