telegram-botbuilder 1.1.7 → 1.1.9
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 +1 -0
- package/lib/bot-service.js +78 -49
- package/lib/bot-struct.d.ts +2 -2
- package/package.json +3 -1
- package/src/bot-service.ts +28 -9
- package/src/bot-struct.ts +2 -2
- package/tsconfig.json +1 -1
package/lib/bot-service.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export declare class BotBuilder {
|
|
|
15
15
|
use(func: BotBuilderMiddleware): void;
|
|
16
16
|
private _mwba;
|
|
17
17
|
private _runmw;
|
|
18
|
+
private _hashbtn;
|
|
18
19
|
constructor(schema: Schema, token: string, options?: TelegramBot.ConstructorOptions);
|
|
19
20
|
private _getDialog;
|
|
20
21
|
private _getButton;
|
package/lib/bot-service.js
CHANGED
|
@@ -54,8 +54,17 @@ exports.CallbackAction = CallbackAction;
|
|
|
54
54
|
exports.WaitForData = WaitForData;
|
|
55
55
|
var node_telegram_bot_api_1 = __importDefault(require("node-telegram-bot-api"));
|
|
56
56
|
var events_1 = __importDefault(require("events"));
|
|
57
|
-
var crypto_1 = require("crypto");
|
|
58
57
|
var fs_1 = require("fs");
|
|
58
|
+
var hash = require('object-hash');
|
|
59
|
+
function _typed(object) {
|
|
60
|
+
if ('text' in object && 'action' in object)
|
|
61
|
+
return 'Button';
|
|
62
|
+
if ('id' in object && 'text' in object && 'buttons' in object)
|
|
63
|
+
return 'Dialog';
|
|
64
|
+
if ('start' in object && 'content' in object && 'commands' in object)
|
|
65
|
+
return 'Schema';
|
|
66
|
+
return 'undefined';
|
|
67
|
+
}
|
|
59
68
|
var BotBuilder = /** @class */ (function () {
|
|
60
69
|
function BotBuilder(schema, token, options) {
|
|
61
70
|
var _this = this;
|
|
@@ -65,9 +74,14 @@ var BotBuilder = /** @class */ (function () {
|
|
|
65
74
|
this._schema = schema;
|
|
66
75
|
this._schema.buttons = [];
|
|
67
76
|
for (var x = 0; x < this._schema.content.length; x++) {
|
|
77
|
+
if (typeof this._schema.content[x].buttons === 'function')
|
|
78
|
+
continue;
|
|
68
79
|
for (var y = 0; y < this._schema.content[x].buttons.length; y++) {
|
|
80
|
+
// @ts-ignore
|
|
69
81
|
for (var b = 0; b < this._schema.content[x].buttons[y].length; b++) {
|
|
70
|
-
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
this._schema.content[x].buttons[y][b]._callback = this._hashbtn(this._schema.content[x], this._schema.content[x].buttons[y][b]);
|
|
84
|
+
// @ts-ignore
|
|
71
85
|
this._schema.buttons.push(this._schema.content[x].buttons[y][b]);
|
|
72
86
|
}
|
|
73
87
|
}
|
|
@@ -236,6 +250,9 @@ var BotBuilder = /** @class */ (function () {
|
|
|
236
250
|
});
|
|
237
251
|
});
|
|
238
252
|
};
|
|
253
|
+
BotBuilder.prototype._hashbtn = function (dia, btn) {
|
|
254
|
+
return hash([dia.id, btn]);
|
|
255
|
+
};
|
|
239
256
|
BotBuilder.prototype._getDialog = function (id) {
|
|
240
257
|
return this._schema.content.find(function (x) { return x.id === id; });
|
|
241
258
|
};
|
|
@@ -279,9 +296,9 @@ var BotBuilder = /** @class */ (function () {
|
|
|
279
296
|
};
|
|
280
297
|
BotBuilder.prototype.ChangeDialog = function (chat, id) {
|
|
281
298
|
return __awaiter(this, void 0, void 0, function () {
|
|
282
|
-
var dialog, markup, b, _i,
|
|
283
|
-
return __generator(this, function (
|
|
284
|
-
switch (
|
|
299
|
+
var dialog, btns, _a, markup, b, _i, _b, btn, text_1, _c, text, _d, _e, msg;
|
|
300
|
+
return __generator(this, function (_f) {
|
|
301
|
+
switch (_f.label) {
|
|
285
302
|
case 0:
|
|
286
303
|
this._userdialogs[chat].dialog = id;
|
|
287
304
|
if (this._userdialogs[chat].waiter.statewait) {
|
|
@@ -289,65 +306,77 @@ var BotBuilder = /** @class */ (function () {
|
|
|
289
306
|
this.ActionSystem.emit("cancel_".concat(this._userdialogs[chat].waiter.descriptor), chat);
|
|
290
307
|
}
|
|
291
308
|
dialog = this._getDialog(id);
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
markup = [];
|
|
295
|
-
b = 0;
|
|
296
|
-
_e.label = 1;
|
|
309
|
+
if (!(typeof (dialog === null || dialog === void 0 ? void 0 : dialog.buttons) === 'function')) return [3 /*break*/, 2];
|
|
310
|
+
return [4 /*yield*/, (dialog === null || dialog === void 0 ? void 0 : dialog.buttons(chat))];
|
|
297
311
|
case 1:
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
_i = 0, _a = dialog === null || dialog === void 0 ? void 0 : dialog.buttons[b];
|
|
301
|
-
_e.label = 2;
|
|
312
|
+
_a = _f.sent();
|
|
313
|
+
return [3 /*break*/, 3];
|
|
302
314
|
case 2:
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (!(typeof btn.text === 'function')) return [3 /*break*/, 4];
|
|
306
|
-
return [4 /*yield*/, btn.text(chat)];
|
|
315
|
+
_a = dialog === null || dialog === void 0 ? void 0 : dialog.buttons;
|
|
316
|
+
_f.label = 3;
|
|
307
317
|
case 3:
|
|
308
|
-
|
|
309
|
-
|
|
318
|
+
btns = (_a) || [];
|
|
319
|
+
markup = undefined;
|
|
320
|
+
if (!(btns.length > 0)) return [3 /*break*/, 11];
|
|
321
|
+
markup = [];
|
|
322
|
+
b = 0;
|
|
323
|
+
_f.label = 4;
|
|
310
324
|
case 4:
|
|
311
|
-
|
|
312
|
-
|
|
325
|
+
if (!(b < btns.length)) return [3 /*break*/, 11];
|
|
326
|
+
markup[b] = [];
|
|
327
|
+
_i = 0, _b = btns[b];
|
|
328
|
+
_f.label = 5;
|
|
313
329
|
case 5:
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
330
|
+
if (!(_i < _b.length)) return [3 /*break*/, 10];
|
|
331
|
+
btn = _b[_i];
|
|
332
|
+
if (!(typeof btn.text === 'function')) return [3 /*break*/, 7];
|
|
333
|
+
return [4 /*yield*/, btn.text(chat)];
|
|
317
334
|
case 6:
|
|
318
|
-
|
|
319
|
-
return [3 /*break*/,
|
|
335
|
+
_c = _f.sent();
|
|
336
|
+
return [3 /*break*/, 8];
|
|
320
337
|
case 7:
|
|
321
|
-
|
|
322
|
-
|
|
338
|
+
_c = btn.text;
|
|
339
|
+
_f.label = 8;
|
|
323
340
|
case 8:
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
341
|
+
text_1 = _c;
|
|
342
|
+
btn._callback = this._hashbtn(dialog, btn);
|
|
343
|
+
this._schema.buttons.push(btn);
|
|
344
|
+
markup[b].push({ text: text_1, callback_data: btn._callback });
|
|
345
|
+
_f.label = 9;
|
|
328
346
|
case 9:
|
|
329
|
-
|
|
330
|
-
return [3 /*break*/,
|
|
347
|
+
_i++;
|
|
348
|
+
return [3 /*break*/, 5];
|
|
331
349
|
case 10:
|
|
332
|
-
|
|
333
|
-
|
|
350
|
+
b++;
|
|
351
|
+
return [3 /*break*/, 4];
|
|
334
352
|
case 11:
|
|
335
|
-
|
|
336
|
-
|
|
353
|
+
if (dialog == undefined)
|
|
354
|
+
markup = [[{ text: "Меню", callback_data: this._schema.start }]]; // FIX RAND BTN
|
|
355
|
+
if (!(typeof (dialog === null || dialog === void 0 ? void 0 : dialog.text) === 'function')) return [3 /*break*/, 13];
|
|
356
|
+
return [4 /*yield*/, (dialog === null || dialog === void 0 ? void 0 : dialog.text(chat))];
|
|
337
357
|
case 12:
|
|
338
|
-
|
|
339
|
-
return [
|
|
358
|
+
_d = _f.sent();
|
|
359
|
+
return [3 /*break*/, 14];
|
|
340
360
|
case 13:
|
|
341
|
-
|
|
342
|
-
|
|
361
|
+
_d = dialog === null || dialog === void 0 ? void 0 : dialog.text;
|
|
362
|
+
_f.label = 14;
|
|
343
363
|
case 14:
|
|
344
|
-
|
|
345
|
-
|
|
364
|
+
text = _d;
|
|
365
|
+
_f.label = 15;
|
|
346
366
|
case 15:
|
|
347
|
-
|
|
367
|
+
_f.trys.push([15, 17, , 19]);
|
|
368
|
+
return [4 /*yield*/, this._bot.editMessageText(text || "404", { chat_id: chat, message_id: this._userdialogs[chat].lastid, reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML' })];
|
|
369
|
+
case 16:
|
|
370
|
+
_f.sent();
|
|
371
|
+
return [3 /*break*/, 19];
|
|
372
|
+
case 17:
|
|
373
|
+
_e = _f.sent();
|
|
374
|
+
return [4 /*yield*/, this._bot.sendMessage(chat, text || "404", { reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML' })];
|
|
375
|
+
case 18:
|
|
376
|
+
msg = _f.sent();
|
|
348
377
|
this._userdialogs[chat].lastid = msg.message_id;
|
|
349
|
-
return [3 /*break*/,
|
|
350
|
-
case
|
|
378
|
+
return [3 /*break*/, 19];
|
|
379
|
+
case 19: return [2 /*return*/];
|
|
351
380
|
}
|
|
352
381
|
});
|
|
353
382
|
});
|
|
@@ -357,7 +386,7 @@ var BotBuilder = /** @class */ (function () {
|
|
|
357
386
|
this._userdialogs[chat].waiter.statewait = true;
|
|
358
387
|
};
|
|
359
388
|
BotBuilder.prototype.Message = function (chat, content) {
|
|
360
|
-
return this._bot.sendMessage(chat, content, { parse_mode: '
|
|
389
|
+
return this._bot.sendMessage(chat, content, { parse_mode: 'HTML' });
|
|
361
390
|
};
|
|
362
391
|
return BotBuilder;
|
|
363
392
|
}());
|
package/lib/bot-struct.d.ts
CHANGED
|
@@ -3,10 +3,10 @@ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => (Promis
|
|
|
3
3
|
export interface Dialog {
|
|
4
4
|
id: string;
|
|
5
5
|
text: string | ((chat: number) => Promise<string>) | ((chat: number) => string);
|
|
6
|
-
buttons: Button[][];
|
|
6
|
+
buttons: Button[][] | ((chat: number) => Promise<Button[][]>) | ((chat: number) => Button[][]);
|
|
7
7
|
}
|
|
8
8
|
export interface BotDialog extends Dialog {
|
|
9
|
-
buttons: BotButton[][];
|
|
9
|
+
buttons: BotButton[][] | ((chat: number) => Promise<BotButton[][]>) | ((chat: number) => BotButton[][]);
|
|
10
10
|
}
|
|
11
11
|
export interface Button {
|
|
12
12
|
text: string | ((chat: number) => Promise<string>) | ((chat: number) => string);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telegram-botbuilder",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"description": "",
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"@types/object-hash": "^3.0.6",
|
|
13
14
|
"node-telegram-bot-api": "^0.66.0",
|
|
15
|
+
"object-hash": "^3.0.0",
|
|
14
16
|
"ts-node": "^10.9.2",
|
|
15
17
|
"typescript": "^5.5.3"
|
|
16
18
|
},
|
package/src/bot-service.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import TelegramBot, { InlineKeyboardButton } from 'node-telegram-bot-api';
|
|
2
|
-
import { Schema, Action, BotSchema, Button, BotButton } from './bot-struct';
|
|
2
|
+
import { Schema, Action, BotSchema, Button, BotButton, BotDialog, Dialog } from './bot-struct';
|
|
3
3
|
import EventEmitter from 'events';
|
|
4
4
|
import { randomInt } from 'crypto';
|
|
5
5
|
import { readFileSync, rmSync } from 'fs';
|
|
6
|
+
var hash = require('object-hash');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
interface UserDialog {
|
|
@@ -17,6 +18,13 @@ export interface ActionCallback {
|
|
|
17
18
|
chatid: number;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
function _typed(object: any): 'Button' | 'Dialog' | 'Schema' | 'undefined' {
|
|
22
|
+
if ('text' in object && 'action' in object) return 'Button';
|
|
23
|
+
if ('id' in object && 'text' in object && 'buttons' in object) return 'Dialog';
|
|
24
|
+
if ('start' in object && 'content' in object && 'commands' in object) return 'Schema';
|
|
25
|
+
return 'undefined';
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
export class BotBuilder {
|
|
21
29
|
public _bot: TelegramBot;
|
|
22
30
|
private _schema: BotSchema;
|
|
@@ -28,7 +36,6 @@ export class BotBuilder {
|
|
|
28
36
|
public use(func: BotBuilderMiddleware) {
|
|
29
37
|
this._middleware.push(func);
|
|
30
38
|
}
|
|
31
|
-
|
|
32
39
|
private _mwba(desc: string): Promise<boolean> {
|
|
33
40
|
return new Promise(r => {
|
|
34
41
|
this.ActionSystem.once(`mwb_${desc}`, r)
|
|
@@ -50,15 +57,23 @@ export class BotBuilder {
|
|
|
50
57
|
return res;
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
private _hashbtn(dia: BotDialog, btn: Button) {
|
|
61
|
+
return hash([dia.id, btn]);
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
constructor (schema: Schema, token: string, options?: TelegramBot.ConstructorOptions) {
|
|
54
65
|
this._bot = new TelegramBot(token, options);
|
|
55
66
|
this._schema = schema as BotSchema;
|
|
56
67
|
this._schema.buttons = [];
|
|
57
68
|
|
|
58
69
|
for (let x = 0; x < this._schema.content.length; x++) {
|
|
70
|
+
if (typeof this._schema.content[x].buttons === 'function') continue;
|
|
59
71
|
for (let y = 0; y < this._schema.content[x].buttons.length; y++) {
|
|
72
|
+
// @ts-ignore
|
|
60
73
|
for (let b = 0; b < this._schema.content[x].buttons[y].length; b++) {
|
|
61
|
-
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
this._schema.content[x].buttons[y][b]._callback = this._hashbtn(this._schema.content[x], this._schema.content[x].buttons[y][b]);
|
|
76
|
+
// @ts-ignore
|
|
62
77
|
this._schema.buttons.push(this._schema.content[x].buttons[y][b]);
|
|
63
78
|
}
|
|
64
79
|
}
|
|
@@ -155,13 +170,17 @@ export class BotBuilder {
|
|
|
155
170
|
}
|
|
156
171
|
|
|
157
172
|
let dialog = this._getDialog(id);
|
|
173
|
+
let btns: BotButton[][] = (typeof dialog?.buttons === 'function' ? await dialog?.buttons(chat) : dialog?.buttons) || [];
|
|
174
|
+
|
|
158
175
|
let markup: InlineKeyboardButton[][] | undefined = undefined;
|
|
159
|
-
if (
|
|
176
|
+
if (btns.length > 0) {
|
|
160
177
|
markup = [];
|
|
161
|
-
for (let b = 0; b <
|
|
178
|
+
for (let b = 0; b < btns.length; b++) {
|
|
162
179
|
markup[b] = [];
|
|
163
|
-
for (let btn of
|
|
180
|
+
for (let btn of btns[b]) {
|
|
164
181
|
let text = typeof btn.text === 'function' ? await btn.text(chat) : btn.text;
|
|
182
|
+
btn._callback = this._hashbtn(dialog!, btn);
|
|
183
|
+
this._schema.buttons.push(btn);
|
|
165
184
|
markup[b].push({ text, callback_data: btn._callback});
|
|
166
185
|
}
|
|
167
186
|
}
|
|
@@ -170,9 +189,9 @@ export class BotBuilder {
|
|
|
170
189
|
if (dialog == undefined) markup = [[{text: "Меню", callback_data: this._schema.start}]]; // FIX RAND BTN
|
|
171
190
|
let text = typeof dialog?.text === 'function' ? await dialog?.text(chat) : dialog?.text;
|
|
172
191
|
try {
|
|
173
|
-
await this._bot.editMessageText(text || "404", {chat_id: chat, message_id: this._userdialogs[chat].lastid, reply_markup: { inline_keyboard: markup || [] }, parse_mode: '
|
|
192
|
+
await this._bot.editMessageText(text || "404", {chat_id: chat, message_id: this._userdialogs[chat].lastid, reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML'})
|
|
174
193
|
} catch {
|
|
175
|
-
let msg = await this._bot.sendMessage(chat, text || "404", {reply_markup: { inline_keyboard: markup || [] }, parse_mode: '
|
|
194
|
+
let msg = await this._bot.sendMessage(chat, text || "404", {reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML'});
|
|
176
195
|
this._userdialogs[chat].lastid = msg.message_id;
|
|
177
196
|
}
|
|
178
197
|
}
|
|
@@ -181,7 +200,7 @@ export class BotBuilder {
|
|
|
181
200
|
this._userdialogs[chat].waiter.statewait = true;
|
|
182
201
|
}
|
|
183
202
|
public Message(chat: number, content: string) {
|
|
184
|
-
return this._bot.sendMessage(chat, content, {parse_mode: '
|
|
203
|
+
return this._bot.sendMessage(chat, content, {parse_mode: 'HTML'});
|
|
185
204
|
}
|
|
186
205
|
}
|
|
187
206
|
|
package/src/bot-struct.ts
CHANGED
|
@@ -6,10 +6,10 @@ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => (Promis
|
|
|
6
6
|
export interface Dialog {
|
|
7
7
|
id: string;
|
|
8
8
|
text: string | ((chat: number) => Promise<string>) | ((chat: number) => string);
|
|
9
|
-
buttons: Button[][]
|
|
9
|
+
buttons: Button[][] | ((chat: number) => Promise<Button[][]>) | ((chat: number) => Button[][]);
|
|
10
10
|
}
|
|
11
11
|
export interface BotDialog extends Dialog {
|
|
12
|
-
buttons: BotButton[][]
|
|
12
|
+
buttons: BotButton[][] | ((chat: number) => Promise<BotButton[][]>) | ((chat: number) => BotButton[][]);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export interface Button {
|
package/tsconfig.json
CHANGED
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
87
87
|
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
88
88
|
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
89
|
-
|
|
89
|
+
"strictPropertyInitialization": false, /* Check for class properties that are declared but not set in the constructor. */
|
|
90
90
|
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
91
91
|
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
92
92
|
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|