【问题标题】:Telegram bot: example json, inline_keyboard电报机器人:示例 json,inline_keyboard
【发布时间】:2017-08-17 23:29:32
【问题描述】:

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

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"            
                }]
            ]
        }
    }

【问题讨论】:

标签: json telegram-bot


【解决方案1】:

我很难让它在我的 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 的链接。

【讨论】:

    【解决方案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);
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2021-09-01
      • 2021-11-17
      • 1970-01-01
      • 2018-07-22
      • 2020-10-28
      • 1970-01-01
      相关资源
      最近更新 更多