How hamz-06/telegram-logger-package works
Project Type and Purpose
• Type: npm library (backend utility)
• Purpose: Provide a simple, type‑safe Logger that sends error or custom log messages to Telegram chat topics and optionally to the console.
File Structure
• Top‑level: configuration files (tsconfig, eslint, ci/release workflows), tests folder, and telegram-logger-pkg source directory.
• src/index.ts: entry point exporting Logger class.
• src/handler: orchestrates log handling; contains loggerHandler.ts & messageHandler.ts.
• src/model: domain entities and builders (chatTopic.ts, logBuilder.ts, logMessageHandler.ts, telegramBot.ts).
• src/helper: utility functions.
• src/types: shared TypeScript types.
• Tests mirror the above to cover handlers, models, and helpers.
Key Components in the System
a. Client Application
• Any Node.js code that imports and uses new Logger<Channels>(...).
b. Logger API (Public Interface)
• Logger.logMessage(channelKey, message)
c. Handler Layer
• LoggerHandler: receives calls from the Logger API, orchestrates processing.
• MessageHandler: formats and dispatches messages.
d. Model/Domain Layer
• LogBuilder: constructs the payload.
• LogMessageHandler: wraps the log entry with metadata.
• TelegramBot: encapsulates HTTP calls to Telegram Bot API.
• ChatTopic: represents mapping of channel keys to Telegram topic URLs.
e. Helper Utilities
f. External Service
• Telegram Bot API: receives HTTPS requests, delivers messages to group topics.
g. Optional Console Sink
• Writes to stdout/stderr when displayConsoleLogs is enabled.
Relationships and Data Flow
• The Client App invokes Logger.logMessage().
• Logger API → LoggerHandler (read settings) → MessageHandler.
• MessageHandler uses LogBuilder + ChatTopic to assemble the payload.
• Payload is sent via TelegramBot (HTTP) to the Telegram servers.
• Optionally, the same MessageHandler also writes to the console sink.
• All components communicate synchronously in a single process.
Architectural Patterns & Principles
• Separation of concerns: API layer, handler/orchestrator, domain models, external adapter (TelegramBot).
• Dependency injection via constructor parameters (botToken, channels, settings).
• Single responsibility: each module has one clear purpose (formatting vs. dispatching).