Node.js Automation: Building a Telegram Notification Bot

![[Node.JS 자동화] Node.js 자동화 프로그래밍, 반복 작업을 AI와 함께 자동화하는 실전 개발(프롤로그)](/uploads/editor/Node-JS-%EC%9E%90%EB%8F%99%ED%99%94-Node-js-%EC%9E%90%EB%8F%99%ED%99%94-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EB%B0%98%EB%B3%B5-%EC%9E%91%EC%97%85%EC%9D%84-AI%EC%99%80-%ED%95%A8%EA%BB%98-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%98%EB%8A%94-%EC%8B%A4%EC%A0%84-%EA%B0%9C%EB%B0%9C-%ED%94%84%EB%A1%A4%EB%A1%9C%EA%B7%B8-20260702-e78eab.webp)
Why Do You Need a Notification Bot?
Automation programs typically run continuously on servers.
For example:
AI content generation
News collection
Image creation
Data backup
Scheduled tasks
These processes run automatically without manual intervention.
However, to check if the program is running correctly,
if an error has occurred,
or if a task has completed,
you need to access the server manually.
This is where a Telegram notification bot comes in handy.
What Is a Telegram Bot?
A Telegram bot is an account used by programs rather than humans.
Your Node.js program can use the bot to:
Send messages
Send images
Transfer files
Receive commands
It's one of the most commonly used notification methods in automation systems.
Creating a Bot with BotFather
In Telegram, you create new bots using BotFather.
The process is straightforward:
Search for BotFather in Telegram
Type
/newbotEnter a bot name
Enter a bot ID
Receive the Bot Token
After creation, an API token is generated.
This token functions like a password and should not be shared publicly.
Managing Your Bot Token
In professional settings, you shouldn't hardcode the token in your code.
Using a .env file is recommended.
For example:
TELEGRAM_BOT_TOKEN=123456:xxxxxxxxxxxxxxxxxxxxxxxx
CHAT_ID=123456789
This approach keeps your token secure and makes maintenance easier.
Installing the Library
In Node.js, install the Telegram Bot API library:
npm install node-telegram-bot-api
Once installed, you can use it in your project.
Connecting Your Node.js Bot
First, import the library:
const TelegramBot = require("node-telegram-bot-api");
Then create your bot using the token:
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, {
polling: false
});
Your Node.js application is now ready to send messages via Telegram.
Sending Your First Message
Here's a simple example:
await bot.sendMessage(
process.env.CHAT_ID,
"Hello from your Node.js Telegram bot."
);
When you run the program, a message will arrive in your Telegram inbox.
Finding Your Chat ID
To send messages, you need your Chat ID.
The easiest way to find it is:
Send any message to the bot
Use a Telegram API or ID checking bot
You only need to check this once—it's reusable for all future messages.
Sending HTML and Markdown Messages
You can format your messages in Telegram using HTML or Markdown.
For example:
await bot.sendMessage(chatId,
"<b>Task completed</b>",
{
parse_mode: "HTML"
});
Or:
await bot.sendMessage(chatId,
"*Task completed*",
{
parse_mode: "Markdown"
});
This allows you to send formatted messages with emphasis.
Sending Multi-Line Messages
Organize your results neatly in multi-line messages:
const msg = `
AI content generation completed
Title: Node.js Automation
Category: Development
Image: Created
Post published
`;
await bot.sendMessage(chatId, msg);
This format is commonly used in professional settings.
Alerting on Errors
It's crucial to get immediate alerts for errors in automation systems.
try {
await makePost();
} catch (err) {
await bot.sendMessage(
chatId,
"Error occurred: " + err.message
);
}
This way, you can spot issues without logging into the server.
Creating a Reusable Function
In your project, create a reusable function for sending notifications:
async function sendTelegram(message) {
await bot.sendMessage(
process.env.CHAT_ID,
message
);
}
You can then use it like this:
await sendTelegram("AI content generation completed");
This simplifies your code and improves maintainability.
How to Use This in Your Automation Projects
In the project we're building, Telegram notifications will be used for key events:
AI content generation started
AI content generation completed
News collection completed
Image creation completed
Scheduled task started
Server error occurred
Data backup completed
These notifications help you track progress without manual server checks.
Using with PM2 for 24/7 Operation
Running your program via PM2 ensures it runs continuously on the server.
When combined with Telegram notifications, you can immediately see if:
- The program stops working
- Errors occur
This is the most common operational method used in professional settings.
Avoid Overloading Your Notifications
Beginners often send notifications for every single task.
However, too many alerts can make it harder to spot important messages.
We recommend sending notifications only for:
Task start
Task completion
Error occurrence
Server shutdown
Backup completion
This is sufficient for most use cases.
Continued Use in Future Projects
The Telegram bot we've built will be reused across multiple projects:
- AI blog auto-posting system
- News collector
- Image generator
- Scheduled tasks manager
- Server management program
One well-built notification system can serve many different automation projects.
Conclusion
In this lesson, we've learned how to create and integrate a Telegram bot with Node.js for real-time notifications in automation systems.
Just as important as running your system is quickly verifying results.
Adding Telegram notifications allows you to monitor completion and errors in real time, making your system more stable and reliable.
In the next lesson, we'll build a practical project: an AI blog auto-posting system that integrates all the Node.js concepts we've learned—Axios, MySQL, OpenAI API, Cron scheduling, file handling, and Telegram notifications—all into one automation workflow.
FAQ
What is BotFather?
BotFather is a Telegram bot that helps you create and manage other Telegram bots. It's used to generate API tokens for your custom bots.
Why use .env files for storing tokens?
.env files keep sensitive information like API tokens secure by not hardcoding them in your source code, making it easier to manage and protect credentials.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 자동화] 11강. 텔레그램 알림봇 만들기