An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
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. Lets 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
After downloading Node.js, let's check its version.
Step 2: Creating file for our simple telegram bot
mkdir bot-appcd bot-app
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.
Step 4: We download Telegraf.js via NPM.
npm install telegraf
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.jsconst { Telegraf } = require('telegraf'); // importing telegraf.js
Now we declare a variable named bot and add to it the token of the bot we created in botfather.
// bot.jsvar bot = new Telegraf('1928550358:AAH9Y4Bscfu2-y_OptOkzi3VyhbLaV84a8Q') // We saved our bot token to the bot variable
We write down what our bot will do when it receives the start command.
// bot.jsbot.start(ctx => ctx.reply(` Hi, I'm a simple bot`))bot.launch();
Let's check it out now:
Well add some options to our boat.
// bot.jsbot.start(ctx => ctx.reply(` Hi, I'm a simple bot (please write /help)`))
Enter what the bot will do when the word /help is typed.
// bot.jsbot.help(ctx => ctx.reply(` Hello, world!!!`))
All codes:
const { Telegraf } = require('telegraf'); // importing telegraf.jsvar 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();
Original Link: https://dev.to/jahongir2007/creating-simple-telegram-bot-in-node-js-and-telegraf-js-step-by-step-30pn
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To