簡體   English   中英

使用 Google web 應用程序 Z686155AF75A60A0F6E9D80C1F7EDD3E 將 JSON object 發送到電報機器人

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

0stone0:修復問題; 謝謝,謝謝+謝謝;

ADW:感謝您提供鍵盤object的說明,采納;

基於 JavaScript 的 Google web 應用程序將 JSON 對象發送到 Telegram 機器人; 使用按鈕實現基於 Telegram 的菜單,以便用戶按下選定的按鈕並執行相應的操作;

用戶類型:菜單

電報組屏幕上顯示按鈕列表

解決方案如下:

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

這假設您正在嘗試使用 Google Apps 腳本向 Telegram 發送內聯鍵盤。

我編寫了這個示例腳本,可能會有所幫助:

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

剛剛度過了一個忙碌的周末;)

設法使其在 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);
}

問題是由於嵌套的有效負載/reply_markup 對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM