Last active
August 8, 2023 03:10
-
-
Save Max-Makhrov/0c46f191109f2b269abd2d02800bccd7 to your computer and use it in GitHub Desktop.
Telegram Bot for Lazy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// __ | |
// /_ | | |
// | | | |
// | | | |
// | | | |
// |_| | |
// Set Bot, copy token: | |
// https://t.me/botfather | |
// set your token ↓ | |
var token = "1815465433:AAGpRcd--KsZycuSYmBOCDcVvws5lSvkuqw"; | |
// ___ | |
// |__ \ | |
// ) | | |
// / / | |
// / /_ | |
// |____| | |
// | |
// | |
// Here in script: | |
// Deploy > New Deployment > WebApp > | |
// Who can access: Anyone | |
// Set your URL ↓ | |
var webAppUrl = "https://script.google.com/macros/s/AKfycbxLgEpxM8EFX86skZ0XK0r4KhCoqZBX-z3k4PiUFDHws0TjrtNZZuRIJJfS09_-ot-O/exec"; | |
// ____ | |
// |___ \ | |
// __) | | |
// |__ < | |
// ___) | | |
// |____/ | |
// | |
// Here in script run function: | |
// setBotHook | |
var telegramUrl = "https://api.telegram.org/bot" + token; | |
function setBotHook() { | |
var url = telegramUrl + "/setWebhook?url=" + webAppUrl; | |
var response = UrlFetchApp.fetch(url); | |
if (response.getResponseCode() === 200) { | |
console.log("Success! You don't need this function any more"); | |
} else { | |
console.log("Something went wrong..."); | |
} | |
} | |
// _ _ | |
// | || | | |
// | || |_ | |
// |__ _| | |
// | | | |
// |_| | |
// | |
// Go to your Bot & Deploy it | |
// to test the connection | |
function doPost(e) { | |
runBot_(e); | |
} | |
function doGet(e) { | |
runBot_(e); | |
} | |
// function to react when peaple write to Bot | |
function runBot_(e) { | |
try { | |
executeBot_(e); | |
} catch(err) { | |
save2mem_(err.stack); | |
throw err; | |
} | |
} | |
function executeBot_(e) { | |
var contents = JSON.parse(e.postData.contents); | |
// run logMemory after exexution | |
save2mem_(JSON.stringify(contents, null, 4)); | |
// var chat_id = contents.message.from.id; | |
// var text = "Hello there 😊. Your bot ID: " + chat_id; | |
// sendMessage2Bot_(text, chat_id); | |
} | |
// Here in script run function: | |
// logMemory | |
// to see what's inside Bot's responce | |
var bot_key = 'telegrambot_info'; | |
function save2mem_(msg) { | |
var m = get_mem_(); | |
m.put(bot_key, msg); | |
} | |
function get_mem_() { | |
return CacheService.getScriptCache(); | |
} | |
function logMemory() { | |
var m = get_mem_(); | |
var res = m.get(bot_key); | |
console.log(res); | |
} | |
// _____ | |
// | ____| | |
// | |__ | |
// |___ \ | |
// ___) | | |
// |____/ | |
// | |
// 1. Create a Telegram Group | |
// 2. Add your bot as admin | |
// 3. Write a message to group | |
// 4. Run function `logMemory` here | |
// 5. Copy chat.id from logs | |
// | |
// Test of sending message to group | |
function testSend() { | |
sendMessage2Bot_( | |
'Hello, Annual <i>Inspection</i> is soon:\n<b>Unit:</b> <code>{UNIT}</code>\n<b>Date:</b> <code>{DATE}</code>', | |
// ↓ put your chat id here! | |
-123465789); | |
} | |
function sendMessage2Bot_(msg, chat_id) { | |
var data = { | |
"text" : msg, | |
"parse_mode": 'html' | |
}; | |
var payload = JSON.stringify(data); | |
var options = { | |
"method" : "POST", | |
"contentType" : "application/json", | |
"payload" : payload, | |
"muteHttpExceptions": true | |
}; | |
var url = telegramUrl + "/sendMessage?chat_id=" + chat_id; | |
var response = UrlFetchApp.fetch(url, options); | |
console.log(response.getContentText()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment