telegram-botbuilder 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
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/lib/index.d.ts DELETED
File without changes
package/lib/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
package/src/index.ts DELETED
File without changes