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
-
Open Telegram app
-
In the search bar, type:
BotFather -
Start the chat and type
/start -
Then type
/newbotand 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)
-
-
Copy the HTTP API token that BotFather gives you — we’ll use this later in our
.envfile.
💻 Step 2: Set Up Your Project in VS Code (or Cursor)
-
Open your preferred IDE (I used Cursor but you can use VS Code)
-
Create a new folder for your project:
-
Initialize the project:
-
Install required packages:
📁 Step 3: Project Structure
Create the following files:
🔐 Step 4: Store Your Telegram Bot Token Securely
Inside the .env file:
Never share this token publicly.
https://unhealthyirreparable.com/cit2c8ca?key=7566cfdb82de49ba4912160b26b7621f
Step 5: Write the Bot Logic (bot.js)
To run the bot:
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
-
Create a GitHub repository and push your code.
-
Follow my guide here to upload your project properly to GitHub:
👉 How I Learned the Right Way to Upload Code to GitHub -
Visit https://render.com
-
Create a new Web Service
-
Connect your GitHub repo
-
Set the Start Command to:
-
Add your
.envvariable 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:
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.
.png)
Comments
Post a Comment