chz-telegram-bot 0.0.51 → 0.0.52

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.
Files changed (70) hide show
  1. package/dist/dtos/commandTriggerCheckResult.d.ts +5 -5
  2. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
  3. package/dist/dtos/commandTriggerCheckResult.js +3 -6
  4. package/dist/dtos/incomingMessage.d.ts +6 -6
  5. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  6. package/dist/entities/actions/commandAction.d.ts +1 -0
  7. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  8. package/dist/entities/actions/commandAction.js +38 -37
  9. package/dist/entities/actions/scheduledAction.d.ts +1 -1
  10. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  11. package/dist/entities/actions/scheduledAction.js +12 -16
  12. package/dist/entities/botInstance.d.ts +11 -3
  13. package/dist/entities/botInstance.d.ts.map +1 -1
  14. package/dist/entities/botInstance.js +24 -17
  15. package/dist/entities/context/chatContext.d.ts +10 -10
  16. package/dist/entities/context/chatContext.d.ts.map +1 -1
  17. package/dist/entities/context/chatContext.js +5 -12
  18. package/dist/entities/context/messageContext.d.ts +4 -2
  19. package/dist/entities/context/messageContext.d.ts.map +1 -1
  20. package/dist/entities/context/messageContext.js +4 -4
  21. package/dist/helpers/noop.d.ts +4 -2
  22. package/dist/helpers/noop.d.ts.map +1 -1
  23. package/dist/helpers/noop.js +3 -2
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/main.d.ts +10 -2
  27. package/dist/main.d.ts.map +1 -1
  28. package/dist/main.js +6 -11
  29. package/dist/services/jsonFileStorage.d.ts +1 -2
  30. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  31. package/dist/services/jsonFileStorage.js +3 -5
  32. package/dist/services/jsonLogger.d.ts +8 -0
  33. package/dist/services/jsonLogger.d.ts.map +1 -0
  34. package/dist/services/jsonLogger.js +28 -0
  35. package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
  36. package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
  37. package/dist/services/nodeTimeoutScheduler.js +33 -0
  38. package/dist/services/telegramApi.d.ts +7 -5
  39. package/dist/services/telegramApi.d.ts.map +1 -1
  40. package/dist/services/telegramApi.js +4 -4
  41. package/dist/types/commandCondition.d.ts +1 -1
  42. package/dist/types/commandCondition.d.ts.map +1 -1
  43. package/dist/types/logger.d.ts +6 -0
  44. package/dist/types/logger.d.ts.map +1 -0
  45. package/dist/types/logger.js +2 -0
  46. package/dist/types/scheduler.d.ts +7 -0
  47. package/dist/types/scheduler.d.ts.map +1 -0
  48. package/dist/types/scheduler.js +2 -0
  49. package/dist/types/storage.d.ts +1 -2
  50. package/dist/types/storage.d.ts.map +1 -1
  51. package/dtos/commandTriggerCheckResult.ts +10 -10
  52. package/dtos/incomingMessage.ts +6 -6
  53. package/entities/actions/commandAction.ts +48 -41
  54. package/entities/actions/scheduledAction.ts +28 -31
  55. package/entities/botInstance.ts +50 -32
  56. package/entities/context/chatContext.ts +20 -16
  57. package/entities/context/messageContext.ts +10 -6
  58. package/helpers/noop.ts +5 -2
  59. package/index.ts +3 -1
  60. package/main.ts +16 -15
  61. package/package.json +1 -1
  62. package/services/jsonFileStorage.ts +4 -6
  63. package/services/{logger.ts → jsonLogger.ts} +3 -3
  64. package/services/{taskScheduler.ts → nodeTimeoutScheduler.ts} +11 -7
  65. package/services/telegramApi.ts +15 -8
  66. package/types/commandCondition.ts +1 -1
  67. package/types/logger.ts +24 -0
  68. package/types/scheduler.ts +20 -0
  69. package/types/storage.ts +1 -2
  70. package/dtos/actionExecutionResult.ts +0 -11
@@ -1,10 +1,10 @@
1
1
  export declare class CommandTriggerCheckResult {
2
2
  static get DontTriggerAndSkipCooldown(): CommandTriggerCheckResult;
3
3
  static get DoNotTrigger(): CommandTriggerCheckResult;
4
- shouldTrigger: boolean;
5
- matchResults: RegExpExecArray[];
6
- skipCooldown: boolean;
7
- constructor(shouldTrigger: boolean, matchResults: RegExpExecArray[], skipCooldown: boolean);
8
- mergeWith(other: CommandTriggerCheckResult): this;
4
+ readonly shouldExecute: boolean;
5
+ readonly matchResults: RegExpExecArray[];
6
+ readonly skipCooldown: boolean;
7
+ constructor(shouldExecute: boolean, matchResults: RegExpExecArray[], skipCooldown: boolean);
8
+ mergeWith(other: CommandTriggerCheckResult): CommandTriggerCheckResult;
9
9
  }
