Telegram bot: example json, inline_keyboard

…衆ロ難τιáo~ 提交于 2019-12-03 21:36:00

I just had a hard time trying to get it to work on my API and I've found the problem. You need to JSON.stringify() the contents of reply_markup that converts the keyboard object and contents into a string first.

Here's an example.

bot.onCommand = function (chat, from, message_id, text, command, commandData) {
    if (command === "test") {
        var keyboard = {
            "inline_keyboard": [
                [
                    {"text": "Yes", "url": "http://www.google.com/"},
                    {"text": "No", "url": "http://www.google.com/"}
                ]
            ]
        };

        var data = {
            "reply_to_message_id": message_id,
            "reply_markup": JSON.stringify(keyboard)
        };


        bot.sendText(chat.id, "test", data, function (isSuccess) {
            console.log(isSuccess);
        });

        return;
    }
}

I wrote this to hopefully make it less confusing.

The output will be:

(test    )
[Yes] [No]

The circler brackets is the message and the square brackets is the buttons. Both in this example opens a link to Google.

Well, I think I understood what you mean, jeissonp. It seems that you are using Node.js to write Telegram bots, and that's how you provide a user with an inline keyboard:

Create a keyboard:

const opts = {
"reply_markup": {
            "inline_keyboard": [[
                {
                    "text": "A",
                    "callback_data": "A1"            
                }, 
                {
                    "text": "B",
                    "callback_data": "C1"            
                }]
            ]
        }
}

And then send a message with the opts:

bot.sendMessage(chatID, "Message text", opts);

Hope it helps!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!