@sugansociety/gallagherrich 4.16.8

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 (180) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1024 -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 +102 -0
  10. package/lib/cli.mjs +105 -0
  11. package/lib/composer.js +583 -0
  12. package/lib/context.js +1280 -0
  13. package/lib/core/helpers/args.js +59 -0
  14. package/lib/core/helpers/check.js +57 -0
  15. package/lib/core/helpers/compact.js +18 -0
  16. package/lib/core/helpers/deunionize.js +14 -0
  17. package/lib/core/helpers/formatting.js +92 -0
  18. package/lib/core/helpers/util.js +51 -0
  19. package/lib/core/network/client.js +321 -0
  20. package/lib/core/network/error.js +22 -0
  21. package/lib/core/network/multipart-stream.js +62 -0
  22. package/lib/core/network/polling.js +88 -0
  23. package/lib/core/network/webhook.js +55 -0
  24. package/lib/core/types/rich-message.js +288 -0
  25. package/lib/core/types/typegram.js +29 -0
  26. package/lib/filters.js +70 -0
  27. package/lib/filters.js.map +1 -0
  28. package/lib/format.js +39 -0
  29. package/lib/format.js.map +1 -0
  30. package/lib/future.js +167 -0
  31. package/lib/future.js.map +1 -0
  32. package/lib/index.js +50 -0
  33. package/lib/input.js +62 -0
  34. package/lib/markup.js +112 -0
  35. package/lib/markup.js.map +1 -0
  36. package/lib/middleware.js +3 -0
  37. package/lib/reactions.js +85 -0
  38. package/lib/router.js +47 -0
  39. package/lib/scenes/base.js +40 -0
  40. package/lib/scenes/context.js +105 -0
  41. package/lib/scenes/index.js +22 -0
  42. package/lib/scenes/stage.js +50 -0
  43. package/lib/scenes/wizard/context.js +32 -0
  44. package/lib/scenes/wizard/index.js +46 -0
  45. package/lib/scenes.js +18 -0
  46. package/lib/scenes.js.map +1 -0
  47. package/lib/session.js +167 -0
  48. package/lib/session.js.map +1 -0
  49. package/lib/telegraf.js +273 -0
  50. package/lib/telegram-types.js +7 -0
  51. package/lib/telegram.js +1270 -0
  52. package/lib/types.js +3 -0
  53. package/lib/types.js.map +1 -0
  54. package/lib/utils.js +6 -0
  55. package/lib/utils.js.map +1 -0
  56. package/markup.d.ts +1 -0
  57. package/markup.js +1 -0
  58. package/package.json +136 -0
  59. package/scenes.d.ts +1 -0
  60. package/scenes.js +1 -0
  61. package/session.d.ts +1 -0
  62. package/session.js +1 -0
  63. package/src/button.ts +182 -0
  64. package/src/composer.ts +1008 -0
  65. package/src/context.ts +1747 -0
  66. package/src/core/helpers/args.ts +63 -0
  67. package/src/core/helpers/check.ts +71 -0
  68. package/src/core/helpers/compact.ts +18 -0
  69. package/src/core/helpers/deunionize.ts +26 -0
  70. package/src/core/helpers/formatting.ts +119 -0
  71. package/src/core/helpers/util.ts +96 -0
  72. package/src/core/network/client.ts +396 -0
  73. package/src/core/network/error.ts +29 -0
  74. package/src/core/network/multipart-stream.ts +45 -0
  75. package/src/core/network/polling.ts +94 -0
  76. package/src/core/network/webhook.ts +58 -0
  77. package/src/core/types/rich-message.ts +375 -0
  78. package/src/core/types/typegram.ts +55 -0
  79. package/src/filters.ts +109 -0
  80. package/src/format.ts +110 -0
  81. package/src/future.ts +231 -0
  82. package/src/index.ts +18 -0
  83. package/src/input.ts +59 -0
  84. package/src/markup.ts +142 -0
  85. package/src/middleware.ts +24 -0
  86. package/src/reactions.ts +118 -0
  87. package/src/router.ts +55 -0
  88. package/src/scenes/base.ts +52 -0
  89. package/src/scenes/context.ts +136 -0
  90. package/src/scenes/index.ts +21 -0
  91. package/src/scenes/stage.ts +71 -0
  92. package/src/scenes/wizard/context.ts +58 -0
  93. package/src/scenes/wizard/index.ts +63 -0
  94. package/src/scenes.ts +1 -0
  95. package/src/session.ts +204 -0
  96. package/src/telegraf.ts +380 -0
  97. package/src/telegram-types.ts +256 -0
  98. package/src/telegram.ts +1676 -0
  99. package/src/types.ts +2 -0
  100. package/src/utils.ts +1 -0
  101. package/types.d.ts +1 -0
  102. package/types.js +1 -0
  103. package/typings/button.d.ts +36 -0
  104. package/typings/button.d.ts.map +1 -0
  105. package/typings/composer.d.ts +227 -0
  106. package/typings/composer.d.ts.map +1 -0
  107. package/typings/context.d.ts +699 -0
  108. package/typings/context.d.ts.map +1 -0
  109. package/typings/core/helpers/args.d.ts +11 -0
  110. package/typings/core/helpers/args.d.ts.map +1 -0
  111. package/typings/core/helpers/check.d.ts +56 -0
  112. package/typings/core/helpers/check.d.ts.map +1 -0
  113. package/typings/core/helpers/compact.d.ts +4 -0
  114. package/typings/core/helpers/compact.d.ts.map +1 -0
  115. package/typings/core/helpers/deunionize.d.ts +18 -0
  116. package/typings/core/helpers/deunionize.d.ts.map +1 -0
  117. package/typings/core/helpers/formatting.d.ts +30 -0
  118. package/typings/core/helpers/formatting.d.ts.map +1 -0
  119. package/typings/core/helpers/util.d.ts +27 -0
  120. package/typings/core/helpers/util.d.ts.map +1 -0
  121. package/typings/core/network/client.d.ts +55 -0
  122. package/typings/core/network/client.d.ts.map +1 -0
  123. package/typings/core/network/error.d.ts +16 -0
  124. package/typings/core/network/error.d.ts.map +1 -0
  125. package/typings/core/network/multipart-stream.d.ts +20 -0
  126. package/typings/core/network/multipart-stream.d.ts.map +1 -0
  127. package/typings/core/network/polling.d.ts +16 -0
  128. package/typings/core/network/polling.d.ts.map +1 -0
  129. package/typings/core/network/webhook.d.ts +7 -0
  130. package/typings/core/network/webhook.d.ts.map +1 -0
  131. package/typings/core/types/rich-message.d.ts +144 -0
  132. package/typings/core/types/rich-message.d.ts.map +1 -0
  133. package/typings/core/types/typegram.d.ts +46 -0
  134. package/typings/core/types/typegram.d.ts.map +1 -0
  135. package/typings/filters.d.ts +18 -0
  136. package/typings/filters.d.ts.map +1 -0
  137. package/typings/format.d.ts +22 -0
  138. package/typings/format.d.ts.map +1 -0
  139. package/typings/future.d.ts +12 -0
  140. package/typings/future.d.ts.map +1 -0
  141. package/typings/index.d.ts +16 -0
  142. package/typings/index.d.ts.map +1 -0
  143. package/typings/input.d.ts +59 -0
  144. package/typings/input.d.ts.map +1 -0
  145. package/typings/markup.d.ts +27 -0
  146. package/typings/markup.d.ts.map +1 -0
  147. package/typings/middleware.d.ts +8 -0
  148. package/typings/middleware.d.ts.map +1 -0
  149. package/typings/reactions.d.ts +32 -0
  150. package/typings/reactions.d.ts.map +1 -0
  151. package/typings/router.d.ts +21 -0
  152. package/typings/router.d.ts.map +1 -0
  153. package/typings/scenes/base.d.ts +22 -0
  154. package/typings/scenes/base.d.ts.map +1 -0
  155. package/typings/scenes/context.d.ts +36 -0
  156. package/typings/scenes/context.d.ts.map +1 -0
  157. package/typings/scenes/index.d.ts +11 -0
  158. package/typings/scenes/index.d.ts.map +1 -0
  159. package/typings/scenes/stage.d.ts +24 -0
  160. package/typings/scenes/stage.d.ts.map +1 -0
  161. package/typings/scenes/wizard/context.d.ts +29 -0
  162. package/typings/scenes/wizard/context.d.ts.map +1 -0
  163. package/typings/scenes/wizard/index.d.ts +16 -0
  164. package/typings/scenes/wizard/index.d.ts.map +1 -0
  165. package/typings/scenes.d.ts +2 -0
  166. package/typings/scenes.d.ts.map +1 -0
  167. package/typings/session.d.ts +55 -0
  168. package/typings/session.d.ts.map +1 -0
  169. package/typings/telegraf.d.ts +119 -0
  170. package/typings/telegraf.d.ts.map +1 -0
  171. package/typings/telegram-types.d.ts +141 -0
  172. package/typings/telegram-types.d.ts.map +1 -0
  173. package/typings/telegram.d.ts +694 -0
  174. package/typings/telegram.d.ts.map +1 -0
  175. package/typings/types.d.ts +3 -0
  176. package/typings/types.d.ts.map +1 -0
  177. package/typings/utils.d.ts +2 -0
  178. package/typings/utils.d.ts.map +1 -0
  179. package/utils.d.ts +1 -0
  180. package/utils.js +1 -0
