Modern Telegram Bot API framework for App Script
A Telegram bot is a program that offers functions and automations that Telegram users can integrate in their chats, channels, or groups
Telesun is a library that makes it simple for you to develop your own Telegram bots using JavaScript and Apps Script
- Full Telegram Bot API 6.0 support
- Simpler 🌟
- easier working across Google products
- per click Deployment on google cloud
- Real-Time Database(Google sheet) already integrated
once you import library
, and you identify your telegram chat id
, then you can create function like the following and send direct message to the bot chat id
Bot.Telesun(botToken)
//create and run this function, the message will directly sent to the user chat Id
function SendMessage() {
let chatId = '1173180004' //change this chat id to yours
return Bot.sendMessage(chatId, 'hello User')
}
For additional bot examples see examples folder
To use the Telegram Bot API, you first have to get a bot account by chatting with BotFather.
BotFather will give you a token, something like 123456789:AbCdfGhIJKlmNoQQRsTUVwxyZ
.
- check here to follow the steps of importing library
You only need to set webhook once(1)
, but :
- if you
delete webhook
then you need to set it up again - also when you deploy using
new Deployment
you need to set webhook to theupdated url
for webhook url
check >> here
To set webhook
then you need to run
the following function called SettingWebHook()
// find from bot father
let botToken = '123456789:AbCdfGhIJKlmNoQQRsTUVwxyZ'
//find on Deployment
let webhookUrl =
'https://script.google.com/macros/s/AKfycbyTJNTD5HsnQMUsT-qX4AUQCd6Moex3zyf9cgdmlzly-mPxmlRlaxzt8lKhljq1zr6Ow/exec'
Bot.Telesun(botToken)
function SettingWebHook() {
return Bot.setWebHook(webHookUrl)
}
When a program sends the app an HTTP POST request, Apps Script runs doPost(e)
function
For more >> here
Update
your previous code like this 👇
// find from bot father
let botToken = '123456789:AbCdfGhIJKlmNoQQRsTUVwxyZ'
Bot.Telesun(botToken)
//the bot will reply the same text message you sent
function doPost(e) {
const apiResponse = JSON.parse(e.postData.contents)
let chatId = Bot.TextContents(apiResponse).id
let text = Bot.TextContents(apiResponse).text
return Bot.sendMessage(chatId, text)
}
once you create your doPost(e)
function this way then, you can delpoy with managed deployment and check your bot
after make any change on your code
, then it is must you Deploy
your code, otherwise you can't see any change, for more Deployment
- if you are deploying your code first time follow New Deploymet
- if not follow Manage Deployment
you can use
new Deployment
any time you like, but after you deploy
- Copy the web app url and paste on
webhookurl variable
- run
setWebHook
function(you don't need to deploy for this)
let botToken = '779238246:AAEkFeunpG-lg3pc8eoAda2svGHu3O_dIA'
let webHookUrl =
'https://script.google.com/macros/s/AKfycbxr03EKxm336KxtsaoHJ49JlEfaw5CzOG0ys0DMxPmKjlsaFkIFeqBVYM-1CGs-KjT_g/exec'
Bot.Telesun(botToken)
// for only new Deployment
// this function will set webhook on
function setWebHook() {
return Bot.setWebHook(webHookUrl)
}
//the bot will reply the same text message you sent
function doPost(e) {
const apiResponse = JSON.parse(e.postData.contents)
let chatId = Bot.TextContents(apiResponse).id
let text = Bot.TextContents(apiResponse).text
return Bot.sendMessage(chatId, text)
}
you can delete
setWebHook
function andwebhookurl variable
when deploying withmanaged deployment
app script won't show you any code error
from doPost
, so we need to track the error's by sending to bot as a message, for more Best Practices
- edit
doPost
function as the following - add
try catch
error handling
let botToken = '779238246:AAEkFeunpG-lg3pc8eoAda2svGHu3O_dIA'
Bot.Telesun(botToken)
//admin chat id to send errors to
let admin = '1173180004'
//this will send any error to the Admin chat id you specified
function doPost(e) {
try {
const apiResponse = JSON.parse(e.postData.contents)
let chatId = Bot.TextContents(apiResponse).id
let text = Bot.TextContents(apiResponse).text
return Bot.sendMessage(chatId, text)
} catch (err) {
return Bot.sendMessage(admin, err)
}
}