FreePascal/Lazarus library for the MAX Bot API (dev.max.ru). Target operating systems: Linux and macOS.
The library does not use any third-party libraries, besides the built-in FPC units (fcl-web, fcl-net, openssl are all part of the FPC distribution). You can use it both independently and together with other libraries, for example, as a plug-in to Brook-framework/Brook4FreePascal.
The pinned toolchain (both CI jobs and local builds): FPC 3.2.2 + Lazarus 4.4 (see .github/workflows/make.yml).
Console builds. Run from the repository root; always build with -FU into a dedicated lib directory:
# console test runner + offline suites
mkdir -p test/lib
fpc -B -dni18n -dUseCThreads -Futest -Fubot_ex -Fu. -Fuexamples/daemon -FEtest -FUtest/lib test/console.lpr
# examples (each one is built from its own folder)
cd examples/LongPolling && mkdir -p lib && fpc -B -Fu../.. -FUlib max_longpolling.lpr
cd examples/Webhook && mkdir -p lib && fpc -B -dUseCThreads -Fu../.. -FUlib max_webhook.lpr
cd examples/daemon && mkdir -p lib && fpc -B -dni18n -dUseCThreads -Fu../.. -Fu../../bot_ex -FUlib max_daemon.lprmkdir -p is required because the compiler does not create the output directory itself. -FU keeps the compiled .ppu/.o files out of the source folders, where they could shadow the ppu of the package on the next IDE build. The daemon example additionally needs -dni18n (excludes the Lazarus Translations unit pulled by bot_ex/maxbot, the same switch the test runner builds with) and its own -Fu../../bot_ex.
Lazarus projects (*.lpi of the examples) are built with lazbuild; the library itself is installed into the IDE through the fpmax.lpk / fpmax_dt.lpk packages.
fpmax.lpk- the runtime units:maxtypes(data model),maxsendertypes(TMaxSenderAPI sender with TLS trust resolution), the HTTP client brokers (maxbasehttpclient+ the FCL-basedmaxfclhttpclientbroker),maxutils,maxstatlog,maxwebhook(TMaxWebhookReceiver, the webhook endpoint).fpmax_dt.lpk- the design-time componentDTMaxLongPollBot(a ready-made long polling bot).fpmaxbot_ex.lpk- theTMaxBotapplication framework (bot_exunits) on top of the sender, plus the DT component runtime unitsmaxbot_dt.pasandreceiverthread.pas(both LCL-free).
Note: the design-time package links the LCL. Non-GUI hosts should take the runtime units from fpmaxbot_ex instead - the receiver unit is LCL-free, and the daemon and Webhook examples show headless hosts.
Security note: the Token property of the design-time component is declared stored False (td-026). The guarantee is write-side and covers forms saved by this and later versions of the package: they never write the token into the .lfm form file, nor into the binary resource the form is compiled into. Loading never checks the flag - a .lfm saved by an older package version, or with a hand-added Token = ... line, still carries a working token and still loads it when the form opens. After upgrading, grep your .lfm files for Token =; the first re-save of such an old form in the IDE silently drops the token line, and the bot then fails at start with Token for bot is not specified!. A Token value typed into the Object Inspector is never written to the form file or any other artifact the package produces, so it never reaches the running program either - set the token at run time, as the examples do.
The library sends the raw access token in the Authorization header, without the Bearer prefix (per the dev.max.ru API docs).
How the library resolves TLS trust (TMaxSender):
- an explicit
SSLCAFilepins the trust and is the only trust anchor on the FCL transport; the canonical contract is theSSLCAFiledoc comment inmaxbasehttpclient.pas(td-010); - by default (empty
SSLCAFile, verification on) it merges the shippedcerts/russian-trusted-root-ca.pem(looked up next to the binary only) with the system bundle into one anchor; - when neither source is found, the resolution fails with an exception (fail-closed).
Linux (Debian/Ubuntu): the system bundle is /etc/ssl/certs/ca-certificates.crt. Install the root system-wide:
cp certs/russian-trusted-root-ca.pem /usr/local/share/ca-certificates/russian-trusted-root-ca.crt
update-ca-certificatesmacOS: the library reads PEM bundles, not the Keychain. The simplest way is to keep the certs/ folder next to the binary: the shipped bundle already trusts the API host, and merging it with the system bundle adds Let's Encrypt for the upload hosts. Alternatives: append the root to a system PEM bundle (/etc/ssl/cert.pem, /opt/homebrew/etc/openssl@3/cert.pem, /usr/local/etc/openssl@3/cert.pem), or set SSLCAFile explicitly. Installing the root into the Keychain is the system-wide option for GUI applications and curl:
security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain certs/russian-trusted-root-ca.pemNote that fp-max does not read the Keychain. To export the root as PEM for manual checks:
security find-certificate -a -c "Russian Trusted Root CA" -pCaveats:
SSLVerifyCert := Falsedisables verification - only for a local test: it opens the channel to a man-in-the-middle.- On stock hosts the usual barrier is loading the OpenSSL library, not certificates (known limitations: on macOS
InitSSLInterfaceneeds 1.1-named OpenSSL libraries; on Debian the unversionedlibssl.socomes withlibssl-dev).
- Long polling:
TMaxSender.GetUpdatesEx, or theDTMaxLongPollBotdesign-time component (see theSynchronizeEventscontract inreceiverthread.pas). - Webhook: the
SetWebhook/DeleteWebhookwrappers ofTMaxSender, theTMaxWebhookReceiverendpoint of themaxwebhookunit (Startblocks untilStop; the answer is 200 on every delivery fault and 403 only on a secret mismatch; a failed bind raisesEMaxConfignaming the port; any otherESocketErrorof the startup window - a failed accept, not a bind - propagates as is and stops the host with the honest diagnosis), and a full example inexamples/Webhook(an HTTPS webhook client ofTMaxWebhookReceiverwith OpenSSL). The two delivery mechanisms are mutually exclusive: while the URL subscription is active, long polling delivery is paused. The platform requires strictly HTTPS on port 443 with a publicly trusted certificate; the server key must be RSA. - Inline keyboards:
MakeInlineKeyboardAttachmentofmaxsendertypesbuilds theSendMessageExattachments array from flatText,Payloadpairs - one row of callback buttons, two-dimensional by construction (payload.buttonsmust be 2D: a flat array is rejected with HTTP 400). - Non-GUI hosts: supported - run the receiver with
SynchronizeEvents := Falseand use the runtime units directly; thedaemonexample is a headless long-polling host withTEventLoglogging, graceful SIGTERM stop and shipped systemd/launchd unit templates (examples/daemon), the Webhook example is a headless sample built onTMaxWebhookReceiver.
| Folder | What it shows |
|---|---|
LongPolling |
console echo bot, single thread, long polling |
Webhook |
console echo bot, HTTPS webhook client of TMaxWebhookReceiver (OpenSSL) |
daemon |
headless daemon: long polling with SynchronizeEvents=False, TEventLog, graceful SIGTERM stop, systemd/launchd unit templates |
LongPollingDesktop |
desktop GUI, updates received in a separate thread |
DesignTime |
desktop GUI built around the DTMaxLongPollBot component |
getMe |
desktop GUI, a single API call, no update receiving |
SimpleSendMessage |
console, sends one message |
linux_pclocker |
console with an X11/Cairo window, Linux only |
See test/readme.md: how to build the console runner, which suites run offline (TTestSenderTypes, TTestMaxBot, TTestTLSTrust) and which run online against a real bot (TTestSender).
{$mode objfpc}{$H+}in every unit/program; comments and identifiers in English; WHY-comments on non-obvious decisions (examples get copied).- Debt records:
docs/tech-debt/(catalog:docs/tech-debt/INDEX.md) — closed records keep their file as archive and only lose their INDEX line; anchor by symbols, never by line numbers (they drift).