@tgify/tgify 0.1.0 → 0.1.5
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 +23 -23
- package/README.md +358 -356
- package/lib/cli.mjs +9 -9
- package/package.json +1 -1
- package/src/button.ts +182 -182
- package/src/composer.ts +1008 -1008
- package/src/context.ts +1661 -1661
- package/src/core/helpers/args.ts +63 -63
- package/src/core/helpers/check.ts +71 -71
- package/src/core/helpers/compact.ts +18 -18
- package/src/core/helpers/deunionize.ts +26 -26
- package/src/core/helpers/formatting.ts +119 -119
- package/src/core/helpers/util.ts +96 -96
- package/src/core/network/client.ts +396 -396
- package/src/core/network/error.ts +29 -29
- package/src/core/network/multipart-stream.ts +45 -45
- package/src/core/network/polling.ts +94 -94
- package/src/core/network/webhook.ts +58 -58
- package/src/core/types/typegram.ts +54 -54
- package/src/filters.ts +109 -109
- package/src/format.ts +110 -110
- package/src/future.ts +213 -213
- package/src/index.ts +17 -17
- package/src/input.ts +59 -59
- package/src/markup.ts +142 -142
- package/src/middleware.ts +24 -24
- package/src/reactions.ts +118 -118
- package/src/router.ts +55 -55
- package/src/scenes/base.ts +52 -52
- package/src/scenes/context.ts +136 -136
- package/src/scenes/index.ts +21 -21
- package/src/scenes/stage.ts +71 -71
- package/src/scenes/wizard/context.ts +58 -58
- package/src/scenes/wizard/index.ts +63 -63
- package/src/scenes.ts +1 -1
- package/src/session.ts +204 -204
- package/src/telegraf.ts +354 -354
- package/src/telegram-types.ts +219 -219
- package/src/telegram.ts +1635 -1635
- package/src/types.ts +2 -2
- package/src/utils.ts +1 -1
- package/typings/telegraf.d.ts.map +1 -1
|
@@ -1,396 +1,396 @@
|
|
|
1
|
-
/* eslint @typescript-eslint/restrict-template-expressions: [ "error", { "allowNumber": true, "allowBoolean": true } ] */
|
|
2
|
-
import * as crypto from 'crypto'
|
|
3
|
-
import * as fs from 'fs'
|
|
4
|
-
import { stat, realpath } from 'fs/promises'
|
|
5
|
-
import * as http from 'http'
|
|
6
|
-
import * as https from 'https'
|
|
7
|
-
import * as path from 'path'
|
|
8
|
-
import fetch, { RequestInit } from 'node-fetch'
|
|
9
|
-
import { hasProp, hasPropType } from '../helpers/check'
|
|
10
|
-
import { InputFile, Opts, Telegram } from '../types/typegram'
|
|
11
|
-
import { AbortSignal } from 'abort-controller'
|
|
12
|
-
import { compactOptions } from '../helpers/compact'
|
|
13
|
-
import MultipartStream from './multipart-stream'
|
|
14
|
-
import TelegramError from './error'
|
|
15
|
-
import { URL } from 'url'
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
17
|
-
const debug = require('debug')('telegraf:client')
|
|
18
|
-
const { isStream } = MultipartStream
|
|
19
|
-
|
|
20
|
-
const WEBHOOK_REPLY_METHOD_ALLOWLIST = new Set<keyof Telegram>([
|
|
21
|
-
'answerCallbackQuery',
|
|
22
|
-
'answerInlineQuery',
|
|
23
|
-
'deleteMessage',
|
|
24
|
-
'leaveChat',
|
|
25
|
-
'sendChatAction',
|
|
26
|
-
])
|
|
27
|
-
|
|
28
|
-
namespace ApiClient {
|
|
29
|
-
export type Agent = http.Agent | ((parsedUrl: URL) => http.Agent) | undefined
|
|
30
|
-
export interface Options {
|
|
31
|
-
/**
|
|
32
|
-
* Agent for communicating with the bot API.
|
|
33
|
-
*/
|
|
34
|
-
agent?: http.Agent
|
|
35
|
-
/**
|
|
36
|
-
* Agent for attaching files via URL.
|
|
37
|
-
* 1. Not all agents support both `http:` and `https:`.
|
|
38
|
-
* 2. When passing a function, create the agents once, outside of the function.
|
|
39
|
-
* Creating new agent every request probably breaks `keepAlive`.
|
|
40
|
-
*/
|
|
41
|
-
attachmentAgent?: Agent
|
|
42
|
-
apiRoot: string
|
|
43
|
-
/**
|
|
44
|
-
* @default 'bot'
|
|
45
|
-
* @see https://github.com/tdlight-team/tdlight-telegram-bot-api#user-mode
|
|
46
|
-
*/
|
|
47
|
-
apiMode: 'bot' | 'user'
|
|
48
|
-
webhookReply: boolean
|
|
49
|
-
testEnv: boolean
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface CallApiOptions {
|
|
53
|
-
signal?: AbortSignal
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const DEFAULT_EXTENSIONS: Record<string, string | undefined> = {
|
|
58
|
-
audio: 'mp3',
|
|
59
|
-
photo: 'jpg',
|
|
60
|
-
sticker: 'webp',
|
|
61
|
-
video: 'mp4',
|
|
62
|
-
animation: 'mp4',
|
|
63
|
-
video_note: 'mp4',
|
|
64
|
-
voice: 'ogg',
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const DEFAULT_OPTIONS: ApiClient.Options = {
|
|
68
|
-
apiRoot: 'https://api.telegram.org',
|
|
69
|
-
apiMode: 'bot',
|
|
70
|
-
webhookReply: true,
|
|
71
|
-
agent: new https.Agent({
|
|
72
|
-
keepAlive: true,
|
|
73
|
-
keepAliveMsecs: 10000,
|
|
74
|
-
}),
|
|
75
|
-
attachmentAgent: undefined,
|
|
76
|
-
testEnv: false,
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function includesMedia(payload: Record<string, unknown>) {
|
|
80
|
-
return Object.entries(payload).some(([key, value]) => {
|
|
81
|
-
if (key === 'link_preview_options') return false
|
|
82
|
-
|
|
83
|
-
if (Array.isArray(value)) {
|
|
84
|
-
return value.some(
|
|
85
|
-
({ media }) =>
|
|
86
|
-
media && typeof media === 'object' && (media.source || media.url)
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
return (
|
|
90
|
-
value &&
|
|
91
|
-
typeof value === 'object' &&
|
|
92
|
-
((hasProp(value, 'source') && value.source) ||
|
|
93
|
-
(hasProp(value, 'url') && value.url) ||
|
|
94
|
-
(hasPropType(value, 'media', 'object') &&
|
|
95
|
-
((hasProp(value.media, 'source') && value.media.source) ||
|
|
96
|
-
(hasProp(value.media, 'url') && value.media.url))))
|
|
97
|
-
)
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function replacer(_: unknown, value: unknown) {
|
|
102
|
-
if (value == null) return undefined
|
|
103
|
-
return value
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function buildJSONConfig(payload: unknown): Promise<RequestInit> {
|
|
107
|
-
return Promise.resolve({
|
|
108
|
-
method: 'POST',
|
|
109
|
-
compress: true,
|
|
110
|
-
headers: { 'content-type': 'application/json', connection: 'keep-alive' },
|
|
111
|
-
body: JSON.stringify(payload, replacer),
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const FORM_DATA_JSON_FIELDS = [
|
|
116
|
-
'results',
|
|
117
|
-
'reply_markup',
|
|
118
|
-
'mask_position',
|
|
119
|
-
'shipping_options',
|
|
120
|
-
'errors',
|
|
121
|
-
] as const
|
|
122
|
-
|
|
123
|
-
async function buildFormDataConfig(
|
|
124
|
-
payload: Opts<keyof Telegram>,
|
|
125
|
-
agent: ApiClient.Agent
|
|
126
|
-
) {
|
|
127
|
-
for (const field of FORM_DATA_JSON_FIELDS) {
|
|
128
|
-
if (hasProp(payload, field) && typeof payload[field] !== 'string') {
|
|
129
|
-
payload[field] = JSON.stringify(payload[field])
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
const boundary = crypto.randomBytes(32).toString('hex')
|
|
133
|
-
const formData = new MultipartStream(boundary)
|
|
134
|
-
await Promise.all(
|
|
135
|
-
Object.keys(payload).map((key) =>
|
|
136
|
-
// @ts-expect-error payload[key] can obviously index payload, but TS doesn't trust us
|
|
137
|
-
attachFormValue(formData, key, payload[key], agent)
|
|
138
|
-
)
|
|
139
|
-
)
|
|
140
|
-
return {
|
|
141
|
-
method: 'POST',
|
|
142
|
-
compress: true,
|
|
143
|
-
headers: {
|
|
144
|
-
'content-type': `multipart/form-data; boundary=${boundary}`,
|
|
145
|
-
connection: 'keep-alive',
|
|
146
|
-
},
|
|
147
|
-
body: formData,
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async function attachFormValue(
|
|
152
|
-
form: MultipartStream,
|
|
153
|
-
id: string,
|
|
154
|
-
value: unknown,
|
|
155
|
-
agent: ApiClient.Agent
|
|
156
|
-
) {
|
|
157
|
-
if (value == null) {
|
|
158
|
-
return
|
|
159
|
-
}
|
|
160
|
-
if (
|
|
161
|
-
typeof value === 'string' ||
|
|
162
|
-
typeof value === 'boolean' ||
|
|
163
|
-
typeof value === 'number'
|
|
164
|
-
) {
|
|
165
|
-
form.addPart({
|
|
166
|
-
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
167
|
-
body: `${value}`,
|
|
168
|
-
})
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
if (id === 'thumb' || id === 'thumbnail') {
|
|
172
|
-
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
173
|
-
await attachFormMedia(form, value as InputFile, attachmentId, agent)
|
|
174
|
-
return form.addPart({
|
|
175
|
-
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
176
|
-
body: `attach://${attachmentId}`,
|
|
177
|
-
})
|
|
178
|
-
}
|
|
179
|
-
if (Array.isArray(value)) {
|
|
180
|
-
const items = await Promise.all(
|
|
181
|
-
value.map(async (item) => {
|
|
182
|
-
if (typeof item.media !== 'object') {
|
|
183
|
-
return await Promise.resolve(item)
|
|
184
|
-
}
|
|
185
|
-
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
186
|
-
await attachFormMedia(form, item.media, attachmentId, agent)
|
|
187
|
-
const thumb = item.thumb ?? item.thumbnail
|
|
188
|
-
if (typeof thumb === 'object') {
|
|
189
|
-
const thumbAttachmentId = crypto.randomBytes(16).toString('hex')
|
|
190
|
-
await attachFormMedia(form, thumb, thumbAttachmentId, agent)
|
|
191
|
-
return {
|
|
192
|
-
...item,
|
|
193
|
-
media: `attach://${attachmentId}`,
|
|
194
|
-
thumbnail: `attach://${thumbAttachmentId}`,
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return { ...item, media: `attach://${attachmentId}` }
|
|
198
|
-
})
|
|
199
|
-
)
|
|
200
|
-
return form.addPart({
|
|
201
|
-
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
202
|
-
body: JSON.stringify(items),
|
|
203
|
-
})
|
|
204
|
-
}
|
|
205
|
-
if (
|
|
206
|
-
value &&
|
|
207
|
-
typeof value === 'object' &&
|
|
208
|
-
hasProp(value, 'media') &&
|
|
209
|
-
hasProp(value, 'type') &&
|
|
210
|
-
typeof value.media !== 'undefined' &&
|
|
211
|
-
typeof value.type !== 'undefined'
|
|
212
|
-
) {
|
|
213
|
-
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
214
|
-
await attachFormMedia(form, value.media as InputFile, attachmentId, agent)
|
|
215
|
-
return form.addPart({
|
|
216
|
-
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
217
|
-
body: JSON.stringify({
|
|
218
|
-
...value,
|
|
219
|
-
media: `attach://${attachmentId}`,
|
|
220
|
-
}),
|
|
221
|
-
})
|
|
222
|
-
}
|
|
223
|
-
return await attachFormMedia(form, value as InputFile, id, agent)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async function attachFormMedia(
|
|
227
|
-
form: MultipartStream,
|
|
228
|
-
media: InputFile,
|
|
229
|
-
id: string,
|
|
230
|
-
agent: ApiClient.Agent
|
|
231
|
-
) {
|
|
232
|
-
let fileName = media.filename ?? `${id}.${DEFAULT_EXTENSIONS[id] ?? 'dat'}`
|
|
233
|
-
if ('url' in media && media.url !== undefined) {
|
|
234
|
-
const timeout = 500_000 // ms
|
|
235
|
-
const res = await fetch(media.url, { agent, timeout })
|
|
236
|
-
return form.addPart({
|
|
237
|
-
headers: {
|
|
238
|
-
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
239
|
-
},
|
|
240
|
-
body: res.body,
|
|
241
|
-
})
|
|
242
|
-
}
|
|
243
|
-
if ('source' in media && media.source) {
|
|
244
|
-
let mediaSource = media.source
|
|
245
|
-
if (typeof media.source === 'string') {
|
|
246
|
-
const source = await realpath(media.source)
|
|
247
|
-
if ((await stat(source)).isFile()) {
|
|
248
|
-
fileName = media.filename ?? path.basename(media.source)
|
|
249
|
-
mediaSource = await fs.createReadStream(media.source)
|
|
250
|
-
} else {
|
|
251
|
-
throw new TypeError(`Unable to upload '${media.source}', not a file`)
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
if (isStream(mediaSource) || Buffer.isBuffer(mediaSource)) {
|
|
255
|
-
form.addPart({
|
|
256
|
-
headers: {
|
|
257
|
-
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
258
|
-
},
|
|
259
|
-
body: mediaSource,
|
|
260
|
-
})
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
async function answerToWebhook(
|
|
266
|
-
response: Response,
|
|
267
|
-
payload: Opts<keyof Telegram>,
|
|
268
|
-
options: ApiClient.Options
|
|
269
|
-
): Promise<true> {
|
|
270
|
-
if (!includesMedia(payload)) {
|
|
271
|
-
if (!response.headersSent) {
|
|
272
|
-
response.setHeader('content-type', 'application/json')
|
|
273
|
-
}
|
|
274
|
-
response.end(JSON.stringify(payload), 'utf-8')
|
|
275
|
-
return true
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const { headers, body } = await buildFormDataConfig(
|
|
279
|
-
payload,
|
|
280
|
-
options.attachmentAgent
|
|
281
|
-
)
|
|
282
|
-
if (!response.headersSent) {
|
|
283
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
284
|
-
response.setHeader(key, value)
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
await new Promise((resolve) => {
|
|
288
|
-
response.on('finish', resolve)
|
|
289
|
-
body.pipe(response)
|
|
290
|
-
})
|
|
291
|
-
return true
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
function redactToken(error: Error): never {
|
|
295
|
-
error.message = error.message.replace(
|
|
296
|
-
/\/(bot|user)(\d+):[^/]+\//,
|
|
297
|
-
'/$1$2:[REDACTED]/'
|
|
298
|
-
)
|
|
299
|
-
throw error
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
type Response = http.ServerResponse
|
|
303
|
-
class ApiClient {
|
|
304
|
-
readonly options: ApiClient.Options
|
|
305
|
-
|
|
306
|
-
constructor(
|
|
307
|
-
readonly token: string,
|
|
308
|
-
options?: Partial<ApiClient.Options>,
|
|
309
|
-
private readonly response?: Response
|
|
310
|
-
) {
|
|
311
|
-
this.options = {
|
|
312
|
-
...DEFAULT_OPTIONS,
|
|
313
|
-
...compactOptions(options),
|
|
314
|
-
}
|
|
315
|
-
if (this.options.apiRoot.startsWith('http://')) {
|
|
316
|
-
this.options.agent = undefined
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* If set to `true`, first _eligible_ call will avoid performing a POST request.
|
|
322
|
-
* Note that such a call:
|
|
323
|
-
* 1. cannot report errors or return meaningful values,
|
|
324
|
-
* 2. resolves before bot API has a chance to process it,
|
|
325
|
-
* 3. prematurely confirms the update as processed.
|
|
326
|
-
*
|
|
327
|
-
* https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates
|
|
328
|
-
* https://github.com/telegraf/telegraf/pull/1250
|
|
329
|
-
*/
|
|
330
|
-
set webhookReply(enable: boolean) {
|
|
331
|
-
this.options.webhookReply = enable
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
get webhookReply() {
|
|
335
|
-
return this.options.webhookReply
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
async callApi<M extends keyof Telegram>(
|
|
339
|
-
method: M,
|
|
340
|
-
payload: Opts<M>,
|
|
341
|
-
{ signal }: ApiClient.CallApiOptions = {}
|
|
342
|
-
): Promise<ReturnType<Telegram[M]>> {
|
|
343
|
-
const { token, options, response } = this
|
|
344
|
-
|
|
345
|
-
if (
|
|
346
|
-
options.webhookReply &&
|
|
347
|
-
response?.writableEnded === false &&
|
|
348
|
-
WEBHOOK_REPLY_METHOD_ALLOWLIST.has(method)
|
|
349
|
-
) {
|
|
350
|
-
debug('Call via webhook', method, payload)
|
|
351
|
-
// @ts-expect-error using webhookReply is an optimisation that doesn't respond with normal result
|
|
352
|
-
// up to the user to deal with this
|
|
353
|
-
return await answerToWebhook(response, { method, ...payload }, options)
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
if (!token) {
|
|
357
|
-
throw new TelegramError({
|
|
358
|
-
error_code: 401,
|
|
359
|
-
description: 'Bot Token is required',
|
|
360
|
-
})
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
debug('HTTP call', method, payload)
|
|
364
|
-
|
|
365
|
-
const config: RequestInit = includesMedia(payload)
|
|
366
|
-
? await buildFormDataConfig(
|
|
367
|
-
{ method, ...payload },
|
|
368
|
-
options.attachmentAgent
|
|
369
|
-
)
|
|
370
|
-
: await buildJSONConfig(payload)
|
|
371
|
-
const apiUrl = new URL(
|
|
372
|
-
`./${options.apiMode}${token}${options.testEnv ? '/test' : ''}/${method}`,
|
|
373
|
-
options.apiRoot
|
|
374
|
-
)
|
|
375
|
-
config.agent = options.agent
|
|
376
|
-
// @ts-expect-error AbortSignal shim is missing some props from Request.AbortSignal
|
|
377
|
-
config.signal = signal
|
|
378
|
-
config.timeout = 500_000 // ms
|
|
379
|
-
const res = await fetch(apiUrl, config).catch(redactToken)
|
|
380
|
-
if (res.status >= 500) {
|
|
381
|
-
const errorPayload = {
|
|
382
|
-
error_code: res.status,
|
|
383
|
-
description: res.statusText,
|
|
384
|
-
}
|
|
385
|
-
throw new TelegramError(errorPayload, { method, payload })
|
|
386
|
-
}
|
|
387
|
-
const data = await res.json()
|
|
388
|
-
if (!data.ok) {
|
|
389
|
-
debug('API call failed', data)
|
|
390
|
-
throw new TelegramError(data, { method, payload })
|
|
391
|
-
}
|
|
392
|
-
return data.result
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export default ApiClient
|
|
1
|
+
/* eslint @typescript-eslint/restrict-template-expressions: [ "error", { "allowNumber": true, "allowBoolean": true } ] */
|
|
2
|
+
import * as crypto from 'crypto'
|
|
3
|
+
import * as fs from 'fs'
|
|
4
|
+
import { stat, realpath } from 'fs/promises'
|
|
5
|
+
import * as http from 'http'
|
|
6
|
+
import * as https from 'https'
|
|
7
|
+
import * as path from 'path'
|
|
8
|
+
import fetch, { RequestInit } from 'node-fetch'
|
|
9
|
+
import { hasProp, hasPropType } from '../helpers/check'
|
|
10
|
+
import { InputFile, Opts, Telegram } from '../types/typegram'
|
|
11
|
+
import { AbortSignal } from 'abort-controller'
|
|
12
|
+
import { compactOptions } from '../helpers/compact'
|
|
13
|
+
import MultipartStream from './multipart-stream'
|
|
14
|
+
import TelegramError from './error'
|
|
15
|
+
import { URL } from 'url'
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
17
|
+
const debug = require('debug')('telegraf:client')
|
|
18
|
+
const { isStream } = MultipartStream
|
|
19
|
+
|
|
20
|
+
const WEBHOOK_REPLY_METHOD_ALLOWLIST = new Set<keyof Telegram>([
|
|
21
|
+
'answerCallbackQuery',
|
|
22
|
+
'answerInlineQuery',
|
|
23
|
+
'deleteMessage',
|
|
24
|
+
'leaveChat',
|
|
25
|
+
'sendChatAction',
|
|
26
|
+
])
|
|
27
|
+
|
|
28
|
+
namespace ApiClient {
|
|
29
|
+
export type Agent = http.Agent | ((parsedUrl: URL) => http.Agent) | undefined
|
|
30
|
+
export interface Options {
|
|
31
|
+
/**
|
|
32
|
+
* Agent for communicating with the bot API.
|
|
33
|
+
*/
|
|
34
|
+
agent?: http.Agent
|
|
35
|
+
/**
|
|
36
|
+
* Agent for attaching files via URL.
|
|
37
|
+
* 1. Not all agents support both `http:` and `https:`.
|
|
38
|
+
* 2. When passing a function, create the agents once, outside of the function.
|
|
39
|
+
* Creating new agent every request probably breaks `keepAlive`.
|
|
40
|
+
*/
|
|
41
|
+
attachmentAgent?: Agent
|
|
42
|
+
apiRoot: string
|
|
43
|
+
/**
|
|
44
|
+
* @default 'bot'
|
|
45
|
+
* @see https://github.com/tdlight-team/tdlight-telegram-bot-api#user-mode
|
|
46
|
+
*/
|
|
47
|
+
apiMode: 'bot' | 'user'
|
|
48
|
+
webhookReply: boolean
|
|
49
|
+
testEnv: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CallApiOptions {
|
|
53
|
+
signal?: AbortSignal
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const DEFAULT_EXTENSIONS: Record<string, string | undefined> = {
|
|
58
|
+
audio: 'mp3',
|
|
59
|
+
photo: 'jpg',
|
|
60
|
+
sticker: 'webp',
|
|
61
|
+
video: 'mp4',
|
|
62
|
+
animation: 'mp4',
|
|
63
|
+
video_note: 'mp4',
|
|
64
|
+
voice: 'ogg',
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DEFAULT_OPTIONS: ApiClient.Options = {
|
|
68
|
+
apiRoot: 'https://api.telegram.org',
|
|
69
|
+
apiMode: 'bot',
|
|
70
|
+
webhookReply: true,
|
|
71
|
+
agent: new https.Agent({
|
|
72
|
+
keepAlive: true,
|
|
73
|
+
keepAliveMsecs: 10000,
|
|
74
|
+
}),
|
|
75
|
+
attachmentAgent: undefined,
|
|
76
|
+
testEnv: false,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function includesMedia(payload: Record<string, unknown>) {
|
|
80
|
+
return Object.entries(payload).some(([key, value]) => {
|
|
81
|
+
if (key === 'link_preview_options') return false
|
|
82
|
+
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
return value.some(
|
|
85
|
+
({ media }) =>
|
|
86
|
+
media && typeof media === 'object' && (media.source || media.url)
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
return (
|
|
90
|
+
value &&
|
|
91
|
+
typeof value === 'object' &&
|
|
92
|
+
((hasProp(value, 'source') && value.source) ||
|
|
93
|
+
(hasProp(value, 'url') && value.url) ||
|
|
94
|
+
(hasPropType(value, 'media', 'object') &&
|
|
95
|
+
((hasProp(value.media, 'source') && value.media.source) ||
|
|
96
|
+
(hasProp(value.media, 'url') && value.media.url))))
|
|
97
|
+
)
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function replacer(_: unknown, value: unknown) {
|
|
102
|
+
if (value == null) return undefined
|
|
103
|
+
return value
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function buildJSONConfig(payload: unknown): Promise<RequestInit> {
|
|
107
|
+
return Promise.resolve({
|
|
108
|
+
method: 'POST',
|
|
109
|
+
compress: true,
|
|
110
|
+
headers: { 'content-type': 'application/json', connection: 'keep-alive' },
|
|
111
|
+
body: JSON.stringify(payload, replacer),
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const FORM_DATA_JSON_FIELDS = [
|
|
116
|
+
'results',
|
|
117
|
+
'reply_markup',
|
|
118
|
+
'mask_position',
|
|
119
|
+
'shipping_options',
|
|
120
|
+
'errors',
|
|
121
|
+
] as const
|
|
122
|
+
|
|
123
|
+
async function buildFormDataConfig(
|
|
124
|
+
payload: Opts<keyof Telegram>,
|
|
125
|
+
agent: ApiClient.Agent
|
|
126
|
+
) {
|
|
127
|
+
for (const field of FORM_DATA_JSON_FIELDS) {
|
|
128
|
+
if (hasProp(payload, field) && typeof payload[field] !== 'string') {
|
|
129
|
+
payload[field] = JSON.stringify(payload[field])
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const boundary = crypto.randomBytes(32).toString('hex')
|
|
133
|
+
const formData = new MultipartStream(boundary)
|
|
134
|
+
await Promise.all(
|
|
135
|
+
Object.keys(payload).map((key) =>
|
|
136
|
+
// @ts-expect-error payload[key] can obviously index payload, but TS doesn't trust us
|
|
137
|
+
attachFormValue(formData, key, payload[key], agent)
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
return {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
compress: true,
|
|
143
|
+
headers: {
|
|
144
|
+
'content-type': `multipart/form-data; boundary=${boundary}`,
|
|
145
|
+
connection: 'keep-alive',
|
|
146
|
+
},
|
|
147
|
+
body: formData,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function attachFormValue(
|
|
152
|
+
form: MultipartStream,
|
|
153
|
+
id: string,
|
|
154
|
+
value: unknown,
|
|
155
|
+
agent: ApiClient.Agent
|
|
156
|
+
) {
|
|
157
|
+
if (value == null) {
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
if (
|
|
161
|
+
typeof value === 'string' ||
|
|
162
|
+
typeof value === 'boolean' ||
|
|
163
|
+
typeof value === 'number'
|
|
164
|
+
) {
|
|
165
|
+
form.addPart({
|
|
166
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
167
|
+
body: `${value}`,
|
|
168
|
+
})
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
if (id === 'thumb' || id === 'thumbnail') {
|
|
172
|
+
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
173
|
+
await attachFormMedia(form, value as InputFile, attachmentId, agent)
|
|
174
|
+
return form.addPart({
|
|
175
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
176
|
+
body: `attach://${attachmentId}`,
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
if (Array.isArray(value)) {
|
|
180
|
+
const items = await Promise.all(
|
|
181
|
+
value.map(async (item) => {
|
|
182
|
+
if (typeof item.media !== 'object') {
|
|
183
|
+
return await Promise.resolve(item)
|
|
184
|
+
}
|
|
185
|
+
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
186
|
+
await attachFormMedia(form, item.media, attachmentId, agent)
|
|
187
|
+
const thumb = item.thumb ?? item.thumbnail
|
|
188
|
+
if (typeof thumb === 'object') {
|
|
189
|
+
const thumbAttachmentId = crypto.randomBytes(16).toString('hex')
|
|
190
|
+
await attachFormMedia(form, thumb, thumbAttachmentId, agent)
|
|
191
|
+
return {
|
|
192
|
+
...item,
|
|
193
|
+
media: `attach://${attachmentId}`,
|
|
194
|
+
thumbnail: `attach://${thumbAttachmentId}`,
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { ...item, media: `attach://${attachmentId}` }
|
|
198
|
+
})
|
|
199
|
+
)
|
|
200
|
+
return form.addPart({
|
|
201
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
202
|
+
body: JSON.stringify(items),
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
if (
|
|
206
|
+
value &&
|
|
207
|
+
typeof value === 'object' &&
|
|
208
|
+
hasProp(value, 'media') &&
|
|
209
|
+
hasProp(value, 'type') &&
|
|
210
|
+
typeof value.media !== 'undefined' &&
|
|
211
|
+
typeof value.type !== 'undefined'
|
|
212
|
+
) {
|
|
213
|
+
const attachmentId = crypto.randomBytes(16).toString('hex')
|
|
214
|
+
await attachFormMedia(form, value.media as InputFile, attachmentId, agent)
|
|
215
|
+
return form.addPart({
|
|
216
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
217
|
+
body: JSON.stringify({
|
|
218
|
+
...value,
|
|
219
|
+
media: `attach://${attachmentId}`,
|
|
220
|
+
}),
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
return await attachFormMedia(form, value as InputFile, id, agent)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function attachFormMedia(
|
|
227
|
+
form: MultipartStream,
|
|
228
|
+
media: InputFile,
|
|
229
|
+
id: string,
|
|
230
|
+
agent: ApiClient.Agent
|
|
231
|
+
) {
|
|
232
|
+
let fileName = media.filename ?? `${id}.${DEFAULT_EXTENSIONS[id] ?? 'dat'}`
|
|
233
|
+
if ('url' in media && media.url !== undefined) {
|
|
234
|
+
const timeout = 500_000 // ms
|
|
235
|
+
const res = await fetch(media.url, { agent, timeout })
|
|
236
|
+
return form.addPart({
|
|
237
|
+
headers: {
|
|
238
|
+
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
239
|
+
},
|
|
240
|
+
body: res.body,
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
if ('source' in media && media.source) {
|
|
244
|
+
let mediaSource = media.source
|
|
245
|
+
if (typeof media.source === 'string') {
|
|
246
|
+
const source = await realpath(media.source)
|
|
247
|
+
if ((await stat(source)).isFile()) {
|
|
248
|
+
fileName = media.filename ?? path.basename(media.source)
|
|
249
|
+
mediaSource = await fs.createReadStream(media.source)
|
|
250
|
+
} else {
|
|
251
|
+
throw new TypeError(`Unable to upload '${media.source}', not a file`)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (isStream(mediaSource) || Buffer.isBuffer(mediaSource)) {
|
|
255
|
+
form.addPart({
|
|
256
|
+
headers: {
|
|
257
|
+
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
258
|
+
},
|
|
259
|
+
body: mediaSource,
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function answerToWebhook(
|
|
266
|
+
response: Response,
|
|
267
|
+
payload: Opts<keyof Telegram>,
|
|
268
|
+
options: ApiClient.Options
|
|
269
|
+
): Promise<true> {
|
|
270
|
+
if (!includesMedia(payload)) {
|
|
271
|
+
if (!response.headersSent) {
|
|
272
|
+
response.setHeader('content-type', 'application/json')
|
|
273
|
+
}
|
|
274
|
+
response.end(JSON.stringify(payload), 'utf-8')
|
|
275
|
+
return true
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const { headers, body } = await buildFormDataConfig(
|
|
279
|
+
payload,
|
|
280
|
+
options.attachmentAgent
|
|
281
|
+
)
|
|
282
|
+
if (!response.headersSent) {
|
|
283
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
284
|
+
response.setHeader(key, value)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
await new Promise((resolve) => {
|
|
288
|
+
response.on('finish', resolve)
|
|
289
|
+
body.pipe(response)
|
|
290
|
+
})
|
|
291
|
+
return true
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function redactToken(error: Error): never {
|
|
295
|
+
error.message = error.message.replace(
|
|
296
|
+
/\/(bot|user)(\d+):[^/]+\//,
|
|
297
|
+
'/$1$2:[REDACTED]/'
|
|
298
|
+
)
|
|
299
|
+
throw error
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
type Response = http.ServerResponse
|
|
303
|
+
class ApiClient {
|
|
304
|
+
readonly options: ApiClient.Options
|
|
305
|
+
|
|
306
|
+
constructor(
|
|
307
|
+
readonly token: string,
|
|
308
|
+
options?: Partial<ApiClient.Options>,
|
|
309
|
+
private readonly response?: Response
|
|
310
|
+
) {
|
|
311
|
+
this.options = {
|
|
312
|
+
...DEFAULT_OPTIONS,
|
|
313
|
+
...compactOptions(options),
|
|
314
|
+
}
|
|
315
|
+
if (this.options.apiRoot.startsWith('http://')) {
|
|
316
|
+
this.options.agent = undefined
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* If set to `true`, first _eligible_ call will avoid performing a POST request.
|
|
322
|
+
* Note that such a call:
|
|
323
|
+
* 1. cannot report errors or return meaningful values,
|
|
324
|
+
* 2. resolves before bot API has a chance to process it,
|
|
325
|
+
* 3. prematurely confirms the update as processed.
|
|
326
|
+
*
|
|
327
|
+
* https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates
|
|
328
|
+
* https://github.com/telegraf/telegraf/pull/1250
|
|
329
|
+
*/
|
|
330
|
+
set webhookReply(enable: boolean) {
|
|
331
|
+
this.options.webhookReply = enable
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
get webhookReply() {
|
|
335
|
+
return this.options.webhookReply
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async callApi<M extends keyof Telegram>(
|
|
339
|
+
method: M,
|
|
340
|
+
payload: Opts<M>,
|
|
341
|
+
{ signal }: ApiClient.CallApiOptions = {}
|
|
342
|
+
): Promise<ReturnType<Telegram[M]>> {
|
|
343
|
+
const { token, options, response } = this
|
|
344
|
+
|
|
345
|
+
if (
|
|
346
|
+
options.webhookReply &&
|
|
347
|
+
response?.writableEnded === false &&
|
|
348
|
+
WEBHOOK_REPLY_METHOD_ALLOWLIST.has(method)
|
|
349
|
+
) {
|
|
350
|
+
debug('Call via webhook', method, payload)
|
|
351
|
+
// @ts-expect-error using webhookReply is an optimisation that doesn't respond with normal result
|
|
352
|
+
// up to the user to deal with this
|
|
353
|
+
return await answerToWebhook(response, { method, ...payload }, options)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (!token) {
|
|
357
|
+
throw new TelegramError({
|
|
358
|
+
error_code: 401,
|
|
359
|
+
description: 'Bot Token is required',
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
debug('HTTP call', method, payload)
|
|
364
|
+
|
|
365
|
+
const config: RequestInit = includesMedia(payload)
|
|
366
|
+
? await buildFormDataConfig(
|
|
367
|
+
{ method, ...payload },
|
|
368
|
+
options.attachmentAgent
|
|
369
|
+
)
|
|
370
|
+
: await buildJSONConfig(payload)
|
|
371
|
+
const apiUrl = new URL(
|
|
372
|
+
`./${options.apiMode}${token}${options.testEnv ? '/test' : ''}/${method}`,
|
|
373
|
+
options.apiRoot
|
|
374
|
+
)
|
|
375
|
+
config.agent = options.agent
|
|
376
|
+
// @ts-expect-error AbortSignal shim is missing some props from Request.AbortSignal
|
|
377
|
+
config.signal = signal
|
|
378
|
+
config.timeout = 500_000 // ms
|
|
379
|
+
const res = await fetch(apiUrl, config).catch(redactToken)
|
|
380
|
+
if (res.status >= 500) {
|
|
381
|
+
const errorPayload = {
|
|
382
|
+
error_code: res.status,
|
|
383
|
+
description: res.statusText,
|
|
384
|
+
}
|
|
385
|
+
throw new TelegramError(errorPayload, { method, payload })
|
|
386
|
+
}
|
|
387
|
+
const data = await res.json()
|
|
388
|
+
if (!data.ok) {
|
|
389
|
+
debug('API call failed', data)
|
|
390
|
+
throw new TelegramError(data, { method, payload })
|
|
391
|
+
}
|
|
392
|
+
return data.result
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export default ApiClient
|