chz-telegram-bot 0.0.24 → 0.0.26

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 (36) hide show
  1. package/dist/entities/actions/commandAction.d.ts +1 -1
  2. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  3. package/dist/entities/actions/commandAction.js +12 -7
  4. package/dist/entities/actions/scheduledAction.d.ts +1 -1
  5. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  6. package/dist/entities/actions/scheduledAction.js +1 -0
  7. package/dist/entities/botInstance.d.ts.map +1 -1
  8. package/dist/entities/botInstance.js +6 -6
  9. package/dist/entities/context/chatContext.d.ts +7 -1
  10. package/dist/entities/context/chatContext.d.ts.map +1 -1
  11. package/dist/entities/context/chatContext.js +8 -0
  12. package/dist/entities/context/messageContext.d.ts +3 -8
  13. package/dist/entities/context/messageContext.d.ts.map +1 -1
  14. package/dist/entities/context/messageContext.js +15 -15
  15. package/dist/helpers/builders/commandActionBuilder.d.ts +3 -3
  16. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  17. package/dist/services/logger.d.ts.map +1 -1
  18. package/dist/services/logger.js +2 -8
  19. package/dist/services/taskScheduler.d.ts.map +1 -1
  20. package/dist/services/taskScheduler.js +0 -1
  21. package/dist/services/telegramApi.d.ts +6 -3
  22. package/dist/services/telegramApi.d.ts.map +1 -1
  23. package/dist/services/telegramApi.js +6 -9
  24. package/dist/types/handlers.d.ts +1 -1
  25. package/dist/types/handlers.d.ts.map +1 -1
  26. package/entities/actions/commandAction.ts +17 -9
  27. package/entities/actions/scheduledAction.ts +2 -1
  28. package/entities/botInstance.ts +9 -6
  29. package/entities/context/chatContext.ts +10 -1
  30. package/entities/context/messageContext.ts +20 -32
  31. package/helpers/builders/commandActionBuilder.ts +3 -3
  32. package/package.json +1 -1
  33. package/services/logger.ts +6 -8
  34. package/services/taskScheduler.ts +0 -1
  35. package/services/telegramApi.ts +19 -22
  36. package/types/handlers.ts +1 -1
@@ -15,7 +15,7 @@ export declare class CommandAction<TActionState extends IActionState> implements
15
15
  condition: CommandCondition<TActionState>;
16
16
  stateConstructor: () => TActionState;
17
17
  key: string;
18
- constructor(trigger: string | RegExp | Array<string> | Array<RegExp>, handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: Array<number>, allowedUsers: Array<number>, condition: CommandCondition<TActionState>, stateConstructor: () => TActionState);
18
+ constructor(trigger: string | RegExp | string[] | RegExp[], handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: number[], allowedUsers: number[], condition: CommandCondition<TActionState>, stateConstructor: () => TActionState);
19
19
  exec(ctx: MessageContext<TActionState>): Promise<void>;
20
20
  private checkTrigger;
21
21
  }
@@ -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,MAAM,6BAA6B,CAAC;AAE/D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAI3D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB;IAE3B,QAAQ,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAC9B,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,EAAE,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,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,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,GAAG,EAAE,MAAM,CAAC;gBAGR,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,EAC9C,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;IAgD5C,OAAO,CAAC,YAAY;CAkDvB"}
@@ -62,15 +62,20 @@ class CommandAction {
62
62
  shouldTrigger = ctx.messageText.toLowerCase() == trigger;
63
63
  }
64
64
  else {
65
- let matchCount = ctx.messageText.match(trigger)?.length ?? 0;
66
- if (matchCount > 0) {
67
- for (; matchCount > 0; matchCount--) {
68
- const execResult = trigger.exec(ctx.messageText);
69
- if (execResult)
70
- matchResults.push(execResult);
65
+ trigger.lastIndex = 0;
66
+ const execResult = trigger.exec(ctx.messageText);
67
+ if (execResult != null) {
68
+ matchResults.push(execResult);
69
+ if (trigger.global) {
70
+ while (true) {
71
+ const nextResult = trigger.exec(ctx.messageText);
72
+ if (nextResult == null)
73
+ break;
74
+ matchResults.push(nextResult);
75
+ }
71
76
  }
72
- shouldTrigger = true;
73
77
  }
78
+ shouldTrigger = matchResults.length > 0;
74
79
  }
75
80
  }
76
81
  return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults, !isUserAllowed);
@@ -17,7 +17,7 @@ export declare class ScheduledAction<TActionState extends IActionState> implemen
17
17
  cachedStateFactories: Map<string, CachedStateFactory>;
18
18
  handler: ScheduledHandler<TActionState>;
19
19
  constructor(name: string, handler: ScheduledHandler<TActionState>, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>, stateConstructor: () => TActionState);
20
- exec(ctx: ChatContext): Promise<void>;
20
+ exec(ctx: ChatContext<TActionState>): Promise<void>;
21
21
  private getCachedValue;
