@im-more/dsh-telegram 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +10 -0
- package/lib/index.js +2067 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +70 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +231 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +34 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +41 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +39 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +382 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +33 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +89 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +19 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +34 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +8 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/media.d.ts +38 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +65 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/html.d.ts +16 -0
- package/lib/types/modes/html.d.ts.map +1 -0
- package/lib/types/modes/html.js +155 -0
- package/lib/types/modes/html.js.map +1 -0
- package/lib/types/modes/plain.d.ts +14 -0
- package/lib/types/modes/plain.d.ts.map +1 -0
- package/lib/types/modes/plain.js +56 -0
- package/lib/types/modes/plain.js.map +1 -0
- package/lib/types/modes/shared.d.ts +140 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +405 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/polling.d.ts +26 -0
- package/lib/types/polling.d.ts.map +1 -0
- package/lib/types/polling.js +149 -0
- package/lib/types/polling.js.map +1 -0
- package/lib/types/request.d.ts +67 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +158 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/secret.d.ts +27 -0
- package/lib/types/secret.d.ts.map +1 -0
- package/lib/types/secret.js +39 -0
- package/lib/types/secret.js.map +1 -0
- package/lib/types/send.d.ts +48 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +326 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/types/game.d.ts +102 -0
- package/lib/types/types/game.d.ts.map +1 -0
- package/lib/types/types/game.js +5 -0
- package/lib/types/types/game.js.map +1 -0
- package/lib/types/types/index.d.ts +2751 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +88 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/inline.d.ts +796 -0
- package/lib/types/types/inline.d.ts.map +1 -0
- package/lib/types/types/inline.js +4 -0
- package/lib/types/types/inline.js.map +1 -0
- package/lib/types/types/internal.d.ts +32 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +30 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/passport.d.ts +229 -0
- package/lib/types/types/passport.d.ts.map +1 -0
- package/lib/types/types/passport.js +3 -0
- package/lib/types/types/passport.js.map +1 -0
- package/lib/types/types/payment.d.ts +285 -0
- package/lib/types/types/payment.d.ts.map +1 -0
- package/lib/types/types/payment.js +6 -0
- package/lib/types/types/payment.js.map +1 -0
- package/lib/types/types/sticker.d.ts +221 -0
- package/lib/types/types/sticker.d.ts.map +1 -0
- package/lib/types/types/sticker.js +11 -0
- package/lib/types/types/sticker.js.map +1 -0
- package/lib/types/types/update.d.ts +97 -0
- package/lib/types/types/update.d.ts.map +1 -0
- package/lib/types/types/update.js +6 -0
- package/lib/types/types/update.js.map +1 -0
- package/lib/types/webhook.d.ts +29 -0
- package/lib/types/webhook.d.ts.map +1 -0
- package/lib/types/webhook.js +129 -0
- package/lib/types/webhook.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +349 -0
- package/src/config.ts +73 -0
- package/src/decode.ts +472 -0
- package/src/errors.ts +103 -0
- package/src/identity.ts +41 -0
- package/src/index.ts +14 -0
- package/src/media.ts +79 -0
- package/src/modes/html.ts +193 -0
- package/src/modes/plain.ts +75 -0
- package/src/modes/shared.ts +505 -0
- package/src/polling.ts +176 -0
- package/src/request.ts +224 -0
- package/src/secret.ts +46 -0
- package/src/send.ts +454 -0
- package/src/types/.eslintrc.yml +2 -0
- package/src/types/game.ts +112 -0
- package/src/types/index.ts +3028 -0
- package/src/types/inline.ts +857 -0
- package/src/types/internal.ts +64 -0
- package/src/types/passport.ts +256 -0
- package/src/types/payment.ts +304 -0
- package/src/types/sticker.ts +244 -0
- package/src/types/update.ts +107 -0
- package/src/webhook.ts +168 -0
- package/README.md +0 -3
package/src/bot.ts
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { type Context } from '@deepseek-ai/cordis'
|
|
2
|
+
import {
|
|
3
|
+
Adapter,
|
|
4
|
+
createEditDriver,
|
|
5
|
+
type Mode,
|
|
6
|
+
type PrepareContext,
|
|
7
|
+
type ResolvedResource,
|
|
8
|
+
type ResourceKind,
|
|
9
|
+
type ResourceRef,
|
|
10
|
+
type SendContext,
|
|
11
|
+
type SentPart,
|
|
12
|
+
type StreamDriver,
|
|
13
|
+
type Target,
|
|
14
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
15
|
+
import { readFile } from 'node:fs/promises'
|
|
16
|
+
import { extname } from 'node:path'
|
|
17
|
+
import { Config } from './config.ts'
|
|
18
|
+
import { classifyTelegramError } from './errors.ts'
|
|
19
|
+
import { botIdFromToken, verifyIdentity } from './identity.ts'
|
|
20
|
+
import { MediaStaging } from './media.ts'
|
|
21
|
+
import {
|
|
22
|
+
createTelegramRequest,
|
|
23
|
+
downloadFile,
|
|
24
|
+
sendMedia,
|
|
25
|
+
sendMediaGroup,
|
|
26
|
+
type TelegramRequest,
|
|
27
|
+
} from './request.ts'
|
|
28
|
+
// The declaration files register their typed methods on `Internal` at import time.
|
|
29
|
+
import { createHtmlMode } from './modes/html.ts'
|
|
30
|
+
import { createPlainMode } from './modes/plain.ts'
|
|
31
|
+
import type { TelegramModeTypes } from './modes/shared.ts'
|
|
32
|
+
import { TelegramPolling } from './polling.ts'
|
|
33
|
+
import {
|
|
34
|
+
deletePart,
|
|
35
|
+
editPayload,
|
|
36
|
+
sendPayload,
|
|
37
|
+
type TelegramSendDeps,
|
|
38
|
+
} from './send.ts'
|
|
39
|
+
import './types/index.ts'
|
|
40
|
+
import type { Message } from './types/index.ts'
|
|
41
|
+
import { Internal } from './types/internal.ts'
|
|
42
|
+
import { TelegramWebhook } from './webhook.ts'
|
|
43
|
+
|
|
44
|
+
/** The two modes this adapter always registers. */
|
|
45
|
+
export interface TelegramModes {
|
|
46
|
+
[mode: string]: Mode<TelegramModeTypes>
|
|
47
|
+
html: Mode<TelegramModeTypes>
|
|
48
|
+
plain: Mode<TelegramModeTypes>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A transport `connect` starts and `disconnect` stops. */
|
|
52
|
+
interface TelegramTransport {
|
|
53
|
+
start(): Promise<void>
|
|
54
|
+
stop(): Promise<void>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** File extension of a MIME type, for the multipart part name. */
|
|
58
|
+
function extensionOf(mime: string | undefined): string {
|
|
59
|
+
switch (mime) {
|
|
60
|
+
case 'image/png':
|
|
61
|
+
return '.png'
|
|
62
|
+
case 'image/jpeg':
|
|
63
|
+
return '.jpg'
|
|
64
|
+
case 'image/webp':
|
|
65
|
+
return '.webp'
|
|
66
|
+
case 'image/gif':
|
|
67
|
+
return '.gif'
|
|
68
|
+
case 'video/mp4':
|
|
69
|
+
return '.mp4'
|
|
70
|
+
case 'video/webm':
|
|
71
|
+
return '.webm'
|
|
72
|
+
case 'audio/mpeg':
|
|
73
|
+
return '.mp3'
|
|
74
|
+
case 'audio/ogg':
|
|
75
|
+
return '.ogg'
|
|
76
|
+
case 'application/pdf':
|
|
77
|
+
return '.pdf'
|
|
78
|
+
default:
|
|
79
|
+
return ''
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** MIME type by file extension for `files.local` reads; unknown extensions are binary. */
|
|
84
|
+
const LOCAL_MIME_TYPES: Record<string, string> = {
|
|
85
|
+
'.gif': 'image/gif',
|
|
86
|
+
'.jpeg': 'image/jpeg',
|
|
87
|
+
'.jpg': 'image/jpeg',
|
|
88
|
+
'.json': 'application/json',
|
|
89
|
+
'.m4a': 'audio/mp4',
|
|
90
|
+
'.mp3': 'audio/mpeg',
|
|
91
|
+
'.mp4': 'video/mp4',
|
|
92
|
+
'.oga': 'audio/ogg',
|
|
93
|
+
'.ogg': 'audio/ogg',
|
|
94
|
+
'.pdf': 'application/pdf',
|
|
95
|
+
'.png': 'image/png',
|
|
96
|
+
'.txt': 'text/plain',
|
|
97
|
+
'.wav': 'audio/wav',
|
|
98
|
+
'.webm': 'video/webm',
|
|
99
|
+
'.webp': 'image/webp',
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Content type of a locally stored Bot API file. */
|
|
103
|
+
function localMimeType(filePath: string): string {
|
|
104
|
+
return (
|
|
105
|
+
LOCAL_MIME_TYPES[extname(filePath).toLowerCase()] ??
|
|
106
|
+
'application/octet-stream'
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Whether a filesystem failure means the file does not exist. */
|
|
111
|
+
function isMissingFile(error: unknown): boolean {
|
|
112
|
+
return (
|
|
113
|
+
error instanceof Error && (error as NodeJS.ErrnoException).code === 'ENOENT'
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Telegram Bot API adapter.
|
|
119
|
+
*
|
|
120
|
+
* Identity comes from `getMe`; polling and webhook are the two transports;
|
|
121
|
+
* `html` and `plain` are the two modes; outbound media is staged in memory and
|
|
122
|
+
* sent as multipart, because Telegram has no standalone upload endpoint.
|
|
123
|
+
*/
|
|
124
|
+
export class TelegramBot extends Adapter<Config, TelegramModes> {
|
|
125
|
+
/** The adapter needs the Satori service and the isolated `http` service (official adapters declare `http` with `@Inject('http')`). */
|
|
126
|
+
static override inject = ['satori', 'http']
|
|
127
|
+
/** The Loader applies this schema to a profile row's `config` before the constructor runs. */
|
|
128
|
+
static readonly Config = Config
|
|
129
|
+
|
|
130
|
+
readonly modes: TelegramModes = {
|
|
131
|
+
html: createHtmlMode(),
|
|
132
|
+
plain: createPlainMode(),
|
|
133
|
+
}
|
|
134
|
+
readonly modeOrder = ['html', 'plain'] as const
|
|
135
|
+
|
|
136
|
+
/** Endpoints shared by the Bot API client and file downloads. */
|
|
137
|
+
readonly request: TelegramRequest
|
|
138
|
+
/** Full typed Bot API surface registered by the vendored type declarations. */
|
|
139
|
+
override readonly internal: Internal
|
|
140
|
+
/** Outbound media bytes between `prepare` and `sendPayload`. */
|
|
141
|
+
readonly staging: MediaStaging
|
|
142
|
+
/** `@username` of the bot, once `getMe` answered. */
|
|
143
|
+
username: string | undefined
|
|
144
|
+
|
|
145
|
+
private transport: TelegramTransport | undefined
|
|
146
|
+
|
|
147
|
+
constructor(ctx: Context, config: Config) {
|
|
148
|
+
super(ctx, config, {
|
|
149
|
+
id: `telegram:${botIdFromToken(config.token)}`,
|
|
150
|
+
platform: 'telegram',
|
|
151
|
+
adapter: 'telegram',
|
|
152
|
+
})
|
|
153
|
+
this.request = createTelegramRequest(
|
|
154
|
+
ctx,
|
|
155
|
+
config.token,
|
|
156
|
+
config.apiBaseUrl,
|
|
157
|
+
config.files.baseUrl ?? config.apiBaseUrl,
|
|
158
|
+
)
|
|
159
|
+
this.internal = new Internal(this.request)
|
|
160
|
+
this.staging = new MediaStaging(
|
|
161
|
+
config.media.stagingTtlMs,
|
|
162
|
+
config.media.maxStagedBytes,
|
|
163
|
+
)
|
|
164
|
+
this.defineInternalRoute('/files/:fileId', async ({ params }) =>
|
|
165
|
+
this.serveFile(params.fileId),
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
override async connect(): Promise<void> {
|
|
170
|
+
try {
|
|
171
|
+
const me = await this.internal.getMe()
|
|
172
|
+
this.selfId = verifyIdentity(this.config.token, me)
|
|
173
|
+
this.username = me.username
|
|
174
|
+
this.user = {
|
|
175
|
+
id: this.selfId,
|
|
176
|
+
name: me.username ?? me.first_name!,
|
|
177
|
+
isBot: true,
|
|
178
|
+
}
|
|
179
|
+
} catch (error) {
|
|
180
|
+
// `Bot.start` only stores a connect failure on `bot.error`, so log it
|
|
181
|
+
// here; transports log their own start failures at their start sites.
|
|
182
|
+
this.ctx.logger.error(
|
|
183
|
+
'telegram: connect failed: %s',
|
|
184
|
+
error instanceof Error ? error.message : String(error),
|
|
185
|
+
)
|
|
186
|
+
throw error
|
|
187
|
+
}
|
|
188
|
+
this.transport =
|
|
189
|
+
this.config.transport === 'webhook'
|
|
190
|
+
? new TelegramWebhook(this)
|
|
191
|
+
: new TelegramPolling(this)
|
|
192
|
+
await this.transport.start()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
override async disconnect(): Promise<void> {
|
|
196
|
+
const transport = this.transport
|
|
197
|
+
this.transport = undefined
|
|
198
|
+
await transport?.stop()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
override async upload(
|
|
202
|
+
ref: ResourceRef,
|
|
203
|
+
_scope: string,
|
|
204
|
+
ctx: PrepareContext,
|
|
205
|
+
): Promise<ResolvedResource> {
|
|
206
|
+
const fetched = await ctx.fetch(ref.src, ctx.signal)
|
|
207
|
+
const mime = ref.meta.mime ?? fetched.mime
|
|
208
|
+
const name =
|
|
209
|
+
ref.meta.name ??
|
|
210
|
+
fetched.name ??
|
|
211
|
+
`telegram-${ref.kind}${extensionOf(mime)}`
|
|
212
|
+
return this.staging.stage({
|
|
213
|
+
data: fetched.data,
|
|
214
|
+
name,
|
|
215
|
+
...(mime === undefined ? {} : { mime }),
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
override resourceScope(_target: Target, _kind: ResourceKind): string {
|
|
220
|
+
return this.id
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
override async sendPayload(
|
|
224
|
+
payload: unknown,
|
|
225
|
+
target: Target,
|
|
226
|
+
ctx: SendContext,
|
|
227
|
+
): Promise<SentPart> {
|
|
228
|
+
return await sendPayload(
|
|
229
|
+
this.sendDeps(),
|
|
230
|
+
payload as Parameters<typeof sendPayload>[1],
|
|
231
|
+
target,
|
|
232
|
+
ctx,
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
override async editPayload(
|
|
237
|
+
part: SentPart,
|
|
238
|
+
payload: unknown,
|
|
239
|
+
handle: Parameters<typeof editPayload>[3],
|
|
240
|
+
signal?: AbortSignal,
|
|
241
|
+
): Promise<void> {
|
|
242
|
+
await editPayload(
|
|
243
|
+
this.sendDeps(),
|
|
244
|
+
part,
|
|
245
|
+
payload as Parameters<typeof editPayload>[2],
|
|
246
|
+
handle,
|
|
247
|
+
signal,
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override async deletePart(
|
|
252
|
+
part: SentPart,
|
|
253
|
+
handle: Parameters<typeof deletePart>[2],
|
|
254
|
+
): Promise<void> {
|
|
255
|
+
await deletePart(this.sendDeps(), part, handle)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
override streamDriver(
|
|
259
|
+
_mode: string,
|
|
260
|
+
_target: Target,
|
|
261
|
+
): StreamDriver | undefined {
|
|
262
|
+
return createEditDriver<TelegramModeTypes>(this)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
override classify(error: unknown) {
|
|
266
|
+
return classifyTelegramError(error)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Resolve one `satori:` file route.
|
|
271
|
+
*
|
|
272
|
+
* `files.local` reads the Bot API server's host filesystem; otherwise the
|
|
273
|
+
* file is downloaded from the configured file endpoint.
|
|
274
|
+
*/
|
|
275
|
+
private async serveFile(fileId: string): Promise<Response> {
|
|
276
|
+
if (this.config.files.local) return await this.serveLocalFile(fileId)
|
|
277
|
+
try {
|
|
278
|
+
const file = await this.internal.getFile({ file_id: fileId })
|
|
279
|
+
if (file.file_path === undefined)
|
|
280
|
+
return new Response(null, { status: 404 })
|
|
281
|
+
const response = await downloadFile(this.request, file.file_path)
|
|
282
|
+
const headers = new Headers()
|
|
283
|
+
const mime = response.headers.get('content-type')
|
|
284
|
+
if (mime !== null) headers.set('content-type', mime)
|
|
285
|
+
return new Response(response.body, { status: response.status, headers })
|
|
286
|
+
} catch (error) {
|
|
287
|
+
this.ctx.logger.warn(
|
|
288
|
+
'telegram: file %s could not be served: %s',
|
|
289
|
+
fileId,
|
|
290
|
+
error instanceof Error ? error.message : String(error),
|
|
291
|
+
)
|
|
292
|
+
return new Response(null, { status: 502 })
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Serve one file from the local Bot API server's filesystem, where
|
|
298
|
+
* `file_path` is an absolute host path instead of a URL segment.
|
|
299
|
+
*/
|
|
300
|
+
private async serveLocalFile(fileId: string): Promise<Response> {
|
|
301
|
+
try {
|
|
302
|
+
const file = await this.internal.getFile({ file_id: fileId })
|
|
303
|
+
if (file.file_path === undefined)
|
|
304
|
+
return new Response(null, { status: 404 })
|
|
305
|
+
const data = await readFile(file.file_path)
|
|
306
|
+
return new Response(data, {
|
|
307
|
+
status: 200,
|
|
308
|
+
headers: { 'content-type': localMimeType(file.file_path) },
|
|
309
|
+
})
|
|
310
|
+
} catch (error) {
|
|
311
|
+
if (isMissingFile(error)) return new Response(null, { status: 404 })
|
|
312
|
+
this.ctx.logger.warn(
|
|
313
|
+
'telegram: file %s could not be served: %s',
|
|
314
|
+
fileId,
|
|
315
|
+
error instanceof Error ? error.message : String(error),
|
|
316
|
+
)
|
|
317
|
+
return new Response(null, { status: 502 })
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private sendDeps(): TelegramSendDeps {
|
|
322
|
+
return {
|
|
323
|
+
internal: this.internal,
|
|
324
|
+
config: this.config,
|
|
325
|
+
staging: this.staging,
|
|
326
|
+
sendMedia: (method, form, signal) =>
|
|
327
|
+
this.sendMediaForm(method, form, signal),
|
|
328
|
+
sendMediaGroup: (form, signal) => this.sendMediaGroupForm(form, signal),
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Send one multipart media form; overridable so send tests never touch the network. */
|
|
333
|
+
sendMediaForm(
|
|
334
|
+
method: string,
|
|
335
|
+
form: FormData,
|
|
336
|
+
signal?: AbortSignal,
|
|
337
|
+
): Promise<Message> {
|
|
338
|
+
return sendMedia(this.request, method, form, signal)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Send one `sendMediaGroup` multipart form. */
|
|
342
|
+
sendMediaGroupForm(form: FormData, signal?: AbortSignal): Promise<Message[]> {
|
|
343
|
+
return sendMediaGroup(this.request, form, signal)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export { Config }
|
|
348
|
+
|
|
349
|
+
export default TelegramBot
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery'
|
|
2
|
+
|
|
3
|
+
/** How the adapter receives updates. */
|
|
4
|
+
export type TelegramTransport = 'polling' | 'webhook'
|
|
5
|
+
|
|
6
|
+
/** One Telegram bot. */
|
|
7
|
+
export interface Config {
|
|
8
|
+
token: string
|
|
9
|
+
transport: TelegramTransport
|
|
10
|
+
dropPendingUpdates: boolean
|
|
11
|
+
apiBaseUrl: string
|
|
12
|
+
files: {
|
|
13
|
+
baseUrl?: string | null
|
|
14
|
+
local: boolean
|
|
15
|
+
}
|
|
16
|
+
polling: {
|
|
17
|
+
timeoutMs: number
|
|
18
|
+
retryTimes: number
|
|
19
|
+
retryIntervalMs: number
|
|
20
|
+
retryLazyMs: number
|
|
21
|
+
mediaGroupWindowMs: number
|
|
22
|
+
}
|
|
23
|
+
webhook: {
|
|
24
|
+
path: string
|
|
25
|
+
publicUrl?: string | null
|
|
26
|
+
maxBodyBytes: number
|
|
27
|
+
}
|
|
28
|
+
media: {
|
|
29
|
+
stagingTtlMs: number
|
|
30
|
+
maxStagedBytes: number
|
|
31
|
+
}
|
|
32
|
+
linkPreview: boolean
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Validated Telegram adapter configuration. */
|
|
36
|
+
export const Config: z<Config> = z.object({
|
|
37
|
+
token: z.string().role('secret').required(),
|
|
38
|
+
transport: z.union(['polling', 'webhook']).default('polling'),
|
|
39
|
+
dropPendingUpdates: z.boolean().default(true),
|
|
40
|
+
apiBaseUrl: z.string().role('url').default('https://api.telegram.org'),
|
|
41
|
+
files: z
|
|
42
|
+
.object({
|
|
43
|
+
baseUrl: z.string().role('url'),
|
|
44
|
+
local: z.boolean().default(false),
|
|
45
|
+
})
|
|
46
|
+
.default({}),
|
|
47
|
+
polling: z
|
|
48
|
+
.object({
|
|
49
|
+
timeoutMs: z.natural().role('ms').default(25_000),
|
|
50
|
+
retryTimes: z.natural().default(6),
|
|
51
|
+
retryIntervalMs: z.natural().role('ms').default(5_000),
|
|
52
|
+
retryLazyMs: z.natural().role('ms').default(60_000),
|
|
53
|
+
mediaGroupWindowMs: z.natural().role('ms').default(1_200),
|
|
54
|
+
})
|
|
55
|
+
.default({}),
|
|
56
|
+
webhook: z
|
|
57
|
+
.object({
|
|
58
|
+
path: z.string().default('/telegram/webhook'),
|
|
59
|
+
publicUrl: z.string().role('url'),
|
|
60
|
+
maxBodyBytes: z.natural().default(1_048_576),
|
|
61
|
+
})
|
|
62
|
+
.default({}),
|
|
63
|
+
media: z
|
|
64
|
+
.object({
|
|
65
|
+
stagingTtlMs: z
|
|
66
|
+
.natural()
|
|
67
|
+
.role('ms')
|
|
68
|
+
.default(10 * 60_000),
|
|
69
|
+
maxStagedBytes: z.natural().default(64 * 1024 * 1024),
|
|
70
|
+
})
|
|
71
|
+
.default({}),
|
|
72
|
+
linkPreview: z.boolean().default(true),
|
|
73
|
+
})
|