10
10
  //# sourceMappingURL=commandTriggerCheckResult.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandTriggerCheckResult.d.ts","sourceRoot":"","sources":["../../dtos/commandTriggerCheckResult.ts"],"names":[],"mappings":"AAAA,qBAAa,yBAAyB;IAClC,MAAM,KAAK,0BAA0B,8BAEpC;IACD,MAAM,KAAK,YAAY,8BAEtB;IAED,aAAa,EAAE,OAAO,CAAC;IACvB,YAAY,EAAE,eAAe,EAAE,CAAC;IAChC,YAAY,EAAE,OAAO,CAAC;gBAGlB,aAAa,EAAE,OAAO,EACtB,YAAY,EAAE,eAAe,EAAE,EAC/B,YAAY,EAAE,OAAO;IAOzB,SAAS,CAAC,KAAK,EAAE,yBAAyB;CAO7C"}
1
+ {"version":3,"file":"commandTriggerCheckResult.d.ts","sourceRoot":"","sources":["../../dtos/commandTriggerCheckResult.ts"],"names":[],"mappings":"AAAA,qBAAa,yBAAyB;IAClC,MAAM,KAAK,0BAA0B,8BAEpC;IACD,MAAM,KAAK,YAAY,8BAEtB;IAED,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,YAAY,EAAE,eAAe,EAAE,CAAC;IACzC,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;gBAG3B,aAAa,EAAE,OAAO,EACtB,YAAY,EAAE,eAAe,EAAE,EAC/B,YAAY,EAAE,OAAO;IAOzB,SAAS,CAAC,KAAK,EAAE,yBAAyB;CAO7C"}
@@ -8,16 +8,13 @@ class CommandTriggerCheckResult {
8
8
  static get DoNotTrigger() {
9
9
  return new CommandTriggerCheckResult(false, [], false);
10
10
  }
11
- constructor(shouldTrigger, matchResults, skipCooldown) {
12
- this.shouldTrigger = shouldTrigger;
11
+ constructor(shouldExecute, matchResults, skipCooldown) {
12
+ this.shouldExecute = shouldExecute;
13
13
  this.matchResults = matchResults;
14
14
  this.skipCooldown = skipCooldown;
15
15
  }
16
16
  mergeWith(other) {
17
- this.shouldTrigger = this.shouldTrigger || other.shouldTrigger;
18
- this.matchResults = this.matchResults.concat(other.matchResults);
19
- this.skipCooldown = this.skipCooldown || other.skipCooldown;
20
- return this;
17
+ return new CommandTriggerCheckResult(this.shouldExecute || other.shouldExecute, this.matchResults.concat(other.matchResults), this.skipCooldown || other.skipCooldown);
21
18
  }
22
19
  }
23
20
  exports.CommandTriggerCheckResult = CommandTriggerCheckResult;
@@ -2,12 +2,12 @@ import { Message, Update, User } from 'telegraf/types';
2
2
  import { MessageTypeValue } from '../types/messageTypes';
3
3
  import { ChatInfo } from './chatInfo';
4
4
  export declare class IncomingMessage {
5
- message_id: number;
6
- chatInfo: ChatInfo;
7
- from: User | undefined;
8
- text: string;
9
- type: MessageTypeValue;
10
- traceId: number;
5
+ readonly message_id: number;
6
+ readonly chatInfo: ChatInfo;
7
+ readonly from: User | undefined;
8
+ readonly text: string;
9
+ readonly type: MessageTypeValue;
10
+ readonly traceId: number;
11
11
  private detectMessageType;
12
12
  constructor(ctxMessage: Update.New & (Update.NonChannel & Message));
13
13
  }
@@ -1 +1 @@
1
- {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../dtos/incomingMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,qBAAa,eAAe;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,SAA2B;IAElC,OAAO,CAAC,iBAAiB;gBAkBb,UAAU,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC;CAYrE"}
1
+ {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../dtos/incomingMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,qBAAa,eAAe;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,OAAO,SAA2B;IAE3C,OAAO,CAAC,iBAAiB;gBAkBb,UAAU,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC;CAYrE"}
@@ -18,6 +18,7 @@ export declare class CommandAction<TActionState extends IActionState> implements
18
18
  readonly key: ActionKey;
19
19
  constructor(trigger: CommandTrigger | CommandTrigger[], handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: number[], allowedUsers: number[], condition: CommandCondition<TActionState>, stateConstructor: () => TActionState);
20
20
  exec(ctx: MessageContext<TActionState>): Promise<import("../../types/response").BotResponse[]>;
21
+ private checkIfShouldBeExecuted;
21
22
  private checkTrigger;
22
23
  }
23
24
  //# sourceMappingURL=commandAction.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAG3D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAE5D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB,CAAC,YAAY,CAAC;IAEzC,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IACnD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;gBAGpB,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EA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ACnD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;gBAGpB,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,EACjB,cAAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY;IAelC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAkD5C,OAAO,CAAC,uBAAuB;IAqC/B,OAAO,CAAC,YAAY;CAmCvB"}
@@ -8,8 +8,7 @@ const moment_1 = __importDefault(require("moment"));
8
8
  const timeConvertions_1 = require("../../helpers/timeConvertions");
9
9
  const toArray_1 = require("../../helpers/toArray");
10
10
  const commandTriggerCheckResult_1 = require("../../dtos/commandTriggerCheckResult");
11
- const logger_1 = require("../../services/logger");
12
- const actionExecutionResult_1 = require("../../dtos/actionExecutionResult");
11
+ const noop_1 = require("../../helpers/noop");
13
12
  class CommandAction {
14
13
  constructor(trigger, handler, name, active, cooldown, chatsBlacklist, allowedUsers, condition, stateConstructor) {
15
14
  this.triggers = (0, toArray_1.toArray)(trigger);
@@ -27,17 +26,14 @@ class CommandAction {
27
26
  if (!ctx.isInitialized)
28
27
  throw new Error(`Context for ${this.key} is not initialized or already consumed`);
29
28
  if (!this.active || this.chatsBlacklist.includes(ctx.chatInfo.id))
30
- return [];
31
- const isConditionMet = await this.condition(ctx);
32
- if (!isConditionMet)
33
- return [];
29
+ return noop_1.Noop.NoResponse;
34
30
  const state = await ctx.storage.getActionState(this, ctx.chatInfo.id);
35
- const { shouldTrigger, matchResults, skipCooldown } = this.triggers
36
- .map((x) => this.checkTrigger(ctx, x, state))
31
+ const { shouldExecute, matchResults, skipCooldown } = this.triggers
32
+ .map((x) => this.checkIfShouldBeExecuted(ctx, x, state))
37
33
  .reduce((acc, curr) => acc.mergeWith(curr), commandTriggerCheckResult_1.CommandTriggerCheckResult.DoNotTrigger);
38
- if (!shouldTrigger)
39
- return [];
40
- logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, ` - Executing [${this.name}] in ${ctx.chatInfo.id}`);
34
+ if (!shouldExecute)
35
+ return noop_1.Noop.NoResponse;
36
+ ctx.logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, ` - Executing [${this.name}] in ${ctx.chatInfo.id}`);
41
37
  ctx.matchResults = matchResults;
42
38
  await this.handler(ctx, state);
43
39
  if (skipCooldown) {
@@ -46,14 +42,10 @@ class CommandAction {
46
42
  if (ctx.startCooldown) {
47
43
  state.lastExecutedDate = (0, moment_1.default)().valueOf();
48
44
  }
49
- ctx.updateActions.forEach((action) => action(state));
50
- await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, new actionExecutionResult_1.ActionExecutionResult(state, ctx.startCooldown && shouldTrigger));
51
- ctx.isInitialized = false;
45
+ await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, state);
52
46
  return ctx.responses;
53
47
  }
54
- checkTrigger(ctx, trigger, state) {
55
- let shouldTrigger = false;
56
- const matchResults = [];
48
+ checkIfShouldBeExecuted(ctx, trigger, state) {
57
49
  if (!ctx.fromUserId)
58
50
  return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
59
51
  const isUserAllowed = this.allowedUsers.length == 0 ||
@@ -65,29 +57,38 @@ class CommandAction {
65
57
  const onCooldown = (0, moment_1.default)().diff(lastExecutedDate) < cooldownInMilliseconds;
66
58
  if (onCooldown)
67
59
  return commandTriggerCheckResult_1.CommandTriggerCheckResult.DoNotTrigger;
68
- if (trigger == ctx.messageType) {
69
- shouldTrigger = true;
70
- }
71
- else if (typeof trigger == 'string') {
72
- shouldTrigger = ctx.messageText.toLowerCase() == trigger;
73
- }
74
- else {
75
- trigger.lastIndex = 0;
76
- const execResult = trigger.exec(ctx.messageText);
77
- if (execResult != null) {
78
- matchResults.push(execResult);
79
- if (trigger.global) {
80
- while (true) {
81
- const nextResult = trigger.exec(ctx.messageText);
82
- if (nextResult == null)
83
- break;
84
- matchResults.push(nextResult);
85
- }
60
+ const isCustomConditionMet = this.condition(ctx);
61
+ if (!isCustomConditionMet)
62
+ return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
63
+ const { shouldTrigger, matchResults } = this.checkTrigger(ctx, trigger);
64
+ return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults, false);
65
+ }
66
+ checkTrigger(ctx, trigger) {
67
+ if (trigger == ctx.messageType)
68
+ return { shouldTrigger: true, matchResults: [] };
69
+ if (typeof trigger == 'string')
70
+ return {
71
+ shouldTrigger: ctx.messageText.toLowerCase() == trigger,
72
+ matchResults: []
73
+ };
74
+ const matchResults = [];
75
+ trigger.lastIndex = 0;
76
+ const execResult = trigger.exec(ctx.messageText);
77
+ if (execResult != null) {
78
+ matchResults.push(execResult);
79
+ if (trigger.global) {
80
+ while (true) {
81
+ const nextResult = trigger.exec(ctx.messageText);
82
+ if (nextResult == null)
83
+ break;
84
+ matchResults.push(nextResult);
86
85
  }
87
86
  }
88
- shouldTrigger = matchResults.length > 0;
89
87
  }
90
- return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults, false);
88
+ return {
89
+ shouldTrigger: matchResults.length > 0,
90
+ matchResults
91
+ };
91
92
  }
92
93
  }
93
94
  exports.CommandAction = CommandAction;
@@ -19,6 +19,6 @@ export declare class ScheduledAction<TActionState extends IActionState> implemen
19
19
  constructor(name: string, handler: ScheduledHandler<TActionState>, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>, stateConstructor: () => TActionState);
20
20
  exec(ctx: ChatContext<TActionState>): Promise<import("../../types/response").BotResponse[]>;
21
21
  private getCachedValue;
22
- private shouldTrigger;
22
+ private checkIfShouldBeExecuted;
23
23
  }
24
24
  //# sourceMappingURL=scheduledAction.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAKrD,qBAAa,eAAe,CAAC,YAAY,SAAS,YAAY,CAC1D,YAAW,gBAAgB,CAAC,YAAY,CAAC;IAEzC,MAAM,CAAC,QAAQ,CAAC,KAAK,yBAAgC;IAErD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,UAAU,CAAC;IACjC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IAExB,QAAQ,CAAC,WAAW,uBAA8B;IAClD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC/D,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAG7C,IAAI,EAAE,MAAM,EAC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QAAQ,CAAC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC/D,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAG7C,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAYlC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC;YA0C3B,cAAc;IA+C5B,OAAO,CAAC,uBAAuB;CAalC"}
@@ -7,9 +7,7 @@ exports.ScheduledAction = void 0;
7
7
  const moment_1 = __importDefault(require("moment"));
8
8
  const async_sema_1 = require("async-sema");
9
9
  const timeConvertions_1 = require("../../helpers/timeConvertions");
10
- const actionExecutionResult_1 = require("../../dtos/actionExecutionResult");
11
- const logger_1 = require("../../services/logger");
12
- const taskScheduler_1 = require("../../services/taskScheduler");
10
+ const noop_1 = require("../../helpers/noop");
13
11
  class ScheduledAction {
14
12
  constructor(name, handler, timeinHours, active, whitelist, cachedStateFactories, stateConstructor) {
15
13
  this.cachedState = new Map();
@@ -26,20 +24,18 @@ class ScheduledAction {
26
24
  if (!ctx.isInitialized)
27
25
  throw new Error(`Context for ${this.key} is not initialized or already consumed`);
28
26
  if (!this.active || !this.chatsWhitelist.includes(ctx.chatInfo.id))
29
- return [];
27
+ return noop_1.Noop.NoResponse;
30
28
  const state = await ctx.storage.getActionState(this, ctx.chatInfo.id);
31
- const isAllowedToTrigger = this.shouldTrigger(state);
32
- if (isAllowedToTrigger) {
33
- logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, ` - Executing [${this.name}] in ${ctx.chatInfo.id}`);
34
- await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName), state);
35
- state.lastExecutedDate = (0, moment_1.default)().valueOf();
36
- ctx.updateActions.forEach((action) => action(state));
37
- await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, new actionExecutionResult_1.ActionExecutionResult(state, isAllowedToTrigger));
38
- }
39
- ctx.isInitialized = false;
29
+ const isAllowedToTrigger = this.checkIfShouldBeExecuted(state);
30
+ if (!isAllowedToTrigger)
31
+ return noop_1.Noop.NoResponse;
32
+ ctx.logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, ` - Executing [${this.name}] in ${ctx.chatInfo.id}`);
33
+ await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName, ctx.scheduler), state);
34
+ state.lastExecutedDate = (0, moment_1.default)().valueOf();
35
+ await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, state);
40
36
  return ctx.responses;
