A Telegram bot that bridges messages directly to OpenCode โ an AI coding agent running on your machine
Project description
๐ค Telegram โ OpenCode Bridge Bot
A lightweight Python bot that bridges your Telegram messages directly to OpenCode โ an AI coding agent running on your machine. Think of it as having Claude/GPT-powered coding assistance right in your pocket via Telegram.
โจ Features
- Direct OpenCode integration โ routes your messages to OpenCode's HTTP API
- Persistent sessions โ conversations maintain context across messages
- Session management โ create, switch, list, and share sessions
- Model switching โ change AI models on the fly (
/model) - Plan/Build modes โ toggle between read-only analysis and full execution
- Smart formatting โ code blocks with syntax highlighting in Telegram
- Auto message splitting โ handles responses longer than Telegram's 4096 char limit
- Security โ whitelist-based access control + rate limiting
- Workspace Switching โ Switching from one workspace to another
๐ Prerequisites
- Python 3.10+
- OpenCode CLI โ install via:
npm install -g opencode-ai # or curl -fsSL https://opencode.ai/install | bash
- Telegram Bot Token โ get one from @BotFather
- Your Telegram User ID โ send
/idto the bot after setup, or use @userinfobot
๐ Quick Start
1. Clone & Install
cd telegram-opencode-bot
pip install -r requirements.txt
2. Run the Bot
python bot.py
python bot.py --env (use --env flag if u want to re-configure later anytime)
๐ก Tip: Don't know your Telegram user ID? Start the bot with
AUTHORIZED_USERS=0temporarily, send/idto the bot, then update the.envwith your real ID.
3. Chat!
Open Telegram, find your bot, and start asking! ๐
๐ฑ Commands
| Command | Description |
|---|---|
/start |
Welcome message & connection check |
/help |
Show all commands |
/new |
Start a fresh conversation |
/sessions |
List your recent sessions |
/switch <id> |
Switch to a different session |
/model <name> |
Change AI model(ex: /model opencode/deepseek-v4-flash-free) |
/mode <plan|build> |
Toggle plan/build mode |
/share |
Share current session (public URL) |
/status |
Check connection & session details |
/id |
Show your Telegram user ID |
/stop |
Abort active model processing |
/models |
List all available models |
/project |
To view the current workspace, sub folder workspaces and to change the workspace (ex: /project 2) |
/project depth <1to5> |
Depth level recursive check for subfolders (ex: if u give depth 2, only 2 sub folders it will show from root) |
/enable |
To enable the streaming |
/disable |
To disable the streaming |
๐๏ธ Architecture
Telegram User
โ
โผ
Telegram Bot (Python)
โ
โโโโบ OpenCode HTTP API (localhost:4096) โ primary
โ
โโโโบ LLM Provider (Claude/GPT/Gemini)
โโโโบ Local Filesystem & Shell
โ๏ธ Configuration
| Variable | Description | Default |
|---|---|---|
TELEGRAM_BOT_TOKEN |
Bot token from @BotFather | Required |
AUTHORIZED_USERS |
Comma-separated Telegram user IDs | Required |
OPENCODE_SERVER_URL |
OpenCode HTTP API URL | http://localhost:4096 |
OPENCODE_SERVER_USERNAME |
OpenCode auth username | (empty) |
OPENCODE_SERVER_PASSWORD |
OpenCode auth password | (empty) |
OPENCODE_MODEL |
Default AI model | anthropic/claude-sonnet-4 |
OPENCODE_WORK_DIR |
Working directory for OpenCode | . |
MAX_MESSAGE_LENGTH |
Max Telegram message length | 4000 |
RESPONSE_TIMEOUT |
Max wait for response (seconds) | 0 |
DB_PATH |
SQLite database path | sessions.db |
๐ Security
- User whitelist โ only Telegram user IDs in
AUTHORIZED_USERScan use the bot - Rate limiting โ 20 requests per minute per user (configurable)
- No public exposure โ designed to run on your local machine
โ ๏ธ Warning: This bot executes AI-driven code on your machine. Only authorize trusted users.
๐ Project Structure
telegram-opencode-bot/
โโโ bot.py # Main entry point
โโโ config.py # Environment configuration
โโโ handlers/
โ โโโ commands.py # Slash command handlers
โ โโโ messages.py # Text message โ OpenCode bridge
โโโ opencode/
โ โโโ client.py # OpenCode HTTP API client
โ
โโโ sessions/
โ โโโ manager.py # Per-user session tracking (SQLite)
โโโ utils/
โ โโโ formatting.py # Telegram message formatting
โ โโโ security.py # Auth, rate limiting, sanitization
โโโ .env.example # Environment template
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_opencode_bridge_bot-0.1.2.tar.gz.
File metadata
- Download URL: telegram_opencode_bridge_bot-0.1.2.tar.gz
- Upload date:
- Size: 37.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
161a3dcafce74744141b25f41f494a9ac9483e3e7c1788c711acc60fceedb623
|
|
| MD5 |
9f3f432c534c3e4cf0bb13d0e29cc361
|
|
| BLAKE2b-256 |
e5686ec4cb12e3c6e1338051b911c97075a326d67685ad3c11c89abfac13e42b
|
File details
Details for the file telegram_opencode_bridge_bot-0.1.2-py3-none-any.whl.
File metadata
- Download URL: telegram_opencode_bridge_bot-0.1.2-py3-none-any.whl
- Upload date:
- Size: 39.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c98df355f13ec33c0c7c0f841882d4823ed938142402f494cbad039925727cac
|
|
| MD5 |
af4d1da5c9a2c8d8ea508df166a3ca99
|
|
| BLAKE2b-256 |
cda78cfa99a79b52a44976443dcbdb67f98c5cb8fad320bd48a1b4da742396e8
|