development:system:telegram
Cài đặt Webhook cho Telegram Bot qua AppScript của Google Sheet
Add đoạn code sau vào mục App Script của Google Sheet:
- AppScript.js
var apiSecret = "***"; var url = "https://api.telegram.org/bot"+apiSecret; var webAppUrl = "https://script.google.com/macros/s/******/exec"; 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 getWebhookInfo() { var response = UrlFetchApp.fetch(url+'/getWebhookInfo'); Logger.log(response.getContentText()); } function deleteWebhook() { var response = UrlFetchApp.fetch(url+'/deleteWebhook'); Logger.log(response.getContentText()); } function sendMessage(id, message) { var response = UrlFetchApp.fetch(url+'/sendMessage?chat_id='+id+'&text='+message); Logger.log(response.getContentText()); } function doGet(e){ return HtmlService.createHtmlOutput('Hello '+JSON.stringify(e)) } function doPost(e){ var contents = JSON.parse(e.postData.contents); Logger.log(contents); if(contents.my_chat_member){ var group_id = contents.my_chat_member.chat.id; var group_name = contents.my_chat_member.chat.title; var message = 'Hello group '+group_name+'. The group id is '+group_id+'. Please enter your group id into the webhook of the Website that I can notify you of the latest information.'; //GmailApp.sendEmail('tungnt.blue@gmail.com', "Telegram Bot Update", JSON.stringify(contents, null, 4)); sendMessage(group_id, message) } } function testSendMessage(){ var group_id = -536117828; var group_name = 'Test'; var message = 'Hello group '+group_name+'. The group id is '+group_id+'. Please enter your group id into the webhook of the Website that I can notify you of the latest information.'; sendMessage(group_id, message) }
development/system/telegram.txt · Last modified: 2024/08/06 06:59 by 127.0.0.1