Telegraf is a modern Telegram Bot framework for Node.js that makes it easy to create powerful and scalable bots. Telegraf provides a simple and intuitive API, as well as a wide range of features and extensions.
NodeJS Requirements
Telegraf requires Node.js version 10.0.0 or higher. You can check your Node.js version by running the following command in a terminal:
node -v
If your Node.js version is lower than 10.0.0, you can upgrade to a newer version by following the instructions on the Node.js website: Download Node.js: https://nodejs.org/en/download/
Code to check NodeJS version:
const { exec } = require('child_process');
const checkNodeVersion = async () => {
const output = await exec('node -v');
const nodeVersion = parseFloat(output.trim());
if (nodeVersion < 10.0) {
console.log('Please upgrade to Node.js version 10.0.0 or higher.');
process.exit(1);
}
};
checkNodeVersion();
If you do not have Node.js installed on your system, you can download it from the Node.js website: Download Node.js: https://nodejs.org/en/download/
Once you have downloaded Node.js, follow the instructions on the website to install it.
Once Node.js is installed, you can verify that it is working correctly by running the following command in a terminal:
node -v
To configure a Telegraf bot with NodeJS, you will need to:
1. Install the Telegraf package:
npm install telegraf@4.15.0 --save
2. Create a new Telegraf bot instance:
const { Telegraf } = require('telegraf');
const bot = new Telegraf('YOUR_BOT_TOKEN');
3. Register middleware:
Middleware is a chain of functions that are executed in order when a bot receives a message. Middleware can be used to validate input, filter messages, or process messages in a specific way.
To register middleware, use the bot.use()
method:
bot.use((ctx, next) => {
// Do something with the context
next();
});
4. Add command handlers:
Command handlers are functions that are executed when a user sends a command to your bot. To add a command handler, use the bot.command()
method:
bot.command('start', (ctx) => {
// Welcome the user
});
5. Launch the bot:
To launch the bot, use the bot.launch()
method:
bot.launch();
Once you have completed these steps, your Telegraf bot will be up and running!
Here are some additional tips for configuring your Telegraf bot:
- You can use the
bot.telegram
property to access the Telegram Bot API directly. This can be useful for more complex tasks, such as sending custom messages or interacting with other bots. - Telegraf provides many extensions that can be used to add additional features to your bot. For example, the
telegraf-session
extension can be used to store user data across multiple sessions. - You can use the
Telegraf
documentation to learn more about all of the features and options that are available.
Concluson
Telegraf is a powerful and flexible Telegram Bot framework for Node.js. By following the steps in this guide, you can easily configure a Telegraf bot to meet your specific needs.
Here another post about using tdlib to backup channel information from telegram.
Leave a Reply