Skip to main content

A Telegram bot that bridges messages directly to OpenCode โ€” an AI coding agent running on your machine

Project description

๐Ÿค– Telegram โž” OpenCode Bridge Bot

License: MIT Python: 3.10+ OpenCode: v0.1+

A premium, feature-rich Python bot that bridges your Telegram client directly to OpenCode โ€” a powerful AI coding agent running on your local machine. Authorize your personal user account and get professional, agentic software engineering assistance right in your pocket.


โœจ Features

  • Direct OpenCode Integration โ€” Routes your chat prompts directly to the OpenCode HTTP API or local server processes.
  • Dynamic Project Isolation โ€” Effortlessly view, create, delete, and switch workspace folders on your local machine using an interactive Telegram file explorer interface.
  • Mobile File-Upload Uploads โ€” Send files, code logs, or images directly from Telegram to upload them into your active workspace folder.
  • Robust Session Registry โ€” Seamlessly list, switch, delete, and share conversations, automatically synced with the backend SQLite database.
  • Conversational History View (/history) โ€” Chronologically fetches the active conversation, filtered of long execution logs, reasoning traces, or step metadata. Messages are sent sequentially with rate-limit respect and support pagination controls.
  • Live Tool Execution Streaming โ€” Watch the agent interact with your filesystem, write code, run bash commands, and search the web in real-time (/enable / /disable).
  • Flexible Model & Agent Mode Selectors โ€” Swap underlying LLM models on the fly (/models) and switch agent personas (e.g. Build, Plan, or Webtester) using clean interactive menus (/mode).
  • Secure by Default โ€” Enforces a strict user whitelist check on every message and callback action to prevent unauthorized access.
  • Robustness & Windows Stability โ€” Configured with automated API retries, suppressed verbose network noise, and Selector Event Loops for clean subprocess termination on Windows systems.

๐Ÿ“‹ Prerequisites

  1. Python 3.10+ installed on your system.
  2. OpenCode CLI installed and initialized:
    npm install -g opencode-ai
    # or
    curl -fsSL https://opencode.ai/install | bash
    
  3. Telegram Bot Token โ€” Obtain one instantly via @BotFather.
  4. Your Telegram User ID โ€” Retrieve it using @userinfobot, or start the bot and send /id.

๐Ÿš€ Installation & Setup

Option A: Standard PyPI Installation

Install and run the bot wrapper directly from the terminal:

pip install telegram-opencode-bridge-bot==0.1.12
telegram-opencode-bot

๐Ÿ’ก Tip: Use the --env flag anytime to reconfigure your variables: telegram-opencode-bot --env


Option B: Manual Git Installation

  1. Clone the repository and install dependencies:

    git clone https://github.com/MaheshNagabhairava/telegram-opencode-bridge-bot.git
    cd telegram-opencode-bot
    pip install -r requirements.txt
    
  2. Start the bot:

    python bot.py
    

๐Ÿ’ก Tip: Use the --env flag anytime to reconfigure your variables: python bot.py --env


๐Ÿ“ฑ Command Reference

Command Parameter Description
/start โ€” Prints the welcome card and verifies connectivity to the OpenCode server.
/help โ€” Displays a complete guide of all available commands and tips.
/new โ€” Clears the current active session cache to start a fresh topic.
/stop โ€” Aborts any active running task or model generation process.
/status โ€” Prints detailed statistics about the server connection and active session settings.
/id โ€” Displays your Telegram User ID (useful for whitelisting).
/project [name] Opens the directory explorer. Optionally switches directly to the specified folder.
/create_project <name> Creates a new isolated folder workspace inside your primary workspace path.
/delete_project [name] Erases a workspace directory from disk (requires inline button verification).
/sessions โ€” Lists your recent conversations in the current workspace to allow switching.
/history [count] Displays the chronological history of the active session, split into text batches.
/delete โ€” Prompts an inline keyboard to permanently delete a session.
/models โ€” Lists all models available on the OpenCode server to easily select a default.
/mode โ€” Opens a menu to select agent personas (e.g. Build, Plan, custom testers).
/plan โ€” Quick switch shortcut to Plan Mode (read-only analysis).
/build โ€” Quick switch shortcut to Build Mode (read, write, execute permissions).
/enable โ€” Enables real-time streaming of tool calls, shell executions, and file edits.
/disable โ€” Disables streaming; the bot will only report the final LLM response.
/share โ€” Fetches a public, shareable web preview URL of the active conversation.
/update โ€” Update to a new available version.

