Telegram bot _ 要我用按钮驱动菜单替换文字驱动菜单 _ 修正案 01

Telegram bot _ wants me to replace word driven menu by a button drive menu _ amendment 01

已根据 20200403 修改了 Google 基于 JavaScript 源代码的 Web App 可悲的是,还是不行

问题:当我输入以下内容时,与 chat_id 群组相关的 Telegram 群组中没有显示按钮: 菜单

JavaScript 应该构建并发送回 Telegram 组: InlineKeyboardMarkup 对象(此对象代表一个内嵌键盘,显示在它所属的消息旁边。)

Google Web App JavaScript源码如下:

var vApiTokenTelegram = "????????????????????????"; // @MediaFlamengoBot API token
var vUrlTelegram = "https://api.telegram.org/bot" + vApiTokenTelegram;
var vWebAppUrl = "https://script.google.com/macros/s/?????????????????????/exec";

function sendReplyMarkupMessage( chat_id, text, oInlineKeyboard ) {
    var encodedText = encodeURIComponent(text);

    GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update", JSON.stringify(oInlineKeyboard, null, 4));    

    var req = new Request(vUrlTelegram +    "/sendMessage?chat_id=" + chat_id + "&text=" + text + "&reply_markup=" + oInlineKeyboard );
    fetch(req)
    .then(response => response.blob())
    .then(blob => {
        console.log('Response: ', blob)
    });
    //Logger.log(response.getContentText());  
}

function menu( chat_id ) {
    var oInlineKeyboard = JSON.stringify({
        inline_keyboard: [
            [{ text: 'Some button text 1', callback_data: '1' }],
            [{ text: 'Some button text 2', callback_data: '2' }],
            [{ text: 'Some button text 3', callback_data: '3' }],
            [{ text: 'Some button text 4', callback_data: '4' }],            
            [{ text: 'Some button text 5', callback_data: '5' }]
        ]
    });
    sendReplyMarkupMessage( chat_id, "test", oInlineKeyboard );
}

gmail 记录以下内容,没有 JSON 对象: "{\"inline_keyboard\":[[{\"text\":\"Some button text 1\",\"callback_data\":\"1\"}],[{\"text\": \"Some button text 2\",\"callback_data\":\"2\"}],[{\"text\":\"Some button text 3\",\"callback_data\":\"3\" }],[{\"text\":\"Some button text 4\",\"callback_data\":\"4\"}],[{\"text\":\"Some button text 5\", \"callback_data\":\"5\"}]]}"

提前感谢您的帮助

图拉哈诺

您定义了 reply_markup 两次。您在 'send' 函数中使用此 "&reply_markup=" + reply_markup。但是 reply_markup 也在选项中定义;

function menu( chat_id ) {
    var options = {
        reply_markup: JSON.stringify({
            inline_keyboard: [
            ...

尝试删除 reply_markup 并将选项设置为数组;

function sendReplyMarkupMessage( chat_id, text, reply_markup ) {
    var encodedText = encodeURIComponent(text);
    var response = UrlFetchApp.fetch(vUrlTelegram +    "/sendMessage?chat_id=" + chat_id + "&text=" + text + "&reply_markup=" + reply_markup );
    Logger.log(response.getContentText());  
}

function menu( chat_id ) {
    var options = JSON.stringify({
        inline_keyboard: [
            [{ text: 'Some button text 1', callback_data: '1' }],
            [{ text: 'Some button text 2', callback_data: '2' }],
            [{ text: 'Some button text 3', callback_data: '3' }]
        }
    ]);
    sendReplyMarkupMessage( chat_id, "test", options );
}   


编辑; 我相信 UrlFetchApp 是一个 google 脚本函数。

编辑 2;没错,JSON.stringify中的数组应该是一个对象!

我已经根据您的代码创建了一个 JSFiddle; Take a look at it here

输出

放置自己的机器人令牌后 + chat_id


基于 Google 应用程序脚本,您应该使用类似这样的东西(已成功测试);

function myFunction() {
    let token = '123456788:AAdadadadbMTcMvY10SZGsbIJ2rdFXJiXmbFw';
    let url = "https://api.telegram.org/bot" + token + "/sendMessage";

    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify({
            'chat_id': 11111111,
            'text': 'fsdfdsfsdf',
            'reply_markup': {
                inline_keyboard: [
                    [{ text: 'Some button text 1', callback_data: '1' }],
                    [{ text: 'Some button text 2', callback_data: '2' }],
                    [{ text: 'Some button text 3', callback_data: '3' }]
                ]
            }
        })
    };
    var response = UrlFetchApp.fetch(url, options);  
    var res = UrlFetchApp.fetch(url);
    Logger.log(res);
}