'telegram bot keyboard with google apps script

I want to meet new clients in telegram bot with a keyboard buttons

So far I have this code written in Google Apps Script, but in result there is nothing.

function doPost(e) {

    var API_TOKEN = 'bla-bla-bla';
    var update = JSON.parse(e.postData.contents);

    if (update.hasOwnProperty('message')) {
        var msg = update.message;
        var chatId = msg.chat.id;
        if (msg.text == '/start') {
            function sendText(chatId, text, keyboard) {

                var payload = {
                    'method': 'sendMessage',
                    'chat_id': String(chatId),
                    'text': "Hello",
                    'parse_mode': 'HTML',
                    reply_markup: JSON.stringify({
                        keyboard: [
                            [ 
                                 "A",
                                 "B"
                            ],
                            [
                                 "C",
                                 "D"
                            ]
                        ],
                        resize_keyboard: true,
                        one_time_keyboard:true
                    })
                }

                var data = {
                    "method": "post",
                    "payload": payload
                }
                UrlFetchApp.fetch('https://api.telegram.org/bot' + API_TOKEN + '/', data);
            }    
        }
    }
}

Is this a missing feature or it's me, doing something wrong?



Solution 1:[1]

You probably want something like this?
PS: replace it with your chat id

function testKeyboard() {
  const chatId = YOURCHATID; // REPLACE THIS
  const choices = ['Mozzarella','Gouda cheese','Camembert','Gorgonzola','Cheddar Cheese'];
  const text = 'Make your choice';
  sendKeyboard(chatId, choices, text);
}

You can do it with this code.
PS: replace token with your bot's token

const token = ''; // REPLACE IT WITH YOUR BOTS TOKEN
const telegramApi = 'https://api.telegram.org/bot' + token;

function sendKeyboard(chatId, choices, text = 'Menu') {
  const buttons = transformArrayToKeyboard(choices);
  const replyKeyboardMarkup = {keyboard: buttons, 
                one_time_keyboard: true,
                resize_keyboard: true};
  const replyMarkup = JSON.stringify(replyKeyboardMarkup);
  const url = telegramApi + '/sendMessage?chat_id=' + encodeURIComponent(chatId) + '&text=' + encodeURIComponent(text) + '&disable_web_page_preview=true&reply_markup=' + encodeURIComponent(replyMarkup);
  const response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function transformArrayToKeyboard(choices) {
  const maxLengthRowKeyboardMenu = 3;
  const arr = choices.map(item => ({text: item }));
  const result = [];
  let index = 0;
  while(arr.slice(index).length > 0) {
    const newRowValuesForButtons = arr.slice(index,index+maxLengthRowKeyboardMenu);
    result.push(newRowValuesForButtons);
    index += maxLengthRowKeyboardMenu;
  }
  return result;
}


//  function transformArrayToKeyboard transforms [4,5,6,7,8] to:
//  [
//    [
//      {text: "4" },{text: "5" },{text: "6" }
//    ]
//    [
//      {text: "7" },{text: "8" }
//    ]
//  ];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wim den Herder