Telegram 机器人:示例 json、inline_keyboard

2024-02-13

在电报机器人中显示 inline_keyboard 的示例 json

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

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

我只是费了很大劲才让它在我的 API 上工作,但我发现了问题。你需要JSON.stringify()的内容回复标记首先将键盘对象和内容转换为字符串。

这是一个例子。

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 的链接。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Telegram 机器人:示例 json、inline_keyboard 的相关文章

随机推荐