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.
var token = "XXXXX";
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 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));
}
}