packages/bot-telegram/README.md
The Telegram PlatformAdapter for @copilotkit/bot. It connects
a Telegram bot to any AG-UI agent: ingress via grammY (long-polling or webhook),
egress as Telegram HTML rendered from the @copilotkit/bot-ui JSX vocabulary,
plus streaming via chunked message edits, opaque-id interactions, and HITL.
You write your UI as JSX once (@copilotkit/bot-ui) and drive the bot with
@copilotkit/bot; this package is the only one that talks to Telegram.
pnpm add @copilotkit/bot-telegram @copilotkit/bot @copilotkit/bot-ui
import { createBot } from "@copilotkit/bot";
import {
telegram,
defaultTelegramTools,
defaultTelegramContext,
} from "@copilotkit/bot-telegram";
const bot = createBot({
adapters: [
telegram({
token: process.env.TELEGRAM_BOT_TOKEN!,
}),
],
agent: (threadId) => makeAgent(threadId),
tools: [...defaultTelegramTools, ...appTools], // lookup_telegram_user + your tools
context: [...defaultTelegramContext, ...appContext], // tagging/HTML/thread guidance
});
bot.onMention(({ thread }) => thread.runAgent());
// Optional: greet users when they start a DM
bot.onThreadStarted(async ({ thread }) => {
await thread.post(<Message><Section>Hi! How can I help?</Section></Message>);
});
await bot.start();
telegram(opts) returns a TelegramAdapter. By default it runs in
long-polling mode — no public URL needed. Set mode: "webhook" (with
webhook.domain) to receive updates via HTTP, or mode: "auto" to let the
adapter pick based on environment variables (prefers webhook in Vercel/Lambda
environments, falls back to polling).
| Var | Purpose |
|---|---|
TELEGRAM_BOT_TOKEN | Bot token from @BotFather (e.g. 123:ABC-xyz) |
renderTelegram(ir) translates the @copilotkit/bot-ui vocabulary to a
Telegram Bot API payload (text, parseMode: "HTML", optional
inlineKeyboard, optional photos): Message → container, Header → <b>,
Section/Markdown → telegramHtml(), Field(s) → <b>label</b> value,
Context → <i>, Actions → inline keyboard rows, Select → inline keyboard rows, Image → photo, Table → <pre> monospace grid, Divider → ──────.
Telegram API limits are enforced via TELEGRAM_LIMITS and the helpers:
| Limit | Value | Element |
|---|---|---|
messageText | 4096 | characters per message |
caption | 1024 | caption characters |
callbackData | 64 | bytes per callback_data |
buttonsPerRow | 8 | buttons per inline keyboard row |
buttonsPerMessage | 100 | total inline keyboard buttons |
buttonText | 64 | button label characters |
photosPerMessage | 10 | photos per message |
Replies stream through ChunkedEditStream: the adapter posts a placeholder
message and edits it as tokens arrive, throttled to one edit per second. When a
reply approaches Telegram's 4 096-char limit (~4 000 characters) the stream
transparently mints a second message and continues — keeping each Telegram
message within limits with no reflow of already-frozen chunk boundaries.
Every Telegram callback_query (inline keyboard button click) is acked
promptly via answerCallbackQuery — the adapter's ackDeadlineMs is 3 s so
the client spinner clears quickly, well within Telegram's ~30 s validity
window for answerCallbackQuery. After acking, decodeInteraction extracts
the conversation key and minted opaque id and hands an InteractionEvent to
the engine. Unrelated clicks decode to events the bot harmlessly ignores.
Use thread.awaitChoice(<Picker .../>) to post an interactive inline keyboard
and block until a click resolves it; the resolved value is the clicked button's
callback data. Agent interrupts (on_interrupt) are captured by the run renderer
and dispatched to your onInterrupt handler, which posts a picker; the click
resumes the agent via thread.resume(value).
/start → onThreadStartedThe listener intercepts the Telegram /start command in private chats and
fires onThreadStarted, letting the bot post a greeting or configure the
conversation before the first turn.
Inbound file attachments (photos, audio, video, documents) can be downloaded
and delivered to the agent as multimodal AG-UI content parts via
buildFileContentParts. The adapter can post files back out via
thread.postFile({ bytes, filename }) (sends as a document).
defaultTelegramTools — ships lookup_telegram_user so the agent can
resolve a public @username handle to a Telegram user id for @-mentions.
The tool calls getChat with the supplied query and only works for public
@username handles; arbitrary display-name queries are not supported and
return undefined. Spread into tools.defaultTelegramContext — tagging procedure, Markdown-vs-HTML guidance, and
the Telegram DM / forum-topic / group-per-user conversation model. Spread into
context.setMyCommandsregisterCommands(specs) calls bot.api.setMyCommands, registering the
command menu visible in the Telegram UI. The listener forwards every bot
command to the engine's onCommand handlers.
| Mode | How it works |
|---|---|
polling | Default. grammY long-polling. No public URL needed. |
webhook | grammY webhook + minimal Node HTTP server. Requires webhook.domain. |
auto | Webhook when VERCEL/AWS_LAMBDA_FUNCTION_NAME/NETLIFY is set, else polling. |
editMessageText calls.TelegramConversationStore is
in-memory; sessions and message history are lost on restart.<Select> option-value round-trip — Telegram callback_data is limited
to 64 bytes. If an option's value or id serializes to more than 64 bytes
the renderer silently drops (degrades) that option — the button simply does
not appear in the keyboard. Use short id strings on <Option> elements
when option values are large objects.user:<userId>): each member's
@mentions form one ongoing conversation for that user, and button clicks
resolve to the clicking user's conversation. The bot does not maintain
a single shared group thread. Forum supergroups use per-topic threads
(topic:<threadId>); DMs are a single flat conversation (dm).update() does not change media — editing a previously-posted message
via thread.update(ref, ir) calls editMessageText and updates text plus
inline keyboard only. Photos attached to the original message are not
changed.getFile are skipped with
a note in their place.lookup_telegram_user is @username-only — the tool resolves public
@username handles by calling getChat. Queries that do not start with
@ return undefined immediately; arbitrary display-name or real-name
searches are not supported.telegram, TelegramAdapter, TelegramAdapterOptions;
createRunRenderer, CreateRunRendererArgs;
decodeInteraction, conversationKeyOf, deriveConversationKey, toPlatformUser;
renderTelegram; TELEGRAM_LIMITS, truncateText, clampArray, byteLen;
defaultTelegramTools, lookupTelegramUserTool;
defaultTelegramContext, telegramTaggingContext, telegramFormattingContext,
telegramConversationModelContext;
telegramHtml, escapeHtml; withTelegramFormatFallback, stripHtml;
TelegramConversationStore; ChunkedEditStream, ChunkedEditStreamConfig;
attachTelegramListener, ListenerConfig;
buildFileContentParts, TelegramFileRef, AgentContentPart, FileDeliveryConfig;
types: ConversationKey, ReplyTarget, TelegramMessageRef, TelegramInlineButton,
TelegramPayload; value DM_SCOPE.