chz-telegram-bot 0.0.23 → 0.0.24

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 (40) hide show
  1. package/dist/entities/botInstance.d.ts +1 -1
  2. package/dist/entities/botInstance.d.ts.map +1 -1
  3. package/dist/entities/botInstance.js +24 -6
  4. package/dist/entities/context/chatContext.d.ts +30 -3
  5. package/dist/entities/context/chatContext.d.ts.map +1 -1
  6. package/dist/entities/context/chatContext.js +22 -0
  7. package/dist/entities/context/messageContext.d.ts +38 -1
  8. package/dist/entities/context/messageContext.d.ts.map +1 -1
  9. package/dist/entities/context/messageContext.js +34 -1
  10. package/dist/helpers/builders/commandActionBuilder.d.ts +38 -0
  11. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  12. package/dist/helpers/builders/commandActionBuilder.js +38 -0
  13. package/dist/helpers/builders/scheduledActionBuilder.d.ts +34 -0
  14. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
  15. package/dist/helpers/builders/scheduledActionBuilder.js +34 -0
  16. package/dist/helpers/reverseRecord.d.ts +2 -0
  17. package/dist/helpers/reverseRecord.d.ts.map +1 -0
  18. package/dist/helpers/reverseRecord.js +6 -0
  19. package/dist/main.d.ts +14 -1
  20. package/dist/main.d.ts.map +1 -1
  21. package/dist/main.js +7 -1
  22. package/dist/services/taskScheduler.d.ts.map +1 -1
  23. package/dist/services/taskScheduler.js +1 -2
  24. package/dist/services/telegramApi.d.ts +2 -2
  25. package/dist/services/telegramApi.d.ts.map +1 -1
  26. package/dist/services/telegramApi.js +5 -4
  27. package/dist/types/handlers.d.ts +12 -2
  28. package/dist/types/handlers.d.ts.map +1 -1
  29. package/entities/botInstance.ts +43 -11
  30. package/entities/context/chatContext.ts +30 -3
  31. package/entities/context/messageContext.ts +38 -1
  32. package/helpers/builders/commandActionBuilder.ts +38 -0
  33. package/helpers/builders/scheduledActionBuilder.ts +34 -0
  34. package/helpers/reverseRecord.ts +7 -0
  35. package/main.ts +15 -2
  36. package/package.json +1 -1
  37. package/services/taskScheduler.ts +2 -3
  38. package/services/telegramApi.ts +8 -6
  39. package/types/handlers.ts +5 -0
  40. package/helpers/reverseMap.ts +0 -3
@@ -15,7 +15,7 @@ export declare class BotInstance {
15
15
  token: string;
16
16
  commands: CommandAction<IActionState>[];
17
17
  scheduled: ScheduledAction<IActionState>[];
18
- chats: Map<string, number>;
18
+ chats: Record<string, number>;
19
19
  storageClient?: IStorageClient;
20
20
  storagePath?: string;
21
21
  });
@@ -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;AAK5D,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,CAAsB;IACnC,OAAO,EAAE,cAAc,CAAC;gBAEZ,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,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IAcrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAsBZ,cAAc;CAmB/B"}
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,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,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IA6CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAsBZ,cAAc;CAmB/B"}
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.BotInstance = void 0;
4
7
  const telegraf_1 = require("telegraf");
@@ -8,6 +11,7 @@ const telegramApi_1 = require("../services/telegramApi");
8
11
  const logger_1 = require("../services/logger");
9
12
  const taskScheduler_1 = require("../services/taskScheduler");
10
13
  const incomingMessage_1 = require("./incomingMessage");
14
+ const moment_1 = __importDefault(require("moment"));
11
15
  class BotInstance {
12
16
  constructor(options) {
13
17
  this.name = options.name;
@@ -27,9 +31,23 @@ class BotInstance {
27
31
  }
28
32
  initializeScheduledProcessing() {
29
33
  if (this.scheduled.length > 0) {
30
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
31
- await this.runScheduled();
32
- }, (0, timeConvertions_1.hoursToMilliseconds)(0.5), true, this.name);
34
+ const now = (0, moment_1.default)();
35
+ let nextExecutionTime = now.clone().startOf('hour');
36
+ if (now.minute() == 0 && now.second() == 0) {
37
+ taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
38
+ await this.runScheduled();
39
+ }, (0, timeConvertions_1.hoursToMilliseconds)(1), true, this.name);
40
+ return;
41
+ }
42
+ if (now.minute() > 0 || now.second() > 0) {
43
+ nextExecutionTime = nextExecutionTime.add(1, 'hour');
44
+ }
45
+ const delay = nextExecutionTime.diff(now);
46
+ taskScheduler_1.Scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
47
+ taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
48
+ await this.runScheduled();
49
+ }, (0, timeConvertions_1.hoursToMilliseconds)(1), true, this.name);
50
+ }, delay, this.name);
33
51
  }
