DEV Community

Cover image for Creating simple telegram bot in Node.js and Telegraf.js (step by step)
Jahongir Sobirov
Jahongir Sobirov

Posted on

Creating simple telegram bot in Node.js and Telegraf.js (step by step)

We will learn how to create a simple bot in Node.js and Telegraf.js. It's very easy. Let’s first learn what Node.js is.

What is Node.js?

Node.js is a runtime application. It allows you to use the JS programming language outside the browser. With Node.js you can work with data analysis or write a telegram bot and create HTTP servers.

What is Telegraf.js?

Telegraf.js is a framework for Node.js that allows you to create awesome bots in Node.js runtime.

Step 1: Download Node.js

Alt Text
After downloading Node.js, let's check its version.
image

Step 2: Creating file for our simple telegram bot

mkdir bot-app
cd bot-app
Enter fullscreen mode Exit fullscreen mode

image

Step 3: With the help of Botfather we will create our new bot

When we create a bot in Botfather, it provides us with a bot token. The bot token, on the other hand, helps us write logic to it in node.js.
image

Step 4: We download Telegraf.js via NPM.

npm install telegraf
Enter fullscreen mode Exit fullscreen mode

We start creating the bot by downloading Telegraf.js.

Step 5: Creating bot in Node.js and Telegraf.js

Let's first create a file called bot.js. Importing Telegraf in bot.js file:

// bot.js
const { Telegraf } = require('telegraf'); // importing telegraf.js
Enter fullscreen mode Exit fullscreen mode

Now we declare a variable named bot and add to it the token of the bot we created in botfather.

// bot.js
var bot = new Telegraf('1928550358:AAH9Y4Bscfu2-y_OptOkzi3VyhbLaV84a8Q') // We saved our bot token to the bot variable 
Enter fullscreen mode Exit fullscreen mode

We write down what our bot will do when it receives the start command.

// bot.js
bot.start(ctx => ctx.reply(`
   Hi, I'm a simple bot
`))

bot.launch();
Enter fullscreen mode Exit fullscreen mode

Let's check it out now:
image
We’ll add some options to our boat.

// bot.js
bot.start(ctx => ctx.reply(`
   Hi, I'm a simple bot (please write /help)
`))
Enter fullscreen mode Exit fullscreen mode

Enter what the bot will do when the word /help is typed.

// bot.js
bot.help(ctx => ctx.reply(`
   Hello, world!!!
`))
Enter fullscreen mode Exit fullscreen mode

All codes:

const { Telegraf } = require('telegraf'); // importing telegraf.js
var bot = new Telegraf('1928550358:AAH9Y4Bscfu2-y_OptOkzi3VyhbLaV84a8Q') // We saved our bot token to the bot variable 
bot.start(ctx => ctx.reply(`
Hi, I'm a simple bot (please write /help)
`))

bot.help(ctx => ctx.reply(`
   Hello world!
`))

bot.launch();
Enter fullscreen mode Exit fullscreen mode

Result:
image

Top comments (7)

Collapse
 
rodrigoibarra profile image
Rodrigo Ibarra

Hello! thanks for the tutorial. Where can I find all the methods that I can create with Telegraf? I saw the Telegraf documentation but can't find it.
Thank you.

Collapse
 
jahongir2007 profile image
Jahongir Sobirov
Collapse
 
rickyo86 profile image
richard

Cool

Collapse
 
barzi92367868 profile image
Barzi

This barely scratches the surface... I wasn't expecting a full-fledged article, but this is borderline useless

Collapse
 
jahongir2007 profile image
Jahongir Sobirov

Ok sir

Collapse
 
aryaanish121 profile image
Arya Anish

Nice tuotial, but please hide you token

Collapse
 
jahongir2007 profile image
Jahongir Sobirov

I don’t want that and it’s no secret to me