chz-telegram-bot 0.5.5 → 0.6.9

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 (74) hide show
  1. package/README.md +41 -31
  2. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  3. package/dist/entities/actions/commandAction.js +11 -1
  4. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  5. package/dist/entities/actions/inlineQueryAction.js +9 -1
  6. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
  7. package/dist/entities/actions/replyCaptureAction.js +9 -1
  8. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  9. package/dist/entities/actions/scheduledAction.js +24 -4
  10. package/dist/entities/botInstance.d.ts +3 -4
  11. package/dist/entities/botInstance.d.ts.map +1 -1
  12. package/dist/entities/botInstance.js +13 -11
  13. package/dist/entities/context/baseContext.d.ts +4 -4
  14. package/dist/entities/context/baseContext.d.ts.map +1 -1
  15. package/dist/entities/context/baseContext.js +3 -2
  16. package/dist/eslint.config.d.ts +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -1
  20. package/dist/main.d.ts +0 -3
  21. package/dist/main.d.ts.map +1 -1
  22. package/dist/main.js +1 -2
  23. package/dist/services/actionProcessingService.d.ts +4 -4
  24. package/dist/services/actionProcessingService.d.ts.map +1 -1
  25. package/dist/services/actionProcessingService.js +10 -10
  26. package/dist/services/actionProcessors/baseProcessor.d.ts +3 -3
  27. package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
  28. package/dist/services/actionProcessors/baseProcessor.js +9 -5
  29. package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
  30. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  31. package/dist/services/actionProcessors/commandActionProcessor.js +32 -18
  32. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
  33. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +16 -8
  34. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +2 -2
  35. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
  36. package/dist/services/actionProcessors/scheduledActionProcessor.js +4 -4
  37. package/dist/services/jsonFileStorage.d.ts +3 -1
  38. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  39. package/dist/services/jsonFileStorage.js +26 -2
  40. package/dist/services/nodeTimeoutScheduler.d.ts +3 -3
  41. package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -1
  42. package/dist/services/nodeTimeoutScheduler.js +27 -8
  43. package/dist/services/telegramApi.d.ts +4 -4
  44. package/dist/services/telegramApi.d.ts.map +1 -1
  45. package/dist/services/telegramApi.js +39 -9
  46. package/dist/types/events.d.ts +193 -0
  47. package/dist/types/events.d.ts.map +1 -0
  48. package/dist/types/events.js +69 -0
  49. package/dist/types/logger.d.ts +0 -12
  50. package/dist/types/logger.d.ts.map +1 -1
  51. package/dist/types/logger.js +1 -1
  52. package/entities/actions/commandAction.ts +11 -3
  53. package/entities/actions/inlineQueryAction.ts +9 -1
  54. package/entities/actions/replyCaptureAction.ts +9 -3
  55. package/entities/actions/scheduledAction.ts +31 -10
  56. package/entities/botInstance.ts +18 -25
  57. package/entities/context/baseContext.ts +9 -4
  58. package/index.ts +1 -1
  59. package/main.ts +1 -10
  60. package/package.json +1 -1
  61. package/services/actionProcessingService.ts +11 -15
  62. package/services/actionProcessors/baseProcessor.ts +9 -9
  63. package/services/actionProcessors/commandActionProcessor.ts +35 -46
  64. package/services/actionProcessors/inlineQueryActionProcessor.ts +24 -20
  65. package/services/actionProcessors/scheduledActionProcessor.ts +7 -10
  66. package/services/jsonFileStorage.ts +27 -1
  67. package/services/nodeTimeoutScheduler.ts +22 -22
  68. package/services/telegramApi.ts +53 -23
  69. package/types/events.ts +285 -0
  70. package/dist/services/jsonLogger.d.ts +0 -11
  71. package/dist/services/jsonLogger.d.ts.map +0 -1
  72. package/dist/services/jsonLogger.js +0 -66
  73. package/services/jsonLogger.ts +0 -112
  74. package/types/logger.ts +0 -39
