telegram-botbuilder 1.1.2 → 1.1.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.
@@ -23,7 +23,8 @@ export declare class BotBuilder {
23
23
  AttachDataWait(chat: number, descriptor: string): void;
24
24
  Message(chat: number, content: string): Promise<TelegramBot.Message>;
25
25
  }
26
+ type CallbackActionFunc = ((chatid: number, ...args: any[]) => void) | ((chatid: number, ...args: any[]) => Promise<void>);
26
27
  export declare function ChangeDialog(id: string): Action;
27
- export declare function CallbackAction(descriptor: string, ...args: any[]): Action;
28
- export declare function WaitForData(descriptor: string): Action;
28
+ export declare function CallbackAction(descriptor: string | CallbackActionFunc, ...args: any[]): Action;
29
+ export declare function WaitForData(descriptor: string | CallbackActionFunc): Action;
29
30
  export {};
@@ -366,25 +366,59 @@ function ChangeDialog(id) {
366
366
  return function (chat, _bot) { return _bot.ChangeDialog(chat, id); };
367
367
  }
368
368
  function CallbackAction(descriptor) {
369
+ var _this = this;
369
370
  var args = [];
370
371
  for (var _i = 1; _i < arguments.length; _i++) {
371
372
  args[_i - 1] = arguments[_i];
372
373
  }
373
374
  return function (chat, _bot) {
374
- var _a;
375
375
  var _args = [];
376
376
  for (var _i = 2; _i < arguments.length; _i++) {
377
377
  _args[_i - 2] = arguments[_i];
378
378
  }
379
- (_a = _bot.ActionSystem).emit.apply(_a, __spreadArray(__spreadArray([descriptor, chat], args, false), _args, false));
379
+ return __awaiter(_this, void 0, void 0, function () {
380
+ var _a;
381
+ var _b;
382
+ return __generator(this, function (_c) {
383
+ switch (_c.label) {
384
+ case 0:
385
+ if (!(typeof descriptor == 'function')) return [3 /*break*/, 2];
386
+ return [4 /*yield*/, descriptor.apply(void 0, __spreadArray(__spreadArray([chat], args, false), _args, false))];
387
+ case 1:
388
+ _a = _c.sent();
389
+ return [3 /*break*/, 3];
390
+ case 2:
391
+ _a = (_b = _bot.ActionSystem).emit.apply(_b, __spreadArray(__spreadArray([descriptor, chat], args, false), _args, false));
392
+ _c.label = 3;
393
+ case 3: return [2 /*return*/, _a];
394
+ }
395
+ });
396
+ });
380
397
  };
381
398
  }
