Insert data google spreadsheet from telegram bot

47 views
Skip to first unread message

Gabriele Serpi

unread,
Apr 29, 2020, 4:40:51 PM4/29/20
to Google Apps Script Community
Hello everyone, it's first time that i create this topic because i want to learn more for telegram bot because i'm organizing the two groups for association no profit with disability.
i've followed this script from youtube:

[code]
var token = "XXXXX";
var url = "https://api.telegram.org/bot"+ token;
var WebAppUrl = "XXXXX";
var ssId = "XXXXX";
//var encodedText = encodeURIComponent(what + " - " + who);
var serpiId = "XXXXX";


function getMe() {
  var response = UrlFetchApp.fetch(url + "/getMe");
  Logger.log(response.getContentText());
}

function getUpdates() {
  var response = UrlFetchApp.fetch(url + "/getUpdates");
  Logger.log(response.getContentText());
}

function setWebhook() {
  var response = UrlFetchApp.fetch(url + "/setWebhook?url=" + WebAppUrl);
  Logger.log(response.getContentText());
}

function sendText(id, text) {
  var response = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + id + "&text=" + encodeURIComponent(id + " - " + text));
  Logger.log(response.getContentText());
}

function doGet(e) {
  return HtmlService.createHtmlOutput("Hello" + JSON.stringify(e));
}

function doPost(e) {
  try {
    var contents = JSON.parse(e.postData.contents);
    GmailApp.sendEmail(Session.getEffectiveUser().getEmail(),"Telegram Bot Update",JSON.stringify(contents,null,4));
    var text = contents.message.text;
    var id = contents.message.from.id;
    var name = contents.message.from.first_name + " " + contents.message.from.last_name;
    sendText(id, "Ciao " + name);
    
    var ss = SpreadsheetApp.openById(ssId);
    
    ss.appendRow([new Date(),id,name,text,contents]);
    
    if(/^@/.test(text)) {
      var sheetName = text.slice(1).split(" ")[0];
      var newText = text.split(" ").slice(1).join(" ");
      var sheet = ss.getSheetByName(sheetName) ? ss.getSheetByName(sheetName) : ss.insertSheet(sheetName);
      sheet.appendRow([new Date(),id,name,newText]);
      sendText(id, "tuo testo "+ newText + " è stato inserito nella lista " + sheetName);
    }
  } catch(e) {
    sendText(serpiId,JSON.stringify(e,null,4));
  }
}
  
[/code]

but i want to insert on different columns, for example on firs association:

First Name        Last Name      City       Nickname      City       Email


And other association football deaf:

Squad_1   Result_Squad1     Result_Squad2       Squad_2    (for insert the result after matchs    Rome    3    0    Milan, with @squad for to recognize squad and result, on telegram bot "@Rome 3 @Milan 0")

I'm working on telegram bot, how edit this script? Thanks and greetings
Gabriele

Alan Wells

unread,
Apr 29, 2020, 7:35:23 PM4/29/20
to Google Apps Script Community
The data for setting values in a Sheet must be in a 2D array unless you are appending data.  Each inner array represents a different row, and each element in the inner array represents an individual cell.  So, the column values are each element in the inner array.  Two change the column, you need to change the index of the value in the array.
So, you'll need to understand basic JavaScript and how to add values to and get values out of an array.  If you don't know how to do that, please learn that first.
Reply all
Reply to author
Forward
0 new messages