A
A
ambal2452021-02-11 12:47:23
JavaScript
ambal245, 2021-02-11 12:47:23

How to create inline_keyboard in Google Apps from table cells?

How to create a keyboard in a for loop to send to the user. There is a google table in cell A, 4 values ​​\u200b\u200bare written, which we read them into a variable. Below is the code from the script editor

function doPost(e){
var update = JSON.parse(e.postData.contents);
var DOC = SpreadsheetApp;
//нам нужен только тип "сообщение"
if (update.hasOwnProperty('message')){
var msg = update.message;
var chat_id = msg.chat.id;
var text = msg.text;
var msg_array = msg.text.split(" ");
// проверяем, является ли сообщение командой к боту
    if (msg.hasOwnProperty('entities') && msg.entities[0].type == 'bot_command') {
       if (msg_array[0] == "/start"){
   var sheet = DOC.getActiveSpreadsheet();
   var sheet1 = sheet.getSheetByName('Прайс лист');

      function getData(){
      var result = [],     
      range = sheet1.getRange("A1:A"), // определяем диапозон ячеек
      values = range.getValues(),
      last = parseInt(sheet1.getLastRow()); //  определяем количество заполненных ячеек
           for (var i = 1; i < last; i++) {
          result.push({'text' : values[i]});   }
      return result; 
    }
 var data = getData();
    var keydoards = [{'inline_keyboard' : data}];

    send('кнопки', chat_id, keydoards)
 }
}
}}

function send (msg, chat_id, keyboard){
var payload = {
'method': 'sendMessage',
'chat_id': String(chat_id),
'text': msg,
'reply_markup' :  JSON.stringify(keyboard),
'parse_mode': 'markdown'
}
var data = {
"method": "post",
"payload": payload
}
var API_TOKEN = 'xxxxxxxxxxxxxxxx'
UrlFetchApp.fetch('https://api.telegram.org/bot' + API_TOKEN + '/', data);
}

As a result of running this code, the following is generated and the keyboard is not sent.
{"inline_keyboard":[{"text":["TEXT1"},{"text":"TEXT2"},{"text":"TEXT3"},{"text":"TEXT4"}]}]

Tell me How to create a keyboard in a JSON array in a for loop?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question