@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.
Files changed (168) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +356 -0
  3. package/filters.d.ts +1 -0
  4. package/filters.js +1 -0
  5. package/format.d.ts +1 -0
  6. package/format.js +1 -0
  7. package/future.d.ts +1 -0
  8. package/future.js +1 -0
  9. package/lib/button.js +100 -0
  10. package/lib/cli.mjs +105 -0
  11. package/lib/composer.js +582 -0
  12. package/lib/context.js +1219 -0
  13. package/lib/core/helpers/args.js +57 -0
  14. package/lib/core/helpers/check.js +55 -0
  15. package/lib/core/helpers/compact.js +16 -0
  16. package/lib/core/helpers/deunionize.js +12 -0
  17. package/lib/core/helpers/formatting.js +91 -0
  18. package/lib/core/helpers/util.js +50 -0
  19. package/lib/core/network/client.js +330 -0
  20. package/lib/core/network/error.js +21 -0
  21. package/lib/core/network/multipart-stream.js +71 -0
  22. package/lib/core/network/polling.js +87 -0
  23. package/lib/core/network/webhook.js +54 -0
  24. package/lib/core/types/typegram.js +27 -0
  25. package/lib/filters.js +69 -0
  26. package/lib/format.js +38 -0
  27. package/lib/future.js +149 -0
  28. package/lib/index.js +58 -0
  29. package/lib/input.js +61 -0
  30. package/lib/markup.js +121 -0
  31. package/lib/middleware.js +2 -0
  32. package/lib/reactions.js +84 -0
  33. package/lib/router.js +46 -0
  34. package/lib/scenes/base.js +39 -0
  35. package/lib/scenes/context.js +104 -0
  36. package/lib/scenes/index.js +21 -0
  37. package/lib/scenes/stage.js +49 -0
  38. package/lib/scenes/wizard/context.js +31 -0
  39. package/lib/scenes/wizard/index.js +45 -0
  40. package/lib/scenes.js +17 -0
  41. package/lib/session.js +166 -0
  42. package/lib/telegraf.js +256 -0
  43. package/lib/telegram-types.js +6 -0
  44. package/lib/telegram.js +1240 -0
  45. package/lib/types.js +2 -0
  46. package/lib/utils.js +5 -0
  47. package/markup.d.ts +1 -0
  48. package/markup.js +1 -0
  49. package/package.json +140 -0
  50. package/scenes.d.ts +1 -0
  51. package/scenes.js +1 -0
  52. package/session.d.ts +1 -0
  53. package/session.js +1 -0
  54. package/src/button.ts +182 -0
  55. package/src/composer.ts +1008 -0
  56. package/src/context.ts +1661 -0
  57. package/src/core/helpers/args.ts +63 -0
  58. package/src/core/helpers/check.ts +71 -0
  59. package/src/core/helpers/compact.ts +18 -0
  60. package/src/core/helpers/deunionize.ts +26 -0
  61. package/src/core/helpers/formatting.ts +119 -0
  62. package/src/core/helpers/util.ts +96 -0
  63. package/src/core/network/client.ts +396 -0
  64. package/src/core/network/error.ts +29 -0
  65. package/src/core/network/multipart-stream.ts +45 -0
  66. package/src/core/network/polling.ts +94 -0
  67. package/src/core/network/webhook.ts +58 -0
  68. package/src/core/types/typegram.ts +54 -0
  69. package/src/filters.ts +109 -0
  70. package/src/format.ts +110 -0
  71. package/src/future.ts +213 -0
  72. package/src/index.ts +17 -0
  73. package/src/input.ts +59 -0
  74. package/src/markup.ts +142 -0
  75. package/src/middleware.ts +24 -0
  76. package/src/reactions.ts +118 -0
  77. package/src/router.ts +55 -0
  78. package/src/scenes/base.ts +52 -0
  79. package/src/scenes/context.ts +136 -0
  80. package/src/scenes/index.ts +21 -0
  81. package/src/scenes/stage.ts +71 -0
  82. package/src/scenes/wizard/context.ts +58 -0
  83. package/src/scenes/wizard/index.ts +63 -0
  84. package/src/scenes.ts +1 -0
  85. package/src/session.ts +204 -0
  86. package/src/telegraf.ts +354 -0
  87. package/src/telegram-types.ts +219 -0
  88. package/src/telegram.ts +1635 -0
  89. package/src/types.ts +2 -0
  90. package/src/utils.ts +1 -0
  91. package/types.d.ts +1 -0
  92. package/types.js +1 -0
  93. package/typings/button.d.ts +36 -0
  94. package/typings/button.d.ts.map +1 -0
  95. package/typings/composer.d.ts +227 -0
  96. package/typings/composer.d.ts.map +1 -0
  97. package/typings/context.d.ts +655 -0
  98. package/typings/context.d.ts.map +1 -0
  99. package/typings/core/helpers/args.d.ts +11 -0
  100. package/typings/core/helpers/args.d.ts.map +1 -0
  101. package/typings/core/helpers/check.d.ts +56 -0
  102. package/typings/core/helpers/check.d.ts.map +1 -0
  103. package/typings/core/helpers/compact.d.ts +4 -0
  104. package/typings/core/helpers/compact.d.ts.map +1 -0
  105. package/typings/core/helpers/deunionize.d.ts +18 -0
  106. package/typings/core/helpers/deunionize.d.ts.map +1 -0
  107. package/typings/core/helpers/formatting.d.ts +30 -0
  108. package/typings/core/helpers/formatting.d.ts.map +1 -0
  109. package/typings/core/helpers/util.d.ts +26 -0
  110. package/typings/core/helpers/util.d.ts.map +1 -0
  111. package/typings/core/network/client.d.ts +53 -0
  112. package/typings/core/network/client.d.ts.map +1 -0
  113. package/typings/core/network/error.d.ts +16 -0
  114. package/typings/core/network/error.d.ts.map +1 -0
  115. package/typings/core/network/multipart-stream.d.ts +16 -0
  116. package/typings/core/network/multipart-stream.d.ts.map +1 -0
  117. package/typings/core/network/polling.d.ts +16 -0
  118. package/typings/core/network/polling.d.ts.map +1 -0
  119. package/typings/core/network/webhook.d.ts +6 -0
  120. package/typings/core/network/webhook.d.ts.map +1 -0
  121. package/typings/core/types/typegram.d.ts +42 -0
  122. package/typings/core/types/typegram.d.ts.map +1 -0
  123. package/typings/filters.d.ts +18 -0
  124. package/typings/filters.d.ts.map +1 -0
  125. package/typings/format.d.ts +22 -0
  126. package/typings/format.d.ts.map +1 -0
  127. package/typings/future.d.ts +12 -0
  128. package/typings/future.d.ts.map +1 -0
  129. package/typings/index.d.ts +15 -0
  130. package/typings/index.d.ts.map +1 -0
  131. package/typings/input.d.ts +50 -0
  132. package/typings/input.d.ts.map +1 -0
  133. package/typings/markup.d.ts +27 -0
  134. package/typings/markup.d.ts.map +1 -0
  135. package/typings/middleware.d.ts +8 -0
  136. package/typings/middleware.d.ts.map +1 -0
  137. package/typings/reactions.d.ts +32 -0
  138. package/typings/reactions.d.ts.map +1 -0
  139. package/typings/router.d.ts +21 -0
  140. package/typings/router.d.ts.map +1 -0
  141. package/typings/scenes/base.d.ts +22 -0
  142. package/typings/scenes/base.d.ts.map +1 -0
  143. package/typings/scenes/context.d.ts +36 -0
  144. package/typings/scenes/context.d.ts.map +1 -0
  145. package/typings/scenes/index.d.ts +11 -0
  146. package/typings/scenes/index.d.ts.map +1 -0
  147. package/typings/scenes/stage.d.ts +24 -0
  148. package/typings/scenes/stage.d.ts.map +1 -0
  149. package/typings/scenes/wizard/context.d.ts +29 -0
  150. package/typings/scenes/wizard/context.d.ts.map +1 -0
  151. package/typings/scenes/wizard/index.d.ts +16 -0
  152. package/typings/scenes/wizard/index.d.ts.map +1 -0
  153. package/typings/scenes.d.ts +2 -0
  154. package/typings/scenes.d.ts.map +1 -0
  155. package/typings/session.d.ts +55 -0
  156. package/typings/session.d.ts.map +1 -0
  157. package/typings/telegraf.d.ts +115 -0
  158. package/typings/telegraf.d.ts.map +1 -0
  159. package/typings/telegram-types.d.ts +117 -0
  160. package/typings/telegram-types.d.ts.map +1 -0
  161. package/typings/telegram.d.ts +675 -0
  162. package/typings/telegram.d.ts.map +1 -0
  163. package/typings/types.d.ts +3 -0
  164. package/typings/types.d.ts.map +1 -0
  165. package/typings/utils.d.ts +2 -0
  166. package/typings/utils.d.ts.map +1 -0
  167. package/utils.d.ts +1 -0
  168. package/utils.js +1 -0
