Trajano Roberto
Trajano Roberto

Reputation: 307

Example 1 of Telegram bot menu access using Google Sheets Apps Script

0stone0: fixed the problem; thank you, thank you + thank you;

ADW: thank you for illustrating keyboard object, adopted;

Google web app based on JavaScript to send JSON objects to a Telegram bot; implementing Telegram based menu using buttons, in order for the user to press selected button and execution of corresponding action;

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

Upvotes: -1

Views: 1109

Answers (2)

0stone0
0stone0

Reputation: 44275

Just had a busy weekend ;)

Managed to make it work in 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.

Upvotes: 1

ADW
ADW

Reputation: 4257

This assumes you are trying to send an inline keyboard to Telegram using Google Apps Script.

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

Upvotes: 1

Related Questions