@grinev/opencode-telegram-bot 0.20.2 → 0.20.3
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/.env.example +15 -0
- package/README.md +51 -0
- package/dist/bot/handlers/voice.js +7 -2
- package/dist/bot/index.js +2 -21
- package/dist/bot/telegram-client-options.js +63 -0
- package/dist/bot/utils/file-download.js +21 -3
- package/dist/config.js +25 -4
- package/package.json +2 -1
package/.env.example
CHANGED
|
@@ -12,6 +12,21 @@ TELEGRAM_ALLOWED_USER_ID=
|
|
|
12
12
|
# TELEGRAM_PROXY_URL=http://proxy.example.com:8080
|
|
13
13
|
# TELEGRAM_PROXY_URL=
|
|
14
14
|
|
|
15
|
+
# Telegram Reverse-Proxy mode (optional, alternative to TELEGRAM_PROXY_URL)
|
|
16
|
+
# For corporate networks that block api.telegram.org but allow your own server.
|
|
17
|
+
# Point TELEGRAM_API_ROOT at an HTTPS endpoint that reverse-proxies to
|
|
18
|
+
# https://api.telegram.org (e.g. nginx). Applied to Bot API calls AND file
|
|
19
|
+
# downloads. TELEGRAM_PROXY_SECRET (optional) is sent as the X-Proxy-Secret
|
|
20
|
+
# header so the reverse proxy can authorize callers. See README for an example
|
|
21
|
+
# nginx config.
|
|
22
|
+
# TELEGRAM_API_ROOT=https://tg-proxy.yourdomain.com
|
|
23
|
+
# TELEGRAM_PROXY_SECRET=some-long-random-string
|
|
24
|
+
|
|
25
|
+
# Force IPv4 for direct Telegram API/file requests (optional, default: false)
|
|
26
|
+
# Enable this if startup fails with "Network request ... failed" in an
|
|
27
|
+
# environment where IPv6 DNS exists but outbound IPv6 connectivity is broken.
|
|
28
|
+
# TELEGRAM_FORCE_IPV4=false
|
|
29
|
+
|
|
15
30
|
# OpenCode API URL (optional, default: http://localhost:4096)
|
|
16
31
|
# OPENCODE_API_URL=http://localhost:4096
|
|
17
32
|
|
package/README.md
CHANGED
|
@@ -198,6 +198,9 @@ When installed via npm, the configuration wizard handles the initial setup. The
|
|
|
198
198
|
| `TELEGRAM_BOT_TOKEN` | Bot token from @BotFather | Yes | — |
|
|
199
199
|
| `TELEGRAM_ALLOWED_USER_ID` | Your numeric Telegram user ID | Yes | — |
|
|
200
200
|
| `TELEGRAM_PROXY_URL` | Proxy URL for Telegram API (SOCKS5/HTTP) | No | — |
|
|
201
|
+
| `TELEGRAM_API_ROOT` | Custom Telegram Bot API root URL (e.g. nginx reverse-proxying `api.telegram.org`); applied to API calls and file downloads | No | `https://api.telegram.org` |
|
|
202
|
+
| `TELEGRAM_PROXY_SECRET` | Shared secret sent as `X-Proxy-Secret` header on every Bot API request and file download (used with `TELEGRAM_API_ROOT`) | No | — |
|
|
203
|
+
| `TELEGRAM_FORCE_IPV4` | Force IPv4 for direct Telegram API and file requests; useful when IPv6 DNS works but outbound IPv6 is broken | No | `false` |
|
|
201
204
|
| `OPENCODE_API_URL` | OpenCode server URL | No | `http://localhost:4096` |
|
|
202
205
|
| `OPENCODE_AUTO_RESTART_ENABLED` | Automatically restart a local OpenCode server when health-checks fail | No | `false` |
|
|
203
206
|
| `OPENCODE_MONITOR_INTERVAL_SEC` | Health monitor interval in seconds when OpenCode auto-restart is enabled | No | `300` |
|
|
@@ -239,6 +242,54 @@ When installed via npm, the configuration wizard handles the initial setup. The
|
|
|
239
242
|
|
|
240
243
|
Logs are written to `./logs` when running from sources and to the runtime config directory `logs/` folder in `installed` mode. Log rotation depends on runtime mode: `sources` creates one file per bot launch, while `installed` appends to one file per day. Old log files are removed according to `LOG_RETENTION`.
|
|
241
244
|
|
|
245
|
+
### Reverse Proxy (Optional)
|
|
246
|
+
|
|
247
|
+
For environments that block `api.telegram.org` but allow your own HTTPS endpoint (corporate networks, restricted regions), you can route Bot API traffic through a reverse proxy you control. This is an alternative to the SOCKS/HTTP forward proxy configured with `TELEGRAM_PROXY_URL`.
|
|
248
|
+
|
|
249
|
+
Set `TELEGRAM_API_ROOT` to your reverse-proxy URL — both Bot API calls and file downloads (including voice/audio files) will use it. Optionally set `TELEGRAM_PROXY_SECRET` so the bot sends an `X-Proxy-Secret` header your proxy can use to authorize callers.
|
|
250
|
+
|
|
251
|
+
`.env`:
|
|
252
|
+
|
|
253
|
+
```env
|
|
254
|
+
TELEGRAM_API_ROOT=https://tg-proxy.yourdomain.com
|
|
255
|
+
TELEGRAM_PROXY_SECRET=some-long-random-string
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Example nginx config:
|
|
259
|
+
|
|
260
|
+
```nginx
|
|
261
|
+
server {
|
|
262
|
+
listen 443 ssl http2;
|
|
263
|
+
server_name tg-proxy.yourdomain.com;
|
|
264
|
+
|
|
265
|
+
ssl_certificate /etc/letsencrypt/live/tg-proxy.yourdomain.com/fullchain.pem;
|
|
266
|
+
ssl_certificate_key /etc/letsencrypt/live/tg-proxy.yourdomain.com/privkey.pem;
|
|
267
|
+
|
|
268
|
+
access_log off; # the bot token appears in URL paths
|
|
269
|
+
client_max_body_size 50m;
|
|
270
|
+
|
|
271
|
+
if ($http_x_proxy_secret != "some-long-random-string") { return 403; }
|
|
272
|
+
|
|
273
|
+
location / {
|
|
274
|
+
proxy_pass https://api.telegram.org;
|
|
275
|
+
proxy_ssl_server_name on;
|
|
276
|
+
proxy_set_header Host api.telegram.org;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
`TELEGRAM_API_ROOT` and `TELEGRAM_PROXY_URL` are alternative connectivity modes — the former picks the URL the bot connects to (a reverse proxy on your side), while the latter tunnels TCP through a forward proxy. Configure only one of them; the bot rejects using both at startup.
|
|
282
|
+
|
|
283
|
+
### Force IPv4 for Telegram (Optional)
|
|
284
|
+
|
|
285
|
+
If the bot fails during startup with errors such as `Network request for 'setMyCommands' failed` or `Network request for 'getWebhookInfo' failed`, and the same machine has broken outbound IPv6 connectivity, force direct Telegram requests to use IPv4:
|
|
286
|
+
|
|
287
|
+
```env
|
|
288
|
+
TELEGRAM_FORCE_IPV4=true
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
This affects direct Bot API calls and Telegram file downloads. It is not a replacement for `TELEGRAM_PROXY_URL` or `TELEGRAM_API_ROOT` when Telegram is blocked by the network.
|
|
292
|
+
|
|
242
293
|
### Voice and Audio Transcription (Optional)
|
|
243
294
|
|
|
244
295
|
If `STT_API_URL` and `STT_API_KEY` are set, the bot will:
|
|
@@ -30,7 +30,11 @@ async function downloadTelegramFileByUrl(url, redirectDepth = 0) {
|
|
|
30
30
|
return new Promise((resolve, reject) => {
|
|
31
31
|
const targetUrl = new URL(url);
|
|
32
32
|
const requestModule = targetUrl.protocol === "http:" ? http : https;
|
|
33
|
-
const
|
|
33
|
+
const proxySecret = config.telegram.proxySecret;
|
|
34
|
+
const request = requestModule.get(targetUrl, {
|
|
35
|
+
agent: getTelegramDownloadAgent(),
|
|
36
|
+
...(proxySecret ? { headers: { "X-Proxy-Secret": proxySecret } } : {}),
|
|
37
|
+
}, (response) => {
|
|
34
38
|
const statusCode = response.statusCode ?? 0;
|
|
35
39
|
if (statusCode >= 300 && statusCode < 400 && response.headers.location) {
|
|
36
40
|
response.resume();
|
|
@@ -76,7 +80,8 @@ async function downloadTelegramFile(ctx, fileId) {
|
|
|
76
80
|
logger.error("[Voice] Telegram getFile returned no file_path");
|
|
77
81
|
return null;
|
|
78
82
|
}
|
|
79
|
-
const
|
|
83
|
+
const apiRoot = (config.telegram.apiRoot ?? "https://api.telegram.org").replace(/\/$/, "");
|
|
84
|
+
const fileUrl = `${apiRoot}/file/bot${ctx.api.token}/${file.file_path}`;
|
|
80
85
|
logger.debug(`[Voice] Downloading file: ${file.file_path} (${file.file_size ?? "?"} bytes)`);
|
|
81
86
|
const buffer = await downloadTelegramFileByUrl(fileUrl);
|
|
82
87
|
// Extract filename from file_path (e.g., "voice/file_123.oga" -> "file_123.oga")
|
package/dist/bot/index.js
CHANGED
|
@@ -2,8 +2,6 @@ import { Bot, InputFile } from "grammy";
|
|
|
2
2
|
import { promises as fs } from "fs";
|
|
3
3
|
import * as path from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
|
-
import { SocksProxyAgent } from "socks-proxy-agent";
|
|
6
|
-
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
7
5
|
import { config } from "../config.js";
|
|
8
6
|
import { authMiddleware } from "./middleware/auth.js";
|
|
9
7
|
import { interactionGuardMiddleware } from "./middleware/interaction-guard.js";
|
|
@@ -55,6 +53,7 @@ import { withTelegramRateLimitRetry } from "../utils/telegram-rate-limit-retry.j
|
|
|
55
53
|
import { pinnedMessageManager } from "../pinned/manager.js";
|
|
56
54
|
import { t } from "../i18n/index.js";
|
|
57
55
|
import { getCurrentProject } from "../settings/manager.js";
|
|
56
|
+
import { createTelegramBotOptions } from "./telegram-client-options.js";
|
|
58
57
|
import { clearPromptResponseMode, processUserPrompt } from "./handlers/prompt.js";
|
|
59
58
|
import { handleVoiceMessage } from "./handlers/voice.js";
|
|
60
59
|
import { handleDocumentMessage } from "./handlers/document.js";
|
|
@@ -838,25 +837,7 @@ export function createBot() {
|
|
|
838
837
|
clearInterval(heartbeatTimer);
|
|
839
838
|
heartbeatTimer = null;
|
|
840
839
|
}
|
|
841
|
-
const botOptions =
|
|
842
|
-
if (config.telegram.proxyUrl) {
|
|
843
|
-
const proxyUrl = config.telegram.proxyUrl;
|
|
844
|
-
let agent;
|
|
845
|
-
if (proxyUrl.startsWith("socks")) {
|
|
846
|
-
agent = new SocksProxyAgent(proxyUrl);
|
|
847
|
-
logger.info(`[Bot] Using SOCKS proxy: ${proxyUrl.replace(/\/\/.*@/, "//***@")}`);
|
|
848
|
-
}
|
|
849
|
-
else {
|
|
850
|
-
agent = new HttpsProxyAgent(proxyUrl);
|
|
851
|
-
logger.info(`[Bot] Using HTTP/HTTPS proxy: ${proxyUrl.replace(/\/\/.*@/, "//***@")}`);
|
|
852
|
-
}
|
|
853
|
-
botOptions.client = {
|
|
854
|
-
baseFetchConfig: {
|
|
855
|
-
agent,
|
|
856
|
-
compress: true,
|
|
857
|
-
},
|
|
858
|
-
};
|
|
859
|
-
}
|
|
840
|
+
const botOptions = createTelegramBotOptions(config.telegram);
|
|
860
841
|
const bot = new Bot(config.telegram.token, botOptions);
|
|
861
842
|
botInstance = bot;
|
|
862
843
|
chatIdInstance = config.telegram.allowedUserId;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// @ts-expect-error — node-fetch v2 ships no TS types and we avoid adding @types/node-fetch
|
|
2
|
+
import nodeFetch from "node-fetch";
|
|
3
|
+
import { Agent as HttpsAgent } from "https";
|
|
4
|
+
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
5
|
+
import { SocksProxyAgent } from "socks-proxy-agent";
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
export function createTelegramIpv4Agent() {
|
|
8
|
+
return new HttpsAgent({ family: 4, keepAlive: true });
|
|
9
|
+
}
|
|
10
|
+
export function createTelegramBotOptions(telegram) {
|
|
11
|
+
const botOptions = {};
|
|
12
|
+
if (telegram.apiRoot || telegram.proxySecret) {
|
|
13
|
+
botOptions.client = botOptions.client ?? {};
|
|
14
|
+
if (telegram.apiRoot) {
|
|
15
|
+
botOptions.client.apiRoot = telegram.apiRoot;
|
|
16
|
+
logger.info(`[Bot] Using custom Telegram API root: ${telegram.apiRoot}`);
|
|
17
|
+
}
|
|
18
|
+
if (telegram.proxySecret) {
|
|
19
|
+
// Inject the shared-secret header via a custom fetch wrapper instead of
|
|
20
|
+
// baseFetchConfig.headers, because grammY's client spreads
|
|
21
|
+
// `{...baseFetchConfig, ...config}` and the per-request config.headers
|
|
22
|
+
// (Content-Type/Length) wipes out anything we put on baseFetchConfig.
|
|
23
|
+
// Plain-object headers merge (not the Headers class) keeps this compatible
|
|
24
|
+
// with node-fetch v2's init shape and avoids the DOM lib HeadersInit type.
|
|
25
|
+
const proxySecret = telegram.proxySecret;
|
|
26
|
+
botOptions.client.fetch = ((url, init) => {
|
|
27
|
+
const existing = init?.headers ?? {};
|
|
28
|
+
const merged = { ...existing, "X-Proxy-Secret": proxySecret };
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
return nodeFetch(url, { ...(init ?? {}), headers: merged });
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
});
|
|
33
|
+
logger.info(`[Bot] Sending X-Proxy-Secret header to Telegram API root`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (telegram.proxyUrl) {
|
|
37
|
+
const proxyUrl = telegram.proxyUrl;
|
|
38
|
+
let agent;
|
|
39
|
+
if (proxyUrl.startsWith("socks")) {
|
|
40
|
+
agent = new SocksProxyAgent(proxyUrl);
|
|
41
|
+
logger.info(`[Bot] Using SOCKS proxy: ${proxyUrl.replace(/\/\/.*@/, "//***@")}`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
agent = new HttpsProxyAgent(proxyUrl);
|
|
45
|
+
logger.info(`[Bot] Using HTTP/HTTPS proxy: ${proxyUrl.replace(/\/\/.*@/, "//***@")}`);
|
|
46
|
+
}
|
|
47
|
+
botOptions.client = botOptions.client ?? {};
|
|
48
|
+
botOptions.client.baseFetchConfig = {
|
|
49
|
+
agent,
|
|
50
|
+
compress: true,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
else if (telegram.forceIpv4) {
|
|
54
|
+
botOptions.client = botOptions.client ?? {};
|
|
55
|
+
botOptions.client.baseFetchConfig = {
|
|
56
|
+
...(botOptions.client.baseFetchConfig ?? {}),
|
|
57
|
+
agent: createTelegramIpv4Agent(),
|
|
58
|
+
compress: true,
|
|
59
|
+
};
|
|
60
|
+
logger.info(`[Bot] Forcing IPv4 for Telegram API requests`);
|
|
61
|
+
}
|
|
62
|
+
return botOptions;
|
|
63
|
+
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
// @ts-expect-error — node-fetch v2 ships no TS types and we avoid adding @types/node-fetch
|
|
2
|
+
import nodeFetch from "node-fetch";
|
|
3
|
+
import { Agent as HttpsAgent } from "https";
|
|
1
4
|
import { config } from "../../config.js";
|
|
2
5
|
import { logger } from "../../utils/logger.js";
|
|
3
|
-
const
|
|
6
|
+
const DEFAULT_TELEGRAM_FILE_URL_BASE = "https://api.telegram.org/file/bot";
|
|
7
|
+
function fileUrlBase() {
|
|
8
|
+
return config.telegram.apiRoot
|
|
9
|
+
? `${config.telegram.apiRoot.replace(/\/$/, "")}/file/bot`
|
|
10
|
+
: DEFAULT_TELEGRAM_FILE_URL_BASE;
|
|
11
|
+
}
|
|
4
12
|
const MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024; // 20MB Telegram limit
|
|
5
13
|
/**
|
|
6
14
|
* Download a photo from Telegram servers
|
|
@@ -18,7 +26,7 @@ export async function downloadTelegramFile(api, fileId) {
|
|
|
18
26
|
const sizeMb = (file.file_size / (1024 * 1024)).toFixed(2);
|
|
19
27
|
throw new Error(`File too large: ${sizeMb}MB (max 20MB)`);
|
|
20
28
|
}
|
|
21
|
-
const fileUrl = `${
|
|
29
|
+
const fileUrl = `${fileUrlBase()}${config.telegram.token}/${file.file_path}`;
|
|
22
30
|
logger.debug(`[FileDownload] Downloading from ${fileUrl.replace(config.telegram.token, "***")}`);
|
|
23
31
|
const fetchOptions = {};
|
|
24
32
|
// Use proxy if configured
|
|
@@ -26,7 +34,17 @@ export async function downloadTelegramFile(api, fileId) {
|
|
|
26
34
|
const { HttpsProxyAgent } = await import("https-proxy-agent");
|
|
27
35
|
fetchOptions.agent = new HttpsProxyAgent(config.telegram.proxyUrl);
|
|
28
36
|
}
|
|
29
|
-
|
|
37
|
+
else if (config.telegram.forceIpv4) {
|
|
38
|
+
fetchOptions.agent = new HttpsAgent({ family: 4, keepAlive: true });
|
|
39
|
+
}
|
|
40
|
+
// Send shared secret when custom API root expects it
|
|
41
|
+
if (config.telegram.proxySecret) {
|
|
42
|
+
fetchOptions.headers = {
|
|
43
|
+
...fetchOptions.headers,
|
|
44
|
+
"X-Proxy-Secret": config.telegram.proxySecret,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const response = await nodeFetch(fileUrl, fetchOptions);
|
|
30
48
|
if (!response.ok) {
|
|
31
49
|
throw new Error(`Failed to download file: ${response.status} ${response.statusText}`);
|
|
32
50
|
}
|
package/dist/config.js
CHANGED
|
@@ -62,12 +62,33 @@ function getOptionalTtsProviderEnvVar(key, defaultValue) {
|
|
|
62
62
|
}
|
|
63
63
|
return defaultValue;
|
|
64
64
|
}
|
|
65
|
-
export
|
|
66
|
-
|
|
65
|
+
export function buildTelegramConfig() {
|
|
66
|
+
const proxyUrl = getEnvVar("TELEGRAM_PROXY_URL", false);
|
|
67
|
+
// grammY rejects an apiRoot ending with `/`, so normalize once at config
|
|
68
|
+
// load instead of leaking the concern into every consumer.
|
|
69
|
+
const apiRoot = getEnvVar("TELEGRAM_API_ROOT", false).replace(/\/+$/, "");
|
|
70
|
+
const proxySecret = getEnvVar("TELEGRAM_PROXY_SECRET", false);
|
|
71
|
+
const forceIpv4 = getOptionalBooleanEnvVar("TELEGRAM_FORCE_IPV4", false);
|
|
72
|
+
if (proxyUrl && apiRoot) {
|
|
73
|
+
throw new Error("TELEGRAM_PROXY_URL and TELEGRAM_API_ROOT are alternative connectivity modes and cannot be used together. " +
|
|
74
|
+
"TELEGRAM_PROXY_URL tunnels TCP through a SOCKS/HTTP forward proxy; " +
|
|
75
|
+
"TELEGRAM_API_ROOT routes API calls through an HTTPS reverse proxy. Pick one.");
|
|
76
|
+
}
|
|
77
|
+
if (proxySecret && !apiRoot) {
|
|
78
|
+
throw new Error("TELEGRAM_PROXY_SECRET requires TELEGRAM_API_ROOT to be set. " +
|
|
79
|
+
"Without a custom API root, the secret header would be sent to api.telegram.org.");
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
67
82
|
token: getEnvVar("TELEGRAM_BOT_TOKEN"),
|
|
68
83
|
allowedUserId: parseInt(getEnvVar("TELEGRAM_ALLOWED_USER_ID"), 10),
|
|
69
|
-
proxyUrl
|
|
70
|
-
|
|
84
|
+
proxyUrl,
|
|
85
|
+
apiRoot,
|
|
86
|
+
proxySecret,
|
|
87
|
+
forceIpv4,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export const config = {
|
|
91
|
+
telegram: buildTelegramConfig(),
|
|
71
92
|
opencode: {
|
|
72
93
|
apiUrl: getEnvVar("OPENCODE_API_URL", false) || "http://localhost:4096",
|
|
73
94
|
username: getEnvVar("OPENCODE_SERVER_USERNAME", false) || "opencode",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grinev/opencode-telegram-bot",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.3",
|
|
4
4
|
"description": "Telegram bot client for OpenCode to run and monitor coding tasks from chat.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"grammy": "^1.39.2",
|
|
60
60
|
"https-proxy-agent": "^7.0.6",
|
|
61
61
|
"mdast-util-to-string": "^4.0.0",
|
|
62
|
+
"node-fetch": "^2.7.0",
|
|
62
63
|
"remark-gfm": "^4.0.1",
|
|
63
64
|
"remark-parse": "^11.0.0",
|
|
64
65
|
"socks-proxy-agent": "^8.0.5",
|