Overview
src/notifier/telegramNotifier.ts handles all subscriber management and message delivery. Chat IDs are persisted to data/chat_ids.json using atomic writes. The broadcast loop applies rate limiting and per-message timeouts, and automatically removes chat IDs that return permanent errors.
Exported functions
saveChatId
data/chat_ids.json if it is not already present. Called when a user sends /start to the bot. Creates the data/ directory if it does not exist.
The Telegram chat ID of the subscriber to add.
loadChatIds
data/chat_ids.json. Returns an empty array if the file does not exist or cannot be parsed.
removeChatId
data/chat_ids.json. Called when a user sends /stop to the bot.
The Telegram chat ID to remove.
notifyAllUsers
The HTML message string to send. Delivered with
parse_mode: 'HTML'.Broadcast behavior
Rate limiting
When there is more than one subscriber, a delay is inserted between each send:Per-message timeout
EachsendMessage call is wrapped in a Promise.race with a 15-second timeout:
Permanent error handling
Errors returned by the Telegram API are classified as permanent or transient:- 403 — bot was blocked or kicked by the user
- 400 with
chat not found— chat was deleted or the ID is invalid
chat_ids.json at the end of the broadcast run via a single batched write.
Atomic file writes
All writes tochat_ids.json use write-file-atomic, which writes to a temporary file and then renames it to the target path. This guarantees that the file is never left in a partial or corrupt state if the process crashes mid-write.
Chat IDs are truncated to their last 4 digits in all log output — e.g.,
***4321. This provides enough information for debugging without exposing full chat IDs in server logs.