【问题标题】:Google App Script - Telegram Bot - Custom KeyboardGoogle App Script - Telegram Bot - 自定义键盘
【发布时间】:2018-01-01 04:37:07
【问题描述】:

我想创建一个自定义键盘。

我有这个 GAS 代码:

function sendText(chatId,text){

 var payload = { "method": "sendMessage", "chat_id": String(chatId),"text": text, "parse_mode": "HTML" }

 var data = {
  "method": "post",
  "payload": payload,
  "reply_markup": JSON.stringify({
    "keyboard": [
      [ 
        "A",
        "B"
      ],
      [
        "C",
        "D"
      ]
    ], 
    "resize_keyboard":true
  })
 }

UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);

}

它可以作为 echo bot 工作,但我无法创建自定义键盘。它只是不起作用,我不知道为什么。我在网上搜索了解决方案,但一无所获。请帮帮我:)

【问题讨论】:

    标签: google-apps-script telegram-bot


    【解决方案1】:

    在@Sean 和@Kos 的帮助下,我解决了我的问题,这是工作代码。我还添加了 inline_keyboard 类型。

    function sendText(chatId,text,keyBoard){
    
       keyBoard = keyBoard || 0;
    
      if(keyBoard.inline_keyboard || keyBoard.keyboard){
         var data = {
          method: "post",
          payload: {
             method: "sendMessage",
             chat_id: String(chatId),
             text: text,
             parse_mode: "HTML",
             reply_markup: JSON.stringify(keyBoard)
           }
         }
        }else{
          var data = {
            method: "post",
            payload: {
              method: "sendMessage",
              chat_id: String(chatId),
              text: text,
              parse_mode: "HTML"
            }
          }
        }
    
       UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
    
     }
    

    键盘格式必须如下:

       {
         keyboard: [
           [ 
             "A",
             "B"
           ],
           [
             "C",
             "D"
           ]
         ],
         resize_keyboard:true,
         one_time_keyboard:true
       }
    
       {
         inline_keyboard: [
           [
             {text:'Sandwich',callback_data:'sandwich'},
             {text:'A juicy steak',callback_data:'steak'}
           ],
           [
             {text:'Sandwich2',callback_data:'sandwich2'},
             {text:'A juicy steak2',callback_data:'steak2'}
           ]
         ]
       } 
    

    【讨论】:

      【解决方案2】:

      请说明,您如何在应用脚本中使用 callback_data?

      【讨论】:

        【解决方案3】:

        您对keyboard 字段使用了错误的格式,请参见以下示例:

        【讨论】:

        • 对于我犯的每一个错误,我深表歉意。非常感谢您的编辑。话虽如此,我尝试了您的代码,但它仍然无法正常工作。自定义键盘不会弹出。我编辑了我的问题以放置您的新代码。
        • @Paolo177 我猜 'reply_markup' 应该是 'payload' 对象的子对象
        • 是的,chat_idtextreply_markup 应该在你的库中换成 payload
        • 你们是对的人@Kos @Sean!非常感谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        • 2016-04-26
        • 2020-03-01
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多