Botlib is a small C framework for building Telegram bots. It provides a threaded event loop that delivers every matching update to your callback together with convenience helpers for SQLite, JSON, and dynamic strings.
- Telegram Bot API helpers to send, edit, and download messages from a per-update worker thread.
BotRequestparsing: split argv/argc, message type, mentions, file metadata (voice OGG), and targeting info.- SQLite helpers and a ready-to-use key/value schema (
TB_CREATE_KV_STORE) plus convenience wrappers likekvSet/kvGet. - JSON selection with
json_select(Parson) and SDS dynamic strings baked in. - HTTP GET wrappers on top of libcurl.
- Minimal dependencies:
libcurl,libsqlite3,pthread, and a C23-capable compiler.
- Install dependencies (Debian/Ubuntu):
sudo apt install build-essential libcurl4-openssl-dev libsqlite3-dev just(or equivalent packages on your distro). - Drop your Telegram bot token in
apikey.txt(one line), or pass it via--apikey <token>when running. - Edit
mybot.cif you want to tweak triggers, responses, or database setup; the example already seeds the key/value store schema. - Build:
just build(release-style) orjust build-sanitize(ASan/UBSan/leak). If you prefer Zig,zig buildproduceszig-out/bin/mybotand accepts-Dsanitize=true. - Run the bot from the repo root:
./mybot --debugfor verbose output, or add--dbfile <path>to pick a different SQLite location. - Add the bot to your chat/channel and promote it to administrator so it can read group messages; private chats work without admin rights.
The default database file is mybot.sqlite in the working directory.
--apikey <token>: Telegram Bot API key (overridesapikey.txt).--dbfile <file>: Path to the SQLite database (default:./mybot.sqlite).--debug: Increases debug level each time it is passed and implies verbose logging of Telegram responses.--verbose: Print basic request handling traces without full debug output.
- Entry point:
startBot(create_sql, argc, argv, flags, handleRequest, cron, triggers); - Your request handler receives
sqlite3 *dbhandleandBotRequest *br; free the request withfreeBotRequest()if you allocate your own. - Respond with
botSendMessage,botSendMessageAndGetInfo, orbotEditMessageText; download voice messages withbotGetFile. - Use
kvSet/kvGet/kvDelfor the bundled key/value store or thesql*helpers for raw queries. json_selectprovides a simple formatted selector for navigating JSON replies.- See
mybot.cfor a complete, runnable example of triggers, storage, and message editing.
- The SQLite and JSON helpers are derived from the Stonky project; check that codebase for deeper examples.
- Additional documentation for the Telegram helper surface is planned; the example bot exercises the core flows today.