mrnavaone

Telegram Bot

Apr 12th, 2020
540
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var token ="TOKEN"; //token del bot
  2. var telegramUrl = "https://api.telegram.org/bot" + token;
  3. var webAppUrl = "WEB APP URL";
  4. var ssId = "ID SSID SPREADSHEET";
  5.  
  6. function getMe() {
  7.   var url = telegramUrl + "/getMe";
  8.   var response = UrlFetchApp.fetch(url);
  9.   Logger.log(response.getContentText());
  10. }
  11.  
  12. function setWebhook() {
  13.   var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  14.   var response = UrlFetchApp.fetch(url);
  15.   Logger.log(response.getContentText());
  16. }
  17.  
  18. function sendText(id,text) {
  19.   var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
  20.   var response = UrlFetchApp.fetch(url);
  21.   Logger.log(response.getContentText());
  22. }
  23.  
  24. function doGet(e) {
  25.   return HtmlService.createHtmlOutput("Hola Mundo");
  26. }
  27.  
  28.  
  29. function doPost(e) {
  30.   // this is where telegram works
  31.   var data = JSON.parse(e.postData.contents);
  32.   var text = data.message.text;
  33.   var id = data.message.chat.id;
  34.   var name = data.message.chat.first_name + " " + data.message.chat.last_name;
  35.   var answer = "Hola " + name + text + ", Tu peticiĆ³n fue enviada ";
  36.   sendText(id,answer);
  37.  
  38.   SpreadsheetApp.openById(ssId).getSheets()[0].appendRow([new Date(),id,name,text,answer]);
  39.  
  40. }
RAW Paste Data