Telegram
Connect CodeSpar to Telegram using BotFather for bot creation and long-polling for real-time message handling.
Telegram Channel
CodeSpar integrates with Telegram via the Bot API using long-polling mode. No webhooks or public URLs are required — the bot polls Telegram's servers for new messages, making it simple to deploy behind firewalls or in local environments.
Prerequisites
- A Telegram account
- CodeSpar instance running (Docker or local)
Create a Telegram Bot
Step 1: Talk to BotFather
- Open Telegram and search for @BotFather
- Send
/newbot - Choose a display name for the bot (e.g.,
CodeSpar) - Choose a username (must end in
bot, e.g.,codespar_dev_bot) - BotFather responds with your bot token — copy it
Step 2: Configure Bot Settings (Optional)
While still in BotFather, you can set additional properties:
Suggested Command Hints
Send /setcommands to BotFather and paste:
These appear as autocomplete suggestions when users type / in the chat.
Environment Variables
Add these to your .env file:
| Variable | Required | Description |
|---|---|---|
ENABLE_TELEGRAM | Yes | Set to true to activate the Telegram channel |
TELEGRAM_BOT_TOKEN | Yes | The token provided by BotFather |
How It Works
Polling Mode
The Telegram adapter uses long-polling via the getUpdates API method. This means:
- The bot opens a persistent HTTP request to Telegram's servers
- Telegram holds the connection open until new messages arrive (or timeout)
- When a message comes in, it's immediately processed and a new poll starts
- No public URL, webhook, or SSL certificate is needed
This is ideal for development and private deployments. For high-traffic production environments, webhook mode can be enabled as an alternative.
Message Processing Flow
Mentions in Groups
In Telegram group chats, the bot only responds when explicitly mentioned using its @username:
The adapter detects the bot's username in the message text, strips it, and passes the remaining text as the command.
Bot Commands in Groups
Telegram also supports the /command@botname syntax:
Both formats work. The adapter normalizes them to the same command: deploy staging.
Direct Messages
In 1:1 chats with the bot, no mention or /command prefix is needed. Every message is treated as a command:
Reply Threading
Telegram supports reply threading. CodeSpar responds as a reply to the original message, creating a visual thread in the chat:
Group Privacy Mode
By default, Telegram bots in Privacy Mode only receive:
- Messages that @mention the bot
- Messages starting with
/command - Replies to the bot's own messages
This is the recommended setting — the bot ignores general conversation and only processes explicit commands. If you need the bot to see all messages (not recommended), disable Privacy Mode via BotFather:
Capabilities
| Feature | Support |
|---|---|
| Reply threads | Yes — replies to the original message |
| Reactions | Yes — emoji reactions (Telegram API v7.0+) |
| Rich formatting | Yes — Markdown and HTML formatting |
| File uploads | Yes — documents, images, code files |
| Message editing | Yes — can edit its own messages |
| Inline keyboards | Yes — interactive buttons for approvals |
Example: Deploy Approval via Inline Keyboard
Telegram's inline keyboards allow interactive approve/reject buttons:
Troubleshooting
Bot doesn't respond in groups
- Verify
ENABLE_TELEGRAM=trueis set - Ensure users are @mentioning the bot's exact username
- Check if Privacy Mode is enabled (it should be — but then only @mentions work)
- Verify the bot was added to the group as a member
"Unauthorized" errors
The bot token is invalid. Get a new one from BotFather:
Polling conflicts
If you see 409 Conflict errors, another instance is polling with the same bot token. Only one process can poll per bot token. Stop the other instance or use a different bot.
Slow responses
Long-polling adds minimal latency (typically under 1 second). If responses are slow, the bottleneck is likely in agent processing, not the Telegram connection. Check agent logs for task execution times.