chz-telegram-bot 0.0.51 → 0.0.53

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 (100) 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 +8 -7
  5. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  6. package/dist/dtos/incomingMessage.js +3 -2
  7. package/dist/dtos/responses/delay.d.ts +3 -2
  8. package/dist/dtos/responses/delay.d.ts.map +1 -1
  9. package/dist/dtos/responses/imageMessage.d.ts +3 -2
  10. package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
  11. package/dist/dtos/responses/reaction.d.ts +3 -2
  12. package/dist/dtos/responses/reaction.d.ts.map +1 -1
  13. package/dist/dtos/responses/textMessage.d.ts +3 -2
  14. package/dist/dtos/responses/textMessage.d.ts.map +1 -1
  15. package/dist/dtos/responses/unpin.d.ts +3 -2
  16. package/dist/dtos/responses/unpin.d.ts.map +1 -1
  17. package/dist/dtos/responses/videoMessage.d.ts +3 -2
  18. package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
  19. package/dist/entities/actions/commandAction.d.ts +1 -0
  20. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  21. package/dist/entities/actions/commandAction.js +38 -37
  22. package/dist/entities/actions/scheduledAction.d.ts +1 -1
  23. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  24. package/dist/entities/actions/scheduledAction.js +12 -16
  25. package/dist/entities/botInstance.d.ts +11 -3
  26. package/dist/entities/botInstance.d.ts.map +1 -1
  27. package/dist/entities/botInstance.js +26 -18
  28. package/dist/entities/context/chatContext.d.ts +12 -11
  29. package/dist/entities/context/chatContext.d.ts.map +1 -1
  30. package/dist/entities/context/chatContext.js +5 -12
  31. package/dist/entities/context/messageContext.d.ts +4 -2
  32. package/dist/entities/context/messageContext.d.ts.map +1 -1
  33. package/dist/entities/context/messageContext.js +4 -4
  34. package/dist/helpers/noop.d.ts +4 -2
  35. package/dist/helpers/noop.d.ts.map +1 -1
  36. package/dist/helpers/noop.js +3 -2
  37. package/dist/helpers/traceFactory.d.ts +3 -0
  38. package/dist/helpers/traceFactory.d.ts.map +1 -0
  39. package/dist/helpers/traceFactory.js +6 -0
  40. package/dist/index.d.ts +4 -1
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/main.d.ts +10 -2
  43. package/dist/main.d.ts.map +1 -1
  44. package/dist/main.js +6 -11
  45. package/dist/services/jsonFileStorage.d.ts +1 -2
  46. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  47. package/dist/services/jsonFileStorage.js +3 -5
  48. package/dist/services/jsonLogger.d.ts +9 -0
  49. package/dist/services/jsonLogger.d.ts.map +1 -0
  50. package/dist/services/jsonLogger.js +28 -0
  51. package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
  52. package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
  53. package/dist/services/nodeTimeoutScheduler.js +34 -0
  54. package/dist/services/telegramApi.d.ts +7 -5
  55. package/dist/services/telegramApi.d.ts.map +1 -1
  56. package/dist/services/telegramApi.js +4 -4
  57. package/dist/types/commandCondition.d.ts +1 -1
  58. package/dist/types/commandCondition.d.ts.map +1 -1
  59. package/dist/types/logger.d.ts +7 -0
  60. package/dist/types/logger.d.ts.map +1 -0
  61. package/dist/types/logger.js +2 -0
  62. package/dist/types/response.d.ts +2 -1
  63. package/dist/types/response.d.ts.map +1 -1
  64. package/dist/types/scheduler.d.ts +7 -0
  65. package/dist/types/scheduler.d.ts.map +1 -0
  66. package/dist/types/scheduler.js +2 -0
  67. package/dist/types/storage.d.ts +1 -2
  68. package/dist/types/storage.d.ts.map +1 -1
  69. package/dist/types/trace.d.ts +2 -0
  70. package/dist/types/trace.d.ts.map +1 -0
  71. package/dist/types/trace.js +2 -0
  72. package/dtos/commandTriggerCheckResult.ts +10 -10
  73. package/dtos/incomingMessage.ts +17 -7
  74. package/dtos/responses/delay.ts +3 -2
  75. package/dtos/responses/imageMessage.ts +3 -2
  76. package/dtos/responses/reaction.ts +3 -2
  77. package/dtos/responses/textMessage.ts +3 -2
  78. package/dtos/responses/unpin.ts +3 -2
  79. package/dtos/responses/videoMessage.ts +3 -2
  80. package/entities/actions/commandAction.ts +48 -41
  81. package/entities/actions/scheduledAction.ts +28 -31
  82. package/entities/botInstance.ts +57 -34
  83. package/entities/context/chatContext.ts +22 -17
  84. package/entities/context/messageContext.ts +10 -6
  85. package/helpers/noop.ts +5 -2
  86. package/helpers/traceFactory.ts +9 -0
  87. package/index.ts +4 -1
  88. package/main.ts +16 -15
  89. package/package.json +1 -1
  90. package/services/jsonFileStorage.ts +4 -6
  91. package/services/{logger.ts → jsonLogger.ts} +7 -6
  92. package/services/{taskScheduler.ts → nodeTimeoutScheduler.ts} +15 -10
  93. package/services/telegramApi.ts +15 -8
  94. package/types/commandCondition.ts +1 -1
  95. package/types/logger.ts +26 -0
  96. package/types/response.ts +2 -1
  97. package/types/scheduler.ts +20 -0
  98. package/types/storage.ts +1 -2
  99. package/types/trace.ts +1 -0
  100. package/dtos/actionExecutionResult.ts +0 -11
