Telegram bot:示例json,inline_keyboard

时间:2017-03-24 21:29:31

标签: telegram-bot

示例json用于电报机器人中的show inline_keyboard

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

enter image description here

{
        "chat_id": "123456",
        "text": "Hi",
        "reply_markup": {
            "inline_keyboard": [[
                {
                    "text": "A",
                    "callback_data": "A1"            
                }, 
                {
                    "text": "B",
                    "callback_data": "C1"            
                }]
            ]
        }
    }

2 个答案:

答案 0 :(得分:6)

我很难尝试使用我的API工作,但我发现了问题。您需要 JSON.stringify() reply_markup 的内容,它首先将键盘对象和内容转换为字符串。

以下是一个例子。

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;
    }
}

我写这篇文章是为了让它不那么混乱。

输出将是:

(test    )
[Yes] [No]

圆圈括号是消息,方括号是按钮。这个示例都打开了一个指向Google的链接。

答案 1 :(得分:2)

嗯,我想我理解你的意思,jeissonp。您似乎正在使用Node.js来编写Telegram机器人,这就是您为用户提供内联键盘的方式:

创建键盘:

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

然后使用opts发送消息:

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

希望它有所帮助!

相关问题