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!
What You’ll Need
- Node.js installed on your computer. (You can download it from Node.js official site)
- A Telegram account.
- Basic knowledge of JavaScript.
Step 1: Create Your Bot on Telegram
Telegram has a built-in bot management bot called BotFather. Here’s how to create your bot:
- Open Telegram and search for BotFather.
- Start a chat with BotFather and type
/newbot
. - Provide a name for your bot (e.g.,
MyFirstBot
). - Set a unique username that ends with
bot
(e.g.,my_first_bot
). - Once the bot is created, BotFather will give you an API token. This token allows you to control your bot through code.
Note: Keep this token secure! Anyone with access to it can control your bot.
Step 2: Set Up Your Node.js Project
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
Step 3: Write Your Bot Code
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...');
Explanation of the Code:
- Importing the library: We require the
node-telegram-bot-api
package. - Token Setup: Replace
'YOUR_TELEGRAM_BOT_TOKEN'
with the actual token you received from BotFather. - Creating the bot: We initialize the bot with polling mode. Polling allows the bot to constantly check Telegram servers for new messages.
- Handling messages: The bot listens for incoming messages and replies with a friendly greeting.
- Running the bot: When you run the script, your bot will go live and respond to messages.
Step 4: Run the Bot
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.
Step 5: Add More Functionality
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}`);
});
Explanation:
- The
onText
method listens for messages that match the/time
command. - When the command is detected, the bot responds with the current time.
Restart your bot and test the new command by typing /time
in the chat.
Next Steps: Make It AI-Powered!
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.
Conclusion
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!