telegram-botbuilder 1.0.4 → 1.0.6

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.
@@ -64,17 +64,20 @@ var BotBuilder = /** @class */ (function () {
64
64
  }
65
65
  this.ActionSystem = new events_1.default();
66
66
  this._userdialogs = {};
67
- this._bot.onText(/\/start/, function (msg) { return __awaiter(_this, void 0, void 0, function () {
67
+ this._bot.onText(/.+/g, function (msg) { return __awaiter(_this, void 0, void 0, function () {
68
+ var chat;
68
69
  return __generator(this, function (_a) {
69
70
  switch (_a.label) {
70
71
  case 0: return [4 /*yield*/, this._runmw(msg.chat.id, msg)];
71
72
  case 1:
72
73
  if (_a.sent())
73
74
  return [2 /*return*/];
74
- if (!this._userdialogs[msg.chat.id])
75
- this._userdialogs[msg.chat.id] = { dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' } };
76
- this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(function () { return undefined; });
77
- this.ChangeDialog(msg.chat.id, this._schema.start);
75
+ chat = msg.chat.id;
76
+ if (this._userdialogs[chat].waiter.statewait) {
77
+ this._userdialogs[chat].waiter.statewait = false;
78
+ this._bot.deleteMessage(chat, msg.message_id).catch(function () { return undefined; });
79
+ this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, msg.text);
80
+ }
78
81
  return [2 /*return*/];
79
82
  }
80
83
  });
@@ -93,24 +96,6 @@ var BotBuilder = /** @class */ (function () {
93
96
  }
94
97
  });
95
98
  }); });
96
- this._bot.onText(/.+/g, function (msg) { return __awaiter(_this, void 0, void 0, function () {
97
- var chat;
98
- return __generator(this, function (_a) {
99
- switch (_a.label) {
100
- case 0: return [4 /*yield*/, this._runmw(msg.chat.id, msg)];
101
- case 1:
102
- if (_a.sent())
103
- return [2 /*return*/];
104
- chat = msg.chat.id;
105
- if (this._userdialogs[chat].waiter.statewait) {
106
- this._userdialogs[chat].waiter.statewait = false;
107
- this._bot.deleteMessage(chat, msg.message_id).catch(function () { return undefined; });
108
- this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, msg.text);
109
- }
110
- return [2 /*return*/];
111
- }
112
- });
113
- }); });
114
99
  this._bot.on('document', function (msg) { return __awaiter(_this, void 0, void 0, function () {
115
100
  var chat;
116
101
  var _this = this;
@@ -136,6 +121,52 @@ var BotBuilder = /** @class */ (function () {
136
121
  }
137
122
  });
138
123
  }); });
124
+ this._bot.onText(/\/start/, function (msg) { return __awaiter(_this, void 0, void 0, function () {
125
+ return __generator(this, function (_a) {
126
+ if (!this._userdialogs[msg.chat.id])
127
+ this._userdialogs[msg.chat.id] = { dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' } };
128
+ this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(function () { return undefined; });
129
+ this.ChangeDialog(msg.chat.id, this._schema.start);
130
+ return [2 /*return*/];
131
+ });
132
+ }); });
133
+ var _loop_1 = function (cmd) {
134
+ this_1._bot.onText(new RegExp('\\/${cmd.text}(?:\\s+(.*))?$'), function (msg, match) { return __awaiter(_this, void 0, void 0, function () {
135
+ var chat, _i, _a, act;
136
+ return __generator(this, function (_b) {
137
+ switch (_b.label) {
138
+ case 0:
139
+ chat = msg.chat.id;
140
+ if (!Array.isArray(cmd === null || cmd === void 0 ? void 0 : cmd.action)) return [3 /*break*/, 5];
141
+ _i = 0, _a = cmd === null || cmd === void 0 ? void 0 : cmd.action;
142
+ _b.label = 1;
143
+ case 1:
144
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
145
+ act = _a[_i];
146
+ return [4 /*yield*/, act(chat, this, match[1])];
147
+ case 2:
148
+ _b.sent();
149
+ _b.label = 3;
150
+ case 3:
151
+ _i++;
152
+ return [3 /*break*/, 1];
153
+ case 4: return [3 /*break*/, 7];
154
+ case 5:
155
+ if (!(cmd === null || cmd === void 0 ? void 0 : cmd.action)) return [3 /*break*/, 7];
156
+ return [4 /*yield*/, (cmd === null || cmd === void 0 ? void 0 : cmd.action)(chat, this, match[1])];
157
+ case 6:
158
+ _b.sent();
159
+ _b.label = 7;
160
+ case 7: return [2 /*return*/];
161
+ }
162
+ });
163
+ }); });
164
+ };
165
+ var this_1 = this;
166
+ for (var _i = 0, _a = this._schema.commands; _i < _a.length; _i++) {
167
+ var cmd = _a[_i];
168
+ _loop_1(cmd);
169
+ }
139
170
  }
140
171
  BotBuilder.prototype.use = function (func) {
141
172
  this._middleware.push(func);
@@ -1,5 +1,5 @@
1
1
  import { BotBuilder } from "./bot-service";
2
- export type Action = (chat: number, _bot: BotBuilder) => Promise<void>;
2
+ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => Promise<void>;
3
3
  export interface Dialog {
4
4
  id: string;
5
5
  text: string | ((chat: number) => Promise<string>);
@@ -18,8 +18,13 @@ export interface BotButton extends Button {
18
18
  export interface Schema {
19
19
  start: string;
20
20
  content: Dialog[];
21
+ commands: Command[];
21
22
  }
22
23
  export interface BotSchema extends Schema {
23
24
  content: BotDialog[];
24
25
  buttons: BotButton[];
25
26
  }
27
+ export interface Command {
28
+ text: string;
29
+ action: Action[] | Action;
30
+ }
package/lib/index.d.ts CHANGED
@@ -1,7 +1,2 @@
1
- import * as BotBuilder from "./bot-service";
2
- import * as BotStruct from "./bot-struct";
3
- declare const _default: {
4
- BotBuilder: typeof BotBuilder;
5
- BotStruct: typeof BotStruct;
6
- };
7
- export = _default;
1
+ export * from './bot-service';
2
+ export * from './bot-struct';
package/lib/index.js CHANGED
@@ -10,18 +10,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
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;
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);
24
15
  };
25
- var BotBuilder = __importStar(require("./bot-service"));
26
- var BotStruct = __importStar(require("./bot-struct"));
27
- module.exports = { BotBuilder: BotBuilder, BotStruct: BotStruct };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./bot-service"), exports);
18
+ __exportStar(require("./bot-struct"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "lib/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -50,12 +50,15 @@ export class BotBuilder {
50
50
  this.ActionSystem = new EventEmitter();
51
51
  this._userdialogs = {};
52
52
 
53
- this._bot.onText(/\/start/, async (msg) => {
53
+ this._bot.onText(/.+/g, async (msg) => {
54
54
  if (await this._runmw(msg.chat.id, msg)) return;
55
55
 
56
- if (!this._userdialogs[msg.chat.id]) this._userdialogs[msg.chat.id] = {dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' }}
57
- this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(() => undefined);
58
- this.ChangeDialog(msg.chat.id, this._schema.start);
56
+ let chat = msg.chat.id;
57
+ if (this._userdialogs[chat].waiter.statewait) {
58
+ this._userdialogs[chat].waiter.statewait = false;
59
+ this._bot.deleteMessage(chat, msg.message_id).catch(() => undefined);
60
+ this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, msg.text);
61
+ }
59
62
  });
60
63
 
61
64
  this._bot.on('callback_query', async (query) => {
@@ -65,17 +68,6 @@ export class BotBuilder {
65
68
  this._activateButton(query.message!.chat.id, query.data!)
66
69
  });
67
70
 
68
- this._bot.onText(/.+/g, async (msg) => {
69
- if (await this._runmw(msg.chat.id, msg)) return;
70
-
71
- let chat = msg.chat.id;
72
- if (this._userdialogs[chat].waiter.statewait) {
73
- this._userdialogs[chat].waiter.statewait = false;
74
- this._bot.deleteMessage(chat, msg.message_id).catch(() => undefined);
75
- this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, msg.text);
76
- }
77
- })
78
-
79
71
  this._bot.on('document', async (msg) => {
80
72
  if (await this._runmw(msg.chat.id, msg)) return;
81
73
 
@@ -88,8 +80,27 @@ export class BotBuilder {
88
80
  let content = readFileSync(x, 'utf8');
89
81
  rmSync(x);
90
82
  this.ActionSystem.emit(this._userdialogs[chat].waiter.descriptor, chat, content);
91
- })
92
- })
83
+ });
84
+ });
85
+
86
+ this._bot.onText(/\/start/, async (msg) => {
87
+ if (!this._userdialogs[msg.chat.id]) this._userdialogs[msg.chat.id] = {dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' }}
88
+ this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(() => undefined);
89
+ this.ChangeDialog(msg.chat.id, this._schema.start);
90
+ });
91
+
92
+ for (let cmd of this._schema.commands) {
93
+ this._bot.onText(new RegExp('\\/${cmd.text}(?:\\s+(.*))?$'), async (msg, match) => {
94
+ let chat = msg.chat.id;
95
+ if (Array.isArray(cmd?.action)) {
96
+ for (let act of (cmd?.action as Action[])) {
97
+ await act(chat, this, match![1]);
98
+ }
99
+ } else if (cmd?.action as Action) {
100
+ await (cmd?.action as Action)(chat, this, match![1]);
101
+ }
102
+ });
103
+ }
93
104
  }
94
105
 
95
106
  private _getDialog(id: string) {
package/src/bot-struct.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { BotBuilder } from "./bot-service";
2
2
 
3
3
 
4
- export type Action = (chat: number, _bot: BotBuilder) => Promise<void>;
4
+ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => Promise<void>;
5
5
 
6
6
  export interface Dialog {
7
7
  id: string;
@@ -23,9 +23,15 @@ export interface BotButton extends Button {
23
23
  export interface Schema {
24
24
  start: string;
25
25
  content: Dialog[];
26
+ commands: Command[];
26
27
  }
27
28
 
28
29
  export interface BotSchema extends Schema {
29
30
  content: BotDialog[];
30
31
  buttons: BotButton[];
32
+ }
33
+
34
+ export interface Command {
35
+ text: string;
36
+ action: Action[] | Action;
31
37
  }
package/src/index.ts CHANGED
@@ -1,4 +1,2 @@
1
- import * as BotBuilder from "./bot-service";
2
- import * as BotStruct from "./bot-struct";
3
-
4
- export = { BotBuilder, BotStruct };
1
+ export * from './bot-service';
2
+ export * from './bot-struct';