Google App脚本-电报机器人-自定义键盘 [英] Google App Script - Telegram Bot - Custom Keyboard

查看:264
本文介绍了Google App脚本-电报机器人-自定义键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a custom keyboard.

我有以下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很好用,但是我无法创建自定义键盘.就是行不通,我也不知道为什么.我在线搜索了解决方案,但没有发现任何问题.请帮助我:)

it works great as echo bot but I am not able to create a custom keyboard. It just doesn't work and I have no idea why. I searched the solution online but I found nothing. Help me, please :)

推荐答案

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

With the help of @Sean and @Kos i have resolved my problem and here is the working code. I also have added the inline_keyboard type.

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格式必须如下:

the keyBoard format must be as follow:

   {
     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'}
       ]
     ]
   } 

这篇关于Google App脚本-电报机器人-自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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