22
22
  private shouldTrigger;
23
23
  }
@@ -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,MAAM,6BAA6B,CAAC;AAC/D,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;IAE3B,MAAM,CAAC,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,uBAA8B;IACzC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAGpC,IAAI,EAAE,MA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E,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;YAmC3B,cAAc;IAqC5B,OAAO,CAAC,aAAa;CASxB"}
@@ -31,6 +31,7 @@ class ScheduledAction {
31
31
  logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
32
32
  await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName), state);
33
33
  state.lastExecutedDate = (0, moment_1.default)().valueOf();
34
+ ctx.updateActions.forEach((action) => action(state));
34
35
  await ctx.storage.saveActionExecutionResult(this, ctx.chatId, new actionExecutionResult_1.ActionExecutionResult(state, isAllowedToTrigger));
35
36
  }
36
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAGA,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;AAM5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EA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CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IA6CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAyBZ,cAAc;CAmB/B"}
@@ -71,10 +71,10 @@ class BotInstance {
71
71
  }
72
72
  async runScheduled() {
73
73
  for (const [chatName, chatId] of Object.entries(this.chats)) {
74
- for (const trig of this.scheduled) {
75
- const ctx = this.api.createContextForChat(chatId, trig.key);
74
+ for (const scheduledAction of this.scheduled) {
75
+ const ctx = this.api.createContextForChat(chatId, scheduledAction);
76
76
  try {
77
- await trig.exec(ctx);
77
+ await scheduledAction.exec(ctx);
78
78
  }
79
79
  catch (error) {
80
80
  logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
@@ -84,10 +84,10 @@ class BotInstance {
84
84
  await this.api.flushResponses();
85
85
  }
86
86
  async processMessage(msg) {
87
- for (const cmd of this.commands) {
88
- const ctx = this.api.createContextForMessage(msg, cmd.key);
87
+ for (const commandAction of this.commands) {
88
+ const ctx = this.api.createContextForMessage(msg, commandAction);
89
89
  try {
90
- await cmd.exec(ctx);
90
+ await commandAction.exec(ctx);
91
91
  }
92
92
  catch (error) {
93
93
  logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, error, ctx);
@@ -4,9 +4,10 @@ import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/me
4
4
  /**
5
5
  * Context of action executed in chat.
6
6
  */
7
- export declare class ChatContext {
7
+ export declare class ChatContext<TActionState> {
8
8
  protected actionKey: string;
9
9
  protected interactions: IBotApiInteractions;
10
+ updateActions: Array<(state: TActionState) => void>;
10
11
  /** Trace id of a action execution. */
11
12
  traceId: number | string;
12
13
  /** Name of a bot that executes this action. */
@@ -18,6 +19,11 @@ export declare class ChatContext {
18
19
  /** Storage client instance for this bot. */
19
20
  storage: IStorageClient;
20
21
  constructor(botName: string, actionKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient);
22
+ /**
23
+ * Manually update the state of an action.
24
+ * @param stateUpdateAction Function that will modify state.
25
+ */
26
+ updateState(stateUpdateAction: (state: TActionState) => void): void;
21
27
  /**
22
28
  * Sends text message to chat.
23
29
  * @param text Message contents.
@@ -1 +1 @@
1
- {"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,WAAW;IACpB,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC;IAC5B,SAAS,CAAC,YAAY,EAAE,mBAAmB,CAAC;IAC5C,sCAAsC;IACtC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,+CAA+C;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,OAAO,EAAE,cAAc,CAAC;gBAGpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,mBAAmB,EACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,OAAO,EAAE,cAAc;IAW3B;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAahE;;;;OAIG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;OAIG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IA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AAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;OAGG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM;CAUjC"}
@@ -11,6 +11,7 @@ const unpin_1 = require("../responses/unpin");
11
11
  */
12
12
  class ChatContext {
13
13
  constructor(botName, actionKey, interactions, chatId, chatName, traceId, storage) {
14
+ this.updateActions = [];
14
15
  this.botName = botName;
15
16
  this.actionKey = actionKey;
16
17
  this.interactions = interactions;
@@ -19,6 +20,13 @@ class ChatContext {
19
20
  this.traceId = traceId;
20
21
  this.storage = storage;
21
22
  }
23
+ /**
24
+ * Manually update the state of an action.
25
+ * @param stateUpdateAction Function that will modify state.
26
+ */
27
+ updateState(stateUpdateAction) {
28
+ this.updateActions.push(stateUpdateAction);
29
+ }
22
30
  /**
23
31
  * Sends text message to chat.
24
32
  * @param text Message contents.
@@ -3,12 +3,12 @@ import { TelegramEmoji } from 'telegraf/types';
3
3
  import { IStorageClient } from '../../types/storage';
4
4
  import { IActionState } from '../../types/actionState';
5
5
  import { ChatContext } from './chatContext';
6
+ import { IncomingMessage } from '../incomingMessage';
6
7
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
7
8
  /**
8
9
  * Context of action executed in chat, in response to a message
9
10
  */
10
- export declare class MessageContext<TActionState extends IActionState> extends ChatContext {
11
- updateActions: Array<(state: TActionState) => void>;
11
+ export declare class MessageContext<TActionState extends IActionState> extends ChatContext<TActionState> {
12
12
  /** Id of a message that triggered this action. */
13
13
  messageId: number;
14
14
  /** Text of a message that triggered this action. */
@@ -21,18 +21,13 @@ export declare class MessageContext<TActionState extends IActionState> extends C
21
21
  startCooldown: boolean;
22
22
  /** Name of a user that sent a message that triggered this action. */
23
23
  fromUserName: string;
24
- constructor(botName: string, scheduledKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, messageId: number, messageText: string, fromUserId: number | undefined, traceId: number | string, fromUserName: string, storage: IStorageClient);
24
+ constructor(botName: string, scheduledKey: string, interactions: IBotApiInteractions, message: IncomingMessage, storage: IStorageClient);
25
25
  /**
26
26
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
27
27
  * @param commandName Name of an action to load state of.
28
28
  * @template TAnotherActionState - Type of a state that is used by another action.
29
29
  */
30
30
  loadStateOf<TAnotherActionState extends IActionState>(commandName: string): Promise<TAnotherActionState>;
31
- /**
32
- * Manually update the state of an action.
33
- * @param stateUpdateAction Function that will modify state.
34
- */
35
- updateState(stateUpdateAction: (state: TActionState) => void): void;
36
31
  /**
37
32
  * Reply with text message to message that triggered this action.
38
33
  * @param text Message contents.
@@ -1 +1 @@
1
- {"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,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,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW;IACjB,aAAa,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,CAAM;IACzD,kDAAkD;IAClD,SAAS,EAAE,MAAM,CAAC;IAClB,oDAAoD;IACpD,WAAW,EAAE,MAAM,CAAC;IACpB,4HAA4H;IAC5H,YAAY,EAAE,gBAAgB,EAAE,CAAM;IACtC,mEAAmE;IACnE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,kGAAkG;IAClG,aAAa,EAAE,OAAO,CAAQ;IAC9B,qEAAqE;IACrE,YAAY,EAAE,MAAM,CAAC;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MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW,CAAC,YAAY,CAAC;IAC/B,kDAAkD;IAClD,SAAS,EAAE,MAAM,CAAC;IAClB,oDAAoD;IACpD,WAAW,EAAE,MAAM,CAAC;IACpB,4HAA4H;IAC5H,YAAY,EAAE,gBAAgB,EAAE,CAAM;IACtC,mEAAmE;IACnE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,kGAAkG;IAClG,aAAa,EAAE,OAAO,CAAQ;IAC9B,qEAAqE;IACrE,YAAY,EAAE,MAAM,CAAC;gBAGjB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,mBAAmB,EACjC,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,cAAc;IAoB3B;;;;OAIG;IACG,WAAW,CAAC,mBAAmB,SAAS,YAAY,EACtD,WAAW,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,CAAC;IAY/B;;;;OAIG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAa/D;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,aAAa;CAW7B"}
@@ -12,17 +12,18 @@ 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(botName, scheduledKey, interactions, chatId, chatName, messageId, messageText, fromUserId, traceId, fromUserName, storage) {
16
- super(botName, scheduledKey, interactions, chatId, chatName, traceId, storage);
17
- this.updateActions = [];
15
+ constructor(botName, scheduledKey, interactions, message, storage) {
16
+ super(botName, scheduledKey, interactions, message.chat.id, message.chatName, message.traceId, storage);
18
17
  /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
19
18
  this.matchResults = [];
20
19
  /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
21
20
  this.startCooldown = true;
22
- this.messageId = messageId;
23
- this.messageText = messageText;
24
- this.fromUserId = fromUserId;
25
- this.fromUserName = fromUserName;
21
+ this.messageId = message.message_id;
22
+ this.messageText = message.text ?? '';
23
+ this.fromUserId = message.from?.id;
24
+ this.fromUserName =
25
+ (message.from?.first_name ?? 'Unknown user') +
26
+ (message.from?.last_name ? ` ${message.from.last_name}` : '');
26
27
  }
27
28
  /**
28
29
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
@@ -30,14 +31,13 @@ class MessageContext extends chatContext_1.ChatContext {
30
31
  * @template TAnotherActionState - Type of a state that is used by another action.
31
32
  */
32
33
  async loadStateOf(commandName) {
33
- return ((await this.storage.load(`command:${commandName.replace('.', '-')}`))[this.chatId] ?? new actionStateBase_1.ActionStateBase());
34
- }
35
- /**
36
- * Manually update the state of an action.
37
- * @param stateUpdateAction Function that will modify state.
38
- */
39
- updateState(stateUpdateAction) {
40
- this.updateActions.push(stateUpdateAction);
34
+ const storageKey = `command:${commandName.replace('.', '-')}`;
35
+ const allStates = await this.storage.load(storageKey);
36
+ const stateForChat = allStates[this.chatId];
37
+ if (!stateForChat) {
38
+ return new actionStateBase_1.ActionStateBase();
39
+ }
40
+ return stateForChat;
41
41
  }
42
42
  /**
43
43
  * Reply with text message to message that triggered this action.
@@ -9,7 +9,7 @@ import { IActionState } from '../../types/actionState';
9
9
  */
10
10
  export declare class CommandActionBuilderWithState<TActionState extends IActionState> {
11
11
  name: string;
12
- trigger: string | RegExp | Array<string> | Array<RegExp>;
12
+ trigger: string | RegExp | string[] | RegExp[];
13
13
  active: boolean;
14
14
  cooldownSeconds: Seconds;
15
15
  blacklist: number[];
@@ -29,11 +29,11 @@ export declare class CommandActionBuilderWithState<TActionState extends IActionS
29
29
  *
30
30
  * If `RegExp` or `RegExp[]` is provided, will be triggered on successful match.
31
31
  */
32
- on(trigger: string | RegExp | Array<string> | Array<RegExp>): this;
32
+ on(trigger: string | RegExp | string[] | RegExp[]): this;
33
33
  /** Defines id (or ids) of users that are allowed to trigger this action.
34
34
  * @param id User id or ids
35
35
  */
36
- from(id: number | Array<number>): this;
36
+ from(id: number | number[]): this;
37
37
  /** Defines action logic itself, will be executed on trigger.
38
38
  * @param handler Callback that will be called on trigger
39
39
  */
@@ -1 +1 @@
1
- {"version":3,"file":"commandActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/commandActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAIvD;;GAEG;AACH,qBAAa,6BAA6B,CAAC,YAAY,SAAS,YAAY;IACxE,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAM;IAE9D,MAAM,UAAQ;IACd,eAAe,EAAE,OAAO,CAAgB;IACxC,SAAS,EAAE,MAAM,EAAE,CAAM;IACzB,YAAY,EAAE,MAAM,EAAE,CAAM;IAC5B,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAa;IAClD,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAa;IAEtD;;;;OAIG;gBACS,IAAI,EAAE,M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EACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS;CAYpC;AAED,eAAO,MAAM,MAAM,YAAmB,CAAC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../services/logger.ts"],"names":[],"mappings":"AAAA,cAAM,UAAU;IAEZ,OAAO,CAAC,cAAc;IAQtB,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM;IAOhB,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS;CAQpC;AAED,eAAO,MAAM,MAAM,YAAmB,CAAC"}
@@ -11,16 +11,10 @@ class JsonLogger {
11
11
  return JSON.stringify(plainObject);
12
12
  }
13
13
  logWithTraceId(botName, traceId, chatName, text) {
14
- console.log(JSON.stringify({ botName, traceId, chatName, text }));
14
+ console.log(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`);
15
15
  }
16
16
  errorWithTraceId(botName, traceId, chatName, errorObj, extraData) {
17
- console.error(JSON.stringify({
18
- botName,
19
- traceId,
20
- chatName,
21
- error: this.serializeError(errorObj),
22
- extraData
23
- }));
17
+ console.error(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","error":${this.serializeError(errorObj)}${extraData ? `,"extraData":${JSON.stringify(extraData)}` : ''}}`);
24
18
  }
25
19
  }
26
20
  exports.Logger = new JsonLogger();
@@ -1 +1 @@
1
- {"version":3,"file":"taskScheduler.d.ts","sourceRoot":"","sources":["../../services/taskScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,cAAM,aAAa;IACf,WAAW,EAAE,UAAU,EAAE,CAAM;IAE/B,OAAO;IAMP,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM;IAoBrB,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM;CAoBxB;AAED,eAAO,MAAM,SAAS,eAAsB,CAAC"}
1
+ {"version":3,"file":"taskScheduler.d.ts","sourceRoot":"","sources":["../../services/taskScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,cAAM,aAAa;IACf,WAAW,EAAE,UAAU,EAAE,CAAM;IAE/B,OAAO;IAMP,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM;IAmBrB,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM;CAoBxB;AAED,eAAO,MAAM,SAAS,eAAsB,CAAC"}
@@ -13,7 +13,6 @@ class TaskScheduler {
13
13
  });
14
14
  }
15
15
  createTask(name, action, interval, executeRightAway, ownerName) {
16
- executeRightAway = executeRightAway ?? false;
17
16
  const taskId = setInterval(action, interval);
18
17
  const task = new taskRecord_1.TaskRecord(name, taskId, interval);
19
18
  if (executeRightAway) {
@@ -9,9 +9,12 @@ import { TextMessage } from '../entities/responses/textMessage';
9
9
  import { VideoMessage } from '../entities/responses/videoMessage';
10
10
  import { ImageMessage } from '../entities/responses/imageMessage';
11
11
  import { Telegram } from 'telegraf/typings/telegram';
12
+ import { ScheduledAction } from '../entities/actions/scheduledAction';
13
+ import { IActionState } from '../types/actionState';
14
+ import { CommandAction } from '../entities/actions/commandAction';
12
15
  export declare class TelegramApiService {
13
16
  isFlushing: boolean;
14
- messageQueue: Array<BotResponse>;
17
+ messageQueue: BotResponse[];
15
18
  botName: string;
16
19
  telegram: Telegram;
17
20
  chats: Record<number, string>;
@@ -22,8 +25,8 @@ export declare class TelegramApiService {
22
25
  private processResponse;
23
26
  private enqueue;
24
27
  private getInteractions;
25
- createContextForMessage(incomingMessage: IncomingMessage, commandKey: string): MessageContext<import("..").IActionState>;
26
- createContextForChat(chatId: number, scheduledKey: string): ChatContext;
28
+ createContextForMessage<TActionState extends IActionState>(incomingMessage: IncomingMessage, command: CommandAction<TActionState>): MessageContext<TActionState>;
29
+ createContextForChat<TActionState extends IActionState>(chatId: number, scheduledAction: ScheduledAction<TActionState>): ChatContext<TActionState>;
27
30
  }
28
31
  export interface IBotApiInteractions {
29
32
  respond: (response: TextMessage | VideoMessage | ImageMessage) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AACh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,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,KAAK,IAAI,CAAC;IACvE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IACpC,KAAK,EAAE,CAAC,YAAY,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD"}
@@ -6,6 +6,7 @@ const messageContext_1 = require("../entities/context/messageContext");
6
6
  const reverseRecord_1 = require("../helpers/reverseRecord");
7
7
  const logger_1 = require("./logger");
8
8
  const promises_1 = require("timers/promises");
9
+ const TELEGRAM_RATELIMIT_DELAY = 35;
9
10
  class TelegramApiService {
10
11
  constructor(botName, telegram, storage, chats) {
11
12
  this.isFlushing = false;
@@ -25,7 +26,7 @@ class TelegramApiService {
25
26
  break;
26
27
  try {
27
28
  await this.processResponse(message);
28
- await (0, promises_1.setTimeout)(100);
29
+ await (0, promises_1.setTimeout)(TELEGRAM_RATELIMIT_DELAY);
29
30
  }
30
31
  catch (error) {
31
32
  logger_1.Logger.errorWithTraceId(this.botName, message.traceId, this.chats[message.chatId], error, message);
@@ -93,15 +94,11 @@ class TelegramApiService {
93
94
  unpin: (unpinMessage) => this.enqueue(unpinMessage)
94
95
  };
95
96
  }
96
- createContextForMessage(incomingMessage, commandKey) {
97
- const firstName = incomingMessage.from?.first_name ?? 'Unknown user';
98
- const lastName = incomingMessage.from?.last_name
99
- ? ` ${incomingMessage.from?.last_name}`
100
- : '';
101
- return new messageContext_1.MessageContext(this.botName, commandKey, this.getInteractions(), incomingMessage.chat.id, incomingMessage.chatName, incomingMessage.message_id, incomingMessage.text, incomingMessage.from?.id, incomingMessage.traceId, firstName + lastName, this.storage);
97
+ createContextForMessage(incomingMessage, command) {
98
+ return new messageContext_1.MessageContext(this.botName, command.key, this.getInteractions(), incomingMessage, this.storage);
102
99
  }
103
- createContextForChat(chatId, scheduledKey) {
104
- return new chatContext_1.ChatContext(this.botName, scheduledKey, this.getInteractions(), chatId, this.chats[chatId], `Scheduled:${scheduledKey}:${chatId}`, this.storage);
100
+ createContextForChat(chatId, scheduledAction) {
101
+ return new chatContext_1.ChatContext(this.botName, scheduledAction.key, this.getInteractions(), chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
105
102
  }
106
103
  }
107
104
  exports.TelegramApiService = TelegramApiService;
@@ -9,7 +9,7 @@ ctx: MessageContext<TActionState>,
9
9
  state: TActionState) => Promise<void>;
10
10
  export type ScheduledHandler<TActionState extends IActionState> = (
11
11
  /** Context of action executed in chat. */
12
- ctx: ChatContext,
12
+ ctx: ChatContext<TActionState>,
13
13
  /** Function that will attempt to get value from cache. If there is no value found, corresponding cached state factory will be called. */
14
14
  getCached: CachedValueAccessor,
15
15
  /** State of an action being executed. */
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI;AAC5D,oEAAoE;AACpE,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;AACjC,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI;AAC9D,0CAA0C;AAC1C,GAAG,EAAE,WAAW;AAChB,yIAAyI;AACzI,SAAS,EAAE,mBAAmB;AAC9B,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC"}
1
+ {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI;AAC5D,oEAAoE;AACpE,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;AACjC,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI;AAC9D,0CAA0C;AAC1C,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC;AAC9B,yIAAyI;AACzI,SAAS,EAAE,mBAAmB;AAC9B,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC"}
@@ -26,13 +26,13 @@ export class CommandAction<TActionState extends IActionState>
26
26
  key: string;
27
27
 
28
28
  constructor(
29
- trigger: string | RegExp | Array<string> | Array<RegExp>,
29
+ trigger: string | RegExp | string[] | RegExp[],
30
30
  handler: CommandHandler<TActionState>,
31
31
  name: string,
32
32
  active: boolean,
33
33
  cooldown: Seconds,
34
- chatsBlacklist: Array<number>,
35
- allowedUsers: Array<number>,
34
+ chatsBlacklist: number[],
35
+ allowedUsers: number[],
36
36
  condition: CommandCondition<TActionState>,
37
37
  stateConstructor: () => TActionState
38
38
  ) {
@@ -122,14 +122,22 @@ export class CommandAction<TActionState extends IActionState>
122
122
  if (typeof trigger == 'string') {
123
123
  shouldTrigger = ctx.messageText.toLowerCase() == trigger;
124
124
  } else {
125
- let matchCount = ctx.messageText.match(trigger)?.length ?? 0;
126
- if (matchCount > 0) {
127
- for (; matchCount > 0; matchCount--) {
128
- const execResult = trigger.exec(ctx.messageText);
129
- if (execResult) matchResults.push(execResult);
125
+ trigger.lastIndex = 0;
126
+
127
+ const execResult = trigger.exec(ctx.messageText);
128
+ if (execResult != null) {
129
+ matchResults.push(execResult);
130
+
131
+ if (trigger.global) {
132
+ while (true) {
133
+ const nextResult = trigger.exec(ctx.messageText);
134
+ if (nextResult == null) break;
135
+ matchResults.push(nextResult);
136
+ }
130
137
  }
131
- shouldTrigger = true;
132
138
  }
139
+
140
+ shouldTrigger = matchResults.length > 0;
133
141
  }
134
142
  }
135
143
 
@@ -46,7 +46,7 @@ export class ScheduledAction<TActionState extends IActionState>
46
46
  this.stateConstructor = stateConstructor;
47
47
  }
48
48
 
49
- async exec(ctx: ChatContext) {
49
+ async exec(ctx: ChatContext<TActionState>) {
50
50
  if (!this.active || !this.chatsWhitelist.includes(ctx.chatId)) return;
51
51
 
52
52
  const state = await ctx.storage.getActionState<TActionState>(
@@ -72,6 +72,7 @@ export class ScheduledAction<TActionState extends IActionState>
72
72
 
73
73
  state.lastExecutedDate = moment().valueOf();
74
74
 
75
+ ctx.updateActions.forEach((action) => action(state));
75
76
  await ctx.storage.saveActionExecutionResult(
76
77
  this,
77
78
  ctx.chatId,
@@ -144,11 +144,14 @@ export class BotInstance {
144
144
 
145
145
  private async runScheduled() {
146
146
  for (const [chatName, chatId] of Object.entries(this.chats)) {
147
- for (const trig of this.scheduled) {
148
- const ctx = this.api.createContextForChat(chatId, trig.key);
147
+ for (const scheduledAction of this.scheduled) {
148
+ const ctx = this.api.createContextForChat(
149
+ chatId,
150
+ scheduledAction
151
+ );
149
152
 
150
153
  try {
151
- await trig.exec(ctx);
154
+ await scheduledAction.exec(ctx);
152
155
  } catch (error) {
153
156
  Logger.errorWithTraceId(
154
157
  ctx.botName,
@@ -165,11 +168,11 @@ export class BotInstance {
165
168
  }
166
169
 
167
170
  private async processMessage(msg: IncomingMessage) {
168
- for (const cmd of this.commands) {
169
- const ctx = this.api.createContextForMessage(msg, cmd.key);
171
+ for (const commandAction of this.commands) {
172
+ const ctx = this.api.createContextForMessage(msg, commandAction);
170
173
 
171
174
  try {
172
- await cmd.exec(ctx);
175
+ await commandAction.exec(ctx);
173
176
  } catch (error) {
174
177
  Logger.errorWithTraceId(
175
178
  ctx.botName,
@@ -13,9 +13,10 @@ import {
13
13
  /**
14
14
  * Context of action executed in chat.
15
15
  */
16
- export class ChatContext {
16
+ export class ChatContext<TActionState> {
17
17
  protected actionKey: string;
18
18
  protected interactions: IBotApiInteractions;
19
+ updateActions: Array<(state: TActionState) => void> = [];
19
20
  /** Trace id of a action execution. */
20
21
  traceId: number | string;
21
22
  /** Name of a bot that executes this action. */
@@ -45,6 +46,14 @@ export class ChatContext {
45
46
  this.storage = storage;
46
47
  }
47
48
 
49
+ /**
50
+ * Manually update the state of an action.
51
+ * @param stateUpdateAction Function that will modify state.
52
+ */
53
+ updateState(stateUpdateAction: (state: TActionState) => void) {
54
+ this.updateActions.push(stateUpdateAction);
55
+ }
56
+
48
57
  /**
49
58
  * Sends text message to chat.
50
59
  * @param text Message contents.
@@ -9,6 +9,7 @@ import { TextMessage } from '../responses/textMessage';
9
9
  import { VideoMessage } from '../responses/videoMessage';
10
10
  import { ActionStateBase } from '../states/actionStateBase';
11
11
  import { ChatContext } from './chatContext';
12
+ import { IncomingMessage } from '../incomingMessage';
12
13
  import {
13
14
  MessageSendingOptions,
14
15
  TextMessageSendingOptions
@@ -19,8 +20,7 @@ import {
19
20
  */
20
21
  export class MessageContext<
21
22
  TActionState extends IActionState
22
- > extends ChatContext {
23
- updateActions: Array<(state: TActionState) => void> = [];
23
+ > extends ChatContext<TActionState> {
24
24
  /** Id of a message that triggered this action. */
25
25
  messageId: number;
26
26
  /** Text of a message that triggered this action. */
@@ -38,29 +38,25 @@ export class MessageContext<
38
38
  botName: string,
39
39
  scheduledKey: string,
40
40
  interactions: IBotApiInteractions,
41
- chatId: number,
42
- chatName: string,
43
- messageId: number,
44
- messageText: string,
45
- fromUserId: number | undefined,
46
- traceId: number | string,
47
- fromUserName: string,
41
+ message: IncomingMessage,
48
42
  storage: IStorageClient
49
43
  ) {
50
44
  super(
51
45
  botName,
52
46
  scheduledKey,
53
47
  interactions,
54
- chatId,
55
- chatName,
56
- traceId,
48
+ message.chat.id,
49
+ message.chatName,
50
+ message.traceId,
57
51
  storage
58
52
  );
59
53
 
60
- this.messageId = messageId;
61
- this.messageText = messageText;
62
- this.fromUserId = fromUserId;
63
- this.fromUserName = fromUserName;
54
+ this.messageId = message.message_id;
55
+ this.messageText = message.text ?? '';
56
+ this.fromUserId = message.from?.id;
57
+ this.fromUserName =
58
+ (message.from?.first_name ?? 'Unknown user') +
59
+ (message.from?.last_name ? ` ${message.from.last_name}` : '');
64
60
  }
65
61
 
66
62
  /**
@@ -71,23 +67,15 @@ export class MessageContext<
71
67
  async loadStateOf<TAnotherActionState extends IActionState>(
72
68
  commandName: string
73
69
  ): Promise<TAnotherActionState> {
74
- return (
75
- ((
76
- await this.storage.load(
77
- `command:${commandName.replace('.', '-')}`
78
- )
79
- )[this.chatId] as TAnotherActionState) ?? new ActionStateBase()
80
- );
81
- }
70
+ const storageKey = `command:${commandName.replace('.', '-')}`;
71
+ const allStates = await this.storage.load(storageKey);
72
+ const stateForChat = allStates[this.chatId];
82
73
 
83
- /**
84
- * Manually update the state of an action.
85
- * @param stateUpdateAction Function that will modify state.
86
- */
87
- updateState(stateUpdateAction: (state: TActionState) => void) {
88
- this.updateActions.push(
89
- stateUpdateAction as (state: TActionState) => void
90
- );
74
+ if (!stateForChat) {
75
+ return new ActionStateBase() as TAnotherActionState;
76
+ }
77
+
78
+ return stateForChat as TAnotherActionState;
91
79
  }
92
80
 
93
81
  /**
@@ -12,7 +12,7 @@ import { Noop } from '../noop';
12
12
  */
13
13
  export class CommandActionBuilderWithState<TActionState extends IActionState> {
14
14
  name: string;
15
- trigger: string | RegExp | Array<string> | Array<RegExp> = [];
15
+ trigger: string | RegExp | string[] | RegExp[] = [];
16
16
 
17
17
  active = true;
18
18
  cooldownSeconds: Seconds = 0 as Seconds;
@@ -38,7 +38,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
38
38
  *
39
39
  * If `RegExp` or `RegExp[]` is provided, will be triggered on successful match.
40
40
  */
41
- on(trigger: string | RegExp | Array<string> | Array<RegExp>) {
41
+ on(trigger: string | RegExp | string[] | RegExp[]) {
42
42
  this.trigger = trigger;
43
43
 
44
44
  return this;
@@ -47,7 +47,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
47
47
  /** Defines id (or ids) of users that are allowed to trigger this action.
48
48
  * @param id User id or ids
49
49
  */
50
- from(id: number | Array<number>) {
50
+ from(id: number | number[]) {
51
51
  this.allowedUsers = toArray(id);
52
52
 
53
53
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -14,7 +14,9 @@ class JsonLogger {
14
14
  chatName: string,
15
15
  text: string
16
16
  ) {
17
- console.log(JSON.stringify({ botName, traceId, chatName, text }));
17
+ console.log(
18
+ `{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`
19
+ );
18
20
  }
19
21
 
20
22
  errorWithTraceId<TData>(
@@ -25,13 +27,9 @@ class JsonLogger {
25
27
  extraData?: TData | undefined
26
28
  ) {
27
29
  console.error(
28
- JSON.stringify({
29
- botName,
30
- traceId,
31
- chatName,
32
- error: this.serializeError(errorObj),
33
- extraData
34
- })
30
+ `{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","error":${this.serializeError(
31
+ errorObj
32
+ )}${extraData ? `,"extraData":${JSON.stringify(extraData)}` : ''}}`
35
33
  );
36
34
  }
37
35
  }
@@ -18,7 +18,6 @@ class TaskScheduler {
18
18
  executeRightAway: boolean,
19
19
  ownerName: string
20
20
  ) {
21
- executeRightAway = executeRightAway ?? false;
22
21
  const taskId = setInterval(action, interval);
23
22
  const task = new TaskRecord(name, taskId, interval);
24
23
 
@@ -14,10 +14,15 @@ import { ImageMessage } from '../entities/responses/imageMessage';
14
14
  import { Telegram } from 'telegraf/typings/telegram';
15
15
  import { setTimeout } from 'timers/promises';
16
16
  import { Milliseconds } from '../types/timeValues';
17
+ import { ScheduledAction } from '../entities/actions/scheduledAction';
18
+ import { IActionState } from '../types/actionState';
19
+ import { CommandAction } from '../entities/actions/commandAction';
20
+
21
+ const TELEGRAM_RATELIMIT_DELAY = 35 as Milliseconds;
17
22
 
18
23
  export class TelegramApiService {
19
24
  isFlushing = false;
20
- messageQueue: Array<BotResponse> = [];
25
+ messageQueue: BotResponse[] = [];
21
26
 
22
27
  botName: string;
23
28
  telegram: Telegram;
@@ -48,7 +53,7 @@ export class TelegramApiService {
48
53
 
49
54
  try {
50
55
  await this.processResponse(message);
51
- await setTimeout(100 as Milliseconds);
56
+ await setTimeout(TELEGRAM_RATELIMIT_DELAY);
52
57
  } catch (error) {
53
58
  Logger.errorWithTraceId(
54
59
  this.botName,
@@ -171,38 +176,30 @@ export class TelegramApiService {
171
176
  } as IBotApiInteractions;
172
177
  }
173
178
 
174
- createContextForMessage(
179
+ createContextForMessage<TActionState extends IActionState>(
175
180
  incomingMessage: IncomingMessage,
176
- commandKey: string
181
+ command: CommandAction<TActionState>
177
182
  ) {
178
- const firstName = incomingMessage.from?.first_name ?? 'Unknown user';
179
- const lastName = incomingMessage.from?.last_name
180
- ? ` ${incomingMessage.from?.last_name}`
181
- : '';
182
-
183
- return new MessageContext(
183
+ return new MessageContext<TActionState>(
184
184
  this.botName,
185
- commandKey,
185
+ command.key,
186
186
  this.getInteractions(),
187
- incomingMessage.chat.id,
188
- incomingMessage.chatName,
189
- incomingMessage.message_id,
190
- incomingMessage.text,
191
- incomingMessage.from?.id,
192
- incomingMessage.traceId,
193
- firstName + lastName,
187
+ incomingMessage,
194
188
  this.storage
195
189
  );
196
190
  }
197
191
 
198
- createContextForChat(chatId: number, scheduledKey: string) {
199
- return new ChatContext(
192
+ createContextForChat<TActionState extends IActionState>(
193
+ chatId: number,
194
+ scheduledAction: ScheduledAction<TActionState>
195
+ ) {
196
+ return new ChatContext<TActionState>(
200
197
  this.botName,
201
- scheduledKey,
198
+ scheduledAction.key,
202
199
  this.getInteractions(),
203
200
  chatId,
204
201
  this.chats[chatId],
205
- `Scheduled:${scheduledKey}:${chatId}`,
202
+ `Scheduled:${scheduledAction.key}:${chatId}`,
206
203
  this.storage
207
204
  );
208
205
  }
package/types/handlers.ts CHANGED
@@ -12,7 +12,7 @@ export type CommandHandler<TActionState extends IActionState> = (
12
12
 
13
13
  export type ScheduledHandler<TActionState extends IActionState> = (
14
14
  /** Context of action executed in chat. */
15
- ctx: ChatContext,
15
+ ctx: ChatContext<TActionState>,
16
16
  /** Function that will attempt to get value from cache. If there is no value found, corresponding cached state factory will be called. */
17
17
  getCached: CachedValueAccessor,
18
18
  /** State of an action being executed. */