41
37
  }
42
- async getCachedValue(key, botName) {
38
+ async getCachedValue(key, botName, scheduler) {
43
39
  if (!this.cachedStateFactories.has(key)) {
44
40
  throw new Error(`No shared cache was set up for the key [${key}] in action '${this.name}'`);
45
41
  }
@@ -60,14 +56,14 @@ class ScheduledAction {
60
56
  const cachedItemFactory = this.cachedStateFactories.get(key);
61
57
  const value = await cachedItemFactory.getValue();
62
58
  this.cachedState.set(key, value);
63
- taskScheduler_1.Scheduler.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), (0, timeConvertions_1.hoursToMilliseconds)(cachedItemFactory.invalidationTimeoutInHours), botName);
59
+ scheduler.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), (0, timeConvertions_1.hoursToMilliseconds)(cachedItemFactory.invalidationTimeoutInHours), botName);
64
60
  return value;
65
61
  }
66
62
  finally {
67
63
  semaphore.release();
68
64
  }
69
65
  }
70
- shouldTrigger(state) {
66
+ checkIfShouldBeExecuted(state) {
71
67
  const startOfToday = (0, moment_1.default)().startOf('day').valueOf();
72
68
  const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
73
69
  const currentTime = (0, moment_1.default)();
@@ -3,24 +3,32 @@ import { IStorageClient } from '../types/storage';
3
3
  import { IActionState } from '../types/actionState';
4
4
  import { CommandAction } from './actions/commandAction';
5
5
  import { ScheduledAction } from './actions/scheduledAction';
6
+ import { ILogger } from '../types/logger';
7
+ import { IScheduler } from '../types/scheduler';
6
8
  export declare class BotInstance {
7
- readonly name: string;
8
9
  private readonly api;
10
+ private readonly storage;
11
+ private readonly scheduler;
12
+ private readonly logger;
13
+ readonly name: string;
9
14
  private readonly telegraf;
10
15
  private readonly commands;
11
16
  private readonly scheduled;
12
17
  private readonly chats;
13
- readonly storage: IStorageClient;
14
18
  constructor(options: {
15
19
  name: string;
16
20
  token: string;
17
21
  commands: CommandAction<IActionState>[];
18
22
  scheduled: ScheduledAction<IActionState>[];
19
23
  chats: Record<string, number>;
20
- storageClient?: IStorageClient;
21
24
  storagePath?: string;
22
25
  scheduledPeriod?: Seconds;
23
26
  verboseLoggingForIncomingMessage?: boolean;
27
+ services?: {
28
+ storageClient?: IStorageClient;
29
+ logger?: ILogger;
30
+ scheduler?: IScheduler;
31
+ };
24
32
  });
25
33
  private initializeScheduledProcessing;
26
34
  private initializeMessageProcessing;
@@ -1 +1 @@
1
- {"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAS5D,qBAAa,WAAW;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAqB;IACzC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AQ,CAAC,GAAG,CAAqB;IACzC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;IACzD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAkC;IAC5D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;gBAEnC,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAwCD,OAAO,CAAC,6BAA6B;IA2CrC,OAAO,CAAC,2BAA2B;IAiC7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAaT,YAAY;YAmCZ,cAAc;CA2B/B"}
@@ -8,13 +8,13 @@ const telegraf_1 = require("telegraf");
8
8
  const timeConvertions_1 = require("../helpers/timeConvertions");
9
9
  const jsonFileStorage_1 = require("../services/jsonFileStorage");
10
10
  const telegramApi_1 = require("../services/telegramApi");
11
- const logger_1 = require("../services/logger");
12
- const taskScheduler_1 = require("../services/taskScheduler");
11
+ const jsonLogger_1 = require("../services/jsonLogger");
13
12
  const incomingMessage_1 = require("../dtos/incomingMessage");
14
13
  const moment_1 = __importDefault(require("moment"));
15
14
  const chatContext_1 = require("./context/chatContext");
16
15
  const messageContext_1 = require("./context/messageContext");
17
16
  const chatInfo_1 = require("../dtos/chatInfo");
17
+ const nodeTimeoutScheduler_1 = require("../services/nodeTimeoutScheduler");
18
18
  class BotInstance {
19
19
  constructor(options) {
20
20
  this.name = options.name;
@@ -22,22 +22,26 @@ class BotInstance {
22
22
  this.scheduled = options.scheduled;
23
23
  this.chats = options.chats;
24
24
  const actions = [...this.commands, ...this.scheduled];
25
- logger_1.Logger.logWithTraceId(this.name, `System:Bot-${this.name}-Start`, 'System', 'Starting bot...');
26
25
  this.telegraf = new telegraf_1.Telegraf(options.token);
26
+ this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
27
+ this.scheduler =
28
+ options.services?.scheduler ??
29
+ new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
27
30
  this.storage =
28
- options.storageClient ??
31
+ options.services?.storageClient ??
29
32
  new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
30
- this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage);
33
+ this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
31
34
  this.initializeMessageProcessing(options.verboseLoggingForIncomingMessage ?? false);
32
35
  this.initializeScheduledProcessing(options.scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
33
36
  this.storage.saveMetadata(actions, this.name);
37
+ this.logger.logWithTraceId(this.name, `System:Bot-${this.name}-Start`, 'System', 'Starting bot...');
34
38
  this.telegraf.launch();
35
39
  }
36
40
  initializeScheduledProcessing(period) {
37
41
  if (this.scheduled.length > 0) {
38
42
  const now = (0, moment_1.default)();
39
43
  if (now.minute() == 0 && now.second() == 0) {
40
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
44
+ this.scheduler.createTask('ScheduledProcessing', async () => {
41
45
  await this.runScheduled();
42
46
  }, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
43
47
  return;
@@ -47,8 +51,8 @@ class BotInstance {
47
51
  nextExecutionTime = nextExecutionTime.add(1, 'hour');
48
52
  }
49
53
  const delay = nextExecutionTime.diff(now);
50
- taskScheduler_1.Scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
51
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
54
+ this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
55
+ this.scheduler.createTask('ScheduledProcessing', async () => {
52
56
  await this.runScheduled();
53
57
  }, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
54
58
  }, delay, this.name);
@@ -62,46 +66,49 @@ class BotInstance {
62
66
  const messageFromName = msg.from?.first_name ?? 'Unknown';
63
67
  const messageFromId = msg.from?.id ?? 'Unknown';
64
68
  if (verboseLoggingForIncomingMessage) {
65
- logger_1.Logger.logObjectWithTraceId(this.name, msg.traceId, msg.chatInfo.name, ctx.update.message);
69
+ this.logger.logObjectWithTraceId(this.name, msg.traceId, msg.chatInfo.name, ctx.update.message);
66
70
  }
67
71
  else {
68
- logger_1.Logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${messageFromName} (${messageFromId}): ${messageContent}`);
72
+ this.logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${messageFromName} (${messageFromId}): ${messageContent}`);
69
73
  }
70
74
  await this.processMessage(msg);
71
75
  });
72
76
  }
73
77
  }
74
78
  async stop(code) {
75
- logger_1.Logger.logWithTraceId(this.name, `System:Bot-${this.name}-Stop`, 'System', 'Stopping bot...');
79
+ this.logger.logWithTraceId(this.name, `System:Bot-${this.name}-Stop`, 'System', 'Stopping bot...');
80
+ this.scheduler.stopAll();
76
81
  await this.storage.close();
77
82
  this.telegraf.stop(code);
78
83
  }
79
84
  async runScheduled() {
80
- const ctx = new chatContext_1.ChatContext();
85
+ const ctx = new chatContext_1.ChatContext(this.storage, this.logger, this.scheduler);
81
86
  for (const [chatName, chatId] of Object.entries(this.chats)) {
82
87
  for (const scheduledAction of this.scheduled) {
83
- ctx.initializeChatContext(this.name, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
88
+ ctx.initializeChatContext(this.name, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), `Scheduled:${scheduledAction.key}:${chatId}`);
84
89
  try {
85
90
  const responses = await scheduledAction.exec(ctx);
86
91
  this.api.enqueueBatchedResponses(responses);
92
+ ctx.isInitialized = false;
87
93
  }
88
94
  catch (error) {
89
- logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
95
+ this.logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
90
96
  }
91
97
  }
92
98
  }
93
99
  await this.api.flushResponses();
94
100
  }
95
101
  async processMessage(msg) {
96
- const ctx = new messageContext_1.MessageContext();
102
+ const ctx = new messageContext_1.MessageContext(this.storage, this.logger, this.scheduler);
97
103
  for (const commandAction of this.commands) {
98
- ctx.initializeMessageContext(this.name, commandAction, msg, this.storage);
104
+ ctx.initializeMessageContext(this.name, commandAction, msg);
99
105
  try {
100
106
  const responses = await commandAction.exec(ctx);
101
107
  this.api.enqueueBatchedResponses(responses);
108
+ ctx.isInitialized = false;
102
109
  }
103
110
  catch (error) {
104
- logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, error, ctx);
111
+ this.logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, error, ctx);
105
112
  }
106
113
  }
107
114
  await this.api.flushResponses();
@@ -5,30 +5,30 @@ import { IActionState } from '../../types/actionState';
5
5
  import { BotResponse } from '../../types/response';
6
6
  import { Milliseconds } from '../../types/timeValues';
7
7
  import { ChatInfo } from '../../dtos/chatInfo';
8
+ import { ILogger } from '../../types/logger';
9
+ import { IScheduler } from '../../types/scheduler';
8
10
  /**
9
11
  * Context of action executed in chat.
10
12
  */
11
13
  export declare class ChatContext<TActionState extends IActionState> {
12
14
  protected action: IActionWithState<TActionState>;
13
- updateActions: Array<(state: TActionState) => void>;
15
+ /** Storage client instance for the bot executing this action. */
16
+ readonly storage: IStorageClient;
17
+ /** Logger instance for the bot executing this action */
18
+ readonly logger: ILogger;
19
+ /** Scheduler instance for the bot executing this action */
20
+ readonly scheduler: IScheduler;
14
21
  /** Trace id of a action execution. */
15
22
  traceId: number | string;
16
23
  /** Name of a bot that executes this action. */
17
24
  botName: string;
18
25
  /** Chat information. */
19
26
  chatInfo: ChatInfo;
20
- /** Storage client instance for this bot. */
21
- storage: IStorageClient;
22
27
  /** Ordered collection of responses to be processed */
23
28
  responses: BotResponse[];
24
29
  isInitialized: boolean;
25
- constructor();
26
- initializeChatContext(botName: string, action: IActionWithState<TActionState>, chatInfo: ChatInfo, traceId: number | string, storage: IStorageClient): this;
27
- /**
28
- * Manually update the state of an action.
29
- * @param stateUpdateAction Function that will modify state.
30
- */
31
- updateState(stateUpdateAction: (state: TActionState) => void): void;
30
+ constructor(storage: IStorageClient, logger: ILogger, scheduler: IScheduler);
31
+ initializeChatContext(botName: string, action: IActionWithState<TActionState>, chatInfo: ChatInfo, traceId: number | string): this;
32
32
  /**
33
33
  * Sends text message to chat after action execution is finished.
34
34
  * If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
@@ -1 +1 @@
1
- {"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAA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tC,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,MAAM,GAAG,MAAM;IAa5B;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAahE;;;;;OAKG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;;OAKG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;OAIG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM;IAW9B;;;OAGG;IACH,iBAAiB,CAAC,KAAK,EAAE,YAAY;CAKxC"}
@@ -11,30 +11,23 @@ const delay_1 = require("../../dtos/responses/delay");
11
11
  * Context of action executed in chat.
12
12
  */
13
13
  class ChatContext {
14
- constructor() {
15
- this.updateActions = [];
14
+ constructor(storage, logger, scheduler) {
16
15
  /** Ordered collection of responses to be processed */
17
16
  this.responses = [];
18
17
  this.isInitialized = false;
18
+ this.storage = storage;
19
+ this.logger = logger;
20
+ this.scheduler = scheduler;
19
21
  }
20
- initializeChatContext(botName, action, chatInfo, traceId, storage) {
22
+ initializeChatContext(botName, action, chatInfo, traceId) {
21
23
  this.botName = botName;
22
24
  this.action = action;
23
25
  this.chatInfo = chatInfo;
24
26
  this.traceId = traceId;
25
- this.storage = storage;
26
- this.updateActions = [];
27
27
  this.isInitialized = true;
28
28
  this.responses = [];
29
29
  return this;
30
30
  }
31
- /**
32
- * Manually update the state of an action.
33
- * @param stateUpdateAction Function that will modify state.
34
- */
35
- updateState(stateUpdateAction) {
36
- this.updateActions.push(stateUpdateAction);
37
- }
38
31
  /**
39
32
  * Sends text message to chat after action execution is finished.
40
33
  * If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
@@ -6,6 +6,8 @@ import { IncomingMessage } from '../../dtos/incomingMessage';
6
6
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
7
7
  import { IActionWithState } from '../../types/actionWithState';
8
8
  import { MessageTypeValue } from '../../types/messageTypes';
9
+ import { ILogger } from '../../types/logger';
10
+ import { IScheduler } from '../../types/scheduler';
9
11
  /**
10
12
  * Context of action executed in chat, in response to a message
11
13
  */
@@ -24,8 +26,8 @@ export declare class MessageContext<TActionState extends IActionState> extends C
24
26
  fromUserName: string;
25
27
  /** Type of message being received */
26
28
  messageType: MessageTypeValue;
27
- constructor();
28
- initializeMessageContext(botName: string, action: IActionWithState<TActionState>, message: IncomingMessage, storage: IStorageClient): this;
29
+ constructor(storage: IStorageClient, logger: ILogger, scheduler: IScheduler);
30
+ initializeMessageContext(botName: string, action: IActionWithState<TActionState>, message: IncomingMessage): this;
29
31
  /**
30
32
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
31
33
  * @param commandName Name of an action to load state of.
@@ -1 +1 @@
1
- {"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAa,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW,CAAC,YAAY,CAAC;IAC/B,kDAAkD;IAClD,SAAS,EAAG,MAAM,CAAC;IACnB,oDAAoD;IACpD,WAAW,EAAG,MA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a;CAW7B"}
@@ -12,14 +12,14 @@ const chatContext_1 = require("./chatContext");
12
12
  * Context of action executed in chat, in response to a message
13
13
  */
14
14
  class MessageContext extends chatContext_1.ChatContext {
15
- constructor() {
16
- super();
15
+ constructor(storage, logger, scheduler) {
16
+ super(storage, logger, scheduler);
17
17
  /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
18
18
  this.matchResults = [];
19
19
  /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
20
20
  this.startCooldown = true;
21
21
  }
22
- initializeMessageContext(botName, action, message, storage) {
22
+ initializeMessageContext(botName, action, message) {
23
23
  this.messageId = message.message_id;
24
24
  this.messageText = message.text ?? '';
25
25
  this.messageType = message.type;
@@ -29,7 +29,7 @@ class MessageContext extends chatContext_1.ChatContext {
29
29
  (message.from?.last_name ? ` ${message.from.last_name}` : '');
30
30
  this.matchResults = [];
31
31
  this.startCooldown = true;
32
- return this.initializeChatContext(botName, action, message.chatInfo, message.traceId, storage);
32
+ return this.initializeChatContext(botName, action, message.chatInfo, message.traceId);
33
33
  }
34
34
  /**
35
35
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
@@ -1,6 +1,8 @@
1
+ import { BotResponse } from '../types/response';
1
2
  export declare class Noop {
2
- static true<T1>(arg1: T1): Promise<boolean>;
3
- static false<T1>(arg1: T1): Promise<boolean>;
3
+ static NoResponse: BotResponse[];
4
+ static true<T1>(arg1: T1): boolean;
5
+ static false<T1>(arg1: T1): boolean;
4
6
  static call<T1, T2>(arg1: T1, arg2: T2): Promise<void>;
5
7
  }
6
8
  //# sourceMappingURL=noop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../helpers/noop.ts"],"names":[],"mappings":"AACA,qBAAa,IAAI;WACA,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGjB,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGlB,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAE/D"}
1
+ {"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../helpers/noop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,qBAAa,IAAI;IACb,MAAM,CAAC,UAAU,EAAE,WAAW,EAAE,CAAM;IACtC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;IAGxB,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGZ,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAE/D"}
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Noop = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-unused-vars */
5
5
  class Noop {
6
- static async true(arg1) {
6
+ static true(arg1) {
7
7
  return true;
8
8
  }
9
- static async false(arg1) {
9
+ static false(arg1) {
10
10
  return false;
11
11
  }
12
12
  static async call(arg1) { }
13
13
  }
14
14
  exports.Noop = Noop;
15
+ Noop.NoResponse = [];