mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-06 22:49:32 +00:00
ac7cae8fa7
A fully-generated, strongly-typed Go client for the Telegram Bot API. * 176 methods + 301 types generated from Bot API v10.0 * 1408 auto-generated tests (8 scenarios per method) * Typed unions throughout — no 'any' in the public surface * Pluggable HTTP transport and JSON codec (default goccy/go-json) * Built-in retry middleware honouring Telegram's retry_after * Generic dispatcher with filters and conversation handlers * Self-verifying codegen pipeline (regen → audit → emit → run tests) * 14 example bots covering common patterns
admin
Authentication middleware that restricts the bot to an allowlist of Telegram user IDs.
What it shows
router.Use(...)to install a globalMiddleware[*api.Update]- Parsing
ALLOWED_USERSenv var into amap[int64]boollookup set - Extracting sender ID from multiple update types in one helper
- Silent drop pattern for unauthorized updates (no error, no reply)
Environment variables
| Variable | Required | Description |
|---|---|---|
TELEGRAM_BOT_TOKEN |
Yes | Bot token from @BotFather |
ALLOWED_USERS |
No | Comma-separated numeric user IDs, e.g. 123456,789012. If unset, all users are permitted. |
Finding your user ID
Send /whoami to the bot — it replies with your numeric Telegram user ID. Add that ID to ALLOWED_USERS to restrict the bot to you.
Extending
Combine with examples/moderation to ensure only group admins can invoke moderation commands:
router.Use(allowlistMiddleware(adminIDs))
router.OnCommand("/ban", banHandler)
For group-context admin checks (verify the sender is an admin of that specific group), use api.GetChatAdministrators and check the result dynamically rather than a static ID list.
Running
export TELEGRAM_BOT_TOKEN=123456:ABC...
export ALLOWED_USERS=111111,222222
go run ./examples/admin