@@ -0,0 +1,582 @@
1
+ "use strict";
2
+ /** @format */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.Composer = void 0;
8
+ const context_1 = __importDefault(require("./context"));
9
+ const filters_1 = require("./filters");
10
+ const args_1 = require("./core/helpers/args");
11
+ function always(x) {
12
+ return () => x;
13
+ }
14
+ const anoop = always(Promise.resolve());
15
+ class Composer {
16
+ constructor(...fns) {
17
+ this.handler = Composer.compose(fns);
18
+ }
19
+ /**
20
+ * Registers a middleware.
21
+ */
22
+ use(...fns) {
23
+ this.handler = Composer.compose([this.handler, ...fns]);
24
+ return this;
25
+ }
26
+ /**
27
+ * Registers middleware for handling updates
28
+ * matching given type guard function.
29
+ * @deprecated use `Composer::on`
30
+ */
31
+ guard(guardFn, ...fns) {
32
+ return this.use(Composer.guard(guardFn, ...fns));
33
+ }
34
+ on(filters, ...fns) {
35
+ // @ts-expect-error This should get resolved when the overloads are removed in v5
36
+ return this.use(Composer.on(filters, ...fns));
37
+ }
38
+ /**
39
+ * Registers middleware for handling matching text messages.
40
+ */
41
+ hears(triggers, ...fns) {
42
+ return this.use(Composer.hears(triggers, ...fns));
43
+ }
44
+ /**
45
+ * Registers middleware for handling specified commands.
46
+ */
47
+ command(command, ...fns) {
48
+ return this.use(Composer.command(command, ...fns));
49
+ }
50
+ /**
51
+ * Registers middleware for handling matching callback queries.
52
+ */
53
+ action(triggers, ...fns) {
54
+ return this.use(Composer.action(triggers, ...fns));
55
+ }
56
+ /**
57
+ * Registers middleware for handling matching inline queries.
58
+ */
59
+ inlineQuery(triggers, ...fns) {
60
+ return this.use(Composer.inlineQuery(triggers, ...fns));
61
+ }
62
+ /**
63
+ * Registers middleware for handling game queries
64
+ */
65
+ gameQuery(...fns) {
66
+ return this.use(Composer.gameQuery(...fns));
67
+ }
68
+ reaction(reaction, ...fns) {
69
+ return this.use(Composer.reaction(reaction, ...fns));
70
+ }
71
+ /**
72
+ * Registers middleware for dropping matching updates.
73
+ */
74
+ drop(predicate) {
75
+ return this.use(Composer.drop(predicate));
76
+ }
77
+ /** @deprecated use `Composer::drop` */
78
+ filter(predicate) {
79
+ return this.use(Composer.filter(predicate));
80
+ }
81
+ entity(predicate, ...fns) {
82
+ return this.use(Composer.entity(predicate, ...fns));
83
+ }
84
+ email(email, ...fns) {
85
+ return this.use(Composer.email(email, ...fns));
86
+ }
87
+ url(url, ...fns) {
88
+ return this.use(Composer.url(url, ...fns));
89
+ }
90
+ textLink(link, ...fns) {
91
+ return this.use(Composer.textLink(link, ...fns));
92
+ }
93
+ textMention(mention, ...fns) {
94
+ return this.use(Composer.textMention(mention, ...fns));
95
+ }
96
+ mention(mention, ...fns) {
97
+ return this.use(Composer.mention(mention, ...fns));
98
+ }
99
+ phone(number, ...fns) {
100
+ return this.use(Composer.phone(number, ...fns));
101
+ }
102
+ hashtag(hashtag, ...fns) {
103
+ return this.use(Composer.hashtag(hashtag, ...fns));
104
+ }
105
+ cashtag(cashtag, ...fns) {
106
+ return this.use(Composer.cashtag(cashtag, ...fns));
107
+ }
108
+ spoiler(text, ...fns) {
109
+ return this.use(Composer.spoiler(text, ...fns));
110
+ }
111
+ /**
112
+ * Registers a middleware for handling /start
113
+ */
114
+ start(...fns) {
115
+ const handler = Composer.compose(fns);
116
+ return this.command('start', (ctx, next) => handler(Object.assign(ctx, { startPayload: ctx.payload }), next));
117
+ }
118
+ /**
119
+ * Registers a middleware for handling /help
120
+ */
121
+ help(...fns) {
122
+ return this.command('help', ...fns);
123
+ }
124
+ /**
125
+ * Registers a middleware for handling /settings
126
+ */
127
+ settings(...fns) {
128
+ return this.command('settings', ...fns);
129
+ }
130
+ middleware() {
131
+ return this.handler;
132
+ }
133
+ static reply(...args) {
134
+ return (ctx) => ctx.reply(...args);
135
+ }
136
+ static catch(errorHandler, ...fns) {
137
+ const handler = Composer.compose(fns);
138
+ // prettier-ignore
139
+ return (ctx, next) => Promise.resolve(handler(ctx, next))
140
+ .catch((err) => errorHandler(err, ctx));
141
+ }
142
+ /**
143
+ * Generates middleware that runs in the background.
144
+ */
145
+ static fork(middleware) {
146
+ const handler = Composer.unwrap(middleware);
147
+ return async (ctx, next) => {
148
+ await Promise.all([handler(ctx, anoop), next()]);
149
+ };
150
+ }
151
+ static tap(middleware) {
152
+ const handler = Composer.unwrap(middleware);
153
+ return (ctx, next) => Promise.resolve(handler(ctx, anoop)).then(() => next());
154
+ }
155
+ /**
156
+ * Generates middleware that gives up control to the next middleware.
157
+ */
158
+ static passThru() {
159
+ return (ctx, next) => next();
160
+ }
161
+ static lazy(factoryFn) {
162
+ if (typeof factoryFn !== 'function') {
163
+ throw new Error('Argument must be a function');
164
+ }
165
+ return (ctx, next) => Promise.resolve(factoryFn(ctx)).then((middleware) => Composer.unwrap(middleware)(ctx, next));
166
+ }
167
+ static log(logFn = console.log) {
168
+ return (ctx, next) => {
169
+ logFn(JSON.stringify(ctx.update, null, 2));
170
+ return next();
171
+ };
172
+ }
173
+ /**
174
+ * @param trueMiddleware middleware to run if the predicate returns true
175
+ * @param falseMiddleware middleware to run if the predicate returns false
176
+ */
177
+ static branch(predicate, trueMiddleware, falseMiddleware) {
178
+ if (typeof predicate !== 'function') {
179
+ return Composer.unwrap(predicate ? trueMiddleware : falseMiddleware);
180
+ }
181
+ return Composer.lazy((ctx) => Promise.resolve(predicate(ctx)).then((value) => value ? trueMiddleware : falseMiddleware));
182
+ }
183
+ /**
184
+ * Generates optional middleware.
185
+ * @param predicate predicate to decide on a context object whether to run the middleware
186
+ * @param fns middleware to run if the predicate returns true
187
+ */
188
+ static optional(predicate, ...fns) {
189
+ return Composer.branch(predicate, Composer.compose(fns), Composer.passThru());
190
+ }
191
+ /** @deprecated use `Composer.drop` */
192
+ static filter(predicate) {
193
+ return Composer.branch(predicate, Composer.passThru(), anoop);
194
+ }
195
+ /**
196
+ * Generates middleware for dropping matching updates.
197
+ */
198
+ static drop(predicate) {
199
+ return Composer.branch(predicate, anoop, Composer.passThru());
200
+ }
201
+ static dispatch(routeFn, handlers) {
202
+ return Composer.lazy((ctx) => Promise.resolve(routeFn(ctx)).then((value) => handlers[value]));
203
+ }
204
+ // EXPLANATION FOR THE ts-expect-error ANNOTATIONS
205
+ // The annotations around function invocations with `...fns` are there
206
+ // whenever we perform validation logic that the flow analysis of TypeScript
207
+ // cannot comprehend. We always make sure that the middleware functions are
208
+ // only invoked with properly constrained context objects, but this cannot be
209
+ // determined automatically.
210
+ /**
211
+ * Generates optional middleware based on a predicate that only operates on `ctx.update`.
212
+ *
213
+ * Example:
214
+ * ```ts
215
+ * import { Composer, Update } from 'telegraf'
216
+ *
217
+ * const predicate = (u): u is Update.MessageUpdate => 'message' in u
218
+ * const middleware = Composer.guard(predicate, (ctx) => {
219
+ * const message = ctx.update.message
220
+ * })
221
+ * ```
222
+ *
223
+ * Note that `Composer.on('message')` is preferred over this.
224
+ *
225
+ * @param guardFn predicate to decide whether to run the middleware based on the `ctx.update` object
226
+ * @param fns middleware to run if the predicate returns true
227
+ * @see `Composer.optional` for a more generic version of this method that allows the predicate to operate on `ctx` itself
228
+ * @deprecated use `Composer.on`
229
+ */
230
+ static guard(guardFn, ...fns) {
231
+ return Composer.optional((ctx) => guardFn(ctx.update),
232
+ // @ts-expect-error see explanation above
233
+ ...fns);
234
+ }
235
+ static on(updateType, ...fns) {
236
+ const filters = Array.isArray(updateType) ? updateType : [updateType];
237
+ const predicate = (update) => {
238
+ for (const filter of filters) {
239
+ if (
240
+ // TODO: this should change to === 'function' once TS bug is fixed
241
+ // https://github.com/microsoft/TypeScript/pull/51502
242
+ typeof filter !== 'string'
243
+ ? // filter is a type guard
244
+ filter(update)
245
+ : // check if filter is the update type
246
+ filter in update ||
247
+ // check if filter is the msg type
248
+ // TODO: remove in v5!
249
+ ('message' in update && filter in update.message)) {
250
+ return true;
251
+ }
252
+ }
253
+ return false;
254
+ };
255
+ return Composer.optional((ctx) => predicate(ctx.update), ...fns);
256
+ }
257
+ static entity(predicate, ...fns) {
258
+ if (typeof predicate !== 'function') {
259
+ const entityTypes = normaliseTextArguments(predicate);
260
+ return Composer.entity(({ type }) => entityTypes.includes(type), ...fns);
261
+ }
262
+ return Composer.optional((ctx) => {
263
+ var _a;
264
+ const msg = (_a = ctx.message) !== null && _a !== void 0 ? _a : ctx.channelPost;
265
+ if (msg === undefined) {
266
+ return false;
267
+ }
268
+ const text = getText(msg);
269
+ const entities = getEntities(msg);
270
+ if (text === undefined)
271
+ return false;
272
+ return entities.some((entity) => predicate(entity, text.substring(entity.offset, entity.offset + entity.length), ctx));
273
+ },
274
+ // @ts-expect-error see explanation above
275
+ ...fns);
276
+ }
277
+ static entityText(entityType, predicate, ...fns) {
278
+ if (fns.length === 0) {
279
+ // prettier-ignore
280
+ return Array.isArray(predicate)
281
+ // @ts-expect-error predicate is really the middleware
282
+ ? Composer.entity(entityType, ...predicate)
283
+ // @ts-expect-error predicate is really the middleware
284
+ : Composer.entity(entityType, predicate);
285
+ }
286
+ const triggers = normaliseTriggers(predicate);
287
+ return Composer.entity(({ type }, value, ctx) => {
288
+ if (type !== entityType) {
289
+ return false;
290
+ }
291
+ for (const trigger of triggers) {
292
+ // @ts-expect-error define so far unknown property `match`
293
+ if ((ctx.match = trigger(value, ctx))) {
294
+ return true;
295
+ }
296
+ }
297
+ return false;
298
+ },
299
+ // @ts-expect-error see explanation above
300
+ ...fns);
301
+ }
302
+ static email(email, ...fns) {
303
+ return Composer.entityText('email', email, ...fns);
304
+ }
305
+ static phone(number, ...fns) {
306
+ return Composer.entityText('phone_number', number, ...fns);
307
+ }
308
+ static url(url, ...fns) {
309
+ return Composer.entityText('url', url, ...fns);
310
+ }
311
+ static textLink(link, ...fns) {
312
+ return Composer.entityText('text_link', link, ...fns);
313
+ }
314
+ static textMention(mention, ...fns) {
315
+ return Composer.entityText('text_mention', mention, ...fns);
316
+ }
317
+ static mention(mention, ...fns) {
318
+ return Composer.entityText('mention', normaliseTextArguments(mention, '@'), ...fns);
319
+ }
320
+ static hashtag(hashtag, ...fns) {
321
+ return Composer.entityText('hashtag', normaliseTextArguments(hashtag, '#'), ...fns);
322
+ }
323
+ static cashtag(cashtag, ...fns) {
324
+ return Composer.entityText('cashtag', normaliseTextArguments(cashtag, '$'), ...fns);
325
+ }
326
+ static spoiler(text, ...fns) {
327
+ return Composer.entityText('spoiler', text, ...fns);
328
+ }
329
+ static match(triggers, ...fns) {
330
+ const handler = Composer.compose(fns);
331
+ return (ctx, next) => {
332
+ var _a, _b, _c, _d;
333
+ const text = (_c = (_b = (_a = getText(ctx.message)) !== null && _a !== void 0 ? _a : getText(ctx.channelPost)) !== null && _b !== void 0 ? _b : getText(ctx.callbackQuery)) !== null && _c !== void 0 ? _c : (_d = ctx.inlineQuery) === null || _d === void 0 ? void 0 : _d.query;
334
+ if (text === undefined)
335
+ return next();
336
+ for (const trigger of triggers) {
337
+ const match = trigger(text, ctx);
338
+ if (match)
339
+ return handler(Object.assign(ctx, { match }), next);
340
+ }
341
+ return next();
342
+ };
343
+ }
344
+ /**
345
+ * Generates middleware for handling matching text messages.
346
+ */
347
+ static hears(triggers, ...fns) {
348
+ return Composer.on('text', Composer.match(normaliseTriggers(triggers), ...fns));
349
+ }
350
+ /**
351
+ * Generates middleware for handling specified commands.
352
+ */
353
+ static command(command, ...fns) {
354
+ if (fns.length === 0)
355
+ // @ts-expect-error command is really the middleware
356
+ return Composer.entity('bot_command', command);
357
+ const triggers = normaliseTriggers(command);
358
+ const filter = (0, filters_1.message)('text');
359
+ const handler = Composer.compose(fns);
360
+ return Composer.on(filter, (ctx, next) => {
361
+ const { entities } = ctx.message;
362
+ const cmdEntity = entities === null || entities === void 0 ? void 0 : entities[0];
363
+ if ((cmdEntity === null || cmdEntity === void 0 ? void 0 : cmdEntity.type) !== 'bot_command')
364
+ return next();
365
+ if (cmdEntity.offset > 0)
366
+ return next();
367
+ const len = cmdEntity.length;
368
+ const text = ctx.message.text;
369
+ const [cmdPart, to] = text.slice(0, len).split('@');
370
+ if (!cmdPart)
371
+ return next();
372
+ // always check for bot's own username case-insensitively
373
+ if (to && to.toLowerCase() !== ctx.me.toLowerCase())
374
+ return next();
375
+ const command = cmdPart.slice(1);
376
+ for (const trigger of triggers) {
377
+ const match = trigger(command, ctx);
378
+ if (match) {
379
+ const payloadOffset = len + 1;
380
+ const payload = text.slice(payloadOffset);
381
+ const c = Object.assign(ctx, { match, command, payload, args: [] });
382
+ let _args = undefined;
383
+ // using defineProperty only to make parsing lazy on access
384
+ Object.defineProperty(c, 'args', {
385
+ enumerable: true,
386
+ configurable: true,
387
+ get() {
388
+ if (_args != null)
389
+ return _args;
390
+ // once parsed, cache and don't parse again on every access
391
+ return (_args = (0, args_1.argsParser)(payload, entities, payloadOffset));
392
+ },
393
+ set(args) {
394
+ _args = args;
395
+ },
396
+ });
397
+ return handler(c, next);
398
+ }
399
+ }
400
+ return next();
401
+ });
402
+ }
403
+ /**
404
+ * Generates middleware for handling matching callback queries.
405
+ */
406
+ static action(triggers, ...fns) {
407
+ return Composer.on('callback_query', Composer.match(normaliseTriggers(triggers), ...fns));
408
+ }
409
+ /**
410
+ * Generates middleware for handling matching inline queries.
411
+ */
412
+ static inlineQuery(triggers, ...fns) {
413
+ return Composer.on('inline_query', Composer.match(normaliseTriggers(triggers), ...fns));
414
+ }
415
+ static reaction(reaction, ...fns) {
416
+ const reactions = Array.isArray(reaction) ? reaction : [reaction];
417
+ const handler = Composer.compose(fns);
418
+ return Composer.on('message_reaction', (ctx, next) => {
419
+ const match = reactions.find((r) => typeof r === 'string' && r.startsWith('-')
420
+ ? ctx.reactions.removed.has(r.slice(1))
421
+ : ctx.reactions.added.has(r));
422
+ if (match)
423
+ return handler(Object.assign(ctx, { match }), next);
424
+ return next();
425
+ });
426
+ }
427
+ /**
428
+ * Generates middleware responding only to specified users.
429
+ */
430
+ static acl(userId, ...fns) {
431
+ if (typeof userId === 'function') {
432
+ return Composer.optional(userId, ...fns);
433
+ }
434
+ const allowed = Array.isArray(userId) ? userId : [userId];
435
+ // prettier-ignore
436
+ return Composer.optional((ctx) => !ctx.from || allowed.includes(ctx.from.id), ...fns);
437
+ }
438
+ static memberStatus(status, ...fns) {
439
+ const statuses = Array.isArray(status) ? status : [status];
440
+ return Composer.optional(async (ctx) => {
441
+ if (ctx.message === undefined)
442
+ return false;
443
+ const member = await ctx.getChatMember(ctx.message.from.id);
444
+ return statuses.includes(member.status);
445
+ }, ...fns);
446
+ }
447
+ /**
448
+ * Generates middleware responding only to chat admins and chat creator.
449
+ */
450
+ static admin(...fns) {
451
+ return Composer.memberStatus(['administrator', 'creator'], ...fns);
452
+ }
453
+ /**
454
+ * Generates middleware responding only to chat creator.
455
+ */
456
+ static creator(...fns) {
457
+ return Composer.memberStatus('creator', ...fns);
458
+ }
459
+ /**
460
+ * Generates middleware running only in specified chat types.
461
+ */
462
+ static chatType(type, ...fns) {
463
+ const types = Array.isArray(type) ? type : [type];
464
+ return Composer.optional((ctx) => {
465
+ const chat = ctx.chat;
466
+ return chat !== undefined && types.includes(chat.type);
467
+ }, ...fns);
468
+ }
469
+ /**
470
+ * Generates middleware running only in private chats.
471
+ */
472
+ static privateChat(...fns) {
473
+ return Composer.chatType('private', ...fns);
474
+ }
475
+ /**
476
+ * Generates middleware running only in groups and supergroups.
477
+ */
478
+ static groupChat(...fns) {
479
+ return Composer.chatType(['group', 'supergroup'], ...fns);
480
+ }
481
+ /**
482
+ * Generates middleware for handling game queries.
483
+ */
484
+ static gameQuery(...fns) {
485
+ return Composer.guard((0, filters_1.callbackQuery)('game_short_name'), ...fns);
486
+ }
487
+ static unwrap(handler) {
488
+ if (!handler) {
489
+ throw new Error('Handler is undefined');
490
+ }
491
+ return 'middleware' in handler ? handler.middleware() : handler;
492
+ }
493
+ static compose(middlewares) {
494
+ if (!Array.isArray(middlewares)) {
495
+ throw new Error('Middlewares must be an array');
496
+ }
497
+ if (middlewares.length === 0) {
498
+ return Composer.passThru();
499
+ }
500
+ if (middlewares.length === 1) {
501
+ // Quite literally asserted in the above condition
502
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
503
+ return Composer.unwrap(middlewares[0]);
504
+ }
505
+ return (ctx, next) => {
506
+ let index = -1;
507
+ return execute(0, ctx);
508
+ async function execute(i, context) {
509
+ var _a;
510
+ if (!(context instanceof context_1.default)) {
511
+ throw new Error('next(ctx) called with invalid context');
512
+ }
513
+ if (i <= index) {
514
+ throw new Error('next() called multiple times');
515
+ }
516
+ index = i;
517
+ const handler = Composer.unwrap((_a = middlewares[i]) !== null && _a !== void 0 ? _a : next);
518
+ await handler(context, async (ctx = context) => {
519
+ await execute(i + 1, ctx);
520
+ });
521
+ }
522
+ };
523
+ }
524
+ }
525
+ exports.Composer = Composer;
526
+ /**
527
+ * Generates middleware for handling provided update types.
528
+ * @deprecated use `Composer.on` instead
529
+ */
530
+ Composer.mount = Composer.on;
531
+ function escapeRegExp(s) {
532
+ // $& means the whole matched string
533
+ return s.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
534
+ }
535
+ function normaliseTriggers(triggers) {
536
+ if (!Array.isArray(triggers))
537
+ triggers = [triggers];
538
+ return triggers.map((trigger) => {
539
+ if (!trigger)
540
+ throw new Error('Invalid trigger');
541
+ if (typeof trigger === 'function')
542
+ return trigger;
543
+ if (trigger instanceof RegExp)
544
+ return (value = '') => {
545
+ trigger.lastIndex = 0;
546
+ return trigger.exec(value);
547
+ };
548
+ const regex = new RegExp(`^${escapeRegExp(trigger)}$`);
549
+ return (value) => regex.exec(value);
550
+ });
551
+ }
552
+ function getEntities(msg) {
553
+ var _a, _b;
554
+ if (msg == null)
555
+ return [];
556
+ if ('caption_entities' in msg)
557
+ return (_a = msg.caption_entities) !== null && _a !== void 0 ? _a : [];
558
+ if ('entities' in msg)
559
+ return (_b = msg.entities) !== null && _b !== void 0 ? _b : [];
560
+ return [];
561
+ }
562
+ function getText(msg) {
563
+ if (msg == null)
564
+ return undefined;
565
+ if ('caption' in msg)
566
+ return msg.caption;
567
+ if ('text' in msg)
568
+ return msg.text;
569
+ if ('data' in msg)
570
+ return msg.data;
571
+ if ('game_short_name' in msg)
572
+ return msg.game_short_name;
573
+ return undefined;
574
+ }
575
+ function normaliseTextArguments(argument, prefix = '') {
576
+ const args = Array.isArray(argument) ? argument : [argument];
577
+ // prettier-ignore
578
+ return args
579
+ .filter(Boolean)
580
+ .map((arg) => prefix && typeof arg === 'string' && !arg.startsWith(prefix) ? `${prefix}${arg}` : arg);
581
+ }
582
+ exports.default = Composer;