Skip to content

Telegram Gateway

The Telegram gateway lets users talk to KosmoKrator through a bot while still using the same sessions, providers, permissions, settings, tasks, memories, integrations, MCP, Lua runtime, and subagent system as the terminal agent.

Telegram update
-> TelegramGatewayRuntime
-> TelegramSessionRouter
-> AgentSessionBuilder
-> AgentLoop
-> TelegramGatewayRenderer
-> Telegram sendMessage/editMessage

The gateway worker long-polls Telegram, normalizes updates, routes each message to a Kosmo session, and streams agent output back to Telegram through a gateway renderer.

Terminal window
printf %s "$TELEGRAM_BOT_TOKEN" | \
kosmokrator gateway:telegram:configure \
--token-stdin \
--enabled on \
--session-mode thread_user \
--allowed-users "123456789,@alice" \
--require-mention on \
--global \
--json

Options:

OptionValuesPurpose
--tokentokenStore bot token directly. Prefer stdin/env for automation.
--token-stdinflagRead token from stdin.
--token-envenv var nameRead token from an environment variable.
--enabledon, offEnable or disable gateway.
--session-modethread, thread_user, chat, chat_userRoute Telegram conversations to sessions.
--allowed-userslistAllowed user IDs or usernames.
--allowed-chatslistAllowed chat IDs.
--require-mentionon, offRequire bot mention/reply in group chats.
--free-response-chatslistChats where mention is not required.
--poll-timeout-secondsintegerTelegram long-poll timeout.
--global, --projectflagWrite scope.
--jsonflagMachine-readable output.
Terminal window
kosmokrator gateway:telegram:status --json
kosmokrator gateway:telegram

For a system service, run the installed binary from a working directory that has the desired project config:

[Unit]
Description=KosmoKrator Telegram Gateway
After=network-online.target
[Service]
Type=simple
WorkingDirectory=/srv/my-project
ExecStart=/usr/local/bin/kosmokrator gateway:telegram
Restart=always
RestartSec=5
Environment=KOSMOKRATOR_TELEGRAM_BOT_TOKEN=123:abc
[Install]
WantedBy=multi-user.target
ModeSession key
threadOne session per Telegram thread/topic when available, falling back to chat.
thread_userOne session per thread and user.
chatOne session per chat.
chat_userOne session per chat and user.

Use thread_user for shared group chats where multiple people may ask independent questions. Use chat for a private bot used by one person.

SettingDefaultMeaning
gateway.telegram.allowed_usersemptyEmpty allows any reachable user. Set user IDs or usernames to restrict access.
gateway.telegram.allowed_chatsemptyEmpty allows all chats. Set chat IDs for deployment safety.
gateway.telegram.require_mentiononIn group chats, respond only when mentioned or replied to.
gateway.telegram.free_response_chatsemptyChat IDs where the bot can answer without mention.

Recommended group setup:

Terminal window
kosmokrator gateway:telegram:configure \
--allowed-users "123456789,@maintainer" \
--allowed-chats "-1001234567890" \
--require-mention on \
--session-mode thread_user \
--json

Gateway sessions use the same permission system as terminal sessions.

  • Guardian auto-approves safe reads and asks for risky operations.
  • Argus asks for every governed tool call.
  • Prometheus auto-approves governed calls.
  • Hard denies, blocked paths, blocked commands, project boundaries, integration permissions, and MCP trust still apply.

Because Telegram is remote, be conservative with project-level settings. A common deployment policy is:

Terminal window
kosmokrator settings:set tools.default_permission_mode guardian --project --json
kosmokrator settings:set tools.denied_tools "file_write,file_edit,apply_patch" --project --json

For a bot that should only answer questions, start sessions in Ask mode:

Terminal window
kosmokrator settings:set agent.mode ask --project --json

The gateway bridges supported Telegram text commands to Kosmo slash commands through TelegramSlashCommandBridge. Useful commands include:

CommandPurpose
/newStart a new session.
/resumeResume a previous session where supported.
/sessionsList sessions.
/compactCompact context.
/guardian, /argus, /prometheusChange permission mode.
/edit, /plan, /askChange agent mode.

The bot token can come from:

  1. Managed secret gateway.telegram.token.
  2. YAML config value gateway.telegram.token.
  3. Environment variable KOSMOKRATOR_TELEGRAM_BOT_TOKEN.

Preferred setup:

Terminal window
printf %s "$TELEGRAM_BOT_TOKEN" | kosmokrator secrets:set gateway.telegram.token --stdin --json
kosmokrator gateway:telegram:configure --enabled on --json
Terminal window
kosmokrator gateway:telegram:status --json
kosmokrator settings:list --category gateway --json
kosmokrator secrets:status gateway.telegram.token --json
SymptomCheck
Worker exits with disabled errorgateway.telegram.enabled must be on.
Worker exits with missing tokenConfigure gateway.telegram.token or KOSMOKRATOR_TELEGRAM_BOT_TOKEN.
Bot ignores group messagesMention the bot, reply to the bot, or add the chat to free_response_chats.
Some users cannot talk to the botCheck allowed_users; usernames are stored without the leading @.
Messages go to the wrong sessionChange session_mode to thread_user or chat_user.