telegram-botbuilder 1.0.11 → 1.0.13
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/lib/bot-service.d.ts +2 -0
- package/lib/bot-service.js +31 -12
- package/package.json +1 -1
- package/src/bot-service.ts +18 -1
package/lib/bot-service.d.ts
CHANGED
|
@@ -11,7 +11,9 @@ export declare class BotBuilder {
|
|
|
11
11
|
ActionSystem: EventEmitter;
|
|
12
12
|
private _userdialogs;
|
|
13
13
|
private _middleware;
|
|
14
|
+
private _mwbreak;
|
|
14
15
|
use(func: BotBuilderMiddleware): void;
|
|
16
|
+
private _mwba;
|
|
15
17
|
private _runmw;
|
|
16
18
|
constructor(schema: Schema, token: string, options?: TelegramBot.ConstructorOptions);
|
|
17
19
|
private _getDialog;
|
package/lib/bot-service.js
CHANGED
|
@@ -60,6 +60,7 @@ var BotBuilder = /** @class */ (function () {
|
|
|
60
60
|
function BotBuilder(schema, token, options) {
|
|
61
61
|
var _this = this;
|
|
62
62
|
this._middleware = [];
|
|
63
|
+
this._mwbreak = {};
|
|
63
64
|
this._bot = new node_telegram_bot_api_1.default(token, options);
|
|
64
65
|
this._schema = schema;
|
|
65
66
|
this._schema.buttons = [];
|
|
@@ -181,31 +182,49 @@ var BotBuilder = /** @class */ (function () {
|
|
|
181
182
|
BotBuilder.prototype.use = function (func) {
|
|
182
183
|
this._middleware.push(func);
|
|
183
184
|
};
|
|
185
|
+
BotBuilder.prototype._mwba = function (desc) {
|
|
186
|
+
var _this = this;
|
|
187
|
+
return new Promise(function (r) {
|
|
188
|
+
_this.ActionSystem.once("mwb_".concat(desc), r);
|
|
189
|
+
});
|
|
190
|
+
};
|
|
184
191
|
BotBuilder.prototype._runmw = function (chat, msg) {
|
|
185
192
|
return __awaiter(this, void 0, void 0, function () {
|
|
186
|
-
var res, _i, _a, mw, _b;
|
|
193
|
+
var desc, mwb, res, _i, _a, mw, _b;
|
|
187
194
|
return __generator(this, function (_c) {
|
|
188
195
|
switch (_c.label) {
|
|
189
196
|
case 0:
|
|
197
|
+
desc = "".concat(chat, "_").concat(msg.message_id || msg.id);
|
|
198
|
+
mwb = this._mwbreak[desc];
|
|
199
|
+
if (typeof mwb === 'boolean')
|
|
200
|
+
return [2 /*return*/, mwb];
|
|
201
|
+
if (!(typeof mwb === 'object')) return [3 /*break*/, 2];
|
|
202
|
+
return [4 /*yield*/, this._mwba(desc)];
|
|
203
|
+
case 1: return [2 /*return*/, _c.sent()];
|
|
204
|
+
case 2:
|
|
205
|
+
this._mwbreak[desc] = null;
|
|
190
206
|
res = false;
|
|
191
207
|
_i = 0, _a = this._middleware;
|
|
192
|
-
_c.label =
|
|
193
|
-
case
|
|
194
|
-
if (!(_i < _a.length)) return [3 /*break*/,
|
|
208
|
+
_c.label = 3;
|
|
209
|
+
case 3:
|
|
210
|
+
if (!(_i < _a.length)) return [3 /*break*/, 7];
|
|
195
211
|
mw = _a[_i];
|
|
196
212
|
_b = res;
|
|
197
|
-
if (_b) return [3 /*break*/,
|
|
213
|
+
if (_b) return [3 /*break*/, 5];
|
|
198
214
|
return [4 /*yield*/, mw(chat, msg)];
|
|
199
|
-
case
|
|
215
|
+
case 4:
|
|
200
216
|
_b = (_c.sent());
|
|
201
|
-
_c.label =
|
|
202
|
-
case
|
|
217
|
+
_c.label = 5;
|
|
218
|
+
case 5:
|
|
203
219
|
res = _b;
|
|
204
|
-
_c.label =
|
|
205
|
-
case
|
|
220
|
+
_c.label = 6;
|
|
221
|
+
case 6:
|
|
206
222
|
_i++;
|
|
207
|
-
return [3 /*break*/,
|
|
208
|
-
case
|
|
223
|
+
return [3 /*break*/, 3];
|
|
224
|
+
case 7:
|
|
225
|
+
this._mwbreak[desc] = res;
|
|
226
|
+
this.ActionSystem.emit("mwb_".concat(desc));
|
|
227
|
+
return [2 /*return*/, res];
|
|
209
228
|
}
|
|
210
229
|
});
|
|
211
230
|
});
|
package/package.json
CHANGED
package/src/bot-service.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { readFileSync, rmSync } from 'fs';
|
|
|
8
8
|
interface UserDialog {
|
|
9
9
|
[key: number]: {dialog: string, lastid: number, waiter: { statewait: boolean, descriptor: string }};
|
|
10
10
|
}
|
|
11
|
+
interface MWBreakDown {
|
|
12
|
+
[key: string]: boolean | undefined | null;
|
|
13
|
+
}
|
|
11
14
|
type BotBuilderMiddleware = (chat: number, msg: TelegramBot.Message | TelegramBot.CallbackQuery) => Promise<boolean> | boolean;
|
|
12
15
|
|
|
13
16
|
export interface ActionCallback {
|
|
@@ -20,16 +23,30 @@ export class BotBuilder {
|
|
|
20
23
|
public ActionSystem: EventEmitter;
|
|
21
24
|
private _userdialogs: UserDialog;
|
|
22
25
|
private _middleware: BotBuilderMiddleware[] = [];
|
|
26
|
+
private _mwbreak: MWBreakDown = {};
|
|
23
27
|
|
|
24
28
|
public use(func: BotBuilderMiddleware) {
|
|
25
29
|
this._middleware.push(func);
|
|
26
30
|
}
|
|
27
31
|
|
|
32
|
+
private _mwba(desc: string): Promise<boolean> {
|
|
33
|
+
return new Promise(r => {
|
|
34
|
+
this.ActionSystem.once(`mwb_${desc}`, r)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
28
37
|
private async _runmw(chat: number, msg: TelegramBot.Message | TelegramBot.CallbackQuery) {
|
|
38
|
+
let desc = `${chat}_${(msg as TelegramBot.Message).message_id || (msg as TelegramBot.CallbackQuery).id}`;
|
|
39
|
+
let mwb = this._mwbreak[desc];
|
|
40
|
+
if (typeof mwb === 'boolean') return mwb;
|
|
41
|
+
if (typeof mwb === 'object') return await this._mwba(desc);
|
|
42
|
+
this._mwbreak[desc] = null;
|
|
43
|
+
|
|
29
44
|
let res = false;
|
|
30
45
|
for (let mw of this._middleware) {
|
|
31
46
|
res = res || await mw(chat, msg);
|
|
32
47
|
}
|
|
48
|
+
this._mwbreak[desc] = res;
|
|
49
|
+
this.ActionSystem.emit(`mwb_${desc}`);
|
|
33
50
|
return res;
|
|
34
51
|
}
|
|
35
52
|
|
|
@@ -54,7 +71,7 @@ export class BotBuilder {
|
|
|
54
71
|
if (await this._runmw(msg.chat.id, msg)) return;
|
|
55
72
|
|
|
56
73
|
let chat = msg.chat.id;
|
|
57
|
-
if (this._userdialogs[chat]
|
|
74
|
+
if (this._userdialogs[chat]!.waiter!.statewait) {
|
|
58
75
|
this._userdialogs[chat].waiter.statewait = false;
|
|
59
76
|
this._bot.deleteMessage(chat, msg.message_id).catch(() => undefined);
|
|
60
77
|
this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, msg.text);
|