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
package/lib/input.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromFileId = exports.fromURL = exports.fromURLStream = exports.fromReadableStream = exports.fromBuffer = exports.fromLocalFile = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The local file specified by path will be uploaded to Telegram using multipart/form-data.
|
|
6
|
+
*
|
|
7
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
8
|
+
*/
|
|
9
|
+
// prettier-ignore
|
|
10
|
+
const fromLocalFile = (path, filename) => ({ source: path, filename });
|
|
11
|
+
exports.fromLocalFile = fromLocalFile;
|
|
12
|
+
/**
|
|
13
|
+
* The buffer will be uploaded as file to Telegram using multipart/form-data.
|
|
14
|
+
*
|
|
15
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
16
|
+
*/
|
|
17
|
+
// prettier-ignore
|
|
18
|
+
const fromBuffer = (buffer, filename) => ({ source: buffer, filename });
|
|
19
|
+
exports.fromBuffer = fromBuffer;
|
|
20
|
+
/**
|
|
21
|
+
* Contents of the stream will be uploaded as file to Telegram using multipart/form-data.
|
|
22
|
+
*
|
|
23
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
24
|
+
*/
|
|
25
|
+
// prettier-ignore
|
|
26
|
+
const fromReadableStream = (stream, filename) => ({ source: stream, filename });
|
|
27
|
+
exports.fromReadableStream = fromReadableStream;
|
|
28
|
+
/**
|
|
29
|
+
* Contents of the URL will be streamed to Telegram.
|
|
30
|
+
*
|
|
31
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
32
|
+
*/
|
|
33
|
+
// prettier-ignore
|
|
34
|
+
const fromURLStream = (url, filename) => ({ url: url.toString(), filename });
|
|
35
|
+
exports.fromURLStream = fromURLStream;
|
|
36
|
+
/**
|
|
37
|
+
* Provide Telegram with an HTTP URL for the file to be sent.
|
|
38
|
+
* Telegram will download and send the file.
|
|
39
|
+
*
|
|
40
|
+
* * The target file must have the correct MIME type (e.g., audio/mpeg for `sendAudio`, etc.).
|
|
41
|
+
* * `sendDocument` with URL will currently only work for GIF, PDF and ZIP files.
|
|
42
|
+
* * To use `sendVoice`, the file must have the type audio/ogg and be no more than 1MB in size.
|
|
43
|
+
* 1-20MB voice notes will be sent as files.
|
|
44
|
+
*
|
|
45
|
+
* 5 MB max size for photos and 20 MB max for other types of content.
|
|
46
|
+
*/
|
|
47
|
+
const fromURL = (url) => url.toString();
|
|
48
|
+
exports.fromURL = fromURL;
|
|
49
|
+
/**
|
|
50
|
+
* If the file is already stored somewhere on the Telegram servers, you don't need to reupload it:
|
|
51
|
+
* each file object has a file_id field, simply pass this file_id as a parameter instead of uploading.
|
|
52
|
+
*
|
|
53
|
+
* It is not possible to change the file type when resending by file_id.
|
|
54
|
+
*
|
|
55
|
+
* It is not possible to resend thumbnails using file_id.
|
|
56
|
+
* They have to be uploaded using one of the other Input methods.
|
|
57
|
+
*
|
|
58
|
+
* There are no limits for files sent this way.
|
|
59
|
+
*/
|
|
60
|
+
const fromFileId = (fileId) => fileId;
|
|
61
|
+
exports.fromFileId = fromFileId;
|
package/lib/markup.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.inlineKeyboard = exports.keyboard = exports.forceReply = exports.removeKeyboard = exports.button = exports.Markup = void 0;
|
|
27
|
+
const check_1 = require("./core/helpers/check");
|
|
28
|
+
class Markup {
|
|
29
|
+
constructor(reply_markup) {
|
|
30
|
+
this.reply_markup = reply_markup;
|
|
31
|
+
}
|
|
32
|
+
selective(value = true) {
|
|
33
|
+
return new Markup({ ...this.reply_markup, selective: value });
|
|
34
|
+
}
|
|
35
|
+
placeholder(placeholder) {
|
|
36
|
+
return new Markup({
|
|
37
|
+
...this.reply_markup,
|
|
38
|
+
input_field_placeholder: placeholder,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
resize(value = true) {
|
|
42
|
+
return new Markup({
|
|
43
|
+
...this.reply_markup,
|
|
44
|
+
resize_keyboard: value,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
oneTime(value = true) {
|
|
48
|
+
return new Markup({
|
|
49
|
+
...this.reply_markup,
|
|
50
|
+
one_time_keyboard: value,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
persistent(value = true) {
|
|
54
|
+
return new Markup({
|
|
55
|
+
...this.reply_markup,
|
|
56
|
+
is_persistent: value,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.Markup = Markup;
|
|
61
|
+
exports.button = __importStar(require("./button"));
|
|
62
|
+
function removeKeyboard() {
|
|
63
|
+
return new Markup({ remove_keyboard: true });
|
|
64
|
+
}
|
|
65
|
+
exports.removeKeyboard = removeKeyboard;
|
|
66
|
+
function forceReply() {
|
|
67
|
+
return new Markup({ force_reply: true });
|
|
68
|
+
}
|
|
69
|
+
exports.forceReply = forceReply;
|
|
70
|
+
function keyboard(buttons, options) {
|
|
71
|
+
const keyboard = buildKeyboard(buttons, {
|
|
72
|
+
columns: 1,
|
|
73
|
+
...options,
|
|
74
|
+
});
|
|
75
|
+
return new Markup({ keyboard });
|
|
76
|
+
}
|
|
77
|
+
exports.keyboard = keyboard;
|
|
78
|
+
function inlineKeyboard(buttons, options) {
|
|
79
|
+
const inlineKeyboard = buildKeyboard(buttons, {
|
|
80
|
+
columns: buttons.length,
|
|
81
|
+
...options,
|
|
82
|
+
});
|
|
83
|
+
return new Markup({ inline_keyboard: inlineKeyboard });
|
|
84
|
+
}
|
|
85
|
+
exports.inlineKeyboard = inlineKeyboard;
|
|
86
|
+
function buildKeyboard(buttons, options) {
|
|
87
|
+
const result = [];
|
|
88
|
+
if (!Array.isArray(buttons)) {
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
if ((0, check_1.is2D)(buttons)) {
|
|
92
|
+
return buttons.map((row) => row.filter((button) => !button.hide));
|
|
93
|
+
}
|
|
94
|
+
const wrapFn = options.wrap !== undefined
|
|
95
|
+
? options.wrap
|
|
96
|
+
: (_btn, _index, currentRow) => currentRow.length >= options.columns;
|
|
97
|
+
let currentRow = [];
|
|
98
|
+
let index = 0;
|
|
99
|
+
for (const btn of buttons.filter((button) => !button.hide)) {
|
|
100
|
+
if (wrapFn(btn, index, currentRow) && currentRow.length > 0) {
|
|
101
|
+
result.push(currentRow);
|
|
102
|
+
currentRow = [];
|
|
103
|
+
}
|
|
104
|
+
currentRow.push(btn);
|
|
105
|
+
index++;
|
|
106
|
+
}
|
|
107
|
+
if (currentRow.length > 0) {
|
|
108
|
+
result.push(currentRow);
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
package/lib/paginated.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.PaginatedBuilder = void 0;
|
|
27
|
+
const Markup = __importStar(require("./markup"));
|
|
28
|
+
class PaginatedBuilder {
|
|
29
|
+
static create(items, options = {}) {
|
|
30
|
+
const page = options.page ?? 1;
|
|
31
|
+
const pageSize = options.pageSize ?? 5;
|
|
32
|
+
const prefix = options.callbackPrefix ?? 'page';
|
|
33
|
+
const totalItems = items.length;
|
|
34
|
+
const totalPages = Math.max(1, Math.ceil(totalItems / pageSize));
|
|
35
|
+
const currentPage = Math.min(Math.max(1, page), totalPages);
|
|
36
|
+
const startIndex = (currentPage - 1) * pageSize;
|
|
37
|
+
const pageItems = items.slice(startIndex, startIndex + pageSize);
|
|
38
|
+
let text = '';
|
|
39
|
+
if (options.formatItem) {
|
|
40
|
+
text = pageItems.map((item, idx) => options.formatItem(item, startIndex + idx)).join('\n');
|
|
41
|
+
text += `\n\n<i>Halaman ${currentPage}/${totalPages} (Total: ${totalItems})</i>`;
|
|
42
|
+
}
|
|
43
|
+
const buttons = [];
|
|
44
|
+
// Item buttons if defined
|
|
45
|
+
if (options.buttonText && options.buttonData) {
|
|
46
|
+
pageItems.forEach((item) => {
|
|
47
|
+
buttons.push([Markup.button.callback(options.buttonText(item), options.buttonData(item))]);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Navigation row
|
|
51
|
+
const navRow = [];
|
|
52
|
+
if (currentPage > 1) {
|
|
53
|
+
navRow.push(Markup.button.callback('« Prev', `${prefix}:${currentPage - 1}`));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
navRow.push(Markup.button.callback('·', 'noop'));
|
|
57
|
+
}
|
|
58
|
+
navRow.push(Markup.button.callback(`${currentPage}/${totalPages}`, 'noop'));
|
|
59
|
+
if (currentPage < totalPages) {
|
|
60
|
+
navRow.push(Markup.button.callback('Next »', `${prefix}:${currentPage + 1}`));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
navRow.push(Markup.button.callback('·', 'noop'));
|
|
64
|
+
}
|
|
65
|
+
buttons.push(navRow);
|
|
66
|
+
return {
|
|
67
|
+
items: pageItems,
|
|
68
|
+
page: currentPage,
|
|
69
|
+
totalPages,
|
|
70
|
+
totalItems,
|
|
71
|
+
text,
|
|
72
|
+
keyboard: Markup.inlineKeyboard(buttons),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.PaginatedBuilder = PaginatedBuilder;
|
package/lib/reactions.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageReactions = exports.ReactionList = exports.Digit = void 0;
|
|
4
|
+
const util_1 = require("./core/helpers/util");
|
|
5
|
+
exports.Digit = new Set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
|
|
6
|
+
const inspectReaction = (reaction) => {
|
|
7
|
+
if (reaction.type === 'custom_emoji')
|
|
8
|
+
return `Custom(${reaction.custom_emoji_id})`;
|
|
9
|
+
else
|
|
10
|
+
return reaction.emoji;
|
|
11
|
+
};
|
|
12
|
+
class ReactionList {
|
|
13
|
+
constructor(list) {
|
|
14
|
+
this.list = list;
|
|
15
|
+
}
|
|
16
|
+
static fromArray(list = []) {
|
|
17
|
+
return (0, util_1.indexed)(new ReactionList(list), function (index) {
|
|
18
|
+
return this.list[index];
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
static has(reactions, reaction) {
|
|
22
|
+
if (typeof reaction === 'string')
|
|
23
|
+
if (exports.Digit.has(reaction[0]))
|
|
24
|
+
return reactions.some((r) => r.custom_emoji_id === reaction);
|
|
25
|
+
else
|
|
26
|
+
return reactions.some((r) => r.emoji === reaction);
|
|
27
|
+
return reactions.some((r) => {
|
|
28
|
+
if (r.type === 'custom_emoji')
|
|
29
|
+
return r.custom_emoji_id === reaction.custom_emoji_id;
|
|
30
|
+
else if (r.type === 'emoji')
|
|
31
|
+
return r.emoji === reaction.emoji;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
toArray() {
|
|
35
|
+
return [...this.list];
|
|
36
|
+
}
|
|
37
|
+
filter(filterFn) {
|
|
38
|
+
return ReactionList.fromArray(this.list.filter(filterFn));
|
|
39
|
+
}
|
|
40
|
+
has(reaction) {
|
|
41
|
+
return ReactionList.has(this.list, reaction);
|
|
42
|
+
}
|
|
43
|
+
get count() {
|
|
44
|
+
return this.list.length;
|
|
45
|
+
}
|
|
46
|
+
[Symbol.iterator]() {
|
|
47
|
+
return this.list[Symbol.iterator]();
|
|
48
|
+
}
|
|
49
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
50
|
+
const flattened = this.list.map(inspectReaction).join(', ');
|
|
51
|
+
return ['ReactionList {', flattened, '}'].join(' ');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.ReactionList = ReactionList;
|
|
55
|
+
class MessageReactions extends ReactionList {
|
|
56
|
+
constructor(ctx) {
|
|
57
|
+
super(ctx.update.message_reaction?.new_reaction ?? []);
|
|
58
|
+
this.ctx = ctx;
|
|
59
|
+
}
|
|
60
|
+
static from(ctx) {
|
|
61
|
+
return (0, util_1.indexed)(new MessageReactions(ctx), function (index) {
|
|
62
|
+
return this.list[index];
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
get old() {
|
|
66
|
+
return ReactionList.fromArray(this.ctx.update.message_reaction?.old_reaction);
|
|
67
|
+
}
|
|
68
|
+
get new() {
|
|
69
|
+
return ReactionList.fromArray(this.ctx.update.message_reaction?.new_reaction);
|
|
70
|
+
}
|
|
71
|
+
get added() {
|
|
72
|
+
return this.new.filter((reaction) => !this.old.has(reaction));
|
|
73
|
+
}
|
|
74
|
+
get removed() {
|
|
75
|
+
return this.old.filter((reaction) => !this.new.has(reaction));
|
|
76
|
+
}
|
|
77
|
+
get kept() {
|
|
78
|
+
return this.new.filter((reaction) => this.old.has(reaction));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.MessageReactions = MessageReactions;
|
package/lib/router.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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.Router = void 0;
|
|
8
|
+
const composer_1 = __importDefault(require("./composer"));
|
|
9
|
+
/** @deprecated in favor of {@link Composer.dispatch} */
|
|
10
|
+
class Router {
|
|
11
|
+
constructor(routeFn, handlers = new Map()) {
|
|
12
|
+
this.routeFn = routeFn;
|
|
13
|
+
this.handlers = handlers;
|
|
14
|
+
this.otherwiseHandler = composer_1.default.passThru();
|
|
15
|
+
if (typeof routeFn !== 'function') {
|
|
16
|
+
throw new Error('Missing routing function');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
on(route, ...fns) {
|
|
20
|
+
if (fns.length === 0) {
|
|
21
|
+
throw new TypeError('At least one handler must be provided');
|
|
22
|
+
}
|
|
23
|
+
this.handlers.set(route, composer_1.default.compose(fns));
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
otherwise(...fns) {
|
|
27
|
+
if (fns.length === 0) {
|
|
28
|
+
throw new TypeError('At least one otherwise handler must be provided');
|
|
29
|
+
}
|
|
30
|
+
this.otherwiseHandler = composer_1.default.compose(fns);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
middleware() {
|
|
34
|
+
return composer_1.default.lazy((ctx) => {
|
|
35
|
+
const result = this.routeFn(ctx);
|
|
36
|
+
if (result == null) {
|
|
37
|
+
return this.otherwiseHandler;
|
|
38
|
+
}
|
|
39
|
+
Object.assign(ctx, result.context);
|
|
40
|
+
Object.assign(ctx.state, result.state);
|
|
41
|
+
return this.handlers.get(result.route) ?? this.otherwiseHandler;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.Router = Router;
|
|
@@ -0,0 +1,39 @@
|
|
|
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.BaseScene = void 0;
|
|
7
|
+
const composer_1 = __importDefault(require("../composer"));
|
|
8
|
+
const { compose } = composer_1.default;
|
|
9
|
+
class BaseScene extends composer_1.default {
|
|
10
|
+
constructor(id, options) {
|
|
11
|
+
const opts = {
|
|
12
|
+
handlers: [],
|
|
13
|
+
enterHandlers: [],
|
|
14
|
+
leaveHandlers: [],
|
|
15
|
+
...options,
|
|
16
|
+
};
|
|
17
|
+
super(...opts.handlers);
|
|
18
|
+
this.id = id;
|
|
19
|
+
this.ttl = opts.ttl;
|
|
20
|
+
this.enterHandler = compose(opts.enterHandlers);
|
|
21
|
+
this.leaveHandler = compose(opts.leaveHandlers);
|
|
22
|
+
}
|
|
23
|
+
enter(...fns) {
|
|
24
|
+
this.enterHandler = compose([this.enterHandler, ...fns]);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
leave(...fns) {
|
|
28
|
+
this.leaveHandler = compose([this.leaveHandler, ...fns]);
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
enterMiddleware() {
|
|
32
|
+
return this.enterHandler;
|
|
33
|
+
}
|
|
34
|
+
leaveMiddleware() {
|
|
35
|
+
return this.leaveHandler;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.BaseScene = BaseScene;
|
|
39
|
+
exports.default = BaseScene;
|
|
@@ -0,0 +1,99 @@
|
|
|
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 composer_1 = __importDefault(require("../composer"));
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('telegraf:scenes:context');
|
|
9
|
+
const noop = () => Promise.resolve();
|
|
10
|
+
const now = () => Math.floor(Date.now() / 1000);
|
|
11
|
+
class SceneContextScene {
|
|
12
|
+
constructor(ctx, scenes, options) {
|
|
13
|
+
this.ctx = ctx;
|
|
14
|
+
this.scenes = scenes;
|
|
15
|
+
this.leaving = false;
|
|
16
|
+
// @ts-expect-error {} might not be assignable to D
|
|
17
|
+
const fallbackSessionDefault = {};
|
|
18
|
+
this.options = { defaultSession: fallbackSessionDefault, ...options };
|
|
19
|
+
}
|
|
20
|
+
get session() {
|
|
21
|
+
const defaultSession = Object.assign({}, this.options.defaultSession);
|
|
22
|
+
let session = this.ctx.session?.__scenes ?? defaultSession;
|
|
23
|
+
if (session.expires !== undefined && session.expires < now()) {
|
|
24
|
+
session = defaultSession;
|
|
25
|
+
}
|
|
26
|
+
if (this.ctx.session === undefined) {
|
|
27
|
+
this.ctx.session = { __scenes: session };
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.ctx.session.__scenes = session;
|
|
31
|
+
}
|
|
32
|
+
return session;
|
|
33
|
+
}
|
|
34
|
+
get state() {
|
|
35
|
+
return (this.session.state ??= {});
|
|
36
|
+
}
|
|
37
|
+
set state(value) {
|
|
38
|
+
this.session.state = { ...value };
|
|
39
|
+
}
|
|
40
|
+
get current() {
|
|
41
|
+
const sceneId = this.session.current ?? this.options.default;
|
|
42
|
+
return sceneId === undefined || !this.scenes.has(sceneId)
|
|
43
|
+
? undefined
|
|
44
|
+
: this.scenes.get(sceneId);
|
|
45
|
+
}
|
|
46
|
+
reset() {
|
|
47
|
+
if (this.ctx.session !== undefined)
|
|
48
|
+
this.ctx.session.__scenes = Object.assign({}, this.options.defaultSession);
|
|
49
|
+
}
|
|
50
|
+
async enter(sceneId, initialState = {}, silent = false) {
|
|
51
|
+
if (!this.scenes.has(sceneId)) {
|
|
52
|
+
throw new Error(`Can't find scene: ${sceneId}`);
|
|
53
|
+
}
|
|
54
|
+
if (!silent) {
|
|
55
|
+
await this.leave();
|
|
56
|
+
}
|
|
57
|
+
debug('Entering scene', sceneId, initialState, silent);
|
|
58
|
+
this.session.current = sceneId;
|
|
59
|
+
this.state = initialState;
|
|
60
|
+
const ttl = this.current?.ttl ?? this.options.ttl;
|
|
61
|
+
if (ttl !== undefined) {
|
|
62
|
+
this.session.expires = now() + ttl;
|
|
63
|
+
}
|
|
64
|
+
if (this.current === undefined || silent) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const handler = 'enterMiddleware' in this.current &&
|
|
68
|
+
typeof this.current.enterMiddleware === 'function'
|
|
69
|
+
? this.current.enterMiddleware()
|
|
70
|
+
: this.current.middleware();
|
|
71
|
+
return await handler(this.ctx, noop);
|
|
72
|
+
}
|
|
73
|
+
reenter() {
|
|
74
|
+
return this.session.current === undefined
|
|
75
|
+
? undefined
|
|
76
|
+
: this.enter(this.session.current, this.state);
|
|
77
|
+
}
|
|
78
|
+
async leave() {
|
|
79
|
+
if (this.leaving)
|
|
80
|
+
return;
|
|
81
|
+
debug('Leaving scene');
|
|
82
|
+
try {
|
|
83
|
+
this.leaving = true;
|
|
84
|
+
if (this.current === undefined) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const handler = 'leaveMiddleware' in this.current &&
|
|
88
|
+
typeof this.current.leaveMiddleware === 'function'
|
|
89
|
+
? this.current.leaveMiddleware()
|
|
90
|
+
: composer_1.default.passThru();
|
|
91
|
+
await handler(this.ctx, noop);
|
|
92
|
+
return this.reset();
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
this.leaving = false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.default = SceneContextScene;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FormScene = void 0;
|
|
4
|
+
const composer_1 = require("../composer");
|
|
5
|
+
class FormScene extends composer_1.Composer {
|
|
6
|
+
constructor(id) {
|
|
7
|
+
super();
|
|
8
|
+
this.steps = [];
|
|
9
|
+
this.id = id;
|
|
10
|
+
}
|
|
11
|
+
field(field, options) {
|
|
12
|
+
this.steps.push({ field, ...options });
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
onComplete(handler) {
|
|
16
|
+
this.completionHandler = handler;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
getSteps() {
|
|
20
|
+
return this.steps;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.FormScene = FormScene;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @see https://github.com/telegraf/telegraf/issues/705#issuecomment-549056045
|
|
4
|
+
* @see https://www.npmjs.com/package/telegraf-stateless-question
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.WizardContextWizard = exports.WizardScene = exports.BaseScene = exports.SceneContextScene = exports.Stage = void 0;
|
|
12
|
+
var stage_1 = require("./stage");
|
|
13
|
+
Object.defineProperty(exports, "Stage", { enumerable: true, get: function () { return stage_1.Stage; } });
|
|
14
|
+
var context_1 = require("./context");
|
|
15
|
+
Object.defineProperty(exports, "SceneContextScene", { enumerable: true, get: function () { return __importDefault(context_1).default; } });
|
|
16
|
+
var base_1 = require("./base");
|
|
17
|
+
Object.defineProperty(exports, "BaseScene", { enumerable: true, get: function () { return base_1.BaseScene; } });
|
|
18
|
+
var wizard_1 = require("./wizard");
|
|
19
|
+
Object.defineProperty(exports, "WizardScene", { enumerable: true, get: function () { return wizard_1.WizardScene; } });
|
|
20
|
+
var context_2 = require("./wizard/context");
|
|
21
|
+
Object.defineProperty(exports, "WizardContextWizard", { enumerable: true, get: function () { return __importDefault(context_2).default; } });
|
|
@@ -0,0 +1,49 @@
|
|
|
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.Stage = void 0;
|
|
7
|
+
const session_1 = require("../session");
|
|
8
|
+
const context_1 = __importDefault(require("./context"));
|
|
9
|
+
const composer_1 = require("../composer");
|
|
10
|
+
class Stage extends composer_1.Composer {
|
|
11
|
+
constructor(scenes = [], options) {
|
|
12
|
+
super();
|
|
13
|
+
this.options = { ...options };
|
|
14
|
+
this.scenes = new Map();
|
|
15
|
+
scenes.forEach((scene) => this.register(scene));
|
|
16
|
+
}
|
|
17
|
+
register(...scenes) {
|
|
18
|
+
scenes.forEach((scene) => {
|
|
19
|
+
if (scene?.id == null || typeof scene.middleware !== 'function') {
|
|
20
|
+
throw new Error('telegraf: Unsupported scene');
|
|
21
|
+
}
|
|
22
|
+
this.scenes.set(scene.id, scene);
|
|
23
|
+
});
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
middleware() {
|
|
27
|
+
const handler = composer_1.Composer.compose([
|
|
28
|
+
(ctx, next) => {
|
|
29
|
+
const scenes = this.scenes;
|
|
30
|
+
const scene = new context_1.default(ctx, scenes, this.options);
|
|
31
|
+
ctx.scene = scene;
|
|
32
|
+
return next();
|
|
33
|
+
},
|
|
34
|
+
super.middleware(),
|
|
35
|
+
composer_1.Composer.lazy((ctx) => ctx.scene.current ?? composer_1.Composer.passThru()),
|
|
36
|
+
]);
|
|
37
|
+
return composer_1.Composer.optional(session_1.isSessionContext, handler);
|
|
38
|
+
}
|
|
39
|
+
static enter(...args) {
|
|
40
|
+
return (ctx) => ctx.scene.enter(...args);
|
|
41
|
+
}
|
|
42
|
+
static reenter(...args) {
|
|
43
|
+
return (ctx) => ctx.scene.reenter(...args);
|
|
44
|
+
}
|
|
45
|
+
static leave(...args) {
|
|
46
|
+
return (ctx) => ctx.scene.leave(...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.Stage = Stage;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class WizardContextWizard {
|
|
4
|
+
constructor(ctx, steps) {
|
|
5
|
+
this.ctx = ctx;
|
|
6
|
+
this.steps = steps;
|
|
7
|
+
this.state = ctx.scene.state;
|
|
8
|
+
this.cursor = ctx.scene.session.cursor ?? 0;
|
|
9
|
+
}
|
|
10
|
+
get step() {
|
|
11
|
+
return this.steps[this.cursor];
|
|
12
|
+
}
|
|
13
|
+
get cursor() {
|
|
14
|
+
return this.ctx.scene.session.cursor;
|
|
15
|
+
}
|
|
16
|
+
set cursor(cursor) {
|
|
17
|
+
this.ctx.scene.session.cursor = cursor;
|
|
18
|
+
}
|
|
19
|
+
selectStep(index) {
|
|
20
|
+
this.cursor = index;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
next() {
|
|
24
|
+
return this.selectStep(this.cursor + 1);
|
|
25
|
+
}
|
|
26
|
+
back() {
|
|
27
|
+
return this.selectStep(this.cursor - 1);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.default = WizardContextWizard;
|