-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.telegram.ts
More file actions
69 lines (61 loc) · 2.59 KB
/
Copy pathindex.telegram.ts
File metadata and controls
69 lines (61 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { Config } from '@opencode-ai/sdk';
import { asRecord } from './src/utils';
import { AGENT_TELEGRAM } from './src/constants';
import type { TelegramConfig } from './src/types';
export function parseTelegramConfig(cfg: Config | undefined): TelegramConfig {
const node = cfg?.agent?.[AGENT_TELEGRAM];
const options = asRecord(node?.options);
const mode = options.mode === 'webhook' ? 'webhook' : 'polling';
const botToken = typeof options.bot_token === 'string' ? options.bot_token.trim() : '';
if (!botToken) {
throw new Error(`[Plugin] Missing options for ${AGENT_TELEGRAM}: bot_token`);
}
const callbackUrlRaw = options.callback_url;
const fileStoreDirRaw = options.file_store_dir;
const callbackUrl =
typeof callbackUrlRaw === 'string' && callbackUrlRaw.length > 0
? callbackUrlRaw.startsWith('http')
? callbackUrlRaw
: `http://${callbackUrlRaw}`
: undefined;
const file_store_dir =
typeof fileStoreDirRaw === 'string' && fileStoreDirRaw.trim().length > 0
? fileStoreDirRaw.trim()
: undefined;
const timeoutRaw = Number(options.polling_timeout_sec);
const intervalRaw = Number(options.polling_interval_ms);
const webhookListenPortRaw = Number(options.webhook_listen_port);
const maxMbRaw = Number(options.auto_send_local_files_max_mb);
const polling_timeout_sec = Number.isFinite(timeoutRaw) && timeoutRaw > 0 ? timeoutRaw : 20;
const polling_interval_ms = Number.isFinite(intervalRaw) && intervalRaw >= 0 ? intervalRaw : 300;
const webhook_listen_port =
Number.isFinite(webhookListenPortRaw) && webhookListenPortRaw > 0
? webhookListenPortRaw
: undefined;
const auto_send_local_files =
options.auto_send_local_files === true || options.auto_send_local_files === 'true';
const auto_send_local_files_allow_absolute =
options.auto_send_local_files_allow_absolute === true ||
options.auto_send_local_files_allow_absolute === 'true';
const auto_send_local_files_max_mb =
Number.isFinite(maxMbRaw) && maxMbRaw > 0 ? maxMbRaw : 20;
if (mode === 'webhook' && !callbackUrl) {
throw new Error(`[Plugin] Missing options for ${AGENT_TELEGRAM} in webhook mode: callback_url`);
}
return {
mode,
bot_token: botToken,
polling_timeout_sec,
polling_interval_ms,
callback_url: callbackUrl,
file_store_dir,
webhook_listen_port,
auto_send_local_files,
auto_send_local_files_allow_absolute,
auto_send_local_files_max_mb,
webhook_secret_token:
typeof options.webhook_secret_token === 'string'
? options.webhook_secret_token.trim()
: undefined,
};
}