telegram-botbuilder 1.1.9 → 1.2.0

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.
@@ -19,6 +19,7 @@ export declare class BotBuilder {
19
19
  constructor(schema: Schema, token: string, options?: TelegramBot.ConstructorOptions);
20
20
  private _getDialog;
21
21
  private _getButton;
22
+ private _getButtonID;
22
23
  private _activateButton;
23
24
  ChangeDialog(chat: number, id: string): Promise<void>;
24
25
  AttachDataWait(chat: number, descriptor: string): void;
@@ -73,19 +73,6 @@ var BotBuilder = /** @class */ (function () {
73
73
  this._bot = new node_telegram_bot_api_1.default(token, options);
74
74
  this._schema = schema;
75
75
  this._schema.buttons = [];
76
- for (var x = 0; x < this._schema.content.length; x++) {
77
- if (typeof this._schema.content[x].buttons === 'function')
78
- continue;
79
- for (var y = 0; y < this._schema.content[x].buttons.length; y++) {
80
- // @ts-ignore
81
- for (var b = 0; b < this._schema.content[x].buttons[y].length; b++) {
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
85
- this._schema.buttons.push(this._schema.content[x].buttons[y][b]);
86
- }
87
- }
88
- }
89
76
  this.ActionSystem = new events_1.default();
90
77
  this._userdialogs = {};
91
78
  this._bot.onText(/.+/g, function (msg) { return __awaiter(_this, void 0, void 0, function () {
@@ -259,6 +246,9 @@ var BotBuilder = /** @class */ (function () {
259
246
  BotBuilder.prototype._getButton = function (callback) {
260
247
  return this._schema.buttons.find(function (x) { return x._callback === callback; });
261
248
  };
249
+ BotBuilder.prototype._getButtonID = function (callback) {
250
+ return this._schema.buttons.findIndex(function (x) { return x._callback === callback; });
251
+ };
262
252
  BotBuilder.prototype._activateButton = function (chat, btn) {
263
253
  return __awaiter(this, void 0, void 0, function () {
264
254
  var _btn, _i, _a, act;
@@ -296,7 +286,7 @@ var BotBuilder = /** @class */ (function () {
296
286
  };
297
287
  BotBuilder.prototype.ChangeDialog = function (chat, id) {
298
288
  return __awaiter(this, void 0, void 0, function () {
299
- var dialog, btns, _a, markup, b, _i, _b, btn, text_1, _c, text, _d, _e, msg;
289
+ var dialog, btns, _a, markup, b, _i, _b, btn, _text, _c, psid, text, _d, _e, msg;
300
290
  return __generator(this, function (_f) {
301
291
  switch (_f.label) {
302
292
  case 0:
@@ -338,10 +328,12 @@ var BotBuilder = /** @class */ (function () {
338
328
  _c = btn.text;
339
329
  _f.label = 8;
340
330
  case 8:
341
- text_1 = _c;
331
+ _text = _c;
342
332
  btn._callback = this._hashbtn(dialog, btn);
343
- this._schema.buttons.push(btn);
344
- markup[b].push({ text: text_1, callback_data: btn._callback });
333
+ psid = this._getButtonID(btn._callback);
334
+ if (psid == -1)
335
+ this._schema.buttons.push(btn);
336
+ markup[b].push({ text: _text, callback_data: btn._callback });
345
337
  _f.label = 9;
346
338
  case 9:
347
339
  _i++;
@@ -365,13 +357,13 @@ var BotBuilder = /** @class */ (function () {
365
357
  _f.label = 15;
366
358
  case 15:
367
359
  _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' })];
360
+ return [4 /*yield*/, this._bot.editMessageText(text || "404", { chat_id: chat, message_id: this._userdialogs[chat].lastid, parse_mode: 'HTML' })];
369
361
  case 16:
370
362
  _f.sent();
371
363
  return [3 /*break*/, 19];
372
364
  case 17:
373
365
  _e = _f.sent();
374
- return [4 /*yield*/, this._bot.sendMessage(chat, text || "404", { reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML' })];
366
+ return [4 /*yield*/, this._bot.sendMessage(chat, text || "404", { parse_mode: 'HTML' })];
375
367
  case 18:
376
368
  msg = _f.sent();
377
369
  this._userdialogs[chat].lastid = msg.message_id;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "main": "lib/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -14,9 +14,10 @@
14
14
  "node-telegram-bot-api": "^0.66.0",
15
15
  "object-hash": "^3.0.0",
16
16
  "ts-node": "^10.9.2",
17
- "typescript": "^5.5.3"
17
+ "typescript": "^5.5.3",
18
+ "@types/node-telegram-bot-api": "^0.64.7"
18
19
  },
19
20
  "devDependencies": {
20
- "@types/node-telegram-bot-api": "^0.64.7"
21
+
21
22
  }
22
23
  }
@@ -66,19 +66,6 @@ export class BotBuilder {
66
66
  this._schema = schema as BotSchema;
67
67
  this._schema.buttons = [];
68
68
 
69
- for (let x = 0; x < this._schema.content.length; x++) {
70
- if (typeof this._schema.content[x].buttons === 'function') continue;
71
- for (let y = 0; y < this._schema.content[x].buttons.length; y++) {
72
- // @ts-ignore
73
- for (let b = 0; b < this._schema.content[x].buttons[y].length; b++) {
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
77
- this._schema.buttons.push(this._schema.content[x].buttons[y][b]);
78
- }
79
- }
80
- }
81
-
82
69
  this.ActionSystem = new EventEmitter();
83
70
  this._userdialogs = {};
84
71
 
@@ -148,6 +135,9 @@ export class BotBuilder {
148
135
  private _getButton(callback: string) {
149
136
  return this._schema.buttons.find(x => x._callback === callback);
150
137
  }
138
+ private _getButtonID(callback: string) {
139
+ return this._schema.buttons.findIndex(x => x._callback === callback);
140
+ }
151
141
  private async _activateButton(chat: number, btn: string | BotButton) {
152
142
  let _btn;
153
143
  if (typeof btn === 'string') _btn = this._getButton(btn);
@@ -178,10 +168,11 @@ export class BotBuilder {
178
168
  for (let b = 0; b < btns.length; b++) {
179
169
  markup[b] = [];
180
170
  for (let btn of btns[b]) {
181
- let text = typeof btn.text === 'function' ? await btn.text(chat) : btn.text;
171
+ let _text = typeof btn.text === 'function' ? await btn.text(chat) : btn.text;
182
172
  btn._callback = this._hashbtn(dialog!, btn);
183
- this._schema.buttons.push(btn);
184
- markup[b].push({ text, callback_data: btn._callback});
173
+ let psid = this._getButtonID(btn._callback);
174
+ if (psid == -1) this._schema.buttons.push(btn);
175
+ markup[b].push({ text: _text, callback_data: btn._callback});
185
176
  }
186
177
  }
187
178
  }
@@ -189,9 +180,9 @@ export class BotBuilder {
189
180
  if (dialog == undefined) markup = [[{text: "Меню", callback_data: this._schema.start}]]; // FIX RAND BTN
190
181
  let text = typeof dialog?.text === 'function' ? await dialog?.text(chat) : dialog?.text;
191
182
  try {
192
- await this._bot.editMessageText(text || "404", {chat_id: chat, message_id: this._userdialogs[chat].lastid, reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML'})
183
+ await this._bot.editMessageText(text || "404", {chat_id: chat, message_id: this._userdialogs[chat].lastid, parse_mode: 'HTML'})
193
184
  } catch {
194
- let msg = await this._bot.sendMessage(chat, text || "404", {reply_markup: { inline_keyboard: markup || [] }, parse_mode: 'HTML'});
185
+ let msg = await this._bot.sendMessage(chat, text || "404", {parse_mode: 'HTML'});
195
186
  this._userdialogs[chat].lastid = msg.message_id;
196
187
  }
197
188
  }