@@ -8,13 +8,14 @@ const telegraf_1 = require("telegraf");
8
8
  const timeConvertions_1 = require("../helpers/timeConvertions");
9
9
  const jsonFileStorage_1 = require("../services/jsonFileStorage");
10
10
  const telegramApi_1 = require("../services/telegramApi");
11
- const logger_1 = require("../services/logger");
12
- const taskScheduler_1 = require("../services/taskScheduler");
11
+ const jsonLogger_1 = require("../services/jsonLogger");
13
12
  const incomingMessage_1 = require("../dtos/incomingMessage");
14
13
  const moment_1 = __importDefault(require("moment"));
15
14
  const chatContext_1 = require("./context/chatContext");
16
15
  const messageContext_1 = require("./context/messageContext");
17
16
  const chatInfo_1 = require("../dtos/chatInfo");
17
+ const nodeTimeoutScheduler_1 = require("../services/nodeTimeoutScheduler");
18
+ const traceFactory_1 = require("../helpers/traceFactory");
18
19
  class BotInstance {
19
20
  constructor(options) {
20
21
  this.name = options.name;
@@ -22,22 +23,26 @@ class BotInstance {
22
23
  this.scheduled = options.scheduled;
23
24
  this.chats = options.chats;
24
25
  const actions = [...this.commands, ...this.scheduled];
25
- logger_1.Logger.logWithTraceId(this.name, `System:Bot-${this.name}-Start`, 'System', 'Starting bot...');
26
26
  this.telegraf = new telegraf_1.Telegraf(options.token);
27
+ this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
28
+ this.scheduler =
29
+ options.services?.scheduler ??
30
+ new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
27
31
  this.storage =
28
- options.storageClient ??
32
+ options.services?.storageClient ??
29
33
  new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
30
- this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage);
34
+ this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
31
35
  this.initializeMessageProcessing(options.verboseLoggingForIncomingMessage ?? false);
32
36
  this.initializeScheduledProcessing(options.scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
33
37
  this.storage.saveMetadata(actions, this.name);
38
+ this.logger.logWithTraceId(this.name, (0, traceFactory_1.createTrace)(this, this.name, 'Start'), 'System', 'Starting bot...');
34
39
  this.telegraf.launch();
35
40
  }
36
41
  initializeScheduledProcessing(period) {
37
42
  if (this.scheduled.length > 0) {
38
43
  const now = (0, moment_1.default)();
39
44
  if (now.minute() == 0 && now.second() == 0) {
40
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
45
+ this.scheduler.createTask('ScheduledProcessing', async () => {
41
46
  await this.runScheduled();
42
47
  }, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
43
48
  return;
@@ -47,8 +52,8 @@ class BotInstance {
47
52
  nextExecutionTime = nextExecutionTime.add(1, 'hour');
48
53
  }
49
54
  const delay = nextExecutionTime.diff(now);
50
- taskScheduler_1.Scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
51
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
55
+ this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
56
+ this.scheduler.createTask('ScheduledProcessing', async () => {
52
57
  await this.runScheduled();
53
58
  }, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
54
59
  }, delay, this.name);
@@ -57,51 +62,54 @@ class BotInstance {
57
62
  initializeMessageProcessing(verboseLoggingForIncomingMessage) {
58
63
  if (this.commands.length > 0) {
59
64
  this.telegraf.on('message', async (ctx) => {
60
- const msg = new incomingMessage_1.IncomingMessage(ctx.update.message);
65
+ const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.name);
61
66
  const messageContent = msg.text || `<non-text message: ${msg.type}>`;
62
67
  const messageFromName = msg.from?.first_name ?? 'Unknown';
63
68
  const messageFromId = msg.from?.id ?? 'Unknown';
64
69
  if (verboseLoggingForIncomingMessage) {
65
- logger_1.Logger.logObjectWithTraceId(this.name, msg.traceId, msg.chatInfo.name, ctx.update.message);
70
+ this.logger.logObjectWithTraceId(this.name, msg.traceId, msg.chatInfo.name, ctx.update.message);
66
71
  }
67
72
  else {
68
- logger_1.Logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${messageFromName} (${messageFromId}): ${messageContent}`);
73
+ this.logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${messageFromName} (${messageFromId}): ${messageContent}`);
69
74
  }
70
75
  await this.processMessage(msg);
71
76
  });
72
77
  }
73
78
  }
74
79
  async stop(code) {
75
- logger_1.Logger.logWithTraceId(this.name, `System:Bot-${this.name}-Stop`, 'System', 'Stopping bot...');
80
+ this.logger.logWithTraceId(this.name, (0, traceFactory_1.createTrace)(this, this.name, 'Stop'), 'System', 'Stopping bot...');
81
+ this.scheduler.stopAll();
76
82
  await this.storage.close();
77
83
  this.telegraf.stop(code);
78
84
  }
79
85
  async runScheduled() {
80
- const ctx = new chatContext_1.ChatContext();
86
+ const ctx = new chatContext_1.ChatContext(this.storage, this.logger, this.scheduler);
81
87
  for (const [chatName, chatId] of Object.entries(this.chats)) {
82
88
  for (const scheduledAction of this.scheduled) {
83
- ctx.initializeChatContext(this.name, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
89
+ ctx.initializeChatContext(this.name, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), (0, traceFactory_1.createTrace)(scheduledAction, this.name, `${scheduledAction.key}-${chatId}`));
84
90
  try {
85
91
  const responses = await scheduledAction.exec(ctx);
86
92
  this.api.enqueueBatchedResponses(responses);
93
+ ctx.isInitialized = false;
87
94
  }
88
95
  catch (error) {
89
- logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
96
+ this.logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
90
97
  }
91
98
  }
92
99
  }
93
100
  await this.api.flushResponses();
94
101
  }
95
102
  async processMessage(msg) {
96
- const ctx = new messageContext_1.MessageContext();
103
+ const ctx = new messageContext_1.MessageContext(this.storage, this.logger, this.scheduler);
97
104
  for (const commandAction of this.commands) {
98
- ctx.initializeMessageContext(this.name, commandAction, msg, this.storage);
105
+ ctx.initializeMessageContext(this.name, commandAction, msg);
99
106
  try {
100
107
  const responses = await commandAction.exec(ctx);
101
108
  this.api.enqueueBatchedResponses(responses);
109
+ ctx.isInitialized = false;
102
110
  }
103
111
  catch (error) {
104
- logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, error, ctx);
112
+ this.logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, error, ctx);
105
113
  }
106
114
  }
107
115
  await this.api.flushResponses();
@@ -5,30 +5,31 @@ import { IActionState } from '../../types/actionState';
5
5
  import { BotResponse } from '../../types/response';
6
6
  import { Milliseconds } from '../../types/timeValues';
7
7
  import { ChatInfo } from '../../dtos/chatInfo';
8
+ import { ILogger } from '../../types/logger';
9
+ import { IScheduler } from '../../types/scheduler';
10
+ import { TraceId } from '../../types/trace';
8
11
  /**
9
12
  * Context of action executed in chat.
10
13
  */
11
14
  export declare class ChatContext<TActionState extends IActionState> {
12
15
  protected action: IActionWithState<TActionState>;
13
- updateActions: Array<(state: TActionState) => void>;
16
+ /** Storage client instance for the bot executing this action. */
17
+ readonly storage: IStorageClient;
18
+ /** Logger instance for the bot executing this action */
19
+ readonly logger: ILogger;
20
+ /** Scheduler instance for the bot executing this action */
21
+ readonly scheduler: IScheduler;
14
22
  /** Trace id of a action execution. */
15
- traceId: number | string;
23
+ traceId: TraceId;
16
24
  /** Name of a bot that executes this action. */
17
25
  botName: string;
18
26
  /** Chat information. */
19
27
  chatInfo: ChatInfo;
20
- /** Storage client instance for this bot. */
21
- storage: IStorageClient;
22
28
  /** Ordered collection of responses to be processed */
23
29
  responses: BotResponse[];
24
30
  isInitialized: boolean;
25
- constructor();
26
- initializeChatContext(botName: string, action: IActionWithState<TActionState>, chatInfo: ChatInfo, traceId: number | string, storage: IStorageClient): this;
27
- /**
28
- * Manually update the state of an action.
29
- * @param stateUpdateAction Function that will modify state.
30
- */
31
- updateState(stateUpdateAction: (state: TActionState) => void): void;
31
+ constructor(storage: IStorageClient, logger: ILogger, scheduler: IScheduler);
32
+ initializeChatContext(botName: string, action: IActionWithState<TActionState>, chatInfo: ChatInfo, traceId: TraceId): this;
32
33
  /**
33
34
  * Sends text message to chat after action execution is finished.
34
35
  * If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
@@ -1 +1 @@
1
- {"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO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E,cAAc,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,UAAU;IAOzB,qBAAqB,CACjB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO;IAapB;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAahE;;;;;OAKG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;;OAKG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;OAIG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM;IAW9B;;;OAGG;IACH,iBAAiB,CAAC,KAAK,EAAE,YAAY;CAKxC"}
@@ -11,30 +11,23 @@ const delay_1 = require("../../dtos/responses/delay");
11
11
  * Context of action executed in chat.
12
12
  */
13
13
  class ChatContext {
14
- constructor() {
15
- this.updateActions = [];
14
+ constructor(storage, logger, scheduler) {
16
15
  /** Ordered collection of responses to be processed */
17
16
  this.responses = [];
18
17
  this.isInitialized = false;
18
+ this.storage = storage;
19
+ this.logger = logger;
20
+ this.scheduler = scheduler;
19
21
  }
20
- initializeChatContext(botName, action, chatInfo, traceId, storage) {
22
+ initializeChatContext(botName, action, chatInfo, traceId) {
21
23
  this.botName = botName;
22
24
  this.action = action;
23
25
  this.chatInfo = chatInfo;
24
26
  this.traceId = traceId;
25
- this.storage = storage;
26
- this.updateActions = [];
27
27
  this.isInitialized = true;
28
28
  this.responses = [];
29
29
  return this;
30
30
  }
31
- /**
32
- * Manually update the state of an action.
33
- * @param stateUpdateAction Function that will modify state.
34
- */
35
- updateState(stateUpdateAction) {
36
- this.updateActions.push(stateUpdateAction);
37
- }
38
31
  /**
39
32
  * Sends text message to chat after action execution is finished.
40
33
  * If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
@@ -6,6 +6,8 @@ import { IncomingMessage } from '../../dtos/incomingMessage';
6
6
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
7
7
  import { IActionWithState } from '../../types/actionWithState';
8
8
  import { MessageTypeValue } from '../../types/messageTypes';
9
+ import { ILogger } from '../../types/logger';
10
+ import { IScheduler } from '../../types/scheduler';
9
11
  /**
10
12
  * Context of action executed in chat, in response to a message
11
13
  */
@@ -24,8 +26,8 @@ export declare class MessageContext<TActionState extends IActionState> extends C
24
26
  fromUserName: string;
25
27
  /** Type of message being received */
26
28
  messageType: MessageTypeValue;
27
- constructor();
28
- initializeMessageContext(botName: string, action: IActionWithState<TActionState>, message: IncomingMessage, storage: IStorageClient): this;
29
+ constructor(storage: IStorageClient, logger: ILogger, scheduler: IScheduler);
30
+ initializeMessageContext(botName: string, action: IActionWithState<TActionState>, message: IncomingMessage): this;
29
31
  /**
30
32
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
31
33
  * @param commandName Name of an action to load state of.
@@ -1 +1 @@
1
- {"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAa,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SA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;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,aAAa;CAW7B"}
@@ -12,14 +12,14 @@ const chatContext_1 = require("./chatContext");
12
12
  * Context of action executed in chat, in response to a message
13
13
  */
14
14
  class MessageContext extends chatContext_1.ChatContext {
15
- constructor() {
16
- super();
15
+ constructor(storage, logger, scheduler) {
16
+ super(storage, logger, scheduler);
17
17
  /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
18
18
  this.matchResults = [];
19
19
  /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
20
20
  this.startCooldown = true;
21
21
  }
22
- initializeMessageContext(botName, action, message, storage) {
22
+ initializeMessageContext(botName, action, message) {
23
23
  this.messageId = message.message_id;
24
24
  this.messageText = message.text ?? '';
25
25
  this.messageType = message.type;
@@ -29,7 +29,7 @@ class MessageContext extends chatContext_1.ChatContext {
29
29
  (message.from?.last_name ? ` ${message.from.last_name}` : '');
30
30
  this.matchResults = [];
31
31
  this.startCooldown = true;
32
- return this.initializeChatContext(botName, action, message.chatInfo, message.traceId, storage);
32
+ return this.initializeChatContext(botName, action, message.chatInfo, message.traceId);
33
33
  }
34
34
  /**
35
35
  * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
@@ -1,6 +1,8 @@
1
+ import { BotResponse } from '../types/response';
1
2
  export declare class Noop {
2
- static true<T1>(arg1: T1): Promise<boolean>;
3
- static false<T1>(arg1: T1): Promise<boolean>;
3
+ static NoResponse: BotResponse[];
4
+ static true<T1>(arg1: T1): boolean;
5
+ static false<T1>(arg1: T1): boolean;
4
6
  static call<T1, T2>(arg1: T1, arg2: T2): Promise<void>;
5
7
  }
6
8
  //# sourceMappingURL=noop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../helpers/noop.ts"],"names":[],"mappings":"AACA,qBAAa,IAAI;WACA,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGjB,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGlB,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAE/D"}
1
+ {"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../helpers/noop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,qBAAa,IAAI;IACb,MAAM,CAAC,UAAU,EAAE,WAAW,EAAE,CAAM;IACtC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;IAGxB,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE;WAGZ,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAE/D"}
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Noop = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-unused-vars */
5
5
  class Noop {
6
- static async true(arg1) {
6
+ static true(arg1) {
7
7
  return true;
8
8
  }
9
- static async false(arg1) {
9
+ static false(arg1) {
10
10
  return false;
11
11
  }
12
12
  static async call(arg1) { }
13
13
  }
14
14
  exports.Noop = Noop;
15
+ Noop.NoResponse = [];
@@ -0,0 +1,3 @@
1
+ import { TraceId } from '../types/trace';
2
+ export declare function createTrace<T extends object>(traceOwner: T, botName: string, traceName: string): TraceId;
3
+ //# sourceMappingURL=traceFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traceFactory.d.ts","sourceRoot":"","sources":["../../helpers/traceFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EACxC,UAAU,EAAE,CAAC,EACb,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAClB,OAAO,CAET"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTrace = createTrace;
4
+ function createTrace(traceOwner, botName, traceName) {
5
+ return `${traceOwner.constructor.name}:${botName}-${traceName}`;
6
+ }
package/dist/index.d.ts CHANGED
@@ -1,10 +1,13 @@
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';
9
11
  export { MessageType } from './types/messageTypes';
12
+ export { TraceId } from './types/trace';
10
13
  //# sourceMappingURL=index.d.ts.map
@@ -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;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,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,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAQ7C;;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,qGAAqG;IACrG,aAAa,CAAC,EAAE,cAAc,CAAC;IAC/B,2KAA2K;IAC3K,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6DA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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,OA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C,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,KAAK,EAAE,YAAY;IAWjB,KAAK,IAAI,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,9 @@
1
+ import { ILogger } from '../types/logger';
2
+ import { TraceId } from '../types/trace';
3
+ export declare class JsonLogger implements ILogger {
4
+ private serializeError;
5
+ logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: any): void;
6
+ logWithTraceId(botName: string, traceId: TraceId, chatName: string, text: string): void;
7
+ errorWithTraceId<TData>(botName: string, traceId: TraceId, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
8
+ }
9
+ //# 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;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,qBAAa,UAAW,YAAW,OAAO;IAEtC,OAAO,CAAC,cAAc;IAQtB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG;IAQb,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM;IAOhB,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,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;AAEpD,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,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodeTimeoutScheduler = void 0;
4
+ const taskRecord_1 = require("../entities/taskRecord");
5
+ const traceFactory_1 = require("../helpers/traceFactory");
6
+ class NodeTimeoutScheduler {
7
+ constructor(logger) {
8
+ this.activeTasks = [];
9
+ this.logger = logger;
10
+ }
11
+ stopAll() {
12
+ this.activeTasks.forEach((task) => {
13
+ clearInterval(task.taskId);
14
+ });
15
+ }
16
+ createTask(name, action, interval, executeRightAway, ownerName) {
17
+ const taskId = setInterval(action, interval);
18
+ const task = new taskRecord_1.TaskRecord(name, taskId, interval);
19
+ if (executeRightAway) {
20
+ setImmediate(action);
21
+ }
22
+ this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Created task [${taskId}]${name}, that will run every ${interval}ms.`);
23
+ this.activeTasks.push(task);
24
+ }
25
+ createOnetimeTask(name, action, delay, ownerName) {
26
+ const actionWrapper = () => {
27
+ this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Executing delayed oneshot [${taskId}]${name}`);
28
+ action();
29
+ };
30
+ const taskId = setTimeout(actionWrapper, delay);
31
+ this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Created oneshot task [${taskId}]${name}, that will run in ${delay}ms.`);
32
+ }
33
+ }
34
+ 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,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAMrD,qBAAa,kBAAkB;IAC3B,UAAU,UAAS;IACnB,QAAQ,CAAC,YAAY,EAAE,WAAW,EAAE,CAAM;IAE1C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;gBAErB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,cAAc;IAMxE,uBAAuB,CAAC,SAAS,EAAE,WAAW,EAAE;IAM1C,cAAc;YA2BN,WAAW;YAqBX,eAAe;CA8EhC"}
1
+ {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAA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