34
52
  }
35
53
  initializeMessageProcessing() {
@@ -52,7 +70,7 @@ class BotInstance {
52
70
  this.telegraf.stop(code);
53
71
  }
54
72
  async runScheduled() {
55
- for (const [chatName, chatId] of this.chats.entries()) {
73
+ for (const [chatName, chatId] of Object.entries(this.chats)) {
56
74
  for (const trig of this.scheduled) {
57
75
  const ctx = this.api.createContextForChat(chatId, trig.key);
58
76
  try {
@@ -63,7 +81,7 @@ class BotInstance {
63
81
  }
64
82
  }
65
83
  }
66
- this.api.flushResponses();
84
+ await this.api.flushResponses();
67
85
  }
68
86
  async processMessage(msg) {
69
87
  for (const cmd of this.commands) {
@@ -75,7 +93,7 @@ class BotInstance {
75
93
  logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, error, ctx);
76
94
  }
77
95
  }
78
- this.api.flushResponses();
96
+ await this.api.flushResponses();
79
97
  }
80
98
  }
81
99
  exports.BotInstance = BotInstance;
@@ -1,18 +1,45 @@
1
1
  import { IBotApiInteractions } from '../../services/telegramApi';
2
2
  import { IStorageClient } from '../../types/storage';
3
3
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
4
+ /**
5
+ * Context of action executed in chat.
6
+ */
4
7
  export declare class ChatContext {
8
+ protected actionKey: string;
9
+ protected interactions: IBotApiInteractions;
10
+ /** Trace id of a action execution. */
11
+ traceId: number | string;
12
+ /** Name of a bot that executes this action. */
5
13
  botName: string;
6
- actionKey: string;
7
- interactions: IBotApiInteractions;
14
+ /** Id of a chat that action is executed in. */
8
15
  chatId: number;
16
+ /** Name of a chat that action is executed in. */
9
17
  chatName: string;
10
- traceId: number | string;
18
+ /** Storage client instance for this bot. */
11
19
  storage: IStorageClient;
12
20
  constructor(botName: string, actionKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient);
21
+ /**
22
+ * Sends text message to chat.
23
+ * @param text Message contents.
24
+ * @param options Message sending option.
25
+ */
13
26
  sendTextToChat(text: string, options?: TextMessageSendingOptions): void;
27
+ /**
28
+ * Sends image message to chat.
29
+ * @param name Message contents.
30
+ * @param options Message sending option.
31
+ */
14
32
  sendImageToChat(name: string, options?: MessageSendingOptions): void;
33
+ /**
34
+ * Sends video/gif message to chat.
35
+ * @param name Message contents.
36
+ * @param options Message sending option.
37
+ */
15
38
  sendVideoToChat(name: string, options?: MessageSendingOptions): void;
39
+ /**
40
+ * Unpins message.
41
+ * @param messageId Message id.
42
+ */
16
43
  unpinMessage(messageId: number): void;
17
44
  }
18
45
  //# sourceMappingURL=chatContext.d.ts.map
@@ -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,qBAAa,WAAW;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,mBAAmB,CAAC;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,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,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAahE,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D,YAAY,CAAC,SAAS,EAAE,MAAM;CAUjC"}
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;IAc7D;;;OAGG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM;CAUjC"}
@@ -6,6 +6,9 @@ const imageMessage_1 = require("../responses/imageMessage");
6
6
  const textMessage_1 = require("../responses/textMessage");
7
7
  const videoMessage_1 = require("../responses/videoMessage");
8
8
  const unpin_1 = require("../responses/unpin");
9
+ /**
10
+ * Context of action executed in chat.
11
+ */
9
12
  class ChatContext {
10
13
  constructor(botName, actionKey, interactions, chatId, chatName, traceId, storage) {
11
14
  this.botName = botName;
@@ -16,17 +19,36 @@ class ChatContext {
16
19
  this.traceId = traceId;
17
20
  this.storage = storage;
18
21
  }
22
+ /**
23
+ * Sends text message to chat.
24
+ * @param text Message contents.
25
+ * @param options Message sending option.
26
+ */
19
27
  sendTextToChat(text, options) {
20
28
  this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, undefined, this.traceId, this.actionKey, options));
21
29
  }
30
+ /**
31
+ * Sends image message to chat.
32
+ * @param name Message contents.
33
+ * @param options Message sending option.
34
+ */
22
35
  sendImageToChat(name, options) {
23
36
  const filePath = `./content/${name}.png`;
24
37
  this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, this.actionKey, options));