@@ -0,0 +1,273 @@
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
+ exports.Telegraf = void 0;
30
+ const crypto = __importStar(require("crypto"));
31
+ const http = __importStar(require("http"));
32
+ const https = __importStar(require("https"));
33
+ const composer_1 = require("./composer");
34
+ const compact_1 = require("./core/helpers/compact");
35
+ const context_1 = __importDefault(require("./context"));
36
+ const debug_1 = __importDefault(require("debug"));
37
+ const webhook_1 = __importDefault(require("./core/network/webhook"));
38
+ const polling_1 = require("./core/network/polling");
39
+ const p_timeout_1 = __importDefault(require("p-timeout"));
40
+ const telegram_1 = __importDefault(require("./telegram"));
41
+ const url_1 = require("url");
42
+ const safeCompare = require("safe-compare");
43
+ const debug = (0, debug_1.default)('telegraf:main');
44
+ const DEFAULT_OPTIONS = {
45
+ telegram: {},
46
+ handlerTimeout: 90000, // 90s in ms
47
+ contextType: context_1.default,
48
+ };
49
+ function always(x) {
50
+ return () => x;
51
+ }
52
+ const anoop = always(Promise.resolve());
53
+ const TOKEN_HEADER = 'x-telegram-bot-api-secret-token';
54
+ class Telegraf extends composer_1.Composer {
55
+ constructor(token, options) {
56
+ super();
57
+ this.context = {};
58
+ /** Assign to this to customise the webhook filter middleware.
59
+ * `{ path, secretToken }` will be bound to this rather than the Telegraf instance.
60
+ * Remember to assign a regular function and not an arrow function so it's bindable.
61
+ */
62
+ this.webhookFilter = function (req) {
63
+ const debug = (0, debug_1.default)('telegraf:webhook');
64
+ if (req.method === 'POST') {
65
+ if (safeCompare(this.path, req.url)) {
66
+ // no need to check if secret_token was not set
67
+ if (!this.secretToken)
68
+ return true;
69
+ else {
70
+ const token = req.headers[TOKEN_HEADER];
71
+ if (safeCompare(this.secretToken, token))
72
+ return true;
73
+ else
74
+ debug('Secret token does not match:', token, this.secretToken);
75
+ }
76
+ }
77
+ else
78
+ debug('Path does not match:', req.url, this.path);
79
+ }
80
+ else
81
+ debug('Unexpected request method, not POST. Received:', req.method);
82
+ return false;
83
+ };
84
+ this.handleError = (err, ctx) => {
85
+ // set exit code to emulate `warn-with-error-code` behavior of
86
+ // https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
87
+ // to prevent a clean exit despite an error being thrown
88
+ process.exitCode = 1;
89
+ console.error('Unhandled error while processing', ctx.update);
90
+ throw err;
91
+ };
92
+ // @ts-expect-error Trust me, TS
93
+ this.options = {
94
+ ...DEFAULT_OPTIONS,
95
+ ...(0, compact_1.compactOptions)(options),
96
+ };
97
+ this.telegram = new telegram_1.default(token, this.options.telegram);
98
+ debug('Created a `Telegraf` instance');
99
+ Telegraf.printBanner();
100
+ }
101
+ static printBanner() {
102
+ if (Telegraf._bannerPrinted)
103
+ return;
104
+ Telegraf._bannerPrinted = true;
105
+ const reset = '\x1b[0m';
106
+ const bold = '\x1b[1m';
107
+ const dim = '\x1b[2m';
108
+ const cyan = '\x1b[36m';
109
+ const white = '\x1b[37m';
110
+ console.log(`
111
+ ${cyan}${bold} ╭───╮ ╭───╮${reset}
112
+ ${cyan}${bold} │ ╰──┬──╯ │${reset}
113
+ ${cyan}${bold} ╰──┬─╮ │ ╭─┬──╯${reset}
114
+ ${cyan}${bold} │ ╰─┼─╯ │${reset}
115
+ ${cyan}${bold} ╭──┴─╮ │ ╭─┴──╮${reset}
116
+ ${cyan}${bold} │ ╭──┴──╮ │${reset}
117
+ ${cyan}${bold} ╰───╯ ╰───╯${reset}
118
+ ${reset}
119
+ ${bold}${white} @sugansociety/gallagherrich${reset} ${dim}v4.16.8${reset}
120
+ ${reset}
121
+ ${dim} thanks for using my package${reset}
122
+ ${dim} ~SuganSocietyTech ${cyan}t.me/suganzi${reset}
123
+ `);
124
+ }
125
+ get token() {
126
+ return this.telegram.token;
127
+ }
128
+ /** @deprecated use `ctx.telegram.webhookReply` */
129
+ set webhookReply(webhookReply) {
130
+ this.telegram.webhookReply = webhookReply;
131
+ }
132
+ /** @deprecated use `ctx.telegram.webhookReply` */
133
+ get webhookReply() {
134
+ return this.telegram.webhookReply;
135
+ }
136
+ /**
137
+ * _Override_ error handling
138
+ */
139
+ catch(handler) {
140
+ this.handleError = handler;
141
+ return this;
142
+ }
143
+ /**
144
+ * You must call `bot.telegram.setWebhook` for this to work.
145
+ * You should probably use {@link Telegraf.createWebhook} instead.
146
+ */
147
+ webhookCallback(path = '/', opts = {}) {
148
+ const { secretToken } = opts;
149
+ return (0, webhook_1.default)(this.webhookFilter.bind({ hookPath: path, path, secretToken }), (update, res) => this.handleUpdate(update, res));
150
+ }
151
+ getDomainOpts(opts) {
152
+ var _a;
153
+ const protocol = opts.domain.startsWith('https://') || opts.domain.startsWith('http://');
154
+ if (protocol)
155
+ debug('Unexpected protocol in domain, telegraf will use https:', opts.domain);
156
+ const domain = protocol ? new url_1.URL(opts.domain).host : opts.domain;
157
+ const path = (_a = opts.path) !== null && _a !== void 0 ? _a : `/telegraf/${this.secretPathComponent()}`;
158
+ const url = `https://${domain}${path}`;
159
+ return { domain, path, url };
160
+ }
161
+ /**
162
+ * Specify a url to receive incoming updates via webhook.
163
+ * Returns an Express-style middleware you can pass to app.use()
164
+ */
165
+ async createWebhook(opts) {
166
+ const { domain, path, ...extra } = opts;
167
+ const domainOpts = this.getDomainOpts({ domain, path });
168
+ await this.telegram.setWebhook(domainOpts.url, extra);
169
+ debug(`Webhook set to ${domainOpts.url}`);
170
+ return this.webhookCallback(domainOpts.path, {
171
+ secretToken: extra.secret_token,
172
+ });
173
+ }
174
+ startPolling(allowedUpdates = []) {
175
+ this.polling = new polling_1.Polling(this.telegram, allowedUpdates);
176
+ return this.polling.loop(async (update) => {
177
+ await this.handleUpdate(update);
178
+ });
179
+ }
180
+ startWebhook(path, tlsOptions, port, host, cb, secretToken) {
181
+ const webhookCb = this.webhookCallback(path, { secretToken });
182
+ const callback = typeof cb === 'function'
183
+ ? (req, res) => webhookCb(req, res, () => cb(req, res))
184
+ : webhookCb;
185
+ this.webhookServer =
186
+ tlsOptions != null
187
+ ? https.createServer(tlsOptions, callback)
188
+ : http.createServer(callback);
189
+ this.webhookServer.listen(port, host, () => {
190
+ debug('Webhook listening on port: %s', port);
191
+ });
192
+ return this;
193
+ }
194
+ secretPathComponent() {
195
+ return crypto
196
+ .createHash('sha3-256')
197
+ .update(this.token)
198
+ .update(process.version) // salt
199
+ .digest('hex');
200
+ }
201
+ /**
202
+ * @see https://github.com/telegraf/telegraf/discussions/1344#discussioncomment-335700
203
+ */
204
+ async launch(config = {},
205
+ /** @experimental */
206
+ onLaunch) {
207
+ var _a, _b;
208
+ const [cfg, onMe] = typeof config === 'function' ? [{}, config] : [config, onLaunch];
209
+ const drop_pending_updates = cfg.dropPendingUpdates;
210
+ const allowed_updates = cfg.allowedUpdates;
211
+ const webhook = cfg.webhook;
212
+ debug('Connecting to Telegram');
213
+ (_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = await this.telegram.getMe());
214
+ onMe === null || onMe === void 0 ? void 0 : onMe();
215
+ debug(`Launching @${this.botInfo.username}`);
216
+ if (webhook === undefined) {
217
+ await this.telegram.deleteWebhook({ drop_pending_updates });
218
+ debug('Bot started with long polling');
219
+ await this.startPolling(allowed_updates);
220
+ return;
221
+ }
222
+ const domainOpts = this.getDomainOpts({
223
+ domain: webhook.domain,
224
+ path: (_b = webhook.path) !== null && _b !== void 0 ? _b : webhook.hookPath,
225
+ });
226
+ const { tlsOptions, port, host, cb, secretToken } = webhook;
227
+ this.startWebhook(domainOpts.path, tlsOptions, port, host, cb, secretToken);
228
+ await this.telegram.setWebhook(domainOpts.url, {
229
+ drop_pending_updates: drop_pending_updates,
230
+ allowed_updates: allowed_updates,
231
+ ip_address: webhook.ipAddress,
232
+ max_connections: webhook.maxConnections,
233
+ secret_token: webhook.secretToken,
234
+ certificate: webhook.certificate,
235
+ });
236
+ debug(`Bot started with webhook @ ${domainOpts.url}`);
237
+ }
238
+ stop(reason = 'unspecified') {
239
+ var _a, _b;
240
+ debug('Stopping bot... Reason:', reason);
241
+ // https://github.com/telegraf/telegraf/pull/1224#issuecomment-742693770
242
+ if (this.polling === undefined && this.webhookServer === undefined) {
243
+ throw new Error('Bot is not running!');
244
+ }
245
+ (_a = this.webhookServer) === null || _a === void 0 ? void 0 : _a.close();
246
+ (_b = this.polling) === null || _b === void 0 ? void 0 : _b.stop();
247
+ }
248
+ async handleUpdate(update, webhookResponse) {
249
+ var _a, _b;
250
+ (_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = (debug('Update %d is waiting for `botInfo` to be initialized', update.update_id),
251
+ await ((_b = this.botInfoCall) !== null && _b !== void 0 ? _b : (this.botInfoCall = this.telegram.getMe()))));
252
+ debug('Processing update', update.update_id);
253
+ const tg = new telegram_1.default(this.token, this.telegram.options, webhookResponse);
254
+ const TelegrafContext = this.options.contextType;
255
+ const ctx = new TelegrafContext(update, tg, this.botInfo);
256
+ Object.assign(ctx, this.context);
257
+ try {
258
+ await (0, p_timeout_1.default)(Promise.resolve(this.middleware()(ctx, anoop)), this.options.handlerTimeout);
259
+ }
260
+ catch (err) {
261
+ return await this.handleError(err, ctx);
262
+ }
263
+ finally {
264
+ if ((webhookResponse === null || webhookResponse === void 0 ? void 0 : webhookResponse.writableEnded) === false) {
265
+ webhookResponse.end();
266
+ }
267
+ debug('Finished processing update', update.update_id);
268
+ }
269
+ }
270
+ }
271
+ exports.Telegraf = Telegraf;
272
+ Telegraf._bannerPrinted = false;
273
+ //# sourceMappingURL=telegraf.js.map
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /** @format */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Markup = void 0;
5
+ var markup_1 = require("./markup");
6
+ Object.defineProperty(exports, "Markup", { enumerable: true, get: function () { return markup_1.Markup; } });
7
+ //# sourceMappingURL=telegram-types.js.map