Hey, how's it going? You know, the other day I had to figure out how to create a telegram bot with node js, and let me tell you, it was a mess at first but I got it sorted. How to create a telegram bot with node js? I'll explain it like we're chatting at a bar, me with my beer and you nodding along.
The thing is, you often need a bot to automate boring tasks, like replying to messages or handling commands, and Telegram is great for that. But the catch is, if you don't follow the steps right, you end up wasting hours.
Alright, let's cut to the chase: today we're building a simple bot that greets the user and responds to a /start command.
Before diving into code, make sure you have Node.js installed, a Telegram account, and you've created a bot via BotFather – that bot that gives you the token. I prefer using Node.js for these projects because it's fast and scalable, unlike Python which I tried once and honestly sucked for real-time apps.
Get the prerequisites sorted
You need to install a few packages. Open your terminal and run npm init to create a package.json, then npm install node-telegram-bot-api. It's a library that makes everything easier, for real.And don't forget a code editor, like VS Code, which I always use because it's as comfy as an old sweater.
How to set up the bot on Telegram
First things first, go to Telegram and chat with BotFather. Type /newbot and follow the prompts to name your bot. You'll get a token, like '123456:ABC-DEF1234abc'. Keep it safe, because it's like your house key.The last time I forgot to save it, I lost two hours remaking it – a lesson learned the hard way.
Write the basic code
Now, in your index.js file, let's import the library. Here's a simple snippet:const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_TOKEN_HERE'; // Replace with yours
const bot = new TelegramBot(token, { polling: true });
bot.on('message', (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Hi! I'm your bot.');
});
bot.onText(//start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Welcome! Start chatting.');
});
This is the core of the bot. It replies to any message with a greeting, and if you type /start, it sends a special message. I added that bot.onText because it's more precise for commands, even though I initially thought of using just onMessage and regretted it.
But hold on, don't rush: run it with node index.js and send a message to the bot on Telegram. You'll see it works, but if you don't test first, like I did once, you might deploy a mess.
Add extra features
Now that you have the basics, let's make it more fun. Suppose you want the bot to tell the current time. Add this to the code:bot.onText(//time/, (msg) => {
const chatId = msg.chat.id;
const time = new Date().toLocaleTimeString();
bot.sendMessage(chatId, The current time is ${time}.);
});
See? With a /time command, the bot responds with the time. It's a simple thing, but useful, and I prefer adding features like this because they make the bot more interactive.
And here's a quick aside: last year, I built a similar bot for a friend and spent a whole night debugging silly errors, like a messed-up timestamp.
Deploy to a server
Okay, but we don't want to keep it local. Let's deploy it on Heroku, which is free and straightforward. First, sign up for Heroku and install the CLI.In your project, add a Procfile with: web: node index.js. Then, do git init, git add ., git commit -m "First commit", heroku create, heroku push origin main, and heroku open.
Wait, it's not always that smooth; sometimes Heroku acts up, but I use it because it's quick, even though I tried Glitch once and didn't like it at all.
Test and refine
Once it's deployed, test it properly. Send it a message and check if it responds. If something goes wrong, look at the logs with heroku logs. And remember, the real trick is to iterate: add more commands as you go.In the end, you have a working bot deployed and ready for the world. What do you think, huh? It was fun explaining it like this.