25
38
  }
39
+ /**
40
+ * Sends video/gif message to chat.
41
+ * @param name Message contents.
42
+ * @param options Message sending option.
43
+ */
26
44
  sendVideoToChat(name, options) {
27
45
  const filePath = `./content/${name}.mp4`;
28
46
  this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, this.actionKey, options));
29
47
  }
48
+ /**
49
+ * Unpins message.
50
+ * @param messageId Message id.
51
+ */
30
52
  unpinMessage(messageId) {
31
53
  this.interactions.unpin(new unpin_1.UnpinResponse(messageId, this.chatId, this.traceId, this.actionKey));
32
54
  }
@@ -4,20 +4,57 @@ import { IStorageClient } from '../../types/storage';
4
4
  import { IActionState } from '../../types/actionState';
5
5
  import { ChatContext } from './chatContext';
6
6
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
7
+ /**
8
+ * Context of action executed in chat, in response to a message
9
+ */
7
10
  export declare class MessageContext<TActionState extends IActionState> extends ChatContext {
11
+ updateActions: Array<(state: TActionState) => void>;
12
+ /** Id of a message that triggered this action. */
8
13
  messageId: number;
14
+ /** Text of a message that triggered this action. */
9
15
  messageText: string;
16
+ /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
10
17
  matchResults: RegExpMatchArray[];
18
+ /** Id of a user that sent a message that triggered this action. */
11
19
  fromUserId: number | undefined;
20
+ /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
12
21
  startCooldown: boolean;
13
- updateActions: Array<(state: TActionState) => void>;
22
+ /** Name of a user that sent a message that triggered this action. */
14
23
  fromUserName: string;
15
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);
25
+ /**
26
+ * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
27
+ * @param commandName Name of an action to load state of.
28
+ * @template TAnotherActionState - Type of a state that is used by another action.
29
+ */
16
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
+ */
17
35
  updateState(stateUpdateAction: (state: TActionState) => void): void;
36
+ /**
37
+ * Reply with text message to message that triggered this action.
38
+ * @param text Message contents.
39
+ * @param options Message sending option.
40
+ */
18
41
  replyWithText(text: string, options?: TextMessageSendingOptions): void;
42
+ /**
43
+ * Reply with image message to message that triggered this action.
44
+ * @param text Message contents.
45
+ * @param options Message sending option.
46
+ */
19
47
  replyWithImage(name: string, options?: MessageSendingOptions): void;
48
+ /**
49
+ * Reply with video/gif message to message that triggered this action.
50
+ * @param text Message contents.
51
+ * @param options Message sending option.
52
+ */
20
53
  replyWithVideo(name: string, options?: MessageSendingOptions): void;
54
+ /**
55
+ * React to the message that triggered this action.
56
+ * @param emoji Telegram emoji to react with.
57
+ */
21
58
  react(emoji: TelegramEmoji): void;
22
59
  }
23
60
  //# sourceMappingURL=messageContext.d.ts.map
@@ -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,qBAA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CAW7B"}
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;gBAGjB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,mBAAmB,EACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,cAAc;IAkB3B;;;;OAIG;IACG,WAAW,CAAC,mBAAmB,SAAS,YAAY,EACtD,WAAW,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,CAAC;IAU/B;;;OAGG;IACH,WAAW,CAAC,iBAAiB,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI;IAM5D;;;;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"}
@@ -8,34 +8,67 @@ const textMessage_1 = require("../responses/textMessage");
8
8
  const videoMessage_1 = require("../responses/videoMessage");
9
9
  const actionStateBase_1 = require("../states/actionStateBase");
10
10
  const chatContext_1 = require("./chatContext");
