Telegram bot: example json, inline_keyboard

前端 未结 2 568
野的像风
野的像风 2021-02-10 04:20

Example json for show inline_keyboard in telegram bot

https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating

enter image description here

<
2条回答
  •  悲&欢浪女
    2021-02-10 04:49

    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.

提交回复
热议问题