How to Build a Telegram Bot Using Node.js – Step-by-Step Guide

How to Build a Telegram Bot Using Node.js – Step-by-Step Guide

 Creating your own Telegram bot is easier than you think. In this guide, I’ll walk you through the complete process — from setting up your bot with BotFather, writing the bot using Node.js, to deploying it live 24/7 using Render.

username: tns_server_error_bot

🧭 Step 1: Create a Bot on Telegram Using BotFather

  1. Open Telegram app

  2. In the search bar, type: BotFather

  3. Start the chat and type /start

  4. Then type /newbot and follow the steps:

    • Choose a name for your bot (e.g., TNS Server Bot)

    • Choose a username that must end with _bot (e.g., tns_server_error_bot)

  5. Copy the HTTP API token that BotFather gives you — we’ll use this later in our .env file.

💻 Step 2: Set Up Your Project in VS Code (or Cursor)

  1. Open your preferred IDE (I used Cursor but you can use VS Code)

  2. Create a new folder for your project:

    mkdir tns_server_error_bot cd tns_server_error_bot
  3. Initialize the project:

    npm init -y
  4. Install required packages:

    npm install node-telegram-bot-api dotenv

📁 Step 3: Project Structure

Create the following files:

telegram-bot/
├── bot.js
├── .env
├── .gitignore
├── package.json


🔐 Step 4: Store Your Telegram Bot Token Securely

Inside the .env file:

env
TELEGRAM_BOT_TOKEN=your_token_here

Never share this token publicly.

https://unhealthyirreparable.com/cit2c8ca?key=7566cfdb82de49ba4912160b26b7621f

Step 5: Write the Bot Logic (bot.js)

js
require('dotenv').config(); const TelegramBot = require('node-telegram-bot-api'); const token = process.env.TELEGRAM_BOT_TOKEN; const bot = new TelegramBot(token, { polling: true }); console.log('🤖 Telegram Bot is running...'); bot.on('message', (msg) => { const chatId = msg.chat.id; const userMessage = msg.text; console.log(`📩 Message from ${msg.from.username}: ${userMessage}`); // Example: simple replies if (userMessage.toLowerCase() === 'hello') { bot.sendMessage(chatId, 'Hello! How can I help you today?'); } else if (userMessage.toLowerCase().includes('error')) { bot.sendMessage(chatId, 'Please share the error details, I’ll look into it.'); } else { bot.sendMessage(chatId, 'Thanks for your message!'); } });

To run the bot:

bash
node bot.js

Now go back to Telegram, search your bot username (e.g., @tns_server_error_bot), click /start and try sending messages.

🚀 Step 6: Deploy the Bot on Render for 24/7 time

  1. Create a GitHub repository and push your code.

  2. Follow my guide here to upload your project properly to GitHub:
    👉 How I Learned the Right Way to Upload Code to GitHub

  3. Visit https://render.com

  4. Create a new Web Service

    • Connect your GitHub repo

    • Set the Start Command to:

      bash
      node bot.js
    • Add your .env variable inside the "Environment" tab on Render

Now your Telegram bot will stay live 24/7.


🧠 Bonus: Train Your Bot to Respond to Custom Messages

You can customize how your bot replies using if...else or even advanced logic like keyword matching or AI APIs. Example:

js
if (userMessage.toLowerCase().includes('help')) { bot.sendMessage(chatId, 'I am here to help. Please describe your issue.'); }

Want to add buttons, menus, or AI responses? That’s totally possible


too.


📝 Final Words

You’ve just created a live, responsive Telegram bot using:

  • ✅ BotFather

  • ✅ Node.js + node-telegram-bot-api

  • ✅ Hosted it on Render

💬 Follow me for more tutorials like this on my dev blog:
👉 Avinash Dev Blog

🔗 GitHub Source Code

You can view or clone the complete code for this Telegram bot on my GitHub:

👉 GitHub Repository – telegram_bot

Feel free to ⭐ star the repo if you found it helpful, and fork it if you'd like to build on it.


Comments

Popular posts from this blog

How to Upload Your Local Project to GitHub Properly

YouTube Videos, Translation, AI Tools Aur Hindi Mein Dekhne Ka Experience