A fast, low-level Telegram Bot API library for MQL5.<br/> Requests are built directly into raw byte buffers (fixed-offset literal tables + <code>ArrayCopy</code>, a custom <code>CJsonBuilder</code>, and multipart with boundary reuse) instead of going through an intermediate DOM — no per-field allocation, no generic serialization pass.
- **Independent long-polling updater** (`CTelegramBotUpdater` + `CTelegramHandler`): decoupled from `CTelegramAPI` — the bot only calls the API, the updater only polls and dispatches. Handlers register per event (27 event types) via bitmask or one at a time; `AllowedUpdates()` can auto-compute the `allowed_updates` filter from whichever events actually have a handler registered, so you don't get updates nobody is listening for.
- **Perfect-hash update dispatch**: incoming `getUpdates` keys are matched via a generated perfect hash (`EnumReg`), not string comparisons, before landing in the event `switch`.
- **Keyboard builders** (`CTlgInlineKeyboard`, `CTlgReplyKeyboard`): chainable, raw-buffer builders for `InlineKeyboardMarkup` / `ReplyKeyboardMarkup`. Both share a `CTlgKeyboardBase` (buffer + row/button bookkeeping) to avoid duplicating the same comma/bracket logic twice — not an "is-a" hierarchy, just shared plumbing.
- **Snapshot/rollback for speculative writes**: `JsonBuilderSnapshot` (JSON builder) and `TlgKeyboardSnapshot` (keyboards) capture position + internal stack state so an optional nested object (e.g. `reply_parameters`) can be written speculatively and rolled back cleanly if it ends up empty, without corrupting the builder's structural state.
- **Validated vs. trusted string writes**: library-controlled constants (parse mode names, chat actions, field keys) go through the unvalidated/raw path (`KeyWV`/`ValUWV`/`SetBuffer`); arbitrary user input always goes through the escaping path (`ValS`/`SetStr`) — chosen per call site, never blanket-escaped or blanket-trusted.
### Send a message
```mql5
#include"Src\\MainAPI.mqh"
TSN::CTelegramAPIbot;
bot.Token("123456:ABC-your-bot-token");
bot.ChatId(-100123456789);
bot.SendMessage("Hello from MQL5",TSN::TELEGRAM_PARSE_MODE_HTML);
├── Bottons/ # Inline/Reply keyboard builders + shared keyboard base
├── Emojis/ # Emoji constant table
└── Old/ # Previous (pre-rewrite) implementation, kept for reference
```
---
## Requirements
See [dependencies.json](./dependencies.json) for the full list.
- MetaTrader 5, build 5430+
- [`WebUtilsByLeo`](https://forge.mql5.io/nique_372/WebUtilsByLeo) — currently a private dependency; contact [@nique_372](https://www.mql5.com/es/users/nique_372) for access.
-`WebRequest` access enabled for `https://api.telegram.org`
Requires the `tsndep` package, available on [PyPI](https://pypi.org/project/tsndep). It automatically downloads and installs all declared dependencies.
---
## Quick Start
**1. Include the library:**
```mql5
#include"..\\TelegramByLeo\\Src\\MainAPI.mqh"
```
**2. Set the token and chat, and send something:**
```mql5
TSN::CTelegramAPIbot;
bot.Token("123456:ABC-your-bot-token");
bot.ChatId(-100123456789);
bot.SendMessage("Hello from MQL5");
```
**3. (Optional) Add polling:** see [Long polling with handlers](#long-polling-with-handlers) above.
---
## License
**[Read Full License](./LICENSE)**
By downloading or using this repository, you accept the license terms.