@tgify/tgify 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +356 -0
- package/filters.d.ts +1 -0
- package/filters.js +1 -0
- package/format.d.ts +1 -0
- package/format.js +1 -0
- package/future.d.ts +1 -0
- package/future.js +1 -0
- package/lib/button.js +100 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +582 -0
- package/lib/context.js +1219 -0
- package/lib/core/helpers/args.js +57 -0
- package/lib/core/helpers/check.js +55 -0
- package/lib/core/helpers/compact.js +16 -0
- package/lib/core/helpers/deunionize.js +12 -0
- package/lib/core/helpers/formatting.js +91 -0
- package/lib/core/helpers/util.js +50 -0
- package/lib/core/network/client.js +330 -0
- package/lib/core/network/error.js +21 -0
- package/lib/core/network/multipart-stream.js +71 -0
- package/lib/core/network/polling.js +87 -0
- package/lib/core/network/webhook.js +54 -0
- package/lib/core/types/typegram.js +27 -0
- package/lib/filters.js +69 -0
- package/lib/format.js +38 -0
- package/lib/future.js +149 -0
- package/lib/index.js +58 -0
- package/lib/input.js +61 -0
- package/lib/markup.js +121 -0
- package/lib/middleware.js +2 -0
- package/lib/reactions.js +84 -0
- package/lib/router.js +46 -0
- package/lib/scenes/base.js +39 -0
- package/lib/scenes/context.js +104 -0
- package/lib/scenes/index.js +21 -0
- package/lib/scenes/stage.js +49 -0
- package/lib/scenes/wizard/context.js +31 -0
- package/lib/scenes/wizard/index.js +45 -0
- package/lib/scenes.js +17 -0
- package/lib/session.js +166 -0
- package/lib/telegraf.js +256 -0
- package/lib/telegram-types.js +6 -0
- package/lib/telegram.js +1240 -0
- package/lib/types.js +2 -0
- package/lib/utils.js +5 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +140 -0
- package/scenes.d.ts +1 -0
- package/scenes.js +1 -0
- package/session.d.ts +1 -0
- package/session.js +1 -0
- package/src/button.ts +182 -0
- package/src/composer.ts +1008 -0
- package/src/context.ts +1661 -0
- package/src/core/helpers/args.ts +63 -0
- package/src/core/helpers/check.ts +71 -0
- package/src/core/helpers/compact.ts +18 -0
- package/src/core/helpers/deunionize.ts +26 -0
- package/src/core/helpers/formatting.ts +119 -0
- package/src/core/helpers/util.ts +96 -0
- package/src/core/network/client.ts +396 -0
- package/src/core/network/error.ts +29 -0
- package/src/core/network/multipart-stream.ts +45 -0
- package/src/core/network/polling.ts +94 -0
- package/src/core/network/webhook.ts +58 -0
- package/src/core/types/typegram.ts +54 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +213 -0
- package/src/index.ts +17 -0
- package/src/input.ts +59 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/reactions.ts +118 -0
- package/src/router.ts +55 -0
- package/src/scenes/base.ts +52 -0
- package/src/scenes/context.ts +136 -0
- package/src/scenes/index.ts +21 -0
- package/src/scenes/stage.ts +71 -0
- package/src/scenes/wizard/context.ts +58 -0
- package/src/scenes/wizard/index.ts +63 -0
- package/src/scenes.ts +1 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +354 -0
- package/src/telegram-types.ts +219 -0
- package/src/telegram.ts +1635 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +36 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +227 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +655 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/args.d.ts +11 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +56 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +4 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +18 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +30 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/util.d.ts +26 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +53 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +16 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +16 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +16 -0
- package/typings/core/network/polling.d.ts.map +1 -0
- package/typings/core/network/webhook.d.ts +6 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +42 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +18 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +22 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +12 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +15 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +50 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +27 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +8 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/reactions.d.ts +32 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +21 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +22 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +36 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/index.d.ts +11 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +24 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +29 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +16 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +2 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +55 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +115 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +117 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +675 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +3 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +2 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/lib/markup.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.button = exports.Markup = void 0;
|
|
37
|
+
exports.removeKeyboard = removeKeyboard;
|
|
38
|
+
exports.forceReply = forceReply;
|
|
39
|
+
exports.keyboard = keyboard;
|
|
40
|
+
exports.inlineKeyboard = inlineKeyboard;
|
|
41
|
+
const check_1 = require("./core/helpers/check");
|
|
42
|
+
class Markup {
|
|
43
|
+
constructor(reply_markup) {
|
|
44
|
+
this.reply_markup = reply_markup;
|
|
45
|
+
}
|
|
46
|
+
selective(value = true) {
|
|
47
|
+
return new Markup({ ...this.reply_markup, selective: value });
|
|
48
|
+
}
|
|
49
|
+
placeholder(placeholder) {
|
|
50
|
+
return new Markup({
|
|
51
|
+
...this.reply_markup,
|
|
52
|
+
input_field_placeholder: placeholder,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
resize(value = true) {
|
|
56
|
+
return new Markup({
|
|
57
|
+
...this.reply_markup,
|
|
58
|
+
resize_keyboard: value,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
oneTime(value = true) {
|
|
62
|
+
return new Markup({
|
|
63
|
+
...this.reply_markup,
|
|
64
|
+
one_time_keyboard: value,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
persistent(value = true) {
|
|
68
|
+
return new Markup({
|
|
69
|
+
...this.reply_markup,
|
|
70
|
+
is_persistent: value,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.Markup = Markup;
|
|
75
|
+
exports.button = __importStar(require("./button"));
|
|
76
|
+
function removeKeyboard() {
|
|
77
|
+
return new Markup({ remove_keyboard: true });
|
|
78
|
+
}
|
|
79
|
+
function forceReply() {
|
|
80
|
+
return new Markup({ force_reply: true });
|
|
81
|
+
}
|
|
82
|
+
function keyboard(buttons, options) {
|
|
83
|
+
const keyboard = buildKeyboard(buttons, {
|
|
84
|
+
columns: 1,
|
|
85
|
+
...options,
|
|
86
|
+
});
|
|
87
|
+
return new Markup({ keyboard });
|
|
88
|
+
}
|
|
89
|
+
function inlineKeyboard(buttons, options) {
|
|
90
|
+
const inlineKeyboard = buildKeyboard(buttons, {
|
|
91
|
+
columns: buttons.length,
|
|
92
|
+
...options,
|
|
93
|
+
});
|
|
94
|
+
return new Markup({ inline_keyboard: inlineKeyboard });
|
|
95
|
+
}
|
|
96
|
+
function buildKeyboard(buttons, options) {
|
|
97
|
+
const result = [];
|
|
98
|
+
if (!Array.isArray(buttons)) {
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
if ((0, check_1.is2D)(buttons)) {
|
|
102
|
+
return buttons.map((row) => row.filter((button) => !button.hide));
|
|
103
|
+
}
|
|
104
|
+
const wrapFn = options.wrap !== undefined
|
|
105
|
+
? options.wrap
|
|
106
|
+
: (_btn, _index, currentRow) => currentRow.length >= options.columns;
|
|
107
|
+
let currentRow = [];
|
|
108
|
+
let index = 0;
|
|
109
|
+
for (const btn of buttons.filter((button) => !button.hide)) {
|
|
110
|
+
if (wrapFn(btn, index, currentRow) && currentRow.length > 0) {
|
|
111
|
+
result.push(currentRow);
|
|
112
|
+
currentRow = [];
|
|
113
|
+
}
|
|
114
|
+
currentRow.push(btn);
|
|
115
|
+
index++;
|
|
116
|
+
}
|
|
117
|
+
if (currentRow.length > 0) {
|
|
118
|
+
result.push(currentRow);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
package/lib/reactions.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
var _a, _b;
|
|
58
|
+
super((_b = (_a = ctx.update.message_reaction) === null || _a === void 0 ? void 0 : _a.new_reaction) !== null && _b !== void 0 ? _b : []);
|
|
59
|
+
this.ctx = ctx;
|
|
60
|
+
}
|
|
61
|
+
static from(ctx) {
|
|
62
|
+
return (0, util_1.indexed)(new MessageReactions(ctx), function (index) {
|
|
63
|
+
return this.list[index];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
get old() {
|
|
67
|
+
var _a;
|
|
68
|
+
return ReactionList.fromArray((_a = this.ctx.update.message_reaction) === null || _a === void 0 ? void 0 : _a.old_reaction);
|
|
69
|
+
}
|
|
70
|
+
get new() {
|
|
71
|
+
var _a;
|
|
72
|
+
return ReactionList.fromArray((_a = this.ctx.update.message_reaction) === null || _a === void 0 ? void 0 : _a.new_reaction);
|
|
73
|
+
}
|
|
74
|
+
get added() {
|
|
75
|
+
return this.new.filter((reaction) => !this.old.has(reaction));
|
|
76
|
+
}
|
|
77
|
+
get removed() {
|
|
78
|
+
return this.old.filter((reaction) => !this.new.has(reaction));
|
|
79
|
+
}
|
|
80
|
+
get kept() {
|
|
81
|
+
return this.new.filter((reaction) => this.old.has(reaction));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.MessageReactions = MessageReactions;
|
package/lib/router.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
var _a;
|
|
36
|
+
const result = this.routeFn(ctx);
|
|
37
|
+
if (result == null) {
|
|
38
|
+
return this.otherwiseHandler;
|
|
39
|
+
}
|
|
40
|
+
Object.assign(ctx, result.context);
|
|
41
|
+
Object.assign(ctx.state, result.state);
|
|
42
|
+
return (_a = this.handlers.get(result.route)) !== null && _a !== void 0 ? _a : this.otherwiseHandler;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
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,104 @@
|
|
|
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
|
+
var _a, _b;
|
|
22
|
+
const defaultSession = Object.assign({}, this.options.defaultSession);
|
|
23
|
+
let session = (_b = (_a = this.ctx.session) === null || _a === void 0 ? void 0 : _a.__scenes) !== null && _b !== void 0 ? _b : defaultSession;
|
|
24
|
+
if (session.expires !== undefined && session.expires < now()) {
|
|
25
|
+
session = defaultSession;
|
|
26
|
+
}
|
|
27
|
+
if (this.ctx.session === undefined) {
|
|
28
|
+
this.ctx.session = { __scenes: session };
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.ctx.session.__scenes = session;
|
|
32
|
+
}
|
|
33
|
+
return session;
|
|
34
|
+
}
|
|
35
|
+
get state() {
|
|
36
|
+
var _a;
|
|
37
|
+
var _b;
|
|
38
|
+
return ((_a = (_b = this.session).state) !== null && _a !== void 0 ? _a : (_b.state = {}));
|
|
39
|
+
}
|
|
40
|
+
set state(value) {
|
|
41
|
+
this.session.state = { ...value };
|
|
42
|
+
}
|
|
43
|
+
get current() {
|
|
44
|
+
var _a;
|
|
45
|
+
const sceneId = (_a = this.session.current) !== null && _a !== void 0 ? _a : this.options.default;
|
|
46
|
+
return sceneId === undefined || !this.scenes.has(sceneId)
|
|
47
|
+
? undefined
|
|
48
|
+
: this.scenes.get(sceneId);
|
|
49
|
+
}
|
|
50
|
+
reset() {
|
|
51
|
+
if (this.ctx.session !== undefined)
|
|
52
|
+
this.ctx.session.__scenes = Object.assign({}, this.options.defaultSession);
|
|
53
|
+
}
|
|
54
|
+
async enter(sceneId, initialState = {}, silent = false) {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
if (!this.scenes.has(sceneId)) {
|
|
57
|
+
throw new Error(`Can't find scene: ${sceneId}`);
|
|
58
|
+
}
|
|
59
|
+
if (!silent) {
|
|
60
|
+
await this.leave();
|
|
61
|
+
}
|
|
62
|
+
debug('Entering scene', sceneId, initialState, silent);
|
|
63
|
+
this.session.current = sceneId;
|
|
64
|
+
this.state = initialState;
|
|
65
|
+
const ttl = (_b = (_a = this.current) === null || _a === void 0 ? void 0 : _a.ttl) !== null && _b !== void 0 ? _b : this.options.ttl;
|
|
66
|
+
if (ttl !== undefined) {
|
|
67
|
+
this.session.expires = now() + ttl;
|
|
68
|
+
}
|
|
69
|
+
if (this.current === undefined || silent) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const handler = 'enterMiddleware' in this.current &&
|
|
73
|
+
typeof this.current.enterMiddleware === 'function'
|
|
74
|
+
? this.current.enterMiddleware()
|
|
75
|
+
: this.current.middleware();
|
|
76
|
+
return await handler(this.ctx, noop);
|
|
77
|
+
}
|
|
78
|
+
reenter() {
|
|
79
|
+
return this.session.current === undefined
|
|
80
|
+
? undefined
|
|
81
|
+
: this.enter(this.session.current, this.state);
|
|
82
|
+
}
|
|
83
|
+
async leave() {
|
|
84
|
+
if (this.leaving)
|
|
85
|
+
return;
|
|
86
|
+
debug('Leaving scene');
|
|
87
|
+
try {
|
|
88
|
+
this.leaving = true;
|
|
89
|
+
if (this.current === undefined) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const handler = 'leaveMiddleware' in this.current &&
|
|
93
|
+
typeof this.current.leaveMiddleware === 'function'
|
|
94
|
+
? this.current.leaveMiddleware()
|
|
95
|
+
: composer_1.default.passThru();
|
|
96
|
+
await handler(this.ctx, noop);
|
|
97
|
+
return this.reset();
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
this.leaving = false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.default = SceneContextScene;
|
|
@@ -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 === null || scene === void 0 ? void 0 : 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) => { var _a; return (_a = ctx.scene.current) !== null && _a !== void 0 ? _a : 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,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class WizardContextWizard {
|
|
4
|
+
constructor(ctx, steps) {
|
|
5
|
+
var _a;
|
|
6
|
+
this.ctx = ctx;
|
|
7
|
+
this.steps = steps;
|
|
8
|
+
this.state = ctx.scene.state;
|
|
9
|
+
this.cursor = (_a = ctx.scene.session.cursor) !== null && _a !== void 0 ? _a : 0;
|
|
10
|
+
}
|
|
11
|
+
get step() {
|
|
12
|
+
return this.steps[this.cursor];
|
|
13
|
+
}
|
|
14
|
+
get cursor() {
|
|
15
|
+
return this.ctx.scene.session.cursor;
|
|
16
|
+
}
|
|
17
|
+
set cursor(cursor) {
|
|
18
|
+
this.ctx.scene.session.cursor = cursor;
|
|
19
|
+
}
|
|
20
|
+
selectStep(index) {
|
|
21
|
+
this.cursor = index;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
next() {
|
|
25
|
+
return this.selectStep(this.cursor + 1);
|
|
26
|
+
}
|
|
27
|
+
back() {
|
|
28
|
+
return this.selectStep(this.cursor - 1);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.default = WizardContextWizard;
|
|
@@ -0,0 +1,45 @@
|
|
|
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.WizardScene = void 0;
|
|
7
|
+
const base_1 = __importDefault(require("../base"));
|
|
8
|
+
const context_1 = __importDefault(require("./context"));
|
|
9
|
+
const composer_1 = __importDefault(require("../../composer"));
|
|
10
|
+
class WizardScene extends base_1.default {
|
|
11
|
+
constructor(id, options, ...steps) {
|
|
12
|
+
let opts;
|
|
13
|
+
let s;
|
|
14
|
+
if (typeof options === 'function' || 'middleware' in options) {
|
|
15
|
+
opts = undefined;
|
|
16
|
+
s = [options, ...steps];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
opts = options;
|
|
20
|
+
s = steps;
|
|
21
|
+
}
|
|
22
|
+
super(id, opts);
|
|
23
|
+
this.steps = s;
|
|
24
|
+
}
|
|
25
|
+
middleware() {
|
|
26
|
+
return composer_1.default.compose([
|
|
27
|
+
(ctx, next) => {
|
|
28
|
+
ctx.wizard = new context_1.default(ctx, this.steps);
|
|
29
|
+
return next();
|
|
30
|
+
},
|
|
31
|
+
super.middleware(),
|
|
32
|
+
(ctx, next) => {
|
|
33
|
+
if (ctx.wizard.step === undefined) {
|
|
34
|
+
ctx.wizard.selectStep(0);
|
|
35
|
+
return ctx.scene.leave();
|
|
36
|
+
}
|
|
37
|
+
return composer_1.default.unwrap(ctx.wizard.step)(ctx, next);
|
|
38
|
+
},
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
enterMiddleware() {
|
|
42
|
+
return composer_1.default.compose([this.enterHandler, this.middleware()]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.WizardScene = WizardScene;
|
package/lib/scenes.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./scenes/index.js"), exports);
|