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" | \
kosmo gateway:telegram:configure \
--token-stdin \
--enabled on \
--session-mode thread_user \
--allowed-users "123456789,@alice" \
--admin-users "123456789" \
--require-mention on \
--reply-to-mode first \
--progress-notice-interval-seconds 60 \
--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.
--admin-userslistUsers allowed to press global control buttons and approve any request.
--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.
--reply-to-modeoff, first, allWhether answer chunks reply to the original message.
--disable-link-previewson, offDisable Telegram link previews for gateway messages.
--fresh-final-after-secondsintegerSend a fresh final message after long streamed edits. 0 disables it.
--progress-notice-interval-secondsintegerMinimum seconds between long-run progress status updates. 0 disables it.
--reactionson, offMark processing and completion with Telegram reactions when supported.
--global, --projectflagWrite scope.
--jsonflagMachine-readable output.
Terminal window
kosmo gateway:telegram:status --json
kosmo 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/kosmo gateway:telegram
Restart=always
RestartSec=5
Environment=KOSMO_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.admin_usersemptyUsers allowed to press global control buttons and approve any request.
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.

Approval and control buttons use a stricter policy than normal messages. In group chats, approval buttons can be pressed by the original requester, an explicit allowed user, or an admin user. Global control buttons such as mode switches require an explicit allowed or admin user.

The gateway keeps one status message per route, updates it while tools run, and can send periodic progress notices for long operations. gateway.telegram.progress_notice_interval_seconds controls that cadence.

Telegram message delivery also handles UTF-16 message limits, optional reply threading, HTML fallback, flood-control retry hints, and stale reply/thread targets. gateway.telegram.fresh_final_after_seconds can send a fresh final message after a long edited stream so the completion appears at the current time in the chat.

Recommended group setup:

Terminal window
kosmo 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
kosmo settings:set tools.default_permission_mode guardian --project --json
kosmo 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
kosmo 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 kosmo.gateway.telegram.token.
  3. Environment variable KOSMO_TELEGRAM_BOT_TOKEN.

Preferred setup:

Terminal window
printf %s "$TELEGRAM_BOT_TOKEN" | kosmo secrets:set gateway.telegram.token --stdin --json
kosmo gateway:telegram:configure --enabled on --json
Terminal window
kosmo gateway:telegram:status --json
kosmo settings:list --category gateway --json
kosmo secrets:status gateway.telegram.token --json
SymptomCheck
Worker exits with disabled errorkosmo.gateway.telegram.enabled must be on in YAML, or set gateway.telegram.enabled through kosmo settings:set.
Worker exits with missing tokenConfigure kosmo.gateway.telegram.token or KOSMO_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.