Commands
| Command | Description |
|---|---|
/start | Welcome message with command reference |
/health | Check API server connectivity |
/trade <prompt> | Execute a spot swap via natural language |
/perp <prompt> | Open or close a perpetual position |
/portfolio | View positions across all protocols |
/timeboost <prompt> | Query express lane status or trigger a bid |
POST /agent/prompt on the API
server. The bot polls the job status endpoint until the result is ready or a
timeout is reached.
Free-Form Messages
Any text message that does not start with/ is forwarded directly to the
agent as a natural language prompt. This allows users to type requests like
“swap 100 USDC for ETH” without using a specific command.
Inline Confirmation Buttons
When the agent response contains action-oriented language (e.g. “confirm” or “execute”), the bot presents inline keyboard buttons:Confirm
Triggers execution of the proposed action. Each confirmation token is a
UUID bound to the user ID with a 5-minute TTL.
Cancel
Cancels the proposed action and updates the message text.
Architecture
Setup
1. Create a bot with BotFather
1. Create a bot with BotFather
Open @BotFather on Telegram and run
/newbot.
Copy the bot token.2. Set environment variables
2. Set environment variables
| Variable | Required | Description |
|---|---|---|
TELEGRAM_BOT_TOKEN | Yes | Bot token from BotFather |
API_URL | No | API server URL (default: http://localhost:3000) |
3. Start the bot
3. Start the bot
Deployment
Polling mode (default)
Polling mode (default)
Long polling is the simplest deployment — the bot opens a persistent
connection to Telegram’s servers and receives updates in real time. No
public URL or TLS certificate required.Suitable for development and single-instance production deployments.
Webhook mode
Webhook mode
For higher throughput or serverless deployments, configure grammY’s webhook
handler:
- Set a public HTTPS URL for the bot.
- Call
bot.api.setWebhook(url)on startup. - Route incoming POST requests to
bot.handleUpdate(update).
Response Formatting
Bot responses are sent as Telegram HTML. TheformatResponse helper:
- Escapes HTML entities in agent output to prevent injection.
- Wraps the response in a format suitable for Telegram’s HTML parse mode.
- Preserves code blocks and structured data from the agent.
The bot uses
parse_mode: "HTML" for all messages. Markdown is not used
because Telegram’s Markdown parser is more restrictive and error-prone with
special characters common in DeFi output (addresses, decimals, etc.).Error Handling
- API unreachable —
/healthreports the specific error message. - Job timeout — if the job does not complete within the polling window, the bot reports the current status and job ID so the user can check later.
- Agent failure — failed jobs surface the error message from the agent runner.