How reo7sp/tgbot-cpp works
Main Components
• Library Core: This includes the core classes that the users of the library interact with. For example, the primary classes are housed in the “include/tgbot” directory (e.g., Bot.h, Api.h, and various utility headers). The “src” directory holds the corresponding implementations (e.g., Api.cpp, Bot.cpp).
• Data Types and Parsers: The “include/tgbot/types” along with the corresponding “src/types” provide structures and helper functions to parse and model the Telegram API entities (Message, Chat, Update, etc.).
• Event Handling: The library supports event-driven programming, indicated by components like EventHandler.h and EventBroadcaster.h, which are central to reacting to incoming messages and commands.
• Tools, Tests, and Samples: The “samples” and “test” directories include various example bots and unit tests (using CMake build files) that demonstrate how to use the library. This separation shows a clear distinction between production-quality library code and extra material meant for learning, verifying, or demonstrating usage.
Relationships and Interactions
• The Bot acts as an orchestrator: Clients create an instance of the Bot and register event handlers (e.g., onCommand, onAnyMessage).
• The API module (exposed via Api.h/Api.cpp) communicates with Telegram’s servers. It interacts with the network layer to send HTTP requests/responses.
• The Network Layer is abstracted to allow for different implementations (e.g., Curl or Boost) which the API module can use interchangeably.
• The Data Types provide the intermediate representations of messages, updates, and other Telegram entities. The API layer converts raw HTTP data into these types using parsing utilities.
• The Event Handling component receives incoming updates (whether via long polling or webhook) and dispatches them to the appropriate event handlers registered by the bot.
• Samples demonstrate how to wire all the pieces together, showing the flow from the instantiation of the bot, setup of event handlers, to the initiation of a long polling loop or webhook server.
Architectural Patterns and Design Principles
• Abstraction & Modularity: The clear division of code into “include”/“src” for library components, “net” for network communications, “types” for data modeling, and “tools”/“samples” for auxiliary functions.
• Dependency Inversion: The network layer is pluggable; users can select a preferred HTTP client by choosing the appropriate implementation (e.g., Curl vs. Boost).
• Event-Driven Architecture: Rather than a monolithic procedural design, the library employs callbacks and events to handle diverse Telegram API updates.
• Separation of Concerns: The library cleanly separates API requests, network client implementations, and data handling, making it easier to maintain and extend.