โš™๏ธ Configuration Variables

The following parameters can be defined in your .env configuration file:

Variable Type Description Default
TELEGRAM_BOT_TOKEN String Secret token issued by @BotFather. Required
AUTHORIZED_USERS List Comma-separated list of whitelisted Telegram User IDs. Required
OPENCODE_SERVER_URL String The target URL hosting the OpenCode endpoint. http://localhost:4096
OPENCODE_SERVER_USERNAME String Optional Basic Auth username for server login. (empty)
OPENCODE_SERVER_PASSWORD String Optional Basic Auth password for server login. (empty)
OPENCODE_MODEL String The default LLM provider/model key configured. opencode/deepseek-v4-flash-free
OPENCODE_WORK_DIR String Local parent path containing your projects. .
PROJECT_SCAN_DEPTH Integer Max recursion depth used to search project directories. 2
MAX_MESSAGE_LENGTH Integer Maximum character limit before chunking Telegram texts. 4000
RESPONSE_TIMEOUT Integer Request timeout in seconds (set 0 to disable timeouts). 300
DB_PATH String Storage path for the SQLite session manager. sessions.db

๐Ÿ—๏ธ Architecture Layout

    ๐Ÿ‘ค Telegram Client (Mobile / Desktop)
                   โ”‚
                   โ–ผ (HTTPS Long Polling / Webhook)
        ๐Ÿค– Telegram Bridge Bot (Python)
                   โ”‚
                   โ–ผ (HTTP Local API / SSE Events)
       ๐Ÿ’ป OpenCode Server (localhost:4096)
                   โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                 โ–ผ
   ๐Ÿค– LLM Provider    ๐Ÿ“ Local Workspace
(Claude/DeepSeek/Codex/etc.)  (Shell / Filesystem)

๐Ÿ”’ Security Policy

[!WARNING] This bot allows remote execution of shell commands, filesystem operations, and internet fetching on the host machine.

  1. Keep AUTHORIZED_USERS configured strictly with your own Telegram User ID.
  2. Avoid exposing your Telegram Bot Token or sharing public URLs if your host machine handles sensitive information.
  3. Verify tool permissions in the console log if executing arbitrary files.

๐Ÿ“ Project Structure

telegram-opencode-bot/
โ”œโ”€โ”€ bot.py                  # Main entry point, loops, and setup
โ”œโ”€โ”€ config.py               # validated configuration loader
โ”œโ”€โ”€ sessions.db             # Persistent SQL database (generated)
โ”œโ”€โ”€ requirements.txt        # python dependencies
โ”œโ”€โ”€ handlers/
โ”‚   โ”œโ”€โ”€ commands.py         # Slash command handlers (/start, /history, etc.)
โ”‚   โ””โ”€โ”€ messages.py         # Text & file message routing logic
โ”œโ”€โ”€ opencode/
โ”‚   โ”œโ”€โ”€ client.py           # Async HTTP client wrapper for OpenCode API
โ”‚   โ””โ”€โ”€ server.py           # Background subprocess lifecycle management
โ”œโ”€โ”€ sessions/
โ”‚   โ””โ”€โ”€ manager.py          # SQL persistence and state registry
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ formatting.py       # Conversions, splits, and custom table layouts
    โ””โ”€โ”€ security.py         # Rate-limiting, whitelist guards, and checks

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_opencode_bridge_bot-0.1.12.tar.gz (56.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_opencode_bridge_bot-0.1.12-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

File details

Details for the file telegram_opencode_bridge_bot-0.1.12.tar.gz.

File metadata

File hashes

Hashes for telegram_opencode_bridge_bot-0.1.12.tar.gz
Algorithm Hash digest
SHA256 269f832efb9a6467d290d2ced818fcd605b949861527416b5e176c306c5846d0
MD5 3fbd031ac58c3a52b8dfbf09d92ac93f
BLAKE2b-256 d2c4f5f619cc0864b95d55b4d7415cadc3cdb015bf299fc7b99be1d5e56f7227

See more details on using hashes here.

File details

Details for the file telegram_opencode_bridge_bot-0.1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_opencode_bridge_bot-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 47ff74b75647bcc71d239accbf2d59a151b29d456bce9fc429906fec9804a53a
MD5 a96a706d8531c21a118c8d9efb6af730
BLAKE2b-256 692adf3fd6f4bfc157dff5c9266064fb803c17660e8e5c199e6785aacfc45433

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page