I decided to clone one of my previous post in Russian. I noticed that people from different countries visit that post from Github repository with the code. So, I guess some of them were dissapointed by that fact they were not able to read the details. Here I'll just translate the post in English. No any new details.
In this post I'll provide some useful details of how to host your Telegram bot with help of Google Apps Script.
There are tons of similar posts (good and bad) in Internet. That's why I'm not going to describe every step. Then someone may raise a question: "Why then write a post?" So, the answer is even you find the best article and copy code from there likely it will not work :) Only many of good articles plus Telegram API documentation plus (obviously) StackOverflow will ensure the results. I'll try to summarize some pitfalls here. Hope it will save your time.
NB! developers always change the API, so have in mind that this post was up to date in February, 2020.
Well, basic steps which are well described in Internet:
- create your Telegram bot by using BotFather and get a bot token
- make a scipt in Google Apps Script (you need Google account) with what you want to do (for example, let it be writing some message in a cell Google Spreadsheet and sending a message to a Telegram user)
Short excursus for those who never make a Telegram bot before. It's important to figure out that there are two ways of getting updates from Telegram - long polling mode and setWebhook. In long polling mode your code shall always asking for updates by using getUpdates() method. In setWebhook mode as soon as update is received by Telegram servers, Telegram will send the update to a specified url.
For instance, if you want to create a chat-bot which will reply to user's questions, obviously you'd like bot asnwers immediately to user, not after a minute. In this case by using long polling mode you have to call getUpdates very often (e.g. once per second). In this case the setWebhook mode is much more suitable. But you need a web hosting and ssl certificate. At the same time in the long polling mode you can host your bot even on your laptop. Deploying a web application in Google Apps Script allows you to use webhook and don't take care about SSL certificates and web hosting.
Ok, now the tips.
- if you have made any change in code in Google Script (even it's really small change, like adding a commented line), you have to deploy again your app! Just a click "Save" doesn't apply the changes!
- you always must choose Project version as "New". Otherwise changes will be not applied:
- Functions in Google Apps Script for handling POST и GET requests shall have names doPost(e) и doGet(e) correspondingly.
- Those functions must return the result! Even you don't need it. Code examples without return will not work.
- If for your final goal it's not important what is returned by the functions you anyway have to pay attention to this part. It's easier to explain it with an example.
Imagine a Telegram user with name "user" sends a message "test" to your bot. For instance, you want that bot write a message in a Google Spreadsheet cell and notify you (as bot owner) about new message from user. If setWebhook is set the Telegram server will make a POST request to the specified url. It will lead to calling doPost function in Google Apps which writes a message to a cell and notifies you. And here an object returned by doPost plays an important role. Even it seems to be completely not important for you. For instance, in web you can find examples with using ContentService to return 'raw textual content' as a result of doPost. It might be acomplished with an explanation like "ok, we need just having return in code. Let's return an emply text content"
return ContentService.createTextOutput();
Ok, with code the function doPost will be called correctly and returns HTTP 200 OK. Everything is fine but you will notice your bot will send you the same notification millions of times. In Telegram API documentation it's claimed: "In case of an unsuccessful request, we will give up after a reasonable amount of attempts." But maybe your ideas about "reasonable amount" will differ from Telegram:) The point is that your app has to answer to Telegram with html body (even empty) to inform Telegram that app has handled the update successfuly. An example how to do it:
return HtmlService.createHtmlOutput();
Now Telegram will realize that your app has got the update correctly and there is no need to repeat it.
Few general tips at the end.
If your code anyway doesn't work try to debug it separately from Telegram side and Google side. For debbaging Telegram bot you may temporarely desable webhook (deleteWebhook) and check for updates with getUpdates() method even with just address line of your web browser.
For testing Google Apps Script make POST/GET requests with some third-party tool (for example). Take into account that requests from Postman don't work due to some issues with authentification.
In my Github repository you can find a simple example. A web application deployed in Google Apps Script handles the updates from Telegram, writes in the first three columns in spreadsheet the time, user name and message text of a new message from user to bot, and finally notify the bot owner about received update:
Комментарии
Отправить комментарий