Creating a Telegram bot can sound intimidating at first, but it’s actually easier than you think. In this article, I’ll guide you through the process of building a basic Telegram bot using Node.js. By the end, your bot will be up and running, ready to respond to messages in real-time.
If you're excited about integrating AI capabilities later, I'll tell you how to take things to the next level at the end. But for now, let’s focus on creating the core bot!
Telegram has a built-in bot management bot called BotFather. Here’s how to create your bot:
/newbot
.MyFirstBot
).bot
(e.g., my_first_bot
).
Note: Keep this token secure! Anyone with access to it can control your bot.
Let’s move on to setting up our project:
Create a new directory for your project and navigate into it:
mkdir telegram-bot
cd telegram-bot
Initialize a new Node.js project:
npm init -y
This command will generate a package.json
file with default settings.
Install the Telegram Bot API library:
npm install node-telegram-bot-api
Create a new file called bot.js
. This is where we’ll write the code for our Telegram bot.
Here’s the basic bot setup code:
const TelegramBot = require('node-telegram-bot-api');
// Replace with your actual Telegram bot token from BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Create a bot instance with polling enabled
const bot = new TelegramBot(token, { polling: true });
// Handle incoming messages
bot.on('message', (msg) => {
const chatId = msg.chat.id;
// Respond with a welcome message
bot.sendMessage(chatId, 'Hello! I am your friendly Telegram bot. How can I help you today?');
});
console.log('Bot is up and running...');
node-telegram-bot-api
package.'YOUR_TELEGRAM_BOT_TOKEN'
with the actual token you received from BotFather.To start the bot, use the following command in your terminal:
node bot.js
You should see the message "Bot is up and running..." in your terminal. Now, go to Telegram, find your bot, and send it a message. The bot should reply with the greeting you programmed.
Now that you have a basic bot running, let’s add a feature to make it more interactive. For example, we can add a command that returns the current time.
Modify the bot.js
file:
bot.onText(/\/time/, (msg) => {
const chatId = msg.chat.id;
const currentTime = new Date().toLocaleTimeString();
bot.sendMessage(chatId, `Current time: ${currentTime}`);
});
onText
method listens for messages that match the /time
command.
Restart your bot and test the new command by typing /time
in the chat.
Congratulations! You’ve successfully created a working Telegram bot with Node.js. But what if you want to make your bot even smarter by integrating AI?
That’s where things get exciting! You can add AI capabilities using OpenAI to generate dynamic responses, quizzes, and more.
Want to learn how? Check out my full video tutorial on my YouTube channel MaksDevInsights, where I’ll walk you through connecting your bot to OpenAI step by step. Watch it here.
Building a Telegram bot is a great way to practice Node.js and learn more about real-time messaging systems. By following the steps in this guide, you now have a bot that can interact with users and handle basic commands.
Stay tuned for more tutorials, and feel free to share your bot projects in the comments below!