电报机器人:示例 json、inline_keyboard [英] Telegram bot: example json, inline_keyboard

查看:64
本文介绍了电报机器人:示例 json、inline_keyboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<代码>{chat_id":123456",文本":嗨",回复标记":{inline_keyboard":[[{文本":A",回调数据":A1";},{文本":B",回调数据":C1";}]]}}

解决方案

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

这是一个例子.

bot.onCommand = function (chat, from, message_id, text, command, commandData) {如果(命令 ===测试"){var 键盘 = {inline_keyboard":[[{"text": "Yes", "url": "http://www.google.com/"},{"text": "No", "url": "http://www.google.com/"}]]};变量数据 = {reply_to_message_id":message_id,"reply_markup": JSON.stringify(keyboard)};bot.sendText(chat.id,测试",数据,功能(isSuccess){console.log(isSuccess);});返回;}}

我写这个是为了希望能减少混淆.

输出将是:

(测试)[是] [否]

圆括号是消息,方括号是按钮.本示例中的两者都打开了一个指向 Google 的链接.

Example json for show inline_keyboard in telegram bot

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

解决方案

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.

这篇关于电报机器人:示例 json、inline_keyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