简体   繁体   English

使用 Google web 应用程序 Z686155AF75A60A0F6E9D80C1F7EDD3E 将 JSON object 发送到电报机器人

[英]Send JSON object using Google web app JavaScript to a Telegram bot

0stone0: fixed the problem; 0stone0:修复问题; thank you, thank you + thank you;谢谢,谢谢+谢谢;

ADW: thank you for illustrating keyboard object, adopted; ADW:感谢您提供键盘object的说明,采纳;

Google web app based on JavaScript to send JSON objects to a Telegram bot;基于 JavaScript 的 Google web 应用程序将 JSON 对象发送到 Telegram 机器人; implementing Telegram based menu using buttons, in order for the user to press selected button and execution of corresponding action;使用按钮实现基于 Telegram 的菜单,以便用户按下选定的按钮并执行相应的操作;

user types: menu用户类型:菜单

list of buttons show up on the Telegram group screen电报组屏幕上显示按钮列表

solution follows:解决方案如下:

function menu( chat_id ) {
    let url = vUrlTelegram + "/sendMessage";

    var keyboard = {
        'inline_keyboard' : 
        [
            [{'text' : 'admin',      'callback_data' : 'admin'}], // Row 1 
            [{'text' : 'squad',      'callback_data' : 'squad'}], // Row 2

            [{'text' : 'carioca',    'callback_data' : 'carioca'}], // Row 3
            [{'text' : 'brasileiro', 'callback_data' : 'brasileiro'}], // Row 4

            [{'text' : 'sponsors',   'callback_data' : 'sponsors'}], // Row 5       
            [{'text' : 'test',       'callback_data' : 'test'}] // Row 6       
        ]
    };  

    var data = {
        'chat_id': chat_id,
        'text': "main menu",
        'reply_markup': keyboard
    };   

    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify(data)
    };
    var response = UrlFetchApp.fetch(url, options);  
    Logger.log(response);
}

This assumes you are trying to send an inline keyboard to Telegram using Google Apps Script.这假设您正在尝试使用 Google Apps 脚本向 Telegram 发送内联键盘。

I've written this sample script that may help:我编写了这个示例脚本,可能会有所帮助:

function sample_inlineKeyboard() {
  var chat_id = '123456789';
  var text = 'Please pick a button:';
  var keyboard = {
    'inline_keyboard' : 
    [
      [{'text' : 'blue',   'callback_data' : 'blue'}, 
       {'text' : 'green',  'callback_data' : 'green'}, 
       {'text' : 'red',    'callback_data' : 'red'}], // Row 1
      [{'text' : 'yellow', 'callback_data' : 'yellow'},
       {'text' : 'brown',  'callback_data' : 'brown'}, 
       {'text' : 'black',  'callback_data' : 'black'}] // Row 2
    ]
  }
  var data = {
    'chat_id': chat_id,
    'text': text,
    'reply_markup': keyboard
  };        
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(data)
  };
  var token = "0123456789:AABBCC....."; // Bot token
  var vUrlTelegram = 'https://api.telegram.org/bot' + token + '/sendMessage';
  var response = UrlFetchApp.fetch(vUrlTelegram, options);    
  Logger.log(response);
}

Just had a busy weekend;)刚刚度过了一个忙碌的周末;)

Managed to make it work in Google App Script;设法使其在 Google App Script 中运行;

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);
}

Problem was due the nested payload / reply_markup objects.问题是由于嵌套的有效负载/reply_markup 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM