0% found this document useful (0 votes)
1K views

Telegram Bot

This document contains code for a Telegram bot that can respond to messages. It defines global variables for the bot token, web app URL, and spreadsheet ID. It includes functions to get information about the bot, set a webhook to the web app, and send text messages. The doGet() and doPost() functions handle HTTP requests - doGet() returns a simple response, while doPost() parses Telegram data, responds to the user, and logs messages to a spreadsheet. It can log to different sheets based on the message text.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Telegram Bot

This document contains code for a Telegram bot that can respond to messages. It defines global variables for the bot token, web app URL, and spreadsheet ID. It includes functions to get information about the bot, set a webhook to the web app, and send text messages. The doGet() and doPost() functions handle HTTP requests - doGet() returns a simple response, while doPost() parses Telegram data, responds to the user, and logs messages to a spreadsheet. It can log to different sheets based on the message text.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//

// FILL IN THE GLOBAL VARIABLES token, webAppUrl and ssId


//

var token = ""; // FILL IN YOUR OWN TOKEN


var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = ""; // FILL IN YOUR GOOGLE WEB APP ADDRESS
var ssId = ""; // FILL IN THE ID OF YOUR SPREADSHEET

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 + ", thank you for your comment " + text;
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]);
}
}

You might also like