telegram-botbuilder 1.0.2 → 1.0.4

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/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
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;
package/lib/index.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
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;
24
+ };
25
+ var BotBuilder = __importStar(require("./bot-service"));
26
+ var BotStruct = __importStar(require("./bot-struct"));
27
+ module.exports = { BotBuilder: BotBuilder, BotStruct: BotStruct };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.0.2",
4
- "main": "index.js",
3
+ "version": "1.0.4",
4
+ "main": "lib/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
7
7
  },
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;
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ import * as BotBuilder from "./bot-service";
2
+ import * as BotStruct from "./bot-struct";
3
+
4
+ export = { BotBuilder, BotStruct };