teledzik 1.0.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 +22 -0
- package/README.md +1126 -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 +101 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +581 -0
- package/lib/context.js +1300 -0
- package/lib/core/helpers/ai-stream.js +69 -0
- package/lib/core/helpers/args.js +58 -0
- package/lib/core/helpers/check.js +56 -0
- package/lib/core/helpers/compact.js +17 -0
- package/lib/core/helpers/deunionize.js +13 -0
- package/lib/core/helpers/formatting.js +89 -0
- package/lib/core/helpers/input-box.js +87 -0
- package/lib/core/helpers/rich-sanitizer.js +31 -0
- package/lib/core/helpers/smart-queue.js +70 -0
- package/lib/core/helpers/util.js +50 -0
- package/lib/core/network/client.js +318 -0
- package/lib/core/network/error.js +21 -0
- package/lib/core/network/multipart-stream.js +61 -0
- package/lib/core/network/polling.js +86 -0
- package/lib/core/network/webhook.js +54 -0
- package/lib/core/types/rich-message.js +287 -0
- package/lib/core/types/typegram.js +18 -0
- package/lib/filters.js +69 -0
- package/lib/format.js +38 -0
- package/lib/future.js +166 -0
- package/lib/index.js +61 -0
- package/lib/input.js +61 -0
- package/lib/markup.js +111 -0
- package/lib/middleware.js +2 -0
- package/lib/paginated.js +76 -0
- package/lib/reactions.js +81 -0
- package/lib/router.js +45 -0
- package/lib/scenes/base.js +39 -0
- package/lib/scenes/context.js +99 -0
- package/lib/scenes/form-scene.js +23 -0
- package/lib/scenes/index.js +21 -0
- package/lib/scenes/stage.js +49 -0
- package/lib/scenes/wizard/context.js +30 -0
- package/lib/scenes/wizard/index.js +45 -0
- package/lib/scenes.js +17 -0
- package/lib/session.js +163 -0
- package/lib/telegraf.js +274 -0
- package/lib/telegram-types.js +6 -0
- package/lib/telegram.js +1286 -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 +141 -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 +1778 -0
- package/src/core/helpers/ai-stream.ts +96 -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/input-box.ts +86 -0
- package/src/core/helpers/rich-sanitizer.ts +36 -0
- package/src/core/helpers/smart-queue.ts +89 -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/rich-message.ts +375 -0
- package/src/core/types/typegram.ts +44 -0
- package/src/filters.js +69 -0
- package/src/filters.ts +109 -0
- package/src/format.js +38 -0
- package/src/format.ts +110 -0
- package/src/future.js +147 -0
- package/src/future.ts +231 -0
- package/src/index.ts +27 -0
- package/src/input.ts +59 -0
- package/src/markup.js +93 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/paginated.ts +75 -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/form-scene.ts +34 -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.js +17 -0
- package/src/scenes.ts +1 -0
- package/src/session.js +175 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +385 -0
- package/src/telegram-types.ts +256 -0
- package/src/telegram.ts +1700 -0
- package/src/types.js +2 -0
- package/src/types.ts +2 -0
- package/src/utils.js +5 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +35 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +226 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +711 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/ai-stream.d.ts +33 -0
- package/typings/core/helpers/args.d.ts +10 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +55 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +3 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +17 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +29 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/input-box.d.ts +32 -0
- package/typings/core/helpers/rich-sanitizer.d.ts +5 -0
- package/typings/core/helpers/smart-queue.d.ts +18 -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 +54 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +15 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +19 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +15 -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/rich-message.d.ts +143 -0
- package/typings/core/types/rich-message.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +36 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +17 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +21 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +11 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +21 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +58 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +26 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +7 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/paginated.d.ts +20 -0
- package/typings/reactions.d.ts +31 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +20 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +21 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +35 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/form-scene.d.ts +17 -0
- package/typings/scenes/index.d.ts +10 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +23 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +28 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +15 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +1 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +54 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +118 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +140 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +697 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +2 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +1 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
/* eslint @typescript-eslint/restrict-template-expressions: [ "error", { "allowNumber": true, "allowBoolean": true } ] */
|
|
30
|
+
const crypto = __importStar(require("crypto"));
|
|
31
|
+
const fs = __importStar(require("fs"));
|
|
32
|
+
const promises_1 = require("fs/promises");
|
|
33
|
+
const https = __importStar(require("https"));
|
|
34
|
+
const path = __importStar(require("path"));
|
|
35
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
36
|
+
const check_1 = require("../helpers/check");
|
|
37
|
+
const compact_1 = require("../helpers/compact");
|
|
38
|
+
const multipart_stream_1 = __importDefault(require("./multipart-stream"));
|
|
39
|
+
const error_1 = __importDefault(require("./error"));
|
|
40
|
+
const url_1 = require("url");
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
42
|
+
const debug = require('debug')('telegraf:client');
|
|
43
|
+
const { isStream } = multipart_stream_1.default;
|
|
44
|
+
const WEBHOOK_REPLY_METHOD_ALLOWLIST = new Set([
|
|
45
|
+
'answerCallbackQuery',
|
|
46
|
+
'answerInlineQuery',
|
|
47
|
+
'deleteMessage',
|
|
48
|
+
'leaveChat',
|
|
49
|
+
'sendChatAction',
|
|
50
|
+
]);
|
|
51
|
+
const DEFAULT_EXTENSIONS = {
|
|
52
|
+
audio: 'mp3',
|
|
53
|
+
photo: 'jpg',
|
|
54
|
+
sticker: 'webp',
|
|
55
|
+
video: 'mp4',
|
|
56
|
+
animation: 'mp4',
|
|
57
|
+
video_note: 'mp4',
|
|
58
|
+
voice: 'ogg',
|
|
59
|
+
};
|
|
60
|
+
const DEFAULT_OPTIONS = {
|
|
61
|
+
apiRoot: 'https://api.telegram.org',
|
|
62
|
+
apiMode: 'bot',
|
|
63
|
+
webhookReply: true,
|
|
64
|
+
agent: new https.Agent({
|
|
65
|
+
keepAlive: true,
|
|
66
|
+
keepAliveMsecs: 10000,
|
|
67
|
+
}),
|
|
68
|
+
attachmentAgent: undefined,
|
|
69
|
+
testEnv: false,
|
|
70
|
+
};
|
|
71
|
+
function includesMedia(payload) {
|
|
72
|
+
return Object.entries(payload).some(([key, value]) => {
|
|
73
|
+
if (key === 'link_preview_options')
|
|
74
|
+
return false;
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
return value.some(({ media }) => media && typeof media === 'object' && (media.source || media.url));
|
|
77
|
+
}
|
|
78
|
+
return (value &&
|
|
79
|
+
typeof value === 'object' &&
|
|
80
|
+
(((0, check_1.hasProp)(value, 'source') && value.source) ||
|
|
81
|
+
((0, check_1.hasProp)(value, 'url') && value.url) ||
|
|
82
|
+
((0, check_1.hasPropType)(value, 'media', 'object') &&
|
|
83
|
+
(((0, check_1.hasProp)(value.media, 'source') && value.media.source) ||
|
|
84
|
+
((0, check_1.hasProp)(value.media, 'url') && value.media.url)))));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function replacer(_, value) {
|
|
88
|
+
if (value == null)
|
|
89
|
+
return undefined;
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
function buildJSONConfig(payload) {
|
|
93
|
+
return Promise.resolve({
|
|
94
|
+
method: 'POST',
|
|
95
|
+
compress: true,
|
|
96
|
+
headers: { 'content-type': 'application/json', connection: 'keep-alive' },
|
|
97
|
+
body: JSON.stringify(payload, replacer),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const FORM_DATA_JSON_FIELDS = [
|
|
101
|
+
'results',
|
|
102
|
+
'reply_markup',
|
|
103
|
+
'mask_position',
|
|
104
|
+
'shipping_options',
|
|
105
|
+
'errors',
|
|
106
|
+
];
|
|
107
|
+
async function buildFormDataConfig(payload, agent) {
|
|
108
|
+
for (const field of FORM_DATA_JSON_FIELDS) {
|
|
109
|
+
if ((0, check_1.hasProp)(payload, field) && typeof payload[field] !== 'string') {
|
|
110
|
+
payload[field] = JSON.stringify(payload[field]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const boundary = crypto.randomBytes(32).toString('hex');
|
|
114
|
+
const formData = new multipart_stream_1.default(boundary);
|
|
115
|
+
await Promise.all(Object.keys(payload).map((key) =>
|
|
116
|
+
// @ts-expect-error payload[key] can obviously index payload, but TS doesn't trust us
|
|
117
|
+
attachFormValue(formData, key, payload[key], agent)));
|
|
118
|
+
return {
|
|
119
|
+
method: 'POST',
|
|
120
|
+
compress: true,
|
|
121
|
+
headers: {
|
|
122
|
+
'content-type': `multipart/form-data; boundary=${boundary}`,
|
|
123
|
+
connection: 'keep-alive',
|
|
124
|
+
},
|
|
125
|
+
body: formData,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function attachFormValue(form, id, value, agent) {
|
|
129
|
+
if (value == null) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (typeof value === 'string' ||
|
|
133
|
+
typeof value === 'boolean' ||
|
|
134
|
+
typeof value === 'number') {
|
|
135
|
+
form.addPart({
|
|
136
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
137
|
+
body: `${value}`,
|
|
138
|
+
});
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (id === 'thumb' || id === 'thumbnail') {
|
|
142
|
+
const attachmentId = crypto.randomBytes(16).toString('hex');
|
|
143
|
+
await attachFormMedia(form, value, attachmentId, agent);
|
|
144
|
+
return form.addPart({
|
|
145
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
146
|
+
body: `attach://${attachmentId}`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
const items = await Promise.all(value.map(async (item) => {
|
|
151
|
+
if (typeof item.media !== 'object') {
|
|
152
|
+
return await Promise.resolve(item);
|
|
153
|
+
}
|
|
154
|
+
const attachmentId = crypto.randomBytes(16).toString('hex');
|
|
155
|
+
await attachFormMedia(form, item.media, attachmentId, agent);
|
|
156
|
+
const thumb = item.thumb ?? item.thumbnail;
|
|
157
|
+
if (typeof thumb === 'object') {
|
|
158
|
+
const thumbAttachmentId = crypto.randomBytes(16).toString('hex');
|
|
159
|
+
await attachFormMedia(form, thumb, thumbAttachmentId, agent);
|
|
160
|
+
return {
|
|
161
|
+
...item,
|
|
162
|
+
media: `attach://${attachmentId}`,
|
|
163
|
+
thumbnail: `attach://${thumbAttachmentId}`,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return { ...item, media: `attach://${attachmentId}` };
|
|
167
|
+
}));
|
|
168
|
+
return form.addPart({
|
|
169
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
170
|
+
body: JSON.stringify(items),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (value &&
|
|
174
|
+
typeof value === 'object' &&
|
|
175
|
+
(0, check_1.hasProp)(value, 'media') &&
|
|
176
|
+
(0, check_1.hasProp)(value, 'type') &&
|
|
177
|
+
typeof value.media !== 'undefined' &&
|
|
178
|
+
typeof value.type !== 'undefined') {
|
|
179
|
+
const attachmentId = crypto.randomBytes(16).toString('hex');
|
|
180
|
+
await attachFormMedia(form, value.media, attachmentId, agent);
|
|
181
|
+
return form.addPart({
|
|
182
|
+
headers: { 'content-disposition': `form-data; name="${id}"` },
|
|
183
|
+
body: JSON.stringify({
|
|
184
|
+
...value,
|
|
185
|
+
media: `attach://${attachmentId}`,
|
|
186
|
+
}),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return await attachFormMedia(form, value, id, agent);
|
|
190
|
+
}
|
|
191
|
+
async function attachFormMedia(form, media, id, agent) {
|
|
192
|
+
let fileName = media.filename ?? `${id}.${DEFAULT_EXTENSIONS[id] ?? 'dat'}`;
|
|
193
|
+
if ('url' in media && media.url !== undefined) {
|
|
194
|
+
const timeout = 500_000; // ms
|
|
195
|
+
const res = await (0, node_fetch_1.default)(media.url, { agent, timeout });
|
|
196
|
+
return form.addPart({
|
|
197
|
+
headers: {
|
|
198
|
+
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
199
|
+
},
|
|
200
|
+
body: res.body,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if ('source' in media && media.source) {
|
|
204
|
+
let mediaSource = media.source;
|
|
205
|
+
if (typeof media.source === 'string') {
|
|
206
|
+
const source = await (0, promises_1.realpath)(media.source);
|
|
207
|
+
if ((await (0, promises_1.stat)(source)).isFile()) {
|
|
208
|
+
fileName = media.filename ?? path.basename(media.source);
|
|
209
|
+
mediaSource = await fs.createReadStream(media.source);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
throw new TypeError(`Unable to upload '${media.source}', not a file`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (isStream(mediaSource) || Buffer.isBuffer(mediaSource)) {
|
|
216
|
+
form.addPart({
|
|
217
|
+
headers: {
|
|
218
|
+
'content-disposition': `form-data; name="${id}"; filename="${fileName}"`,
|
|
219
|
+
},
|
|
220
|
+
body: mediaSource,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function answerToWebhook(response, payload, options) {
|
|
226
|
+
if (!includesMedia(payload)) {
|
|
227
|
+
if (!response.headersSent) {
|
|
228
|
+
response.setHeader('content-type', 'application/json');
|
|
229
|
+
}
|
|
230
|
+
response.end(JSON.stringify(payload), 'utf-8');
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
const { headers, body } = await buildFormDataConfig(payload, options.attachmentAgent);
|
|
234
|
+
if (!response.headersSent) {
|
|
235
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
236
|
+
response.setHeader(key, value);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
await new Promise((resolve) => {
|
|
240
|
+
response.on('finish', resolve);
|
|
241
|
+
body.pipe(response);
|
|
242
|
+
});
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
function redactToken(error) {
|
|
246
|
+
error.message = error.message.replace(/\/(bot|user)(\d+):[^/]+\//, '/$1$2:[REDACTED]/');
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
class ApiClient {
|
|
250
|
+
constructor(token, options, response) {
|
|
251
|
+
this.token = token;
|
|
252
|
+
this.response = response;
|
|
253
|
+
this.options = {
|
|
254
|
+
...DEFAULT_OPTIONS,
|
|
255
|
+
...(0, compact_1.compactOptions)(options),
|
|
256
|
+
};
|
|
257
|
+
if (this.options.apiRoot.startsWith('http://')) {
|
|
258
|
+
this.options.agent = undefined;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* If set to `true`, first _eligible_ call will avoid performing a POST request.
|
|
263
|
+
* Note that such a call:
|
|
264
|
+
* 1. cannot report errors or return meaningful values,
|
|
265
|
+
* 2. resolves before bot API has a chance to process it,
|
|
266
|
+
* 3. prematurely confirms the update as processed.
|
|
267
|
+
*
|
|
268
|
+
* https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates
|
|
269
|
+
* https://github.com/telegraf/telegraf/pull/1250
|
|
270
|
+
*/
|
|
271
|
+
set webhookReply(enable) {
|
|
272
|
+
this.options.webhookReply = enable;
|
|
273
|
+
}
|
|
274
|
+
get webhookReply() {
|
|
275
|
+
return this.options.webhookReply;
|
|
276
|
+
}
|
|
277
|
+
async callApi(method, payload, { signal } = {}) {
|
|
278
|
+
const { token, options, response } = this;
|
|
279
|
+
if (options.webhookReply &&
|
|
280
|
+
response?.writableEnded === false &&
|
|
281
|
+
WEBHOOK_REPLY_METHOD_ALLOWLIST.has(method)) {
|
|
282
|
+
debug('Call via webhook', method, payload);
|
|
283
|
+
// @ts-expect-error using webhookReply is an optimisation that doesn't respond with normal result
|
|
284
|
+
// up to the user to deal with this
|
|
285
|
+
return await answerToWebhook(response, { method, ...payload }, options);
|
|
286
|
+
}
|
|
287
|
+
if (!token) {
|
|
288
|
+
throw new error_1.default({
|
|
289
|
+
error_code: 401,
|
|
290
|
+
description: 'Bot Token is required',
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
debug('HTTP call', method, payload);
|
|
294
|
+
const config = includesMedia(payload)
|
|
295
|
+
? await buildFormDataConfig({ method, ...payload }, options.attachmentAgent)
|
|
296
|
+
: await buildJSONConfig(payload);
|
|
297
|
+
const apiUrl = new url_1.URL(`./${options.apiMode}${token}${options.testEnv ? '/test' : ''}/${method}`, options.apiRoot);
|
|
298
|
+
config.agent = options.agent;
|
|
299
|
+
// @ts-expect-error AbortSignal shim is missing some props from Request.AbortSignal
|
|
300
|
+
config.signal = signal;
|
|
301
|
+
config.timeout = 500_000; // ms
|
|
302
|
+
const res = await (0, node_fetch_1.default)(apiUrl, config).catch(redactToken);
|
|
303
|
+
if (res.status >= 500) {
|
|
304
|
+
const errorPayload = {
|
|
305
|
+
error_code: res.status,
|
|
306
|
+
description: res.statusText,
|
|
307
|
+
};
|
|
308
|
+
throw new error_1.default(errorPayload, { method, payload });
|
|
309
|
+
}
|
|
310
|
+
const data = await res.json();
|
|
311
|
+
if (!data.ok) {
|
|
312
|
+
debug('API call failed', data);
|
|
313
|
+
throw new error_1.default(data, { method, payload });
|
|
314
|
+
}
|
|
315
|
+
return data.result;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
exports.default = ApiClient;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TelegramError = void 0;
|
|
4
|
+
class TelegramError extends Error {
|
|
5
|
+
constructor(response, on = {}) {
|
|
6
|
+
super(`${response.error_code}: ${response.description}`);
|
|
7
|
+
this.response = response;
|
|
8
|
+
this.on = on;
|
|
9
|
+
}
|
|
10
|
+
get code() {
|
|
11
|
+
return this.response.error_code;
|
|
12
|
+
}
|
|
13
|
+
get description() {
|
|
14
|
+
return this.response.description;
|
|
15
|
+
}
|
|
16
|
+
get parameters() {
|
|
17
|
+
return this.response.parameters;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.TelegramError = TelegramError;
|
|
21
|
+
exports.default = TelegramError;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const stream = __importStar(require("stream"));
|
|
30
|
+
const check_1 = require("../helpers/check");
|
|
31
|
+
const sandwich_stream_1 = __importDefault(require("sandwich-stream"));
|
|
32
|
+
const CRNL = '\r\n';
|
|
33
|
+
class MultipartStream extends sandwich_stream_1.default {
|
|
34
|
+
constructor(boundary) {
|
|
35
|
+
super({
|
|
36
|
+
head: `--${boundary}${CRNL}`,
|
|
37
|
+
tail: `${CRNL}--${boundary}--`,
|
|
38
|
+
separator: `${CRNL}--${boundary}${CRNL}`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
addPart(part) {
|
|
42
|
+
const partStream = new stream.PassThrough();
|
|
43
|
+
for (const [key, header] of Object.entries(part.headers)) {
|
|
44
|
+
partStream.write(`${key}:${header}${CRNL}`);
|
|
45
|
+
}
|
|
46
|
+
partStream.write(CRNL);
|
|
47
|
+
if (MultipartStream.isStream(part.body)) {
|
|
48
|
+
part.body.pipe(partStream);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
partStream.end(part.body);
|
|
52
|
+
}
|
|
53
|
+
this.add(partStream);
|
|
54
|
+
}
|
|
55
|
+
static isStream(stream) {
|
|
56
|
+
return (typeof stream === 'object' &&
|
|
57
|
+
stream !== null &&
|
|
58
|
+
(0, check_1.hasPropType)(stream, 'pipe', 'function'));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.default = MultipartStream;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Polling = void 0;
|
|
7
|
+
const abort_controller_1 = __importDefault(require("abort-controller"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const util_1 = require("util");
|
|
10
|
+
const error_1 = require("./error");
|
|
11
|
+
const debug = (0, debug_1.default)('telegraf:polling');
|
|
12
|
+
const wait = (0, util_1.promisify)(setTimeout);
|
|
13
|
+
function always(x) {
|
|
14
|
+
return () => x;
|
|
15
|
+
}
|
|
16
|
+
const noop = always(Promise.resolve());
|
|
17
|
+
class Polling {
|
|
18
|
+
constructor(telegram, allowedUpdates) {
|
|
19
|
+
this.telegram = telegram;
|
|
20
|
+
this.allowedUpdates = allowedUpdates;
|
|
21
|
+
this.abortController = new abort_controller_1.default();
|
|
22
|
+
this.skipOffsetSync = false;
|
|
23
|
+
this.offset = 0;
|
|
24
|
+
}
|
|
25
|
+
async *[Symbol.asyncIterator]() {
|
|
26
|
+
debug('Starting long polling');
|
|
27
|
+
do {
|
|
28
|
+
try {
|
|
29
|
+
const updates = await this.telegram.callApi('getUpdates', {
|
|
30
|
+
timeout: 50,
|
|
31
|
+
offset: this.offset,
|
|
32
|
+
allowed_updates: this.allowedUpdates,
|
|
33
|
+
}, this.abortController);
|
|
34
|
+
const last = updates[updates.length - 1];
|
|
35
|
+
if (last !== undefined) {
|
|
36
|
+
this.offset = last.update_id + 1;
|
|
37
|
+
}
|
|
38
|
+
yield updates;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const err = error;
|
|
42
|
+
if (err.name === 'AbortError')
|
|
43
|
+
return;
|
|
44
|
+
if (err.name === 'FetchError' ||
|
|
45
|
+
(err instanceof error_1.TelegramError && err.code === 429) ||
|
|
46
|
+
(err instanceof error_1.TelegramError && err.code >= 500)) {
|
|
47
|
+
const retryAfter = err.parameters?.retry_after ?? 5;
|
|
48
|
+
debug('Failed to fetch updates, retrying after %ds.', retryAfter, err);
|
|
49
|
+
await wait(retryAfter * 1000);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (err instanceof error_1.TelegramError &&
|
|
53
|
+
// Unauthorized Conflict
|
|
54
|
+
(err.code === 401 || err.code === 409)) {
|
|
55
|
+
this.skipOffsetSync = true;
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
} while (!this.abortController.signal.aborted);
|
|
61
|
+
}
|
|
62
|
+
async syncUpdateOffset() {
|
|
63
|
+
if (this.skipOffsetSync)
|
|
64
|
+
return;
|
|
65
|
+
debug('Syncing update offset...');
|
|
66
|
+
await this.telegram.callApi('getUpdates', { offset: this.offset, limit: 1 });
|
|
67
|
+
}
|
|
68
|
+
async loop(handleUpdate) {
|
|
69
|
+
if (this.abortController.signal.aborted)
|
|
70
|
+
throw new Error('Polling instances must not be reused!');
|
|
71
|
+
try {
|
|
72
|
+
for await (const updates of this)
|
|
73
|
+
await Promise.all(updates.map(handleUpdate));
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
debug('Long polling stopped');
|
|
77
|
+
// prevent instance reuse
|
|
78
|
+
this.stop();
|
|
79
|
+
await this.syncUpdateOffset().catch(noop);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
stop() {
|
|
83
|
+
this.abortController.abort();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.Polling = Polling;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const debug_1 = __importDefault(require("debug"));
|
|
7
|
+
const debug = (0, debug_1.default)('telegraf:webhook');
|
|
8
|
+
function generateWebhook(filter, updateHandler) {
|
|
9
|
+
return async (req, res, next = () => {
|
|
10
|
+
res.statusCode = 403;
|
|
11
|
+
debug('Replying with status code', res.statusCode);
|
|
12
|
+
res.end();
|
|
13
|
+
}) => {
|
|
14
|
+
debug('Incoming request', req.method, req.url);
|
|
15
|
+
if (!filter(req)) {
|
|
16
|
+
debug('Webhook filter failed', req.method, req.url);
|
|
17
|
+
return next();
|
|
18
|
+
}
|
|
19
|
+
let update;
|
|
20
|
+
try {
|
|
21
|
+
if (req.body != null) {
|
|
22
|
+
/* If req.body is already set, we expect it to be the parsed
|
|
23
|
+
request body (update object) received from Telegram
|
|
24
|
+
However, some libraries such as `serverless-http` set req.body to the
|
|
25
|
+
raw buffer, so we'll handle that additionally */
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
let body = req.body;
|
|
28
|
+
// if body is Buffer, parse it into string
|
|
29
|
+
if (body instanceof Buffer)
|
|
30
|
+
body = String(req.body);
|
|
31
|
+
// if body is string, parse it into object
|
|
32
|
+
if (typeof body === 'string')
|
|
33
|
+
body = JSON.parse(body);
|
|
34
|
+
update = body;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
let body = '';
|
|
38
|
+
// parse each buffer to string and append to body
|
|
39
|
+
for await (const chunk of req)
|
|
40
|
+
body += String(chunk);
|
|
41
|
+
// parse body to object
|
|
42
|
+
update = JSON.parse(body);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
// if any of the parsing steps fails, give up and respond with error
|
|
47
|
+
res.writeHead(415).end();
|
|
48
|
+
debug('Failed to parse request body:', error);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
return await updateHandler(update, res);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
exports.default = generateWebhook;
|