@tgify/tgify 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 +23 -0
- package/README.md +356 -0
- package/filters.d.ts +1 -0
- package/filters.js +1 -0
- package/format.d.ts +1 -0
- package/format.js +1 -0
- package/future.d.ts +1 -0
- package/future.js +1 -0
- package/lib/button.js +100 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +582 -0
- package/lib/context.js +1219 -0
- package/lib/core/helpers/args.js +57 -0
- package/lib/core/helpers/check.js +55 -0
- package/lib/core/helpers/compact.js +16 -0
- package/lib/core/helpers/deunionize.js +12 -0
- package/lib/core/helpers/formatting.js +91 -0
- package/lib/core/helpers/util.js +50 -0
- package/lib/core/network/client.js +330 -0
- package/lib/core/network/error.js +21 -0
- package/lib/core/network/multipart-stream.js +71 -0
- package/lib/core/network/polling.js +87 -0
- package/lib/core/network/webhook.js +54 -0
- package/lib/core/types/typegram.js +27 -0
- package/lib/filters.js +69 -0
- package/lib/format.js +38 -0
- package/lib/future.js +149 -0
- package/lib/index.js +58 -0
- package/lib/input.js +61 -0
- package/lib/markup.js +121 -0
- package/lib/middleware.js +2 -0
- package/lib/reactions.js +84 -0
- package/lib/router.js +46 -0
- package/lib/scenes/base.js +39 -0
- package/lib/scenes/context.js +104 -0
- package/lib/scenes/index.js +21 -0
- package/lib/scenes/stage.js +49 -0
- package/lib/scenes/wizard/context.js +31 -0
- package/lib/scenes/wizard/index.js +45 -0
- package/lib/scenes.js +17 -0
- package/lib/session.js +166 -0
- package/lib/telegraf.js +256 -0
- package/lib/telegram-types.js +6 -0
- package/lib/telegram.js +1240 -0
- package/lib/types.js +2 -0
- package/lib/utils.js +5 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +140 -0
- package/scenes.d.ts +1 -0
- package/scenes.js +1 -0
- package/session.d.ts +1 -0
- package/session.js +1 -0
- package/src/button.ts +182 -0
- package/src/composer.ts +1008 -0
- package/src/context.ts +1661 -0
- package/src/core/helpers/args.ts +63 -0
- package/src/core/helpers/check.ts +71 -0
- package/src/core/helpers/compact.ts +18 -0
- package/src/core/helpers/deunionize.ts +26 -0
- package/src/core/helpers/formatting.ts +119 -0
- package/src/core/helpers/util.ts +96 -0
- package/src/core/network/client.ts +396 -0
- package/src/core/network/error.ts +29 -0
- package/src/core/network/multipart-stream.ts +45 -0
- package/src/core/network/polling.ts +94 -0
- package/src/core/network/webhook.ts +58 -0
- package/src/core/types/typegram.ts +54 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +213 -0
- package/src/index.ts +17 -0
- package/src/input.ts +59 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/reactions.ts +118 -0
- package/src/router.ts +55 -0
- package/src/scenes/base.ts +52 -0
- package/src/scenes/context.ts +136 -0
- package/src/scenes/index.ts +21 -0
- package/src/scenes/stage.ts +71 -0
- package/src/scenes/wizard/context.ts +58 -0
- package/src/scenes/wizard/index.ts +63 -0
- package/src/scenes.ts +1 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +354 -0
- package/src/telegram-types.ts +219 -0
- package/src/telegram.ts +1635 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +36 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +227 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +655 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/args.d.ts +11 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +56 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +4 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +18 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +30 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/util.d.ts +26 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +53 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +16 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +16 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +16 -0
- package/typings/core/network/polling.d.ts.map +1 -0
- package/typings/core/network/webhook.d.ts +6 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +42 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +18 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +22 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +12 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +15 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +50 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +27 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +8 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/reactions.d.ts +32 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +21 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +22 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +36 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/index.d.ts +11 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +24 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +29 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +16 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +2 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +55 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +115 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +117 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +675 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +3 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +2 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
|
@@ -0,0 +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
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ResponseParameters } from '../types/typegram'
|
|
2
|
+
|
|
3
|
+
interface ErrorPayload {
|
|
4
|
+
error_code: number
|
|
5
|
+
description: string
|
|
6
|
+
parameters?: ResponseParameters
|
|
7
|
+
}
|
|
8
|
+
export class TelegramError extends Error {
|
|
9
|
+
constructor(
|
|
10
|
+
readonly response: ErrorPayload,
|
|
11
|
+
readonly on = {}
|
|
12
|
+
) {
|
|
13
|
+
super(`${response.error_code}: ${response.description}`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get code() {
|
|
17
|
+
return this.response.error_code
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get description() {
|
|
21
|
+
return this.response.description
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get parameters() {
|
|
25
|
+
return this.response.parameters
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default TelegramError
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as stream from 'stream'
|
|
2
|
+
import { hasPropType } from '../helpers/check'
|
|
3
|
+
import SandwichStream from 'sandwich-stream'
|
|
4
|
+
const CRNL = '\r\n'
|
|
5
|
+
|
|
6
|
+
interface Part {
|
|
7
|
+
headers: { [key: string]: string }
|
|
8
|
+
body: NodeJS.ReadStream | NodeJS.ReadableStream | Buffer | string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class MultipartStream extends SandwichStream {
|
|
12
|
+
constructor(boundary: string) {
|
|
13
|
+
super({
|
|
14
|
+
head: `--${boundary}${CRNL}`,
|
|
15
|
+
tail: `${CRNL}--${boundary}--`,
|
|
16
|
+
separator: `${CRNL}--${boundary}${CRNL}`,
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
addPart(part: Part) {
|
|
21
|
+
const partStream = new stream.PassThrough()
|
|
22
|
+
for (const [key, header] of Object.entries(part.headers)) {
|
|
23
|
+
partStream.write(`${key}:${header}${CRNL}`)
|
|
24
|
+
}
|
|
25
|
+
partStream.write(CRNL)
|
|
26
|
+
if (MultipartStream.isStream(part.body)) {
|
|
27
|
+
part.body.pipe(partStream)
|
|
28
|
+
} else {
|
|
29
|
+
partStream.end(part.body)
|
|
30
|
+
}
|
|
31
|
+
this.add(partStream)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static isStream(
|
|
35
|
+
stream: unknown
|
|
36
|
+
): stream is { pipe: MultipartStream['pipe'] } {
|
|
37
|
+
return (
|
|
38
|
+
typeof stream === 'object' &&
|
|
39
|
+
stream !== null &&
|
|
40
|
+
hasPropType(stream, 'pipe', 'function')
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default MultipartStream
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as tg from '../types/typegram'
|
|
2
|
+
import * as tt from '../../telegram-types'
|
|
3
|
+
import AbortController from 'abort-controller'
|
|
4
|
+
import ApiClient from './client'
|
|
5
|
+
import d from 'debug'
|
|
6
|
+
import { promisify } from 'util'
|
|
7
|
+
import { TelegramError } from './error'
|
|
8
|
+
const debug = d('telegraf:polling')
|
|
9
|
+
const wait = promisify(setTimeout)
|
|
10
|
+
function always<T>(x: T) {
|
|
11
|
+
return () => x
|
|
12
|
+
}
|
|
13
|
+
const noop = always(Promise.resolve())
|
|
14
|
+
|
|
15
|
+
export class Polling {
|
|
16
|
+
private readonly abortController = new AbortController()
|
|
17
|
+
private skipOffsetSync = false
|
|
18
|
+
private offset = 0
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly telegram: ApiClient,
|
|
21
|
+
private readonly allowedUpdates: readonly tt.UpdateType[]
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
private async *[Symbol.asyncIterator]() {
|
|
25
|
+
debug('Starting long polling')
|
|
26
|
+
do {
|
|
27
|
+
try {
|
|
28
|
+
const updates = await this.telegram.callApi(
|
|
29
|
+
'getUpdates',
|
|
30
|
+
{
|
|
31
|
+
timeout: 50,
|
|
32
|
+
offset: this.offset,
|
|
33
|
+
allowed_updates: this.allowedUpdates,
|
|
34
|
+
},
|
|
35
|
+
this.abortController
|
|
36
|
+
)
|
|
37
|
+
const last = updates[updates.length - 1]
|
|
38
|
+
if (last !== undefined) {
|
|
39
|
+
this.offset = last.update_id + 1
|
|
40
|
+
}
|
|
41
|
+
yield updates
|
|
42
|
+
} catch (error) {
|
|
43
|
+
const err = error as Error & {
|
|
44
|
+
parameters?: { retry_after: number }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (err.name === 'AbortError') return
|
|
48
|
+
if (
|
|
49
|
+
err.name === 'FetchError' ||
|
|
50
|
+
(err instanceof TelegramError && err.code === 429) ||
|
|
51
|
+
(err instanceof TelegramError && err.code >= 500)
|
|
52
|
+
) {
|
|
53
|
+
const retryAfter: number = err.parameters?.retry_after ?? 5
|
|
54
|
+
debug('Failed to fetch updates, retrying after %ds.', retryAfter, err)
|
|
55
|
+
await wait(retryAfter * 1000)
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
if (
|
|
59
|
+
err instanceof TelegramError &&
|
|
60
|
+
// Unauthorized Conflict
|
|
61
|
+
(err.code === 401 || err.code === 409)
|
|
62
|
+
) {
|
|
63
|
+
this.skipOffsetSync = true
|
|
64
|
+
throw err
|
|
65
|
+
}
|
|
66
|
+
throw err
|
|
67
|
+
}
|
|
68
|
+
} while (!this.abortController.signal.aborted)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private async syncUpdateOffset() {
|
|
72
|
+
if (this.skipOffsetSync) return
|
|
73
|
+
debug('Syncing update offset...')
|
|
74
|
+
await this.telegram.callApi('getUpdates', { offset: this.offset, limit: 1 })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async loop(handleUpdate: (updates: tg.Update) => Promise<void>) {
|
|
78
|
+
if (this.abortController.signal.aborted)
|
|
79
|
+
throw new Error('Polling instances must not be reused!')
|
|
80
|
+
try {
|
|
81
|
+
for await (const updates of this)
|
|
82
|
+
await Promise.all(updates.map(handleUpdate))
|
|
83
|
+
} finally {
|
|
84
|
+
debug('Long polling stopped')
|
|
85
|
+
// prevent instance reuse
|
|
86
|
+
this.stop()
|
|
87
|
+
await this.syncUpdateOffset().catch(noop)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
stop() {
|
|
92
|
+
this.abortController.abort()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as http from 'http'
|
|
2
|
+
import d from 'debug'
|
|
3
|
+
import { type Update } from '../types/typegram'
|
|
4
|
+
const debug = d('telegraf:webhook')
|
|
5
|
+
|
|
6
|
+
export default function generateWebhook(
|
|
7
|
+
filter: (req: http.IncomingMessage) => boolean,
|
|
8
|
+
updateHandler: (update: Update, res: http.ServerResponse) => Promise<void>
|
|
9
|
+
) {
|
|
10
|
+
return async (
|
|
11
|
+
req: http.IncomingMessage & { body?: Update },
|
|
12
|
+
res: http.ServerResponse,
|
|
13
|
+
next = (): void => {
|
|
14
|
+
res.statusCode = 403
|
|
15
|
+
debug('Replying with status code', res.statusCode)
|
|
16
|
+
res.end()
|
|
17
|
+
}
|
|
18
|
+
): Promise<void> => {
|
|
19
|
+
debug('Incoming request', req.method, req.url)
|
|
20
|
+
|
|
21
|
+
if (!filter(req)) {
|
|
22
|
+
debug('Webhook filter failed', req.method, req.url)
|
|
23
|
+
return next()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let update: Update
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (req.body != null) {
|
|
30
|
+
/* If req.body is already set, we expect it to be the parsed
|
|
31
|
+
request body (update object) received from Telegram
|
|
32
|
+
However, some libraries such as `serverless-http` set req.body to the
|
|
33
|
+
raw buffer, so we'll handle that additionally */
|
|
34
|
+
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
let body: any = req.body
|
|
37
|
+
// if body is Buffer, parse it into string
|
|
38
|
+
if (body instanceof Buffer) body = String(req.body)
|
|
39
|
+
// if body is string, parse it into object
|
|
40
|
+
if (typeof body === 'string') body = JSON.parse(body)
|
|
41
|
+
update = body
|
|
42
|
+
} else {
|
|
43
|
+
let body = ''
|
|
44
|
+
// parse each buffer to string and append to body
|
|
45
|
+
for await (const chunk of req) body += String(chunk)
|
|
46
|
+
// parse body to object
|
|
47
|
+
update = JSON.parse(body)
|
|
48
|
+
}
|
|
49
|
+
} catch (error: unknown) {
|
|
50
|
+
// if any of the parsing steps fails, give up and respond with error
|
|
51
|
+
res.writeHead(415).end()
|
|
52
|
+
debug('Failed to parse request body:', error)
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return await updateHandler(update, res)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as Typegram from '@telegraf/types'
|
|
2
|
+
|
|
3
|
+
// internal type provisions
|
|
4
|
+
export * from '@telegraf/types/api'
|
|
5
|
+
export * from '@telegraf/types/inline'
|
|
6
|
+
export * from '@telegraf/types/manage'
|
|
7
|
+
export * from '@telegraf/types/markup'
|
|
8
|
+
export * from '@telegraf/types/message'
|
|
9
|
+
export * from '@telegraf/types/methods'
|
|
10
|
+
export * from '@telegraf/types/passport'
|
|
11
|
+
export * from '@telegraf/types/payment'
|
|
12
|
+
export * from '@telegraf/types/settings'
|
|
13
|
+
export * from '@telegraf/types/update'
|
|
14
|
+
|
|
15
|
+
// telegraf input file definition
|
|
16
|
+
interface InputFileByPath {
|
|
17
|
+
source: string
|
|
18
|
+
filename?: string
|
|
19
|
+
}
|
|
20
|
+
interface InputFileByReadableStream {
|
|
21
|
+
source: NodeJS.ReadableStream
|
|
22
|
+
filename?: string
|
|
23
|
+
}
|
|
24
|
+
interface InputFileByBuffer {
|
|
25
|
+
source: Buffer
|
|
26
|
+
filename?: string
|
|
27
|
+
}
|
|
28
|
+
interface InputFileByURL {
|
|
29
|
+
url: string
|
|
30
|
+
filename?: string
|
|
31
|
+
}
|
|
32
|
+
export type InputFile =
|
|
33
|
+
| InputFileByPath
|
|
34
|
+
| InputFileByReadableStream
|
|
35
|
+
| InputFileByBuffer
|
|
36
|
+
| InputFileByURL
|
|
37
|
+
|
|
38
|
+
export type Telegram = Typegram.ApiMethods<InputFile>
|
|
39
|
+
|
|
40
|
+
export type Opts<M extends keyof Telegram> = Typegram.Opts<InputFile>[M]
|
|
41
|
+
export type InputMedia = Typegram.InputMedia<InputFile>
|
|
42
|
+
export type InputMediaPhoto = Typegram.InputMediaPhoto<InputFile>
|
|
43
|
+
export type InputMediaVideo = Typegram.InputMediaVideo<InputFile>
|
|
44
|
+
export type InputMediaAnimation = Typegram.InputMediaAnimation<InputFile>
|
|
45
|
+
export type InputMediaAudio = Typegram.InputMediaAudio<InputFile>
|
|
46
|
+
export type InputMediaDocument = Typegram.InputMediaDocument<InputFile>
|
|
47
|
+
|
|
48
|
+
// tiny helper types
|
|
49
|
+
export type ChatAction = Opts<'sendChatAction'>['action']
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sending video notes by a URL is currently unsupported
|
|
53
|
+
*/
|
|
54
|
+
export type InputFileVideoNote = Exclude<InputFile, InputFileByURL>
|