Create a Telegram bot using NodeJS, ExpressJS to get CoViD 19 updates.

Malaka Silva
4 min readJan 22, 2021

People create bot applications for different purposes. Some of these bot apps are created from the scratch, while some of them are created using bot APIs provided by different organizations or companies.

As we all know, Telegram is a messaging service like Whatsapp, Viber. Compared to Whatsapp and Viber, Telegram offeres some interesting features for its users. One such feature is letting users to use bots to manage groups. Telegram groups can have upto 200,000 members. A huge number of members such as that can be hard to manage by single admin or couple of admins. As a solution for this, admins can setup a bot to monitor the group and take actions based on the user behaviours. Group Butler is an example for such kind of bots.

Let’s create a bot like Group Butler to get CoViD19 updates using different commands. Telegram provides the ability to its users to create custom bots as per their preferences. If anyone want to get more technical, they can use the API provided by the Telegram to build the bot, rather than trying to do it using Telegram app. However, we still need to use BotFather bot to get a new token.

Enough with the explanations. Let’s get to it. 😉

As explained earlier, first we need to get a new token for our bot. To get a new token, you can simply start a chat with BotFather and send command /newbot. Then follow the given instructions until you get the token. In this example, I will use “Covid bot” and “newcov_bot” as my bot name and bot username respectively.

Once you get a token, your token should look like the one in screenshot below.

Bot token

After that, we can start the development of our bot. So let’s create a new folder and inside that folder let’s initialize a new Node project and create a new JS file as “server.js”

In order to create the Telegram bot, we will use Telegraf package created by Vitaly Domnikov. Let’s install it on our application by executing below command.

npm i telegraf

Now we can start writing the code. Copy below code to your “server.js” file and save.

start() method in this code will initialize the bot. hears() method will check the received messages and if a received message equals to the given parameter in hears method, relevant hears method will be executed. We can add multiple hears methods with different tasks. launch() method will start your bot.

After the above code is copied, open up a terminal window and run below command to start the server and bot.

node server.js

If everything works fine, you should see the console output without any errors. Now try to send a message to your bot via Telegram. If your message says “hi” your bot should reply you with the message “Hey there…”. Note that you can style these messages with different mark downs provided by the bot API.

Now that our bot is working, let’s see how to get the CoViD updates via our bot. In order to get CoViD 19 latest updates, I will use an endpoint provided by the Ministry of Health, Sri Lanka. To initialize the api call, let’s use a seperate JS file in a seperate folder. Let’s create a new folder as “api” and inside the folder, let’s create a JS file as “api.js”. Then add below code to the file. Since fetch api is not implemented in Node, I will use node-fetch package to initialize the api call.

Great. Now let’s add another hears method to our “server.js” so that we can use it to define a command to get CoViD updates. Add below code to “server.js” file to create the command.

If everything is done upto now, your “server.js” file should look like below.

If there are no issues, congratulations! you just created a Telegram bot. 👏

Note that you can extract some extra information like who sent the message and user id of the sender by using the context variable(ctx). You can still continue to develop this bot to provide different functionalities by referencing the documentation. Also, if you want to deploy the bot, you can use PaaS like heroku to deploy your application. There are plenty of good step by step guides to refer if you are hoping to deploy your app. Finally, you can refer the whole project on Github using this link. You can also check an already deployed bot using this link.

Cover image by Mohamed Hassan from Pixabay.

--

--