@@ -4,21 +4,19 @@ import { JsonFileStorage } from '../services/jsonFileStorage';
4
4
  import { IActionState } from '../types/actionState';
5
5
  import { CommandAction } from './actions/commandAction';
6
6
  import { ScheduledAction } from './actions/scheduledAction';
7
- import { JsonLogger } from '../services/jsonLogger';
8
- import { ILogger } from '../types/logger';
9
7
  import { IScheduler } from '../types/scheduler';
10
8
  import { NodeTimeoutScheduler } from '../services/nodeTimeoutScheduler';
11
- import { createTrace } from '../helpers/traceFactory';
12
9
  import { InlineQueryAction } from './actions/inlineQueryAction';
13
10
  import { ActionProcessingService } from '../services/actionProcessingService';
11
+ import { BotEventType, TypedEventEmitter } from '../types/events';
14
12
 
15
13
  export class BotInstance {
16
14
  private readonly storage: IStorageClient;
17
15
  private readonly scheduler: IScheduler;
18
- private readonly logger: ILogger;
19
16
  private readonly actionProcessingService: ActionProcessingService;
20
17
 
21
18
  readonly name: string;
19
+ readonly eventEmitter = new TypedEventEmitter();
22
20
 
23
21
  constructor(options: {
24
22
  name: string;
@@ -31,7 +29,6 @@ export class BotInstance {
31
29
  storagePath?: string;
32
30
  services?: {
33
31
  storageClient?: IStorageClient;
34
- logger?: ILogger;
35
32
  scheduler?: IScheduler;
36
33
  };
37
34
  }) {
@@ -42,19 +39,23 @@ export class BotInstance {
42
39
 
43
40
  this.name = options.name;
44
41
 
45
- this.logger = options.services?.logger ?? new JsonLogger();
46
42
  this.scheduler =
47
43
  options.services?.scheduler ??
48
- new NodeTimeoutScheduler(this.logger);
44
+ new NodeTimeoutScheduler(this.eventEmitter);
49
45
  this.storage =
50
46
  options.services?.storageClient ??
51
- new JsonFileStorage(options.name, actions, options.storagePath);
47
+ new JsonFileStorage(
48
+ options.name,
49
+ actions,
50
+ this.eventEmitter,
51
+ options.storagePath
52
+ );
52
53
  this.actionProcessingService = new ActionProcessingService(
53
54
  this.name,
54
55
  options.chats,
55
56
  this.storage,
56
57
  this.scheduler,
57
- this.logger
58
+ this.eventEmitter
58
59
  );
59
60
  }
60
61
 
@@ -65,31 +66,23 @@ export class BotInstance {
65
66
  scheduled: ScheduledAction<IActionState>[];
66
67
  inlineQueries: InlineQueryAction[];
67
68
  },
68
- scheduledPeriod?: Seconds,
69
- verboseLoggingForIncomingMessage?: boolean
69
+ scheduledPeriod?: Seconds
70
70
  ) {
71
- this.logger.logWithTraceId(
72
- this.name,
73
- createTrace(this, this.name, 'Start'),
74
- 'System',
75
- 'Starting bot...'
76
- );
71
+ this.eventEmitter.emit(BotEventType.botStarting, {
72
+ botName: this.name
73
+ });
77
74
 
78
75
  await this.actionProcessingService.initialize(
79
76
  token,
80
77
  actions,
81
- scheduledPeriod,
82
- verboseLoggingForIncomingMessage
78
+ scheduledPeriod
83
79
  );
84
80
  }
85
81
 
86
82
  async stop() {
87
- this.logger.logWithTraceId(
88
- this.name,
89
- createTrace(this, this.name, 'Stop'),
90
- 'System',
91
- 'Stopping bot...'
92
- );
83
+ this.eventEmitter.emit(BotEventType.botStopping, {
84
+ botName: this.name
85
+ });
93
86
 
94
87
  this.scheduler.stopAll();
95
88
  await this.storage.close();
@@ -3,7 +3,7 @@ import { IAction, IActionWithState } from '../../types/action';
3
3
  import { IActionState } from '../../types/actionState';
4
4
  import { ICaptureController } from '../../types/capture';
5
5
  import { CommandTrigger } from '../../types/commandTrigger';
6
- import { IScopedLogger } from '../../types/logger';
6
+ import { TypedEventEmitter } from '../../types/events';
7
7
  import { BotResponse, IReplyResponse } from '../../types/response';
8
8
  import { IScheduler } from '../../types/scheduler';
9
9
  import { IStorageClient } from '../../types/storage';
@@ -15,7 +15,7 @@ export type BaseContextPropertiesToOmit =
15
15
  | 'isInitialized'
16
16
  | 'storage'
17
17
  | 'scheduler'
18
- | 'logger'
18
+ | 'eventEmitter'
19
19
  | 'responses'
20
20
  | 'traceId';
21
21
 
@@ -29,7 +29,7 @@ export abstract class BaseContextInternal<TAction extends IAction> {
29
29
  readonly storage: IStorageClient;
30
30
  /** Scheduler instance for the bot executing this action */
31
31
  readonly scheduler: IScheduler;
32
- logger!: IScopedLogger;
32
+ readonly eventEmitter: TypedEventEmitter;
33
33
  /** Trace id of a action execution. */
34
34
  traceId!: TraceId;
35
35
  /** Name of a bot that executes this action. */
@@ -48,9 +48,14 @@ export abstract class BaseContextInternal<TAction extends IAction> {
48
48
  this._responses = value;
49
49
  }
50
50
 
51
- constructor(storage: IStorageClient, scheduler: IScheduler) {
51
+ constructor(
52
+ storage: IStorageClient,
53
+ scheduler: IScheduler,
54
+ eventEmitter: TypedEventEmitter
55
+ ) {
52
56
  this.storage = storage;
53
57
  this.scheduler = scheduler;
58
+ this.eventEmitter = eventEmitter;
54
59
  }
55
60
 
56
61
  protected createCaptureController(
package/index.ts CHANGED
@@ -9,7 +9,6 @@ export * from './helpers/timeConvertions';
9
9
  export * from './types/action';
10
10
  export * from './types/externalAliases';
11
11
  export * from './types/storage';
12
- export * from './types/logger';
13
12
  export * from './types/scheduler';
14
13
  export { CommandAction } from './entities/actions/commandAction';
15
14
  export { InlineQueryAction } from './entities/actions/inlineQueryAction';
@@ -24,3 +23,4 @@ export { ICaptureController } from './types/capture';
24
23
  export { ChatInfo } from './dtos/chatInfo';
25
24
  export { MessageInfo } from './dtos/messageInfo';
26
25
  export { UserInfo } from './dtos/userInfo';
26
+ export { BotEventType } from './types/events';
package/main.ts CHANGED
@@ -5,7 +5,6 @@ import { ScheduledAction } from './entities/actions/scheduledAction';
5
5
  import { BotInstance } from './entities/botInstance';
6
6
  import { Seconds } from './types/timeValues';
7
7
  import { IScheduler } from './types/scheduler';
8
- import { ILogger } from './types/logger';
9
8
  import { InlineQueryAction } from './entities/actions/inlineQueryAction';
10
9
  import { IActionState } from './types/actionState';
11
10
 
@@ -39,8 +38,6 @@ class BotOrchestrator {
39
38
  services?: {
40
39
  /** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
41
40
  storageClient?: IStorageClient;
42
- /** Logger client for bot logging. If not provided, default `JsonFileStorage` will be used. */
43
- logger?: ILogger;
44
41
  /** Scheduler client for bot scheduling. If not provided, default `NodeTimeoutScheduler` will be used. */
45
42
  scheduler?: IScheduler;
46
43
  };
@@ -53,18 +50,12 @@ class BotOrchestrator {
53
50
  chats: options.chats,
54
51
  services: {
55
52
  storageClient: options.services?.storageClient,
56
- logger: options.services?.logger,
57
53
  scheduler: options.services?.scheduler
58
54
  },
59
55
  storagePath: options.storagePath
60
56
  });
61
57
 
62
- await bot.start(
63
- token,
64
- options.actions,
65
- options.scheduledPeriod,
66
- options.verboseLoggingForIncomingMessage
67
- );
58
+ await bot.start(token, options.actions, options.scheduledPeriod);
68
59
  this.bots.push(bot);
69
60
 
70
61
  return bot;
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "type": "git",
15
15
  "url": "https://github.com/AlexSolari/botFramework.git"
16
16
  },
17
- "version": "0.5.5",
17
+ "version": "0.6.9",
18
18
  "type": "module",
19
19
  "dependencies": {
20
20
  "async-sema": "^3.1.1",
@@ -1,6 +1,5 @@
1
1
  import { hoursToSeconds } from '../helpers/timeConvertions';
2
2
  import { Seconds, Milliseconds, Hours } from '../types/timeValues';
3
- import { ILogger } from '../types/logger';
4
3
  import { IScheduler } from '../types/scheduler';
5
4
  import { IStorageClient } from '../types/storage';
6
5
  import { TelegramApiService } from './telegramApi';
@@ -14,11 +13,11 @@ import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryAction
14
13
  import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
15
14
  import { TelegramBot } from '../types/externalAliases';
16
15
  import { Telegraf } from 'telegraf';
16
+ import { TypedEventEmitter } from '../types/events';
17
17
 
18
18
  export class ActionProcessingService {
19
+ private readonly eventEmitter: TypedEventEmitter;
19
20
  private readonly storage: IStorageClient;
20
- private readonly logger: ILogger;
21
-
22
21
  private readonly commandProcessor: CommandActionProcessor;
23
22
  private readonly scheduledProcessor: ScheduledActionProcessor;
24
23
  private readonly inlineQueryProcessor: InlineQueryActionProcessor;
@@ -32,29 +31,29 @@ export class ActionProcessingService {
32
31
  chats: Record<string, number>,
33
32
  storage: IStorageClient,
34
33
  scheduler: IScheduler,
35
- logger: ILogger
34
+ eventEmitter: TypedEventEmitter
36
35
  ) {
37
36
  this.storage = storage;
38
- this.logger = logger;
37
+ this.eventEmitter = eventEmitter;
39
38
 
40
39
  this.commandProcessor = new CommandActionProcessor(
41
40
  botName,
42
41
  storage,
43
42
  scheduler,
44
- logger
43
+ this.eventEmitter
45
44
  );
46
45
  this.scheduledProcessor = new ScheduledActionProcessor(
47
46
  botName,
48
47
  chats,
49
48
  storage,
50
49
  scheduler,
51
- logger
50
+ this.eventEmitter
52
51
  );
53
52
  this.inlineQueryProcessor = new InlineQueryActionProcessor(
54
53
  botName,
55
54
  storage,
56
55
  scheduler,
57
- logger
56
+ this.eventEmitter
58
57
  );
59
58
 
60
59
  this.botName = botName;
@@ -67,21 +66,19 @@ export class ActionProcessingService {
67
66
  scheduled: ScheduledAction<IActionState>[];
68
67
  inlineQueries: InlineQueryAction[];
69
68
  },
70
- scheduledPeriod?: Seconds,
71
- verboseLoggingForIncomingMessage?: boolean
69
+ scheduledPeriod?: Seconds
72
70
  ) {
73
71
  this.telegramBot = new Telegraf(token);
74
72
  const api = new TelegramApiService(
75
73
  this.botName,
76
74
  this.telegramBot.telegram,
77
75
  this.storage,
78
- this.logger,
79
- (capture, id, chatInfo, traceId) => {
76
+ this.eventEmitter,
77
+ (capture, id, chatInfo) => {
80
78
  this.commandProcessor.captureRegistrationCallback(
81
79
  capture,
82
80
  id,
83
- chatInfo,
84
- traceId
81
+ chatInfo
85
82
  );
86
83
  }
87
84
  );
@@ -104,7 +101,6 @@ export class ActionProcessingService {
104
101
  api,
105
102
  this.telegramBot,
106
103
  commandActions,
107
- verboseLoggingForIncomingMessage ?? false,
108
104
  botInfo
109
105
  );
110
106
  this.inlineQueryProcessor.initialize(
@@ -1,14 +1,14 @@
1
- import { ILogger } from '../../types/logger';
2
1
  import { IScheduler } from '../../types/scheduler';
3
2
  import { IStorageClient } from '../../types/storage';
4
3
  import { TelegramApiService } from '../telegramApi';
5
4
  import { IAction } from '../../types/action';
6
5
  import { BaseContextInternal } from '../../entities/context/baseContext';
6
+ import { BotEventType, TypedEventEmitter } from '../../types/events';
7
7
 
8
8
  export abstract class BaseActionProcessor {
9
9
  protected readonly storage: IStorageClient;
10
10
  protected readonly scheduler: IScheduler;
11
- protected readonly logger: ILogger;
11
+ protected readonly eventEmitter: TypedEventEmitter;
12
12
 
13
13
  protected readonly botName: string;
14
14
 
@@ -18,20 +18,17 @@ export abstract class BaseActionProcessor {
18
18
  botName: string,
19
19
  storage: IStorageClient,
20
20
  scheduler: IScheduler,
21
- logger: ILogger
21
+ eventEmitter: TypedEventEmitter
22
22
  ) {
23
23
  this.storage = storage;
24
24
  this.scheduler = scheduler;
25
- this.logger = logger;
25
+ this.eventEmitter = eventEmitter;
26
26
 
27
27
  this.botName = botName;
28
28
  }
29
29
 
30
- private defaultErrorHandler<TAction extends IAction>(
31
- error: Error,
32
- ctx: BaseContextInternal<TAction>
33
- ) {
34
- ctx.logger.errorWithTraceId(error, ctx);
30
+ private defaultErrorHandler(error: Error) {
31
+ this.eventEmitter.emit(BotEventType.error, { error });
35
32
  }
36
33
 
37
34
  initializeDependencies(api: TelegramApiService) {
@@ -52,6 +49,9 @@ export abstract class BaseActionProcessor {
52
49
  ctx.isInitialized = false;
53
50
  } catch (error) {
54
51
  (errorHandler ?? this.defaultErrorHandler)(error as Error, ctx);
52
+ this.eventEmitter.emit(BotEventType.error, {
53
+ error: error as Error
54
+ });
55
55
  }
56
56
  }
57
57
  }
@@ -6,7 +6,6 @@ import { ReplyContextInternal } from '../../entities/context/replyContext';
6
6
  import { IActionState } from '../../types/actionState';
7
7
  import { TelegramApiService } from '../telegramApi';
8
8
  import { IReplyCapture } from '../../types/capture';
9
- import { TraceId } from '../../types/trace';
10
9
  import { ChatInfo } from '../../dtos/chatInfo';
11
10
  import {
12
11
  INTERNAL_MESSAGE_TYPE_PREFIX,
@@ -19,6 +18,7 @@ import { MessageInfo } from '../../dtos/messageInfo';
19
18
  import { UserInfo } from '../../dtos/userInfo';
20
19
  import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
21
20
  import { BotInfo, TelegramBot } from '../../types/externalAliases';
21
+ import { BotEventType } from '../../types/events';
22
22
 
23
23
  const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
24
24
 
@@ -38,7 +38,6 @@ export class CommandActionProcessor extends BaseActionProcessor {
38
38
  api: TelegramApiService,
39
39
  telegram: TelegramBot,
40
40
  commands: CommandAction<IActionState>[],
41
- verboseLoggingForIncomingMessage: boolean,
42
41
  botInfo: BotInfo
43
42
  ) {
44
43
  this.botInfo = botInfo;
@@ -69,30 +68,24 @@ export class CommandActionProcessor extends BaseActionProcessor {
69
68
  }
70
69
 
71
70
  if (commands.length > 0) {
72
- telegram.on('message', ({ message }) => {
71
+ telegram.on('message', async ({ message }) => {
73
72
  const internalMessage = new IncomingMessage(
74
73
  message,
75
74
  this.botName,
76
75
  getOrSetIfNotExists(this.chatHistory, message.chat.id, [])
77
76
  );
78
77
 
79
- const logger = this.logger.createScope(
80
- this.botName,
81
- internalMessage.traceId,
82
- internalMessage.chatInfo.name
83
- );
78
+ this.eventEmitter.emit(BotEventType.messageRecieved, {
79
+ botInfo: this.botInfo,
80
+ message: internalMessage
81
+ });
84
82
 
85
- if (verboseLoggingForIncomingMessage) {
86
- logger.logObjectWithTraceId(message);
87
- } else {
88
- logger.logWithTraceId(
89
- `${internalMessage.from?.first_name ?? 'Unknown'} (${
90
- internalMessage.from?.id ?? 'Unknown'
91
- }): ${internalMessage.text || internalMessage.type}`
92
- );
93
- }
83
+ await this.processMessage(internalMessage);
94
84
 
95
- void this.processMessage(internalMessage);
85
+ this.eventEmitter.emit(BotEventType.messageProcessingFinished, {
86
+ botInfo: this.botInfo,
87
+ message: internalMessage
88
+ });
96
89
  });
97
90
  }
98
91
  }
@@ -100,8 +93,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
100
93
  captureRegistrationCallback(
101
94
  capture: IReplyCapture,
102
95
  parentMessageId: number,
103
- chatInfo: ChatInfo,
104
- traceId: TraceId
96
+ chatInfo: ChatInfo
105
97
  ) {
106
98
  const replyAction = new ReplyCaptureAction(
107
99
  parentMessageId,
@@ -111,15 +103,10 @@ export class CommandActionProcessor extends BaseActionProcessor {
111
103
  capture.abortController
112
104
  );
113
105
 
114
- const logger = this.logger.createScope(
115
- this.botName,
116
- traceId,
117
- chatInfo.name
118
- );
119
-
120
- logger.logWithTraceId(
121
- `Starting capturing replies to message ${parentMessageId} with action ${replyAction.key}`
122
- );
106
+ this.eventEmitter.emit(BotEventType.commandActionCaptureStarted, {
107
+ parentMessageId,
108
+ chatInfo
109
+ });
123
110
 
124
111
  this.replyCaptures.push(replyAction);
125
112
 
@@ -127,13 +114,19 @@ export class CommandActionProcessor extends BaseActionProcessor {
127
114
  const index = this.replyCaptures.indexOf(replyAction);
128
115
  this.replyCaptures.splice(index, 1);
129
116
 
130
- logger.logWithTraceId(
131
- `Stopping capturing replies to message ${parentMessageId} with action ${replyAction.key}`
132
- );
117
+ this.eventEmitter.emit(BotEventType.commandActionCaptureAborted, {
118
+ parentMessageId,
119
+ chatInfo
120
+ });
133
121
  });
134
122
  }
135
123
 
136
124
  private async processMessage(msg: IncomingMessage) {
125
+ this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
126
+ botInfo: this.botInfo,
127
+ message: msg
128
+ });
129
+
137
130
  const chatHistoryArray = getOrSetIfNotExists(
138
131
  this.chatHistory,
139
132
  msg.chatInfo.id,
@@ -142,6 +135,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
142
135
 
143
136
  while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
144
137
  chatHistoryArray.shift();
138
+
145
139
  chatHistoryArray.push(
146
140
  new ChatHistoryMessage(
147
141
  msg.messageId,
@@ -156,7 +150,8 @@ export class CommandActionProcessor extends BaseActionProcessor {
156
150
 
157
151
  const ctx = new MessageContextInternal<IActionState>(
158
152
  this.storage,
159
- this.scheduler
153
+ this.scheduler,
154
+ this.eventEmitter
160
155
  );
161
156
 
162
157
  const commandsToCheck = new Set(this.commands[msg.type]);
@@ -166,6 +161,11 @@ export class CommandActionProcessor extends BaseActionProcessor {
166
161
  }
167
162
  }
168
163
 
164
+ this.eventEmitter.emit(BotEventType.beforeActionsExecuting, {
165
+ botInfo: this.botInfo,
166
+ message: msg,
167
+ commands: commandsToCheck
168
+ });
169
169
  for (const commandAction of commandsToCheck) {
170
170
  this.initializeMessageContext(ctx, commandAction, msg);
171
171
  await this.executeAction(commandAction, ctx);
@@ -174,7 +174,8 @@ export class CommandActionProcessor extends BaseActionProcessor {
174
174
  if (this.replyCaptures.length != 0) {
175
175
  const replyCtx = new ReplyContextInternal<IActionState>(
176
176
  this.storage,
177
- this.scheduler
177
+ this.scheduler,
178
+ this.eventEmitter
178
179
  );
179
180
 
180
181
  for (const replyAction of this.replyCaptures) {
@@ -211,12 +212,6 @@ export class CommandActionProcessor extends BaseActionProcessor {
211
212
 
212
213
  ctx.isInitialized = true;
213
214
  ctx.matchResults = [];
214
-
215
- ctx.logger = this.logger.createScope(
216
- this.botName,
217
- message.traceId,
218
- message.chatInfo.name
219
- );
220
215
  }
221
216
 
222
217
  private initializeMessageContext(
@@ -247,11 +242,5 @@ export class CommandActionProcessor extends BaseActionProcessor {
247
242
  ctx.traceId = message.traceId;
248
243
  ctx.botInfo = this.botInfo;
249
244
  ctx.customCooldown = undefined;
250
-
251
- ctx.logger = this.logger.createScope(
252
- this.botName,
253
- message.traceId,
254
- message.chatInfo.name
255
- );
256
245
  }
257
246
  }
@@ -2,6 +2,7 @@ import { IncomingInlineQuery } from '../../dtos/incomingQuery';
2
2
  import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
3
3
  import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
4
4
  import { createTrace } from '../../helpers/traceFactory';
5
+ import { BotEventType } from '../../types/events';
5
6
  import { TelegramBot } from '../../types/externalAliases';
6
7
  import { Milliseconds } from '../../types/timeValues';
7
8
  import { TraceId } from '../../types/trace';
@@ -33,24 +34,20 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
33
34
  createTrace('InlineQuery', this.botName, inlineQuery.id)
34
35
  );
35
36
 
36
- const logger = this.logger.createScope(
37
- this.botName,
38
- query.traceId,
39
- 'Query'
40
- );
41
-
42
- logger.logWithTraceId(
43
- `${inlineQuery.from.username ?? 'Unknown'} (${
44
- inlineQuery.from.id
45
- }): Query for ${inlineQuery.query}`
46
- );
37
+ this.eventEmitter.emit(BotEventType.inlineProcessingStarted, {
38
+ query
39
+ });
47
40
 
48
41
  const queryBeingProcessed = queriesInProcessing.get(
49
42
  query.userId
50
43
  );
51
44
  if (queryBeingProcessed) {
52
- logger.logWithTraceId(
53
- `Aborting query ${queryBeingProcessed.queryId} (${queryBeingProcessed.query}): new query recieved from ${query.userId}`
45
+ this.eventEmitter.emit(
46
+ BotEventType.inlineProcessingAborting,
47
+ {
48
+ newQuery: query,
49
+ abortedQuery: queryBeingProcessed
50
+ }
54
51
  );
55
52
 
56
53
  queryBeingProcessed.abortController.abort();
@@ -69,7 +66,8 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
69
66
  async () => {
70
67
  const ctx = new InlineQueryContextInternal(
71
68
  this.storage,
72
- this.scheduler
69
+ this.scheduler,
70
+ this.eventEmitter
73
71
  );
74
72
 
75
73
  const queriesToProcess = [...pendingInlineQueries];
@@ -94,13 +92,21 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
94
92
  await this.executeAction(
95
93
  inlineQueryAction,
96
94
  ctx,
97
- (error, ctx) => {
95
+ (error, _) => {
98
96
  if (error.name == 'AbortError') {
99
- ctx.logger.logWithTraceId(
100
- `Aborting query ${inlineQuery.queryId} (${inlineQuery.query}) successful.`
97
+ this.eventEmitter.emit(
98
+ BotEventType.inlineProcessingAborted,
99
+ {
100
+ abortedQuery: inlineQuery
101
+ }
101
102
  );
102
103
  } else {
103
- ctx.logger.errorWithTraceId(error, ctx);
104
+ this.eventEmitter.emit(
105
+ BotEventType.error,
106
+ {
107
+ error
108
+ }
109
+ );
104
110
  }
105
111
  }
106
112
  );
@@ -136,7 +142,5 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
136
142
  ctx.isInitialized = true;
137
143
  ctx.queryResults = [];
138
144
  ctx.matchResults = [];
139
-
140
- ctx.logger = this.logger.createScope(this.botName, traceId, 'Unknown');
141
145
  }
142
146
  }
@@ -5,13 +5,13 @@ import { ChatContextInternal } from '../../entities/context/chatContext';
5
5
  import { secondsToMilliseconds } from '../../helpers/timeConvertions';
6
6
  import { createTrace } from '../../helpers/traceFactory';
7
7
  import { IActionState } from '../../types/actionState';
8
- import { ILogger } from '../../types/logger';
9
8
  import { IScheduler } from '../../types/scheduler';
10
9
  import { IStorageClient } from '../../types/storage';
11
10
  import { Seconds, Milliseconds } from '../../types/timeValues';
12
11
  import { TraceId } from '../../types/trace';
13
12
  import { TelegramApiService } from '../telegramApi';
14
13
  import { BaseActionProcessor } from './baseProcessor';
14
+ import { TypedEventEmitter } from '../../types/events';
15
15
 
16
16
  export class ScheduledActionProcessor extends BaseActionProcessor {
17
17
  private readonly chats: Record<string, number>;
@@ -23,9 +23,9 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
23
23
  chats: Record<string, number>,
24
24
  storage: IStorageClient,
25
25
  scheduler: IScheduler,
26
- logger: ILogger
26
+ eventEmitter: TypedEventEmitter
27
27
  ) {
28
- super(botName, storage, scheduler, logger);
28
+ super(botName, storage, scheduler, eventEmitter);
29
29
  this.chats = chats;
30
30
  }
31
31
 
@@ -77,13 +77,16 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
77
77
  delay as Milliseconds,
78
78
  this.botName
79
79
  );
80
+
81
+ void this.runScheduled();
80
82
  }
81
83
  }
82
84
 
83
85
  private async runScheduled() {
84
86
  const ctx = new ChatContextInternal<IActionState>(
85
87
  this.storage,
86
- this.scheduler
88
+ this.scheduler,
89
+ this.eventEmitter
87
90
  );
88
91
 
89
92
  for (const [chatName, chatId] of Object.entries(this.chats)) {
@@ -118,11 +121,5 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
118
121
  ctx.action = action;
119
122
  ctx.chatInfo = chatInfo;
120
123
  ctx.traceId = traceId;
121
-
122
- ctx.logger = this.logger.createScope(
123
- this.botName,
124
- traceId,
125
- chatInfo.name
126
- );
127
124
  }
128
125
  }