11
+ /**
12
+ * Context of action executed in chat, in response to a message
13
+ */
11
14
  class MessageContext extends chatContext_1.ChatContext {
12
15
  constructor(botName, scheduledKey, interactions, chatId, chatName, messageId, messageText, fromUserId, traceId, fromUserName, storage) {
13
16
  super(botName, scheduledKey, interactions, chatId, chatName, traceId, storage);
17
+ this.updateActions = [];
18
+ /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
14
19
  this.matchResults = [];
20
+ /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
15
21
  this.startCooldown = true;
16
- this.updateActions = [];
17
22
  this.messageId = messageId;
18
23
  this.messageText = messageText;
19
24
  this.fromUserId = fromUserId;
20
25
  this.fromUserName = fromUserName;
21
26
  }
27
+ /**
28
+ * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
29
+ * @param commandName Name of an action to load state of.
30
+ * @template TAnotherActionState - Type of a state that is used by another action.
31
+ */
22
32
  async loadStateOf(commandName) {
23
33
  return ((await this.storage.load(`command:${commandName.replace('.', '-')}`))[this.chatId] ?? new actionStateBase_1.ActionStateBase());
24
34
  }
35
+ /**
36
+ * Manually update the state of an action.
37
+ * @param stateUpdateAction Function that will modify state.
38
+ */
25
39
  updateState(stateUpdateAction) {
26
40
  this.updateActions.push(stateUpdateAction);
27
41
  }
42
+ /**
43
+ * Reply with text message to message that triggered this action.
44
+ * @param text Message contents.
45
+ * @param options Message sending option.
46
+ */
28
47
  replyWithText(text, options) {
29
48
  this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, this.messageId, this.traceId, this.actionKey, options));
30
49
  }
50
+ /**
51
+ * Reply with image message to message that triggered this action.
52
+ * @param text Message contents.
53
+ * @param options Message sending option.
54
+ */
31
55
  replyWithImage(name, options) {
32
56
  const filePath = `./content/${name}.png`;
33
57
  this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, this.actionKey, options));
34
58
  }
59
+ /**
60
+ * Reply with video/gif message to message that triggered this action.
61
+ * @param text Message contents.
62
+ * @param options Message sending option.
63
+ */
35
64
  replyWithVideo(name, options) {
36
65
  const filePath = `./content/${name}.mp4`;
37
66
  this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, this.actionKey, options));
38
67
  }
68
+ /**
69
+ * React to the message that triggered this action.
70
+ * @param emoji Telegram emoji to react with.
71
+ */
39
72
  react(emoji) {
40
73
  this.interactions.react(new reaction_1.Reaction(this.traceId, this.chatId, this.messageId, emoji, this.actionKey));
41
74
  }
@@ -4,6 +4,9 @@ import { Seconds } from '../../types/timeValues';
4
4
  import { CommandAction } from '../../entities/actions/commandAction';
5
5
  import { ActionStateBase } from '../../entities/states/actionStateBase';
6
6
  import { IActionState } from '../../types/actionState';
7
+ /**
8
+ * Builder for `CommandAction` with state represented by `TActionState`
9
+ */
7
10
  export declare class CommandActionBuilderWithState<TActionState extends IActionState> {
8
11
  name: string;
9
12
  trigger: string | RegExp | Array<string> | Array<RegExp>;
@@ -14,17 +17,52 @@ export declare class CommandActionBuilderWithState<TActionState extends IActionS
14
17
  stateConstructor: () => TActionState;
15
18
  handler: CommandHandler<TActionState>;
16
19
  condition: CommandCondition<TActionState>;
20
+ /**
21
+ * Builder for `CommandAction` with state represented by `TActionState`
22
+ * @param name Action name, will be used for logging and storage
23
+ * @param stateConstructor Function that creates default state object
24
+ */
17
25
  constructor(name: string, stateConstructor: () => TActionState);
26
+ /**
27
+ * Defines action trigger
28
+ * @param trigger If `string` or `string[]` is provided, will be triggered only on exact message match.
29
+ *
30
+ * If `RegExp` or `RegExp[]` is provided, will be triggered on successful match.
31
+ */
18
32
  on(trigger: string | RegExp | Array<string> | Array<RegExp>): this;
33
+ /** Defines id (or ids) of users that are allowed to trigger this action.
34
+ * @param id User id or ids
35
+ */
19
36
  from(id: number | Array<number>): this;
37
+ /** Defines action logic itself, will be executed on trigger.
38
+ * @param handler Callback that will be called on trigger
39
+ */
20
40
  do(handler: CommandHandler<TActionState>): this;
41
+ /** Defines condition that will be checked before trigger match check is executed.
42
+ * @param condition Condition check predicate
43
+ */
21
44
  when(condition: CommandCondition<TActionState>): this;
45
+ /** If called during building, action is marked as disabled and never checked. */
22
46
  disabled(): this;
47
+ /** Sets action cooldown.
48
+ * @param seconds Cooldown in seconds.
49
+ */
23
50
  cooldown(seconds: Seconds): this;
51
+ /**
52
+ * Adds a chat to ignore list for this action.
53
+ * @param chatId Chat id to ignore.
54
+ */
24
55
  ignoreChat(chatId: number): this;
56
+ /** Builds action */
25
57
  build(): CommandAction<TActionState>;
26
58
  }
59
+ /**
60
+ * Builder for `CommandAction` with state represented by default state (containing only last execution date).
61
+ */
27
62
  export declare class CommandActionBuilder extends CommandActionBuilderWithState<ActionStateBase> {
63
+ /**
64
+ * Builder for `CommandAction` with state represented by default state (containing only last execution date).
65
+ */
28
66
  constructor(name: string);
29
67
  }
30
68
  //# sourceMappingURL=commandActionBuilder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandActionBuilder.d.ts","sourceRoot":"","s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/B;;OAEG;IACH,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC;IAMxC;;OAEG;IACH,IAAI,CAAC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAM9C,iFAAiF;IACjF,QAAQ;IAMR;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO;IAMzB;;;OAGG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM;IAMzB,oBAAoB;IACpB,KAAK;CAaR;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,6BAA6B,CAAC,eAAe,CAAC;IACpF;;OAEG;gBACS,IAAI,EAAE,MAAM;CAG3B"}
@@ -5,7 +5,15 @@ const commandAction_1 = require("../../entities/actions/commandAction");
5
5
  const actionStateBase_1 = require("../../entities/states/actionStateBase");
6
6
  const toArray_1 = require("../toArray");
7
7
  const noop_1 = require("../noop");
8
+ /**
9
+ * Builder for `CommandAction` with state represented by `TActionState`
10
+ */
8
11
  class CommandActionBuilderWithState {
12
+ /**
13
+ * Builder for `CommandAction` with state represented by `TActionState`
14
+ * @param name Action name, will be used for logging and storage
15
+ * @param stateConstructor Function that creates default state object
16
+ */
9
17
  constructor(name, stateConstructor) {
10
18
  this.trigger = [];
11
19
  this.active = true;
@@ -17,40 +25,70 @@ class CommandActionBuilderWithState {
17
25
  this.name = name;
18
26
  this.stateConstructor = stateConstructor;
19
27
  }
28
+ /**
29
+ * Defines action trigger
30
+ * @param trigger If `string` or `string[]` is provided, will be triggered only on exact message match.
31
+ *
32
+ * If `RegExp` or `RegExp[]` is provided, will be triggered on successful match.
33
+ */
20
34
  on(trigger) {
21
35
  this.trigger = trigger;
22
36
  return this;
23
37
  }
38
+ /** Defines id (or ids) of users that are allowed to trigger this action.
39
+ * @param id User id or ids
40
+ */
24
41
  from(id) {
25
42
  this.allowedUsers = (0, toArray_1.toArray)(id);
26
43
  return this;
27
44
  }
45
+ /** Defines action logic itself, will be executed on trigger.
46
+ * @param handler Callback that will be called on trigger
47
+ */
28
48
  do(handler) {
29
49
  this.handler = handler;
30
50
  return this;
31
51
  }
52
+ /** Defines condition that will be checked before trigger match check is executed.
53
+ * @param condition Condition check predicate
54
+ */
32
55
  when(condition) {
33
56
  this.condition = condition;
34
57
  return this;
35
58
  }
59
+ /** If called during building, action is marked as disabled and never checked. */
36
60
  disabled() {
37
61
  this.active = false;
38
62
  return this;
39
63
  }
64
+ /** Sets action cooldown.
65
+ * @param seconds Cooldown in seconds.
66
+ */
40
67
  cooldown(seconds) {
41
68
  this.cooldownSeconds = seconds;
42
69
  return this;
43
70
  }
71
+ /**
72
+ * Adds a chat to ignore list for this action.
73
+ * @param chatId Chat id to ignore.
74
+ */
44
75
  ignoreChat(chatId) {
45
76
  this.blacklist.push(chatId);
46
77
  return this;
47
78
  }
79
+ /** Builds action */
48
80
  build() {
49
81
  return new commandAction_1.CommandAction(this.trigger, this.handler, this.name, this.active, this.cooldownSeconds, this.blacklist, this.allowedUsers, this.condition, this.stateConstructor);
50
82
  }
51
83
  }
52
84
  exports.CommandActionBuilderWithState = CommandActionBuilderWithState;
85
+ /**
86
+ * Builder for `CommandAction` with state represented by default state (containing only last execution date).
87
+ */
53
88
  class CommandActionBuilder extends CommandActionBuilderWithState {
89
+ /**
90
+ * Builder for `CommandAction` with state represented by default state (containing only last execution date).
91
+ */
54
92
  constructor(name) {
55
93
  super(name, () => new actionStateBase_1.ActionStateBase());
56
94
  }
@@ -4,6 +4,9 @@ import { ActionStateBase } from '../../entities/states/actionStateBase';
4
4
  import { IActionState } from '../../types/actionState';
5
5
  import { ScheduledHandler } from '../../types/handlers';
6
6
  import { Hours, HoursOfDay } from '../../types/timeValues';
7
+ /**
8
+ * Builder for `ScheduledAction` with state represented by `TActionState`
9
+ */
7
10
  export declare class ScheduledActionBuilderWithState<TActionState extends IActionState> {
8
11
  active: boolean;
9
12
  time: HoursOfDay;
@@ -12,15 +15,46 @@ export declare class ScheduledActionBuilderWithState<TActionState extends IActio
12
15
  stateConstructor: () => TActionState;
13
16
  handler: ScheduledHandler<TActionState>;
14
17
  name: string;
18
+ /**
19
+ * Builder for `ScheduledAction` with state represented by `TActionState`
20
+ * @param name Action name, will be used for logging and storage
21
+ * @param stateConstructor Function that creates default state object
22
+ */
15
23
  constructor(name: string, stateConstructor: () => TActionState);
24
+ /**
25
+ * Adds a chat to whitelist for this action.
26
+ * @param chatId Chat id to execute in.
27
+ */
16
28
  allowIn(chatId: number): this;
29
+ /**
30
+ * Defines time for scheduled item execution.
31
+ * @param time Time of day (0 - 23) to execute action.
32
+ */
17
33
  runAt(time: HoursOfDay): this;
34
+ /** Defines action logic itself, will be executed on timer.
35
+ * @param handler Callback that will be called on timer.
36
+ */
18
37
  do(handler: ScheduledHandler<TActionState>): this;
38
+ /**
39
+ * Defines process-wide cache, that is shared by all actions of this type (even in different bot instances).
40
+ * Can be used for fetch request de-duping, etc.
41
+ * @param key Key that will be used to retrieve value from cache.
42
+ * @param itemFactory Callback that will be executed once to create cached value.
43
+ * @param invalidationTimeoutInHours Timeout for cache invalidation.
44
+ */
19
45
  withSharedCache(key: string, itemFactory: () => Promise<unknown>, invalidationTimeoutInHours?: Hours): this;
46
+ /** If called during building, action is marked as disabled and never checked. */
20
47
  disabled(): this;
48
+ /** Builds action */
21
49
  build(): ScheduledAction<TActionState>;
22
50
  }
51
+ /**
52
+ * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
53
+ */
23
54
  export declare class ScheduledActionBuilder extends ScheduledActionBuilderWithState<ActionStateBase> {
55
+ /**
56
+ * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
57
+ */
24
58
  constructor(name: string);
25
59
  }
26
60
  //# sourceMappingURL=scheduledActionBuilder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/scheduledActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AACzE,OAAO,EAAE,kBAA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BAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAG3D;;GAEG;AACH,qBAAa,+BAA+B,CACxC,YAAY,SAAS,YAAY;IAEjC,MAAM,UAAQ;IACd,IAAI,EAAE,UAAU,CAAK;IACrB,oBAAoB,kCAAyC;IAC7D,SAAS,EAAE,MAAM,EAAE,CAAM;IACzB,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAa;IAEpD,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;gBACS,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,YAAY;IAK9D;;;OAGG;IACH,OAAO,CAAC,MAAM,EAAE,MAAM;IAMtB;;;OAGG;IACH,KAAK,CAAC,IAAI,EAAE,UAAU;IAMtB;;OAEG;IACH,EAAE,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAM1C;;;;;;OAMG;IACH,eAAe,CACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EACnC,0BAA0B,GAAE,KAAmB;IAUnD,iFAAiF;IACjF,QAAQ;IAMR,oBAAoB;IACpB,KAAK;CAWR;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,+BAA+B,CAAC,eAAe,CAAC;IACxF;;OAEG;gBACS,IAAI,EAAE,MAAM;CAG3B"}
@@ -5,7 +5,15 @@ const scheduledAction_1 = require("../../entities/actions/scheduledAction");
5
5
  const cachedStateFactory_1 = require("../../entities/cachedStateFactory");
6
6
  const actionStateBase_1 = require("../../entities/states/actionStateBase");
7
7
  const noop_1 = require("../noop");
8
+ /**
9
+ * Builder for `ScheduledAction` with state represented by `TActionState`
10
+ */
8
11
  class ScheduledActionBuilderWithState {
12
+ /**
13
+ * Builder for `ScheduledAction` with state represented by `TActionState`
14
+ * @param name Action name, will be used for logging and storage
15
+ * @param stateConstructor Function that creates default state object
16
+ */
9
17
  constructor(name, stateConstructor) {
10
18
  this.active = true;
11
19
  this.time = 0;
@@ -15,32 +23,58 @@ class ScheduledActionBuilderWithState {
15
23
  this.name = name;
16
24
  this.stateConstructor = stateConstructor;
17
25
  }
26
+ /**
27
+ * Adds a chat to whitelist for this action.
28
+ * @param chatId Chat id to execute in.
29
+ */
18
30
  allowIn(chatId) {
19
31
  this.whitelist.push(chatId);
20
32
  return this;
21
33
  }
34
+ /**
35
+ * Defines time for scheduled item execution.
36
+ * @param time Time of day (0 - 23) to execute action.
37
+ */
22
38
  runAt(time) {
23
39
  this.time = time;
24
40
  return this;
25
41
  }
42
+ /** Defines action logic itself, will be executed on timer.
43
+ * @param handler Callback that will be called on timer.
44
+ */
26
45
  do(handler) {
27
46
  this.handler = handler;
28
47
  return this;
29
48
  }
49
+ /**
50
+ * Defines process-wide cache, that is shared by all actions of this type (even in different bot instances).
51
+ * Can be used for fetch request de-duping, etc.
52
+ * @param key Key that will be used to retrieve value from cache.
53
+ * @param itemFactory Callback that will be executed once to create cached value.
54
+ * @param invalidationTimeoutInHours Timeout for cache invalidation.
55
+ */
30
56
  withSharedCache(key, itemFactory, invalidationTimeoutInHours = 20) {
31
57
  this.cachedStateFactories.set(key, new cachedStateFactory_1.CachedStateFactory(itemFactory, invalidationTimeoutInHours));
32
58
  return this;
33
59
  }
60
+ /** If called during building, action is marked as disabled and never checked. */
34
61
  disabled() {
35
62
  this.active = false;
36
63
  return this;
37
64
  }
65
+ /** Builds action */
38
66
  build() {
39
67
  return new scheduledAction_1.ScheduledAction(this.name, this.handler, this.time, this.active, this.whitelist, this.cachedStateFactories, this.stateConstructor);
40
68
  }
41
69
  }
42
70
  exports.ScheduledActionBuilderWithState = ScheduledActionBuilderWithState;
71
+ /**
72
+ * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
73
+ */
43
74
  class ScheduledActionBuilder extends ScheduledActionBuilderWithState {
75
+ /**
76
+ * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
77
+ */
44
78
  constructor(name) {
45
79
  super(name, () => new actionStateBase_1.ActionStateBase());
46
80
  }
@@ -0,0 +1,2 @@
1
+ export declare function reverseRecord<T extends PropertyKey, U extends PropertyKey>(input: Record<T, U>): Record<U, T>;
2
+ //# sourceMappingURL=reverseRecord.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reverseRecord.d.ts","sourceRoot":"","sources":["../../helpers/reverseRecord.ts"],"names":[],"mappings":"AAAA,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,SAAS,WAAW,EACtE,KAAK,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAId,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CACpB"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reverseRecord = reverseRecord;
4
+ function reverseRecord(input) {
5
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [value, key]));
6
+ }