382
399
  function WaitForData(descriptor) {
383
- return function (chat, _bot) {
384
- _bot.AttachDataWait(chat, descriptor);
385
- return new Promise(function (r) {
386
- _bot.ActionSystem.once(descriptor, r);
387
- _bot.ActionSystem.once("cancel_".concat(descriptor), r);
400
+ var _this = this;
401
+ return function (chat, _bot) { return __awaiter(_this, void 0, void 0, function () {
402
+ var desc_1;
403
+ return __generator(this, function (_a) {
404
+ if (typeof descriptor == 'string') {
405
+ _bot.AttachDataWait(chat, descriptor);
406
+ return [2 /*return*/, new Promise(function (r) {
407
+ _bot.ActionSystem.once(descriptor, r);
408
+ _bot.ActionSystem.once("cancel_".concat(descriptor), r);
409
+ })];
410
+ }
411
+ else {
412
+ desc_1 = crypto.randomUUID();
413
+ _bot.AttachDataWait(chat, desc_1);
414
+ return [2 /*return*/, new Promise(function (r) {
415
+ _bot.ActionSystem.once(desc_1, descriptor);
416
+ _bot.ActionSystem.once("cancel_".concat(desc_1), descriptor);
417
+ _bot.ActionSystem.once(desc_1, r);
418
+ _bot.ActionSystem.once("cancel_".concat(desc_1), r);
419
+ })];
420
+ }
421
+ return [2 /*return*/];
388
422
  });
389
- };
423
+ }); };
390
424
  }
@@ -1,5 +1,5 @@
1
1
  import { BotBuilder } from "./bot-service";
2
- export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => Promise<void> | void;
2
+ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => (Promise<any> | any);
3
3
  export interface Dialog {
4
4
  id: string;
5
5
  text: string | ((chat: number) => Promise<string>) | ((chat: number) => string);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-botbuilder",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "lib/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/readme.md CHANGED
@@ -12,7 +12,7 @@ const schema: Schema = {
12
12
  [
13
13
  {
14
14
  text: 'Button 1',
15
- action: [CallbackAction('button1_clicked')],
15
+ action: [CallbackAction('button1_clicked', 'optional arg')],
16
16
  },
17
17
  {
18
18
  text: 'Dialog 2',
@@ -23,7 +23,7 @@ const schema: Schema = {
23
23
  },
24
24
  {
25
25
  id: 'dialog2',
26
- text: 'Test dialog 2',
26
+ text: async (chatid: number) => { return "test dialog 2"; },
27
27
  buttons: [
28
28
  [
29
29
  {
@@ -185,18 +185,31 @@ export class BotBuilder {
185
185
  }
186
186
  }
187
187
 
188
+ type CallbackActionFunc = ((chatid: number, ...args: any[]) => void) | ((chatid: number, ...args: any[]) => Promise<void>)
189
+
188
190
  export function ChangeDialog(id: string): Action {
189
191
  return (chat: number, _bot: BotBuilder) => { return _bot.ChangeDialog(chat, id); };
190
192
  }
191
- export function CallbackAction(descriptor: string, ...args: any[]): Action {
192
- return (chat: number, _bot: BotBuilder, ..._args: any[]) => { _bot.ActionSystem.emit(descriptor, chat, ...args, ..._args); };
193
+ export function CallbackAction(descriptor: string | CallbackActionFunc, ...args: any[]): Action {
194
+ return async (chat: number, _bot: BotBuilder, ..._args: any[]) => { return typeof descriptor == 'function' ? await descriptor(chat, ...args, ..._args) : _bot.ActionSystem.emit(descriptor, chat, ...args, ..._args); };
193
195
  }
194
- export function WaitForData(descriptor: string): Action {
195
- return (chat: number, _bot: BotBuilder) => {
196
- _bot.AttachDataWait(chat, descriptor);
197
- return new Promise(r => {
198
- _bot.ActionSystem.once(descriptor, r);
199
- _bot.ActionSystem.once(`cancel_${descriptor}`, r);
200
- });
196
+ export function WaitForData(descriptor: string | CallbackActionFunc): Action {
197
+ return async (chat: number, _bot: BotBuilder) => {
198
+ if (typeof descriptor == 'string') {
199
+ _bot.AttachDataWait(chat, descriptor);
200
+ return new Promise(r => {
201
+ _bot.ActionSystem.once(descriptor, r);
202
+ _bot.ActionSystem.once(`cancel_${descriptor}`, r);
203
+ });
204
+ } else {
205
+ let desc = crypto.randomUUID();
206
+ _bot.AttachDataWait(chat, desc);
207
+ return new Promise(r => {
208
+ _bot.ActionSystem.once(desc, descriptor);
209
+ _bot.ActionSystem.once(`cancel_${desc}`, descriptor);
210
+ _bot.ActionSystem.once(desc, r);
211
+ _bot.ActionSystem.once(`cancel_${desc}`, r);
212
+ });
213
+ }
201
214
  };
202
215
  }
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, ...args: any[]) => Promise<void> | void;
4
+ export type Action = (chat: number, _bot: BotBuilder, ...args: any[]) => (Promise<any> | any);
5
5
 
6
6
  export interface Dialog {
7
7
  id: string;