|
| 1 | +# Telegram Notifications for OpenCode |
| 2 | + |
| 3 | +This feature sends Telegram messages via the Bot API when OpenCode session events occur, such as when a session becomes idle or encounters an error. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +### 1. Create a Telegram Bot |
| 8 | + |
| 9 | +1. Open Telegram and search for [@BotFather](https://t.me/BotFather) |
| 10 | +2. Send `/newbot` and follow the prompts to create a bot |
| 11 | +3. BotFather will give you a **Bot Token** (e.g., `123456789:ABCdefGhIjKlMnOpQrStUvWxYz`) |
| 12 | + |
| 13 | +### 2. Get Your Chat ID |
| 14 | + |
| 15 | +1. Start a conversation with your new bot (search for it by username and press **Start**) |
| 16 | +2. Send any message to the bot |
| 17 | +3. Open this URL in your browser (replace `YOUR_BOT_TOKEN`): |
| 18 | + ``` |
| 19 | + https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates |
| 20 | + ``` |
| 21 | +4. Look for `"chat":{"id":123456789}` in the response — that number is your **Chat ID** |
| 22 | + |
| 23 | + |
| 24 | +> **Tip:** For group chats, add the bot to the group, send a message, and check `getUpdates`. Group chat IDs are typically negative numbers. |
| 25 | +
|
| 26 | +### 3. Configure Environment Variables |
| 27 | + |
| 28 | +Set the following environment variables: |
| 29 | + |
| 30 | +```bash |
| 31 | +# Required - Enable the feature |
| 32 | +export TELEGRAM_ENABLED=true |
| 33 | + |
| 34 | +# Required - Your bot token from @BotFather |
| 35 | +export TELEGRAM_BOT_TOKEN=123456789:ABCdefGhIjKlMnOpQrStUvWxYz |
| 36 | + |
| 37 | +# Required - The chat ID to send messages to |
| 38 | +export TELEGRAM_CHAT_ID=987654321 |
| 39 | +``` |
| 40 | + |
| 41 | +### 4. Run OpenCode Container |
| 42 | + |
| 43 | +#### Basic Usage |
| 44 | +```bash |
| 45 | +docker run -it --rm \ |
| 46 | + -v "$PWD":/code \ |
| 47 | + -v /etc/localtime:/etc/localtime:ro \ |
| 48 | + -p 8080:8080 \ |
| 49 | + -e TELEGRAM_ENABLED=true \ |
| 50 | + -e TELEGRAM_BOT_TOKEN=your_bot_token \ |
| 51 | + -e TELEGRAM_CHAT_ID=your_chat_id \ |
| 52 | + jdcode-python |
| 53 | +``` |
| 54 | + |
| 55 | +#### Using Environment File |
| 56 | +Create a `.env` file with your Telegram credentials: |
| 57 | + |
| 58 | +```bash |
| 59 | +# .env |
| 60 | +TELEGRAM_ENABLED=true |
| 61 | +TELEGRAM_BOT_TOKEN=your_bot_token_here |
| 62 | +TELEGRAM_CHAT_ID=your_chat_id_here |
| 63 | +``` |
| 64 | + |
| 65 | +Then run with: |
| 66 | +```bash |
| 67 | +docker run -it --rm \ |
| 68 | + -v "$PWD":/code \ |
| 69 | + -v /etc/localtime:/etc/localtime:ro \ |
| 70 | + -p 8080:8080 \ |
| 71 | + --env-file .env \ |
| 72 | + jdcode-python |
| 73 | +``` |
| 74 | + |
| 75 | +## Events That Trigger Notifications |
| 76 | + |
| 77 | +The plugin sends Telegram messages for these OpenCode session events: |
| 78 | + |
| 79 | +| Event | Message | When It Occurs | |
| 80 | +|-------|---------|---------------| |
| 81 | +| `session.created` | 🚀 ProjectName session started at [time] | When a new session is created | |
| 82 | +| `session.idle` | 🤖 ProjectName session has been idle since [time] | When OpenCode session becomes inactive | |
| 83 | +| `session.error` | ❌ ProjectName session encountered an error at [time] | When an error occurs in the session | |
| 84 | +| `session.deleted` | 🗑️ ProjectName session ended at [time] | When the session is terminated | |
| 85 | + |
| 86 | +## Validation and Feedback |
| 87 | + |
| 88 | +When the container starts, you'll see status messages: |
| 89 | + |
| 90 | +### Properly Configured |
| 91 | +``` |
| 92 | +🔔 Telegram notifications: ENABLED |
| 93 | + 🤖 Bot token: 123456***xYz |
| 94 | + 💬 Chat ID: 987654321 |
| 95 | + ✓ All required variables configured |
| 96 | +``` |
| 97 | + |
| 98 | +### Missing Variables |
| 99 | +``` |
| 100 | +🔔 Telegram notifications: ENABLED |
| 101 | +⚠️ WARNING: Telegram enabled but missing required environment variables: |
| 102 | + - TELEGRAM_BOT_TOKEN |
| 103 | + Telegram notifications will not work until all variables are set. |
| 104 | +``` |
| 105 | + |
| 106 | +### Disabled |
| 107 | +``` |
| 108 | +📵 Telegram notifications: DISABLED (set TELEGRAM_ENABLED=true to enable) |
| 109 | +``` |
| 110 | + |
| 111 | +## Security Notes |
| 112 | + |
| 113 | +- **Bot tokens are masked** in logs and startup messages |
| 114 | +- **Credentials are only loaded from environment variables** (not stored in files) |
| 115 | +- The plugin only activates when `TELEGRAM_ENABLED=true` is explicitly set |
| 116 | + |
| 117 | +## Troubleshooting |
| 118 | + |
| 119 | +### No Messages Received |
| 120 | +1. Check that all environment variables are set correctly |
| 121 | +2. Verify you started a conversation with the bot (press Start in Telegram) |
| 122 | +3. Verify your chat ID is correct by checking `getUpdates` |
| 123 | +4. Check OpenCode logs for error messages: |
| 124 | + ```bash |
| 125 | + docker logs <container-id> |
| 126 | + ``` |
| 127 | + |
| 128 | +### Plugin Not Loading |
| 129 | +- The plugin is automatically loaded from `docker/base/.opencode/plugins/` |
| 130 | +- No npm dependencies are required (uses built-in `fetch()`) |
| 131 | +- Check OpenCode startup logs for plugin initialization messages |
| 132 | + |
| 133 | +### Testing |
| 134 | +To test the notification functionality, you can trigger a session idle event by leaving OpenCode inactive for a period of time. |
| 135 | + |
| 136 | +## Example Build and Run Script |
| 137 | + |
| 138 | +Create a `run-with-telegram.sh` script: |
| 139 | + |
| 140 | +```bash |
| 141 | +#!/bin/bash |
| 142 | +set -e |
| 143 | + |
| 144 | +# Build the OpenCode image |
| 145 | +./build.sh |
| 146 | + |
| 147 | +# Run with Telegram notifications |
| 148 | +docker run -it --rm \ |
| 149 | + -v "$PWD":/code \ |
| 150 | + -v /etc/localtime:/etc/localtime:ro \ |
| 151 | + -p 8080:8080 \ |
| 152 | + -e TELEGRAM_ENABLED=true \ |
| 153 | + -e TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" \ |
| 154 | + -e TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" \ |
| 155 | + jdcode-python |
| 156 | +``` |
| 157 | + |
| 158 | +Make it executable and run: |
| 159 | +```bash |
| 160 | +chmod +x run-with-telegram.sh |
| 161 | +./run-with-telegram.sh |
| 162 | +``` |
0 commit comments