Search code examples
pythontornadotelegramtelegram-botpython-telegram-bot

Telegram bot api keyboard


I have problem with Telegram Bot Api and with "ReplyKeyboard". I'm using Python 2.7 and I send post request:

TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, text="", keyboard={'keyboard': keyboard, 'one_time_keyboard': False, 'resize_keyboard': True})

keyboard in this format:

[["A button"], ["B button"]]

But in Telegram I don't see keyboard. What problem can be?


Solution

  • According to the Bot API documentations, a custom keyboard requires a reply_markup parameter, whose value is a JSON-serialized specification of the keyboard. Assuming your TelegramAPI.post() function does not JSON-serialize for you, I would try the following:

    import json
    
    json_keyboard = json.dumps({'keyboard': [["A button"], ["B button"]], 
                                'one_time_keyboard': False, 
                                'resize_keyboard': True})
    
    TelegramAPI.post(TELEGRAM_URL + "sendMessage", 
                     data=dict(chat_id=CHAT_ID, 
                               text="Has to be non-empty", 
                               reply_markup=json_keyboard))
    

    Note that text has to be non-empty.