Although there are many messaging applications available for the users, yet Telegram stands out from the crowd for various reasons. We all are aware about its beneficial features like encrypted messages. However, it is also a highly programmable open source platform whereby users can build their own bot. After realizing this, a blogger named Mohammad Valipour decided to write his own Telegram Bot using node.js and heroku which he later shared on his site. This bot could interact with the users and respond to their queries. It could also further aid in building more complex bots.
Figure 1: Telegram bot
To get started with this, he went on to register his bot which is done through Telegram’s own bot. For this, the app has a designated robot named @BotFather that interacts with the user and helps in the registration and management of their personal bots. When a chat is initiated with the BotFather, a series of options are displayed in the chat box. After typing /newbot it asks to enter a name and identifier for the new bot. After this, it renders a token to the user which is later used for the configuration purpose.
Before proceeding with the programming, you are required to set certain commands and replies that you expect from the bot. Here, Valipour used two commands- one for responding to hello followed by his name and second was to sum up the given figures and respond with the solution.
The next part is programming where a node.js program is written to interpret the received messages from the user and respond accordingly. Here the token received from BotFather is utilized in polling mode. The coding involves instructions regarding the actual implementation of the commands. This completes the procedure and after finishing with this, when you run the bot, it would start responding to your messages.
Apart from the above mentioned steps, the user is also required to add an http end point to the bot’s server so as to use the Heroku app for free and to ensure that the server doesn’t sleep. For the deployment purpose, you need to create a new app in the heroku account. After entering the required command and loading up the code to heroku, the app would build and start on the server.
When Valipour was done with the procedure of building the bot, it still had some issues like shutting down of the web server and delayed processing. Moreover the polling loop wasn’t able to handle the incoming messages efficiently. As a result, he set up a webhook to ease out the procedure in a way that Telegram could call into API as soon as it received a new message. You can view the updated information regarding the webhook here.
Filed Under: Reviews