telegram-botbuilder 1.0.1 → 1.0.3

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.
@@ -1,14 +1,18 @@
1
1
  import TelegramBot from 'node-telegram-bot-api';
2
2
  import { Schema, Action } from './bot-struct';
3
3
  import EventEmitter from 'events';
4
+ type BotBuilderMiddleware = (chat: number, msg: TelegramBot.Message | TelegramBot.CallbackQuery) => Promise<boolean> | boolean;
4
5
  export interface ActionCallback {
5
6
  chatid: number;
6
7
  }
7
- export declare class EasyBot {
8
+ export declare class BotBuilder {
8
9
  private _bot;
9
10
  private _schema;
10
11
  ActionSystem: EventEmitter;
11
12
  private _userdialogs;
13
+ private _middleware;
14
+ use(func: BotBuilderMiddleware): void;
15
+ private _runmw;
12
16
  constructor(schema: Schema, token: string, options?: TelegramBot.ConstructorOptions);
13
17
  private _getDialog;
14
18
  private _getButton;
@@ -20,3 +24,4 @@ export declare class EasyBot {
20
24
  export declare function ChangeDialog(id: string): Action;
21
25
  export declare function CallbackAction(descriptor: string): Action;
22
26
  export declare function WaitForData(descriptor: string): Action;
27
+ export {};
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.EasyBot = void 0;
42
+ exports.BotBuilder = void 0;
43
43
  exports.ChangeDialog = ChangeDialog;
44
44
  exports.CallbackAction = CallbackAction;
45
45
  exports.WaitForData = WaitForData;
@@ -47,9 +47,10 @@ var node_telegram_bot_api_1 = __importDefault(require("node-telegram-bot-api"));
47
47
  var events_1 = __importDefault(require("events"));
48
48
  var crypto_1 = require("crypto");
49
49
  var fs_1 = require("fs");
50
- var EasyBot = /** @class */ (function () {
51
- function EasyBot(schema, token, options) {
50
+ var BotBuilder = /** @class */ (function () {
51
+ function BotBuilder(schema, token, options) {
52
52
  var _this = this;
53
+ this._middleware = [];
53
54
  this._bot = new node_telegram_bot_api_1.default(token, options);
54
55
  this._schema = schema;
55
56
  this._schema.buttons = [];
@@ -63,47 +64,118 @@ var EasyBot = /** @class */ (function () {
63
64
  }
64
65
  this.ActionSystem = new events_1.default();
65
66
  this._userdialogs = {};
66
- this._bot.onText(/\/start/, function (msg) {
67
- if (!_this._userdialogs[msg.chat.id])
68
- _this._userdialogs[msg.chat.id] = { dialog: _this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' } };
69
- _this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(function () { return undefined; });
70
- _this.ChangeDialog(msg.chat.id, _this._schema.start);
71
- });
72
- this._bot.on('callback_query', function (query) {
73
- if (!_this._userdialogs[query.message.chat.id])
74
- _this._userdialogs[query.message.chat.id] = { dialog: _this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' } };
75
- _this._activateButton(query.message.chat.id, query.data);
76
- });
77
- this._bot.onText(/.+/g, function (msg) {
78
- var chat = msg.chat.id;
79
- if (_this._userdialogs[chat].waiter.statewait) {
80
- _this._userdialogs[chat].waiter.statewait = false;
81
- _this._bot.deleteMessage(chat, msg.message_id).catch(function () { return undefined; });
82
- _this.ActionSystem.emit(_this._userdialogs[chat].waiter.descriptor, chat, msg.text);
83
- }
84
- });
85
- this._bot.on('document', function (msg) {
86
- if (!msg.document)
87
- return;
88
- var chat = msg.chat.id;
89
- if (!_this._userdialogs[chat].waiter.statewait)
90
- return;
91
- _this._bot.downloadFile(msg.document.file_id, './').then(function (x) {
92
- _this._userdialogs[chat].waiter.statewait = false;
93
- _this._bot.deleteMessage(chat, msg.message_id).catch(function () { return undefined; });
94
- var content = (0, fs_1.readFileSync)(x, 'utf8');
95
- (0, fs_1.rmSync)(x);
96
- _this.ActionSystem.emit(_this._userdialogs[chat].waiter.descriptor, chat, content);
67
+ this._bot.onText(/\/start/, function (msg) { return __awaiter(_this, void 0, void 0, function () {
68
+ return __generator(this, function (_a) {
69
+ switch (_a.label) {
70
+ case 0: return [4 /*yield*/, this._runmw(msg.chat.id, msg)];
71
+ case 1:
72
+ if (_a.sent())
73
+ 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);
78
+ return [2 /*return*/];
79
+ }
97
80
  });
98
- });
81
+ }); });
82
+ this._bot.on('callback_query', function (query) { return __awaiter(_this, void 0, void 0, function () {
83
+ return __generator(this, function (_a) {
84
+ switch (_a.label) {
85
+ case 0: return [4 /*yield*/, this._runmw(query.message.chat.id, query)];
86
+ case 1:
87
+ if (_a.sent())
88
+ return [2 /*return*/];
89
+ if (!this._userdialogs[query.message.chat.id])
90
+ this._userdialogs[query.message.chat.id] = { dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' } };
91
+ this._activateButton(query.message.chat.id, query.data);
92
+ return [2 /*return*/];
93
+ }
94
+ });
95
+ }); });
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
+ this._bot.on('document', function (msg) { return __awaiter(_this, void 0, void 0, function () {
115
+ var chat;
116
+ var _this = this;
117
+ return __generator(this, function (_a) {
118
+ switch (_a.label) {
119
+ case 0: return [4 /*yield*/, this._runmw(msg.chat.id, msg)];
120
+ case 1:
121
+ if (_a.sent())
122
+ return [2 /*return*/];
123
+ if (!msg.document)
124
+ return [2 /*return*/];
125
+ chat = msg.chat.id;
126
+ if (!this._userdialogs[chat].waiter.statewait)
127
+ return [2 /*return*/];
128
+ this._bot.downloadFile(msg.document.file_id, './').then(function (x) {
129
+ _this._userdialogs[chat].waiter.statewait = false;
130
+ _this._bot.deleteMessage(chat, msg.message_id).catch(function () { return undefined; });
131
+ var content = (0, fs_1.readFileSync)(x, 'utf8');
132
+ (0, fs_1.rmSync)(x);
133
+ _this.ActionSystem.emit(_this._userdialogs[chat].waiter.descriptor, chat, content);
134
+ });
135
+ return [2 /*return*/];
136
+ }
137
+ });
138
+ }); });
99
139
  }
100
- EasyBot.prototype._getDialog = function (id) {
140
+ BotBuilder.prototype.use = function (func) {
141
+ this._middleware.push(func);
142
+ };
143
+ BotBuilder.prototype._runmw = function (chat, msg) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ var res, _i, _a, mw, _b;
146
+ return __generator(this, function (_c) {
147
+ switch (_c.label) {
148
+ case 0:
149
+ res = false;
150
+ _i = 0, _a = this._middleware;
151
+ _c.label = 1;
152
+ case 1:
153
+ if (!(_i < _a.length)) return [3 /*break*/, 5];
154
+ mw = _a[_i];
155
+ _b = res;
156
+ if (_b) return [3 /*break*/, 3];
157
+ return [4 /*yield*/, mw(chat, msg)];
158
+ case 2:
159
+ _b = (_c.sent());
160
+ _c.label = 3;
161
+ case 3:
162
+ res = _b;
163
+ _c.label = 4;
164
+ case 4:
165
+ _i++;
166
+ return [3 /*break*/, 1];
167
+ case 5: return [2 /*return*/, res];
168
+ }
169
+ });
170
+ });
171
+ };
172
+ BotBuilder.prototype._getDialog = function (id) {
101
173
  return this._schema.content.find(function (x) { return x.id === id; });
102
174
  };
103
- EasyBot.prototype._getButton = function (callback) {
175
+ BotBuilder.prototype._getButton = function (callback) {
104
176
  return this._schema.buttons.find(function (x) { return x._callback === callback; });
105
177
  };
106
- EasyBot.prototype._activateButton = function (chat, btn) {
178
+ BotBuilder.prototype._activateButton = function (chat, btn) {
107
179
  return __awaiter(this, void 0, void 0, function () {
108
180
  var _btn, _i, _a, act;
109
181
  return __generator(this, function (_b) {
@@ -138,7 +210,7 @@ var EasyBot = /** @class */ (function () {
138
210
  });
139
211
  });
140
212
  };
141
- EasyBot.prototype.ChangeDialog = function (chat, id) {
213
+ BotBuilder.prototype.ChangeDialog = function (chat, id) {
142
214
  return __awaiter(this, void 0, void 0, function () {
143
215
  var dialog, markup, b, _i, _a, btn, text_1, _b, text, _c, _d, msg;
144
216
  return __generator(this, function (_e) {
@@ -211,16 +283,16 @@ var EasyBot = /** @class */ (function () {
211
283
  });
212
284
  });
213
285
  };
214
- EasyBot.prototype.AttachDataWait = function (chat, descriptor) {
286
+ BotBuilder.prototype.AttachDataWait = function (chat, descriptor) {
215
287
  this._userdialogs[chat].waiter.descriptor = descriptor;
216
288
  this._userdialogs[chat].waiter.statewait = true;
217
289
  };
218
- EasyBot.prototype.Message = function (chat, content) {
290
+ BotBuilder.prototype.Message = function (chat, content) {
219
291
  return this._bot.sendMessage(chat, content);
220
292
  };
221
- return EasyBot;
293
+ return BotBuilder;
222
294
  }());
223
- exports.EasyBot = EasyBot;
295
+ exports.BotBuilder = BotBuilder;
224
296
  function ChangeDialog(id) {
225
297
  var _this = this;
226
298
  return function (chat, _bot) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
@@ -1,5 +1,5 @@
1
- import { EasyBot } from "./bot-service";
2
- export type Action = (chat: number, _bot: EasyBot) => Promise<void>;
1
+ import { BotBuilder } from "./bot-service";
2
+ export type Action = (chat: number, _bot: BotBuilder) => Promise<void>;
3
3
  export interface Dialog {
4
4
  id: string;
5
5
  text: string | ((chat: number) => Promise<string>);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -8,16 +8,30 @@ 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
+ type BotBuilderMiddleware = (chat: number, msg: TelegramBot.Message | TelegramBot.CallbackQuery) => Promise<boolean> | boolean;
11
12
 
12
13
  export interface ActionCallback {
13
14
  chatid: number;
14
15
  }
15
16
 
16
- export class EasyBot {
17
+ export class BotBuilder {
17
18
  private _bot: TelegramBot;
18
19
  private _schema: BotSchema;
19
20
  public ActionSystem: EventEmitter;
20
21
  private _userdialogs: UserDialog;
22
+ private _middleware: BotBuilderMiddleware[] = [];
23
+
24
+ public use(func: BotBuilderMiddleware) {
25
+ this._middleware.push(func);
26
+ }
27
+
28
+ private async _runmw(chat: number, msg: TelegramBot.Message | TelegramBot.CallbackQuery) {
29
+ let res = false;
30
+ for (let mw of this._middleware) {
31
+ res = res || await mw(chat, msg);
32
+ }
33
+ return res;
34
+ }
21
35
 
22
36
  constructor (schema: Schema, token: string, options?: TelegramBot.ConstructorOptions) {
23
37
  this._bot = new TelegramBot(token, options);
@@ -36,18 +50,24 @@ export class EasyBot {
36
50
  this.ActionSystem = new EventEmitter();
37
51
  this._userdialogs = {};
38
52
 
39
- this._bot.onText(/\/start/, (msg) => {
53
+ this._bot.onText(/\/start/, async (msg) => {
54
+ if (await this._runmw(msg.chat.id, msg)) return;
55
+
40
56
  if (!this._userdialogs[msg.chat.id]) this._userdialogs[msg.chat.id] = {dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' }}
41
57
  this._bot.deleteMessage(msg.chat.id, msg.message_id).catch(() => undefined);
42
58
  this.ChangeDialog(msg.chat.id, this._schema.start);
43
59
  });
44
60
 
45
- this._bot.on('callback_query', (query) => {
61
+ this._bot.on('callback_query', async (query) => {
62
+ if (await this._runmw(query.message!.chat.id, query)) return;
63
+
46
64
  if (!this._userdialogs[query.message!.chat.id]) this._userdialogs[query.message!.chat.id] = {dialog: this._schema.start, lastid: -1, waiter: { statewait: false, descriptor: '' }}
47
65
  this._activateButton(query.message!.chat.id, query.data!)
48
66
  });
49
67
 
50
- this._bot.onText(/.+/g, (msg) => {
68
+ this._bot.onText(/.+/g, async (msg) => {
69
+ if (await this._runmw(msg.chat.id, msg)) return;
70
+
51
71
  let chat = msg.chat.id;
52
72
  if (this._userdialogs[chat].waiter.statewait) {
53
73
  this._userdialogs[chat].waiter.statewait = false;
@@ -56,7 +76,9 @@ export class EasyBot {
56
76
  }
57
77
  })
58
78
 
59
- this._bot.on('document', (msg) => {
79
+ this._bot.on('document', async (msg) => {
80
+ if (await this._runmw(msg.chat.id, msg)) return;
81
+
60
82
  if (!msg.document) return;
61
83
  let chat = msg.chat.id;
62
84
  if (!this._userdialogs[chat].waiter.statewait) return;
@@ -128,13 +150,13 @@ export class EasyBot {
128
150
  }
129
151
 
130
152
  export function ChangeDialog(id: string): Action {
131
- return async (chat: number, _bot: EasyBot) => { await _bot.ChangeDialog(chat, id); };
153
+ return async (chat: number, _bot: BotBuilder) => { await _bot.ChangeDialog(chat, id); };
132
154
  }
133
155
  export function CallbackAction(descriptor: string): Action {
134
- return async (chat: number, _bot: EasyBot) => { _bot.ActionSystem.emit(descriptor, {chatid: chat}); };
156
+ return async (chat: number, _bot: BotBuilder) => { _bot.ActionSystem.emit(descriptor, {chatid: chat}); };
135
157
  }
136
158
  export function WaitForData(descriptor: string): Action {
137
- return (chat: number, _bot: EasyBot) => {
159
+ return (chat: number, _bot: BotBuilder) => {
138
160
  _bot.AttachDataWait(chat, descriptor);
139
161
  return new Promise(r => {
140
162
  _bot.ActionSystem.once(descriptor, r);
package/src/bot-struct.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { EasyBot } from "./bot-service";
1
+ import { BotBuilder } from "./bot-service";
2
2
 
3
3
 
4
- export type Action = (chat: number, _bot: EasyBot) => Promise<void>;
4
+ export type Action = (chat: number, _bot: BotBuilder) => Promise<void>;
5
5
 
6
6
  export interface Dialog {
7
7
  id: string;