Skip to content

Instantly share code, notes, and snippets.

@kkprakasa
Created January 4, 2023 09:32
Show Gist options
  • Save kkprakasa/541563349520b9c08d862a995c6eb790 to your computer and use it in GitHub Desktop.
Save kkprakasa/541563349520b9c08d862a995c6eb790 to your computer and use it in GitHub Desktop.
var token = "5195227267:AAHLHhhRQ_kqc6iIgIrNqzLUZF67QyXuk3o"; // FILL IN YOUR OWN TOKEN
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/s/AKfycbw5xC4O1jZ-JYoZ6lYyAg5b71iXqzzhwY9EWEwCET6hW8XJR2R8cThgBCAnUvRiUcg8/exec";
var ssId = "1ivlIZ0_e6EEwydJ06JfBbsuW23K75O7_m8BWUKGka80"; // FILL IN THE ID OF YOUR SPREADSHEET
//AKfycbw5xC4O1jZ-JYoZ6lYyAg5b71iXqzzhwY9EWEwCET6hW8XJR2R8cThgBCAnUvRiUcg8
function getMe() {
var url = telegramUrl + "/getMe";
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function setWebhook() {
var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function sendText(id,text) {
var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function doGet(e) {
return HtmlService.createHtmlOutput("Hi there");
}
function doPost(e) {
// this is where telegram works
var data = JSON.parse(e.postData.contents);
var text = data.message.text;
var id = data.message.chat.id;
var name = data.message.chat.first_name + " " + data.message.chat.last_name;
var answer = "Hi " + name + " input " + text +" sukses";
sendText(id,answer);
SpreadsheetApp.openById(ssId).getSheets()[0].appendRow([new Date(),id,name,text,answer]);
if(/^@/.test(text)) {
var sheetName = text.slice(1).split(" ")[0];
var sheet = SpreadsheetApp.openById(ssId).getSheetByName(sheetName) ? SpreadsheetApp.openById(ssId).getSheetByName(sheetName) : SpreadsheetApp.openById(ssId).insertSheet(sheetName);
var comment = text.split(" ").slice(1).join(" ");
sheet.appendRow([new Date(),id,name,comment,answer]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment