opencode-telegram-bot 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +184 -0
- package/dist/app.d.ts +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +19189 -0
- package/dist/package.json +3 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# OpenCode Telegram Bot
|
|
2
|
+
|
|
3
|
+
A Telegram bot that forwards messages to an [OpenCode](https://opencode.ai) agent and returns the responses. Each chat gets a persistent session, so the agent remembers conversation context across messages.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- [Node.js](https://nodejs.org/) 18+
|
|
8
|
+
- [OpenCode](https://opencode.ai/docs/) installed and configured with at least one provider
|
|
9
|
+
- A Telegram bot token (see below)
|
|
10
|
+
|
|
11
|
+
## Creating a Telegram Bot
|
|
12
|
+
|
|
13
|
+
1. Open Telegram and search for [@BotFather](https://t.me/BotFather).
|
|
14
|
+
2. Send `/newbot` and follow the prompts:
|
|
15
|
+
- Choose a display name for your bot (e.g. "My OpenCode Bot").
|
|
16
|
+
- Choose a username. It must end in `bot` (e.g. `my_opencode_bot`).
|
|
17
|
+
3. BotFather will reply with your **bot token**. It looks like `123456789:ABCdefGHIjklMNOpqrsTUVwxyz`. Copy it.
|
|
18
|
+
4. Optionally, send `/setdescription` to BotFather to set a description for your bot.
|
|
19
|
+
|
|
20
|
+
To find your **Telegram user ID** (for restricting access):
|
|
21
|
+
|
|
22
|
+
1. Search for [@userinfobot](https://t.me/userinfobot) on Telegram.
|
|
23
|
+
2. Send it any message. It will reply with your user ID.
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
1. Install dependencies:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
2. Create a `.env` file in the project root:
|
|
34
|
+
|
|
35
|
+
```env
|
|
36
|
+
TELEGRAM_BOT_TOKEN=your-bot-token-from-botfather
|
|
37
|
+
AUTHORIZED_TELEGRAM_USER_ID=your-telegram-user-id
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`AUTHORIZED_TELEGRAM_USER_ID` is optional. If set, only that user can interact with the bot. If omitted, the bot is open to everyone.
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
You need two processes running: the OpenCode server and the Telegram bot.
|
|
45
|
+
|
|
46
|
+
**Terminal 1** -- Start the OpenCode server:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
opencode serve
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This runs `opencode serve` on port 4096. You can pass additional flags:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
opencode serve --port 8080
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Terminal 2** -- Start the Telegram bot:
|
|
59
|
+
|
|
60
|
+
The quickest way is with `npx` (no install required):
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx opencode-telegram-bot
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or install it globally:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npm install -g opencode-telegram-bot
|
|
70
|
+
opencode-telegram-bot
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The bot connects to `http://localhost:4096` by default.
|
|
74
|
+
|
|
75
|
+
### Options
|
|
76
|
+
|
|
77
|
+
| Flag | Description | Default |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `--url <url>` | OpenCode server URL | `http://localhost:4096` |
|
|
80
|
+
| `--model <model>` | Model to use (provider/model format) | Server default |
|
|
81
|
+
|
|
82
|
+
Examples:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Connect to a custom server URL
|
|
86
|
+
npx opencode-telegram-bot --url http://192.168.1.100:4096
|
|
87
|
+
|
|
88
|
+
# Use a specific model
|
|
89
|
+
npx opencode-telegram-bot --model anthropic/claude-sonnet-4-20250514
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Commands
|
|
93
|
+
|
|
94
|
+
### Bot Commands
|
|
95
|
+
|
|
96
|
+
These are handled directly by the Telegram bot:
|
|
97
|
+
|
|
98
|
+
| Command | Description |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `/start` | Welcome message |
|
|
101
|
+
| `/new` | Start a new conversation |
|
|
102
|
+
| `/sessions` | List all sessions with their IDs |
|
|
103
|
+
| `/switch <id>` | Switch to a different session (prefix match supported) |
|
|
104
|
+
| `/title <text>` | Rename the current session |
|
|
105
|
+
| `/delete <id>` | Delete a session from the server |
|
|
106
|
+
| `/help` | Show available commands |
|
|
107
|
+
|
|
108
|
+
### OpenCode Commands
|
|
109
|
+
|
|
110
|
+
Any `/` command that isn't a bot command is automatically forwarded to the OpenCode server via its command system. The bot fetches the list of available commands on startup and validates them.
|
|
111
|
+
|
|
112
|
+
The exact list depends on your OpenCode configuration. Common commands include `/init` and `/review`. Send `/help` to the bot to see the full list.
|
|
113
|
+
|
|
114
|
+
If you type an unknown command, the bot will reply with the list of available commands.
|
|
115
|
+
|
|
116
|
+
### Regular Messages
|
|
117
|
+
|
|
118
|
+
Any other text message is forwarded to the OpenCode agent as a prompt. Follow-up messages go into the same session, so the agent has full conversation context. Use `/new` when you want a fresh conversation.
|
|
119
|
+
|
|
120
|
+
## Session Management
|
|
121
|
+
|
|
122
|
+
Sessions are never deleted automatically. You can have multiple sessions and switch between them.
|
|
123
|
+
|
|
124
|
+
When you send your first message, a new session is created and automatically titled with that message text. You can rename it anytime with `/title`.
|
|
125
|
+
|
|
126
|
+
Example workflow:
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
You: Help me refactor the auth module
|
|
130
|
+
Bot: [agent response]
|
|
131
|
+
You: /title Auth refactoring
|
|
132
|
+
Bot: Session renamed to: Auth refactoring
|
|
133
|
+
|
|
134
|
+
You: /new
|
|
135
|
+
Bot: Conversation reset.
|
|
136
|
+
|
|
137
|
+
You: Fix the broken tests
|
|
138
|
+
Bot: [agent response about tests - new session auto-titled "Fix the broken tests"]
|
|
139
|
+
|
|
140
|
+
You: /sessions
|
|
141
|
+
Bot: Your sessions:
|
|
142
|
+
[active] Fix the broken tests - 2 min ago (abc12345)
|
|
143
|
+
Auth refactoring - 1 hour ago (def67890)
|
|
144
|
+
Use /switch <id> to switch sessions.
|
|
145
|
+
|
|
146
|
+
You: /switch def6
|
|
147
|
+
Bot: Switched to session: Auth refactoring (def67890)
|
|
148
|
+
|
|
149
|
+
You: What were we working on?
|
|
150
|
+
Bot: [agent responds with context from the auth refactoring session]
|
|
151
|
+
|
|
152
|
+
You: /delete abc1
|
|
153
|
+
Bot: Deleted session: Fix the broken tests (abc12345)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The `/sessions` list only shows sessions created by the Telegram bot, not sessions from other OpenCode clients (like the TUI). You cannot delete the currently active session -- use `/new` or `/switch` first.
|
|
157
|
+
|
|
158
|
+
## Session Persistence
|
|
159
|
+
|
|
160
|
+
Sessions survive bot restarts. The bot saves the chat-to-session mapping to `sessions.json` in the project root. On startup, it:
|
|
161
|
+
|
|
162
|
+
1. Loads the mapping from `sessions.json`
|
|
163
|
+
2. Validates each session still exists on the OpenCode server
|
|
164
|
+
3. If a stored session is gone, falls back to finding a matching session by title (`Telegram chat <chatId>`)
|
|
165
|
+
4. If the file is missing entirely, scans all server sessions for ones matching the title convention
|
|
166
|
+
|
|
167
|
+
This means you can restart the bot (or even delete `sessions.json`) and it will reconnect to existing sessions automatically.
|
|
168
|
+
|
|
169
|
+
## Environment Variables
|
|
170
|
+
|
|
171
|
+
| Variable | Required | Description |
|
|
172
|
+
|---|---|---|
|
|
173
|
+
| `TELEGRAM_BOT_TOKEN` | Yes | Bot token from BotFather |
|
|
174
|
+
| `AUTHORIZED_TELEGRAM_USER_ID` | No | Restrict the bot to a single Telegram user |
|
|
175
|
+
|
|
176
|
+
## Building
|
|
177
|
+
|
|
178
|
+
To bundle the bot into a single file for deployment:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
npm run build
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
This outputs to `dist/index.js` using [@vercel/ncc](https://github.com/vercel/ncc).
|
package/dist/app.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|