How tungaqhd/simple-zalo-bot works
Project Type & Purpose
• This is a Node.js web application that acts as a Zalo bot. Its primary functionalities include responding according to a preset script and sending bulk messages.
• The project also features an admin panel (accessible at http://localhost:3000/admin) for managing settings or messages.
File Structure
• The main entry point is app.js, which likely initializes the Express server and ties together various components.
• The “config” folder includes configuration files (config.js, database.js) to set up environment parameters and database connections.
• The “helpers” folder holds bot helper logic (bot.js) that manages how the bot interacts with Zalo.
• The “middlewares” folder contains middleware functions such as adminMiddleware.js and zaloMiddleware.js, implying that some requests are filtered or processed (perhaps authentication or validation on incoming webhook requests).
• The “models” directory features a model (Robot.js), which likely encapsulates the bot’s data or state.
• The “routes” folder organizes endpoint definitions (admin.js, index.js, webhook.js) to separate concerns across admin, general routes, and the bot’s webhook handling.
• The “views” folder contains EJS templates for rendering the admin panel (admin.ejs), bot interface (robot.ejs), sending messages (send.ejs), and common header/footer components.
Main Components & Their Interactions
• Application Server (app.js): Acts as the orchestrator. It loads configurations, registers middleware, and binds the routes.
• Configuration Layer (config/): Supplies app settings and database connection details and influences the runtime behavior of other modules.
• Routing Layer (routes/):
• The “index.js” handles base or public routes.
• “admin.js” manages admin-specific requests and renders the admin panel which interacts with admin-specific middleware.
• “webhook.js” is designed to receive callbacks or events (likely for the Zalo bot) and hands these off to the proper middleware and helper functions.
• Bot Logic (helpers/bot.js): Contains the core logic for how the bot behaves, interprets scripts, and sends messages.
• Middleware (middlewares/): Provides additional processing (such as authentication or logging) for incoming requests, with special middleware for admin routes and Zalo interactions.
• Data Model (models/Robot.js): Represents the bot’s state or data that might be stored and manipulated.
• Presentation Layer (views/): Uses EJS templates to generate HTML for the admin panel and other user-facing pages.
Architectural Patterns & Design Principles
• MVC-like Separation: Although not strictly following complete MVC, the project clearly decouples configuration, routing, business logic (helpers), and presentation (views).
• Middleware Pattern: Express middlewares are used to preprocess and control request handling.
• Modular Design: Each function (bot logic, admin panel, webhook handling) is isolated into separate files/folders, promoting separation of concerns.
a. Main Components:
• Application Entry Point (app.js)
• Configuration Module (config/)
• Routing Modules (routes/ with admin, webhook, and index endpoints)
• Middleware (middlewares/)
• Bot Logic/Helper (helpers/bot.js)
• Data Model (models/Robot.js)
• Presentation Layer (views/)
b. Their Relationships/Interactions:
• The Server (app.js) loads configurations and middleware then routes incoming requests.
• Requests (from browsers or the Zalo service) hit corresponding routes (admin panel, webhook, or general endpoints) which trigger specific middleware.
• The webhook routes use the “zaloMiddleware.js” for Zalo-specific validations before passing control to the bot helper in “helpers/bot.js”.
• The admin panel routes use “adminMiddleware.js” and eventually render views from the “views/” folder.
• The bot helper may use the “models/Robot.js” to persist or retrieve bot-related data.
c. Important Patterns & Technologies:
• Use of Express.js for request handling.
• Middleware usage for request pre-processing.
• Template rendering using EJS in the views layer.
• A modular structure that separates configuration, routes, business logic, and view layers.
• Node.js runtime as the core environment.
• Separate the layers: Presentation (views), Business Logic (helpers, models), and Routes (controllers).
• Data flow:
• Interactions between routes and middleware.
• Connection from webhook endpoints to the bot helper and then to the model layer.
• Use labels for each component (e.g., “Admin Middleware”, “Zalo Middleware”, “Robot Model”).