Build own Telegram Bot with Node.js

Love Calculator on Telegraf

Gapur Kassym
Nov 11, 2019 · 3 min read
Photo by Alex Knight on Unsplash

Since Telegram Bot has appeared, I always interested in how they work. So I decided to build simple Telegram Bot with Node.js and Telegraf. Telegraf is a modern bot framework for Node.js. They have friendly documentation and examples.

In this tutorial I will develop a love calculator bot. I used free love calculator api. You should enter your name and name of your partner/lover/crush to find Love compatibility & chances of successful love relationship.

Let’s start and do it together.

Create own Bot with BotFather

BotFather

First, We should create own bot with BotFather. BotFather is the one bot to rule them all. We will use it to create new bot accounts and manage your existing bots.

If you open a chat with a BotFather, click on the “Start” button.

First screen

After that, the bot gives us all the commands.

All commands

We should create a new bot by clicking /newbot command. Next, you should enter any name for the bot. I named Cupido . Fortunately, You can delete the test bot or change the name. In the last step, you have to come up with a username for your bot. It must end in `bot`. For example TetrisBot or tetris_bot. I named MrCupidoBot.

MrCupidoBot with the token

Finally, We got a message that the bot was created and a token. We have to keep this token because, it will help us access the Telegram API.

Setting up the Project

We are going to use Nodejs with Telegraf. If you don’t have nodejs you should install it. We will install nodejs via Homebrew:

brew install node

If everything installed successfully then you can type in the following command in the terminal to check the Node and NPM version.

node -v
npm -v

Create a new directory and move into cupido-bot:

mkdir cupido-bot && cd ./cupido-bot

Initialize the project:

npm init

We need to download telegraf and axios to work with api:

npm install telegraf axios --save

Write bot’s code

Our repository is ready and let’s write bot’s code. Create index.js file and add the following code:

Next, we should change bot’s icon by /mybots command. I downloaded the icon from freeicons website.

Edit bot’s icon

Next, launch code on nodejs by the following command:

node index.js
MrCupidoBot

Next, We are going to create loveCalculator.js to work with api:

We will pass two-parameter to getPercentage method to calc love compatibility.

We will use stage from telegraf to implement a two-step conversation between user and bot. Stage is a simple scene-based control flow middleware.

Let’s launch our final code.

Love Telegram Bot

That’s it. We did our first own Telegram Bot.

I hope you found this post useful.

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Gapur Kassym

Written by

Software Engineer, Technical Writer #JavaScript #React #React-Native #Nodejs. Follow me on twitter https://twitter.com/GKassym

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Gapur Kassym

Written by

Software Engineer, Technical Writer #JavaScript #React #React-Native #Nodejs. Follow me on twitter https://twitter.com/GKassym

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store