How albertincx/formatbot1 works
Project type and purpose
• This is a Telegram bot service (“InstantViewBot”) that takes channel post links lacking Telegram Instant View (IV), fetches and processes their content (via parsing and compression utilities), and delivers IV pages back to users.
• It exposes an HTTP API (src/api) for retrieving stored documents (text/html/MD) and uses a message queue for decoupled, asynchronous processing.
High-level architecture components
a. Telegram Bot Layer
• Listens to user updates via the Telegram Bot API (src/index.js, src/service/rabbitmq.js).
• Parses commands and messages (src/service/commands, src/keyboards, src/messages).
• Enqueues tasks onto RabbitMQ.
b. Message Queue
• RabbitMQ: decouples incoming tasks from processing workers.
c. Worker/Service Layer
• Consumes tasks from RabbitMQ (src/service/jobMessage.js).
• Uses parsing utilities (src/api/utils) to fetch, sanitize, convert to IV, compress, support long content, generate PDFs, etc.
• Interacts with external processors: puppeteer (src/api/utils/puppet.js), Mercury API (src/api/utils/mercury.js), Telegra.ph (src/api/utils/makeTelegraph.js).
d. Data Layer
• MongoDB via Mongoose schemas (src/api/models) to store parsed pages and metadata (schema.js, schemaUpdatedAt.js).
• Configuration in src/config/mongoose.js, vars.js.
e. HTTP API Layer
• Express routes (src/api/routes) to serve stored documents and trigger format operations.
• Models and routes grouped under src/api.
f. Scheduler/Cron
• cron.js triggers periodic reprocessing or cleanup.
g. External Dependencies
• Telegram Bot API, RabbitMQ broker, MongoDB, Telegra.ph, cloud-based link shorteners.
Relationships and data flow
1. User → Telegram Bot API → Bot Service
2. Bot Service places message on RabbitMQ queue
3. Worker Service pulls message → invokes parsing utils → stores result in MongoDB and/or sends data to Telegra.ph
4. Worker emits completion event → Bot Service retrieves and sends IV back to user
5. External HTTP clients can call Express API endpoints to retrieve stored documents
Architectural patterns and design principles
• Asynchronous, event-driven architecture (publish/subscribe via RabbitMQ)
• Layered separation: API layer, Service/Worker layer, Data layer, Config layer
• Single codebase implementing both HTTP API and background workers
• Modular utilities for parsing, sanitizing, image handling
Technologies, frameworks, libraries
• Node.js + Express (src/api)
• Mongoose for MongoDB (src/api/models, src/config/mongoose.js)
• amqplib or custom RabbitMQ wrapper (src/service/rabbitmq.js)
• Puppeteer, Mercury parser, Telegra.ph API
• Cron scheduling via node-cron (cron.js)