A little while ago I started using telegram as my messaging app (admittedly along-side dozen other apps). But it didn’t take long till I realized, there is something different about telegram which is really cool. It’s [almost] fully open-source and more importantly highly programmable, so much that they natively support bots! and quickly I found myself wanting to write my own bot.
In this article I will take you through the steps of how to write a very simple telegram bot that interacts with users on telegram and delivers a service. You can use this as a starting-point for building your more complex bots.
Register your bot
Like many other platforms, the first thing you need to do is to register your app. Doing so is amazingly simple in telegram and is done via… well… another bot on telegram!
@BotFather is the telegram’s designated robot for registering and managing your bots. Simply start chatting with it and you will see all of your options. – here we focus only on registering our new bot.
and you will be asked to provide a
identifier for your bot. Once done, BotFather will give you a
token which is what you need to configure in your server component later in this article.
For the sake of this tutorial, let’s assume we need our bot to support the following two commands:
/say_hello <your-name>to reply
/sum <num1> <num2> ...to sum up any given numbers and reply with the result
Next, we need to write a node.js program that listens to the incoming communications from bot users and responds to them.
For this we use node-telegram-bot-api which is a nicely-done wrapper around telegram’s bot api.
This will instantiate a new instance of
Bot in polling mode (i.e. an infinite loop that reads incoming messages as they come) using the token we obtained earlier.
Now, let’s write our first command listener:
Above code registers a text-listener that calls our callback whenever a text message is received matching the given syntax. The reg-ex also extract the name segment out of the message and passes to the callback via the
Then we send a text message back to the sender of the original message (
msg.chat.id which may be a group or individual chat) with the result.
And similarly our
This one has a slightly more complicated reg-ex which extracts all of the numbers given as the command parameter, then split the chunk, convert them to integer (
+ prefix) and sum-up. Then similar to above, send the result back to the user.
Run the bot and bingo!
Your bot should now be responding to the messages:
There are two reasons why we need to add an http end-point to our node.js bot server:
First of all to continue enjoying Heroku for free, we want our polling loop to run for-ever on a web role (as oppose to a paid Heroku scheduler add-on). Heroku shuts the web role process down, if no http port is listened from the process within 30 seconds.
Additionally I would like to set-up a web monitoring service (Uptime Robot provides this for free), to ensure my server is always up and running and never goes to sleep.
So I spin up a simple web server that simply returns app version number as json.
Finally we need to add an
index.js file to start both bot and web servers:
Now by starting the app (
node .), you should get an output like this:
Finally we need to create and deploy our Heroku app.
This will create a new app in your heroku account (you will be asked to setup one if have no account setup) with a random name. To open the app in your browser simply run:
Each time you need to deploy the app, simply run the following command and push your latest code to heroku remote repo, which then results in your node.js app to build and start on Heroku server.
This is a minimalistic example to show you how easy and cool it is to create your own telegram bot in no time. You can keep adding more and more complexity and business logic to your server. For example remember users’ choice in a database to then report back to them in later commands, etc.
Telegram opens up a whole new ways of communicating with users, including sending audio, photo, location, etc. and even communicating with user chat groups. For more information see telegram bot API.
A complete source code of this tutorial can be found on the following GitHub repository: