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
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export { startBot, stopBots } from './main';
2
2
  export { CommandActionBuilder } from './helpers/builders/commandActionBuilder';
3
3
  export { CommandActionBuilderWithState } from './helpers/builders/commandActionBuilder';
4
- export * from './helpers/builders/scheduledActionBuilder';
5
4
  export { IStorageClient } from './types/storage';
5
+ export { ILogger } from './types/logger';
6
+ export { IScheduler } from './types/scheduler';
7
+ export * from './helpers/builders/scheduledActionBuilder';
6
8
  export * from './types/actionState';
7
9
  export * from './entities/states/actionStateBase';
8
10
  export { Hours, Milliseconds, Seconds } from './types/timeValues';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAC;AAC/E,OAAO,EAAE,6BAA6B,EAAE,MAAM,yCAAyC,CAAC;AACxF,cAAc,2CAA2C,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAC;AAC/E,OAAO,EAAE,6BAA6B,EAAE,MAAM,yCAAyC,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC"}
package/dist/main.d.ts CHANGED
@@ -4,6 +4,8 @@ import { ScheduledAction } from './entities/actions/scheduledAction';
4
4
  import { IActionState } from './types/actionState';
5
5
  import { BotInstance } from './entities/botInstance';
6
6
  import { Seconds } from './types/timeValues';
7
+ import { IScheduler } from './types/scheduler';
8
+ import { ILogger } from './types/logger';
7
9
  /**
8
10
  * Starts bot
9
11
  */
@@ -18,14 +20,20 @@ declare function startBot(options: {
18
20
  scheduled: ScheduledAction<IActionState>[];
19
21
  /** Object containing chat name and chat id pairs. Used for logging and execution of scheduled action. */
20
22
  chats: Record<string, number>;
21
- /** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
22
- storageClient?: IStorageClient;
23
23
  /** Storage path for default `JsonFileStorage` client. Will be used only if `storageClient` is not provided. If not provided, default value of `./storage/` will be used.*/
24
24
  storagePath?: string;
25
25
  /** Period of time between execution of scheduled actions. */
26
26
  scheduledPeriod?: Seconds;
27
27
  /** If true, telegram API objects will be logged instead of message content. */
28
28
  verboseLoggingForIncomingMessage?: boolean;
29
+ services?: {
30
+ /** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
31
+ storageClient?: IStorageClient;
32
+ /** Logger client for bot logging. If not provided, default `JsonFileStorage` will be used. */
33
+ logger?: ILogger;
34
+ /** Scheduler client for bot scheduling. If not provided, default `NodeTimeoutScheduler` will be used. */
35
+ scheduler?: IScheduler;
36
+ };
29
37
  }): Promise<BotInstance>;
30
38
  /**
31
39
  * Terminates all scheduled tasks, closes storage connections and stops all bots.
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,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AO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAIzC;;GAEG;AACH,iBAAe,QAAQ,CAAC,OAAO,EAAE;IAC7B,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,2HAA2H;IAC3H,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;IACxC,mGAAmG;IACnG,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;IAC3C,yGAAyG;IACzG,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,2KAA2K;IAC3K,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6DAA6D;IAC7D,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,+EAA+E;IAC/E,gCAAgC,CAAC,EAAE,OAAO,CAAC;IAC3C,QAAQ,CAAC,EAAE;QACP,qGAAqG;QACrG,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,8FAA8F;QAC9F,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,yGAAyG;QACzG,SAAS,CAAC,EAAE,UAAU,CAAC;KAC1B,CAAC;CACL,wBAqBA;AAED;;GAEG;AACH,iBAAe,QAAQ,CAAC,MAAM,EAAE,MAAM,iBAIrC;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC"}
package/dist/main.js CHANGED
@@ -3,13 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.startBot = startBot;
4
4
  exports.stopBots = stopBots;
5
5
  const promises_1 = require("fs/promises");
6
- const logger_1 = require("./services/logger");
7
- const taskScheduler_1 = require("./services/taskScheduler");
8
6
  const botInstance_1 = require("./entities/botInstance");
9
7
  const bots = [];
10
- function log(text) {
11
- logger_1.Logger.logWithTraceId('ALL BOTS', 'System:Bot', 'System', text);
12
- }
13
8
  /**
14
9
  * Starts bot
15
10
  */
@@ -21,10 +16,14 @@ async function startBot(options) {
21
16
  commands: options.commands,
22
17
  scheduled: options.scheduled,
23
18
  chats: options.chats,
24
- storageClient: options.storageClient,
25
19
  storagePath: options.storagePath,
26
20
  scheduledPeriod: options.scheduledPeriod,
27
- verboseLoggingForIncomingMessage: options.verboseLoggingForIncomingMessage
21
+ verboseLoggingForIncomingMessage: options.verboseLoggingForIncomingMessage,
22
+ services: {
23
+ storageClient: options.services?.storageClient,
24
+ logger: options.services?.logger,
25
+ scheduler: options.services?.scheduler
26
+ }
28
27
  });
29
28
  bots.push(bot);
30
29
  return bot;
@@ -33,10 +32,6 @@ async function startBot(options) {
33
32
  * Terminates all scheduled tasks, closes storage connections and stops all bots.
34
33
  */
35
34
  async function stopBots(reason) {
36
- log(`Recieved termination code: ${reason}`);
37
- taskScheduler_1.Scheduler.stopAll();
38
- log('Acquiring storage semaphore...');
39
- log('Stopping bots...');
40
35
  for (const bot of bots) {
41
36
  await bot.stop(reason);
42
37
  }
@@ -1,5 +1,4 @@
1
1
  import { IStorageClient } from '../types/storage';
2
- import { ActionExecutionResult } from '../dtos/actionExecutionResult';
3
2
  import { IActionState } from '../types/actionState';
4
3
  import { IActionWithState, ActionKey } from '../types/actionWithState';
5
4
  export declare class JsonFileStorage implements IStorageClient {
@@ -15,7 +14,7 @@ export declare class JsonFileStorage implements IStorageClient {
15
14
  load<TActionState extends IActionState>(key: ActionKey): Promise<Record<number, TActionState>>;
16
15
  saveMetadata(actions: IActionWithState<IActionState>[], botName: string): Promise<void>;
17
16
  getActionState<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number): Promise<TActionState>;
18
- saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, transactionResult: ActionExecutionResult<TActionState>): Promise<void>;
17
+ saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, state: TActionState): Promise<void>;
19
18
  close(): Promise<void>;
20
19
  updateStateFor<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, update: (state: TActionState) => Promise<void>): Promise<void>;
21
20
  }
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../services/jsonFileStorage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAEvE,qBAAa,eAAgB,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmC;IACzD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA4C;IAClE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG7B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,IAAI,CAAC,EAAE,MAAM;YAiBH,IAAI;YAgBJ,YAAY;YAqBZ,IAAI;IAgBlB,OAAO,CAAC,eAAe;IAOjB,IAAI,CAAC,YAAY,SAAS,YAAY,EAAE,GAAG,EAAE,SAAS;IAMtD,YAAY,CACd,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,OAAO,EAAE,MAAM;IAWb,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM;IAYZ,yBAAyB,CAAC,YAAY,SAAS,YAAY,EAC7D,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,qBAAqB,CAAC,YAAY,CAAC;IAYpD,KAAK,IAAI,OAAO,CAAC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AAI,OAAO,CAAC,IAAI,CAAC;IAMtB,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC;CAcrD"}
@@ -76,13 +76,11 @@ class JsonFileStorage {
76
76
  return Object.assign(action.stateConstructor(), data[chatId]);
77
77
  });
78
78
  }
79
- async saveActionExecutionResult(action, chatId, transactionResult) {
79
+ async saveActionExecutionResult(action, chatId, state) {
80
80
  await this.lock(action.key, async () => {
81
81
  const data = await this.loadInternal(action.key);
82
- if (transactionResult.shouldUpdate) {
83
- data[chatId] = transactionResult.data;
84
- await this.save(data, action.key);
85
- }
82
+ data[chatId] = state;
83
+ await this.save(data, action.key);
86
84
  });
87
85
  }
88
86
  async close() {
@@ -0,0 +1,8 @@
1
+ import { ILogger } from '../types/logger';
2
+ export declare class JsonLogger implements ILogger {
3
+ private serializeError;
4
+ logObjectWithTraceId(botName: string, traceId: string | number, chatName: string, data: any): void;
5
+ logWithTraceId(botName: string, traceId: string | number, chatName: string, text: string): void;
6
+ errorWithTraceId<TData>(botName: string, traceId: string | number, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
7
+ }
8
+ //# sourceMappingURL=jsonLogger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonLogger.d.ts","sourceRoot":"","sources":["../../services/jsonLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,qBAAa,UAAW,YAAW,OAAO;IAEtC,OAAO,CAAC,cAAc;IAQtB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG;IAQb,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"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonLogger = void 0;
4
+ class JsonLogger {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ serializeError(error) {
7
+ const plainObject = {};
8
+ Object.getOwnPropertyNames(error).forEach(function (key) {
9
+ plainObject[key] = error[key];
10
+ });
11
+ return JSON.stringify(plainObject);
12
+ }
13
+ logObjectWithTraceId(botName, traceId, chatName,
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ data) {
16
+ data.botName = botName;
17
+ data.traceId = traceId;
18
+ data.chatName = chatName;
19
+ console.log(data);
20
+ }
21
+ logWithTraceId(botName, traceId, chatName, text) {
22
+ console.log(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`);
23
+ }
24
+ errorWithTraceId(botName, traceId, chatName, errorObj, extraData) {
25
+ console.error(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","error":${this.serializeError(errorObj)}${extraData ? `,"extraData":${JSON.stringify(extraData)}` : ''}}`);
26
+ }
27
+ }
28
+ exports.JsonLogger = JsonLogger;
@@ -0,0 +1,13 @@
1
+ import { TaskRecord } from '../entities/taskRecord';
2
+ import { ILogger } from '../types/logger';
3
+ import { IScheduler } from '../types/scheduler';
4
+ import { Milliseconds } from '../types/timeValues';
5
+ export declare class NodeTimeoutScheduler implements IScheduler {
6
+ private readonly logger;
7
+ readonly activeTasks: TaskRecord[];
8
+ constructor(logger: ILogger);
9
+ stopAll(): void;
10
+ createTask(name: string, action: () => void, interval: Milliseconds, executeRightAway: boolean, ownerName: string): void;
11
+ createOnetimeTask(name: string, action: () => void, delay: Milliseconds, ownerName: string): void;
12
+ }
13
+ //# sourceMappingURL=nodeTimeoutScheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeTimeoutScheduler.d.ts","sourceRoot":"","sources":["../../services/nodeTimeoutScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,qBAAa,oBAAqB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAClC,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAM;gBAE5B,MAAM,EAAE,OAAO;IAI3B,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"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodeTimeoutScheduler = void 0;
4
+ const taskRecord_1 = require("../entities/taskRecord");
5
+ class NodeTimeoutScheduler {
6
+ constructor(logger) {
7
+ this.activeTasks = [];
8
+ this.logger = logger;
9
+ }
10
+ stopAll() {
11
+ this.activeTasks.forEach((task) => {
12
+ clearInterval(task.taskId);
13
+ });
14
+ }
15
+ createTask(name, action, interval, executeRightAway, ownerName) {
16
+ const taskId = setInterval(action, interval);
17
+ const task = new taskRecord_1.TaskRecord(name, taskId, interval);
18
+ if (executeRightAway) {
19
+ setImmediate(action);
20
+ }
21
+ this.logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Created task [${taskId}]${name}, that will run every ${interval}ms.`);
22
+ this.activeTasks.push(task);
23
+ }
24
+ createOnetimeTask(name, action, delay, ownerName) {
25
+ const actionWrapper = () => {
26
+ this.logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Executing delayed oneshot [${taskId}]${name}`);
27
+ action();
28
+ };
29
+ const taskId = setTimeout(actionWrapper, delay);
30
+ this.logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Created oneshot task [${taskId}]${name}, that will run in ${delay}ms.`);
31
+ }
32
+ }
33
+ exports.NodeTimeoutScheduler = NodeTimeoutScheduler;
@@ -1,13 +1,15 @@
1
1
  import { IStorageClient } from '../types/storage';
2
2
  import { BotResponse } from '../types/response';
3
3
  import { Telegram } from 'telegraf/typings/telegram';
4
+ import { ILogger } from '../types/logger';
4
5
  export declare class TelegramApiService {
6
+ private readonly telegram;
7
+ private readonly storage;
8
+ private readonly logger;
9
+ private readonly botName;
10
+ private readonly messageQueue;
5
11
  isFlushing: boolean;
6
- readonly messageQueue: BotResponse[];
7
- readonly botName: string;
8
- readonly telegram: Telegram;
9
- readonly storage: IStorageClient;
10
- constructor(botName: string, telegram: Telegram, storage: IStorageClient);
12
+ constructor(botName: string, telegram: Telegram, storage: IStorageClient, logger: ILogger);
11
13
  enqueueBatchedResponses(responses: BotResponse[]): void;
12
14
  flushResponses(): Promise<void>;
13
15
  private pinIfShould;
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D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ty(exports, "__esModule", { value: true });
3
3
  exports.TelegramApiService = void 0;
4
- const logger_1 = require("./logger");
5
4
  const promises_1 = require("timers/promises");
6
5
  const TELEGRAM_RATELIMIT_DELAY = 35;
7
6
  class TelegramApiService {
8
- constructor(botName, telegram, storage) {
9
- this.isFlushing = false;
7
+ constructor(botName, telegram, storage, logger) {
10
8
  this.messageQueue = [];
9
+ this.isFlushing = false;
11
10
  this.telegram = telegram;
12
11
  this.botName = botName;
13
12
  this.storage = storage;
13
+ this.logger = logger;
14
14
  }
15
15
  enqueueBatchedResponses(responses) {
16
16
  for (const response of responses) {
@@ -30,7 +30,7 @@ class TelegramApiService {
30
30
  await (0, promises_1.setTimeout)(TELEGRAM_RATELIMIT_DELAY);
31
31
  }
32
32
  catch (error) {
33
- logger_1.Logger.errorWithTraceId(this.botName, message.traceId, message.chatInfo.name, error, message);
33
+ this.logger.errorWithTraceId(this.botName, message.traceId, message.chatInfo.name, error, message);
34
34
  }
35
35
  }
36
36
  this.isFlushing = false;
@@ -1,4 +1,4 @@
1
1
  import { MessageContext } from '../entities/context/messageContext';
2
2
  import { IActionState } from './actionState';
3
- export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) => Promise<boolean>;
3
+ export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) => boolean;
4
4
  //# sourceMappingURL=commandCondition.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC,OAAO,CAAC,CAAC"}
1
+ {"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ export interface ILogger {
2
+ logObjectWithTraceId(botName: string, traceId: string | number, chatName: string, data: any): void;
3
+ logWithTraceId(botName: string, traceId: string | number, chatName: string, text: string): void;
4
+ errorWithTraceId<TData>(botName: string, traceId: string | number, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
5
+ }
6
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../types/logger.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACpB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACb,IAAI,CAAC;IAER,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,GAC9B,IAAI,CAAC;CACX"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { Milliseconds } from './timeValues';
2
+ export interface IScheduler {
3
+ stopAll(): void;
4
+ createTask(name: string, action: () => void, interval: Milliseconds, executeRightAway: boolean, ownerName: string): void;
5
+ createOnetimeTask(name: string, action: () => void, delay: Milliseconds, ownerName: string): void;
6
+ }
7
+ //# sourceMappingURL=scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../types/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,WAAW,UAAU;IACvB,OAAO,IAAI,IAAI,CAAC;IAEhB,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM,GAClB,IAAI,CAAC;IAER,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM,GAClB,IAAI,CAAC;CACX"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,3 @@
1
- import { ActionExecutionResult } from '../dtos/actionExecutionResult';
2
1
  import { IActionState } from './actionState';
3
2
  import { ActionKey, IActionWithState } from './actionWithState';
4
3
  export interface IStorageClient {
@@ -7,6 +6,6 @@ export interface IStorageClient {
7
6
  load<TActionState extends IActionState>(key: ActionKey): Promise<Record<number, TActionState>>;
8
7
  saveMetadata<TActionState extends IActionState>(actions: IActionWithState<TActionState>[], botName: string): Promise<void>;
9
8
  getActionState<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number): Promise<TActionState>;
10
- saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, transactionResult: ActionExecutionResult<TActionState>): Promise<void>;
9
+ saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, state: TActionState): Promise<void>;
11
10
  }
12
11
  //# sourceMappingURL=storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC3B,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,SAAS,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,YAAY,SAAS,YAAY,EAC1C,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CAAC,YAAY,SAAS,YAAY,EACvD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,qBAAqB,CAAC,YAAY,CAAC,GACvD,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC3B,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,SAAS,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,YAAY,SAAS,YAAY,EAC1C,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CAAC,YAAY,SAAS,YAAY,EACvD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY,GACpB,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
@@ -6,25 +6,25 @@ export class CommandTriggerCheckResult {
6
6
  return new CommandTriggerCheckResult(false, [], false);
7
7
  }
8
8
 
9
- shouldTrigger: boolean;
10
- matchResults: RegExpExecArray[];
11
- skipCooldown: boolean;
9
+ readonly shouldExecute: boolean;
10
+ readonly matchResults: RegExpExecArray[];
11
+ readonly skipCooldown: boolean;
12
12
 
13
13
  constructor(
14
- shouldTrigger: boolean,
14
+ shouldExecute: boolean,
15
15
  matchResults: RegExpExecArray[],
16
16
  skipCooldown: boolean
17
17
  ) {
18
- this.shouldTrigger = shouldTrigger;
18
+ this.shouldExecute = shouldExecute;
19
19
  this.matchResults = matchResults;
20
20
  this.skipCooldown = skipCooldown;
21
21
  }
22
22
 
23
23
  mergeWith(other: CommandTriggerCheckResult) {
24
- this.shouldTrigger = this.shouldTrigger || other.shouldTrigger;
25
- this.matchResults = this.matchResults.concat(other.matchResults);
26
- this.skipCooldown = this.skipCooldown || other.skipCooldown;
27
-
28
- return this;
24
+ return new CommandTriggerCheckResult(
25
+ this.shouldExecute || other.shouldExecute,
26
+ this.matchResults.concat(other.matchResults),
27
+ this.skipCooldown || other.skipCooldown
28
+ );
29
29
  }
30
30
  }
@@ -4,12 +4,12 @@ import { MessageType, MessageTypeValue } from '../types/messageTypes';
4
4
  import { ChatInfo } from './chatInfo';
5
5
 
6
6
  export class IncomingMessage {
7
- message_id: number;
8
- chatInfo: ChatInfo;
9
- from: User | undefined;
10
- text: string;
11
- type: MessageTypeValue;
12
- traceId = randomInt(10000, 99999);
7
+ readonly message_id: number;
8
+ readonly chatInfo: ChatInfo;
9
+ readonly from: User | undefined;
10
+ readonly text: string;
11
+ readonly type: MessageTypeValue;
12
+ readonly traceId = randomInt(10000, 99999);
13
13
 
14
14
  private detectMessageType(
15
15
  message: Update.New & (Update.NonChannel & Message)
@@ -8,9 +8,8 @@ import { IActionState } from '../../types/actionState';
8
8
  import { IActionWithState, ActionKey } from '../../types/actionWithState';
9
9
  import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
10
10
  import { MessageContext } from '../context/messageContext';
11
- import { Logger } from '../../services/logger';
12
- import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
13
11
  import { CommandTrigger } from '../../types/commandTrigger';
12
+ import { Noop } from '../../helpers/noop';
14
13
 
15
14
  export class CommandAction<TActionState extends IActionState>
16
15
  implements IActionWithState<TActionState>
@@ -57,27 +56,23 @@ export class CommandAction<TActionState extends IActionState>
57
56
  );
58
57
 
59
58
  if (!this.active || this.chatsBlacklist.includes(ctx.chatInfo.id))
60
- return [];
61
-
62
- const isConditionMet = await this.condition(ctx);
63
-
64
- if (!isConditionMet) return [];
59
+ return Noop.NoResponse;
65
60
 
66
61
  const state = await ctx.storage.getActionState<TActionState>(
67
62
  this,
68
63
  ctx.chatInfo.id
69
64
  );
70
65
 
71
- const { shouldTrigger, matchResults, skipCooldown } = this.triggers
72
- .map((x) => this.checkTrigger(ctx, x, state))
66
+ const { shouldExecute, matchResults, skipCooldown } = this.triggers
67
+ .map((x) => this.checkIfShouldBeExecuted(ctx, x, state))
73
68
  .reduce(
74
69
  (acc, curr) => acc.mergeWith(curr),
75
70
  CommandTriggerCheckResult.DoNotTrigger
76
71
  );
77
72
 
78
- if (!shouldTrigger) return [];
73
+ if (!shouldExecute) return Noop.NoResponse;
79
74
 
80
- Logger.logWithTraceId(
75
+ ctx.logger.logWithTraceId(
81
76
  ctx.botName,
82
77
  ctx.traceId,
83
78
  ctx.chatInfo.name,
@@ -95,27 +90,20 @@ export class CommandAction<TActionState extends IActionState>
95
90
  state.lastExecutedDate = moment().valueOf();
96
91
  }
97
92
 
98
- ctx.updateActions.forEach((action) => action(state));
99
-
100
93
  await ctx.storage.saveActionExecutionResult(
101
94
  this,
102
95
  ctx.chatInfo.id,
103
- new ActionExecutionResult(state, ctx.startCooldown && shouldTrigger)
96
+ state
104
97
  );
105
98
 
106
- ctx.isInitialized = false;
107
-
108
99
  return ctx.responses;
109
100
  }
110
101
 
111
- private checkTrigger(
102
+ private checkIfShouldBeExecuted(
112
103
  ctx: MessageContext<TActionState>,
113
104
  trigger: CommandTrigger,
114
105
  state: IActionState
115
106
  ) {
116
- let shouldTrigger = false;
117
- const matchResults: RegExpExecArray[] = [];
118
-
119
107
  if (!ctx.fromUserId)
120
108
  return CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
121
109
 
@@ -135,28 +123,11 @@ export class CommandAction<TActionState extends IActionState>
135
123
 
136
124
  if (onCooldown) return CommandTriggerCheckResult.DoNotTrigger;
137
125
 
138
- if (trigger == ctx.messageType) {
139
- shouldTrigger = true;
140
- } else if (typeof trigger == 'string') {
141
- shouldTrigger = ctx.messageText.toLowerCase() == trigger;
142
- } else {
143
- trigger.lastIndex = 0;
144
-
145
- const execResult = trigger.exec(ctx.messageText);
146
- if (execResult != null) {
147
- matchResults.push(execResult);
148
-
149
- if (trigger.global) {
150
- while (true) {
151
- const nextResult = trigger.exec(ctx.messageText);
152
- if (nextResult == null) break;
153
- matchResults.push(nextResult);
154
- }
155
- }
156
- }
126
+ const isCustomConditionMet = this.condition(ctx);
127
+ if (!isCustomConditionMet)
128
+ return CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
157
129
 
158
- shouldTrigger = matchResults.length > 0;
159
- }
130
+ const { shouldTrigger, matchResults } = this.checkTrigger(ctx, trigger);
160
131
 
161
132
  return new CommandTriggerCheckResult(
162
133
  shouldTrigger,
@@ -164,4 +135,40 @@ export class CommandAction<TActionState extends IActionState>
164
135
  false
165
136
  );
166
137
  }
138
+
139
+ private checkTrigger(
140
+ ctx: MessageContext<TActionState>,
141
+ trigger: CommandTrigger
142
+ ) {
143
+ if (trigger == ctx.messageType)
144
+ return { shouldTrigger: true, matchResults: [] };
145
+
146
+ if (typeof trigger == 'string')
147
+ return {
148
+ shouldTrigger: ctx.messageText.toLowerCase() == trigger,
149
+ matchResults: []
150
+ };
151
+
152
+ const matchResults: RegExpExecArray[] = [];
153
+
154
+ trigger.lastIndex = 0;
155
+
156
+ const execResult = trigger.exec(ctx.messageText);
157
+ if (execResult != null) {
158
+ matchResults.push(execResult);
159
+
160
+ if (trigger.global) {
161
+ while (true) {
162
+ const nextResult = trigger.exec(ctx.messageText);
163
+ if (nextResult == null) break;
164
+ matchResults.push(nextResult);
165
+ }
166
+ }
167
+ }
168
+
169
+ return {
170
+ shouldTrigger: matchResults.length > 0,
171
+ matchResults
172
+ };
173
+ }
167
174
  }