chz-telegram-bot 0.7.13 → 0.7.15

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 (98) hide show
  1. package/dist/dtos/propertyProviderSets.d.ts +0 -1
  2. package/dist/dtos/propertyProviderSets.d.ts.map +1 -1
  3. package/dist/entities/actions/commandAction.d.ts +1 -1
  4. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  5. package/dist/entities/actions/commandAction.js +2 -2
  6. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  7. package/dist/helpers/builders/commandActionBuilder.js +2 -3
  8. package/package.json +4 -1
  9. package/eslint.config.ts +0 -62
  10. package/src/builtin/helpAction.ts +0 -17
  11. package/src/dtos/chatHistoryMessage.ts +0 -22
  12. package/src/dtos/chatInfo.ts +0 -12
  13. package/src/dtos/commandTriggerCheckResult.ts +0 -40
  14. package/src/dtos/cooldownInfo.ts +0 -10
  15. package/src/dtos/incomingMessage.ts +0 -71
  16. package/src/dtos/incomingQuery.ts +0 -14
  17. package/src/dtos/messageInfo.ts +0 -15
  18. package/src/dtos/propertyProviderSets.ts +0 -21
  19. package/src/dtos/replyInfo.ts +0 -9
  20. package/src/dtos/responses/delay.ts +0 -28
  21. package/src/dtos/responses/imageMessage.ts +0 -41
  22. package/src/dtos/responses/inlineQueryResponse.ts +0 -26
  23. package/src/dtos/responses/reaction.ts +0 -30
  24. package/src/dtos/responses/textMessage.ts +0 -44
  25. package/src/dtos/responses/unpin.ts +0 -27
  26. package/src/dtos/responses/videoMessage.ts +0 -41
  27. package/src/dtos/userInfo.ts +0 -8
  28. package/src/entities/actions/commandAction.ts +0 -275
  29. package/src/entities/actions/inlineQueryAction.ts +0 -83
  30. package/src/entities/actions/replyCaptureAction.ts +0 -110
  31. package/src/entities/actions/scheduledAction.ts +0 -182
  32. package/src/entities/botInstance.ts +0 -92
  33. package/src/entities/cachedStateFactory.ts +0 -14
  34. package/src/entities/context/baseContext.ts +0 -111
  35. package/src/entities/context/chatContext.ts +0 -135
  36. package/src/entities/context/inlineQueryContext.ts +0 -63
  37. package/src/entities/context/messageContext.ts +0 -250
  38. package/src/entities/context/replyContext.ts +0 -260
  39. package/src/entities/states/actionStateBase.ts +0 -6
  40. package/src/entities/taskRecord.ts +0 -11
  41. package/src/helpers/builders/commandActionBuilder.ts +0 -214
  42. package/src/helpers/builders/inlineQueryActionBuilder.ts +0 -71
  43. package/src/helpers/builders/scheduledActionBuilder.ts +0 -143
  44. package/src/helpers/mapUtils.ts +0 -28
  45. package/src/helpers/noop.ts +0 -20
  46. package/src/helpers/objectFromEntries.ts +0 -7
  47. package/src/helpers/timeConvertions.ts +0 -13
  48. package/src/helpers/toArray.ts +0 -3
  49. package/src/helpers/traceFactory.ts +0 -11
  50. package/src/index.ts +0 -33
  51. package/src/main.ts +0 -76
  52. package/src/services/actionProcessingService.ts +0 -125
  53. package/src/services/actionProcessors/baseProcessor.ts +0 -67
  54. package/src/services/actionProcessors/commandActionProcessor.ts +0 -231
  55. package/src/services/actionProcessors/inlineQueryActionProcessor.ts +0 -165
  56. package/src/services/actionProcessors/scheduledActionProcessor.ts +0 -136
  57. package/src/services/jsonFileStorage.ts +0 -181
  58. package/src/services/nodeTimeoutScheduler.ts +0 -79
  59. package/src/services/responseProcessingQueue.ts +0 -57
  60. package/src/services/telegramApi.ts +0 -278
  61. package/src/types/action.ts +0 -15
  62. package/src/types/actionState.ts +0 -4
  63. package/src/types/cachedValueAccessor.ts +0 -1
  64. package/src/types/capture.ts +0 -33
  65. package/src/types/commandCondition.ts +0 -9
  66. package/src/types/commandTrigger.ts +0 -1
  67. package/src/types/events.ts +0 -286
  68. package/src/types/externalAliases.ts +0 -18
  69. package/src/types/handlers.ts +0 -26
  70. package/src/types/inputFile.ts +0 -4
  71. package/src/types/messageSendingOptions.ts +0 -10
  72. package/src/types/messageTypes.ts +0 -21
  73. package/src/types/propertyProvider.ts +0 -14
  74. package/src/types/response.ts +0 -51
  75. package/src/types/scheduler.ts +0 -20
  76. package/src/types/storage.ts +0 -23
  77. package/src/types/timeValues.ts +0 -33
  78. package/src/types/trace.ts +0 -5
  79. package/tests/dtos/commandTriggerCheckResult.test.ts +0 -301
  80. package/tests/entities/actions/inlineQueryAction.test.ts +0 -359
  81. package/tests/entities/actions/replyCaptureAction.test.ts +0 -501
  82. package/tests/entities/cachedStateFactory.test.ts +0 -98
  83. package/tests/entities/context/chatContext.test.ts +0 -606
  84. package/tests/entities/context/messageContext.test.ts +0 -370
  85. package/tests/entities/states/actionStateBase.test.ts +0 -138
  86. package/tests/entities/taskRecord.test.ts +0 -195
  87. package/tests/helpers/mapUtils.test.ts +0 -163
  88. package/tests/helpers/timeConvertions.test.ts +0 -129
  89. package/tests/services/actionProcessors/baseActionProcessor.test.ts +0 -359
  90. package/tests/services/actionProcessors/commandActionProcessor.test.ts +0 -268
  91. package/tests/services/actionProcessors/inlineQueryActionProcessor.test.ts +0 -616
  92. package/tests/services/actionProcessors/processorTestHelpers.ts +0 -147
  93. package/tests/services/actionProcessors/scheduledActionProcessor.test.ts +0 -153
  94. package/tests/services/jsonFileStorage.test.ts +0 -927
  95. package/tests/services/nodeTimeoutScheduler.test.ts +0 -421
  96. package/tests/services/responseProcessingQueue.test.ts +0 -388
  97. package/tsconfig.build.json +0 -8
  98. package/tsconfig.json +0 -118
@@ -1,143 +0,0 @@
1
- import { ScheduledAction } from '../../entities/actions/scheduledAction';
2
- import { CachedStateFactory } from '../../entities/cachedStateFactory';
3
- import { ActionStateBase } from '../../entities/states/actionStateBase';
4
- import { IActionState } from '../../types/actionState';
5
- import { ScheduledHandler } from '../../types/handlers';
6
- import { ScheduledActionPropertyProvider } from '../../types/propertyProvider';
7
- import { Hours, HoursOfDay } from '../../types/timeValues';
8
- import { Noop } from '../noop';
9
-
10
- /**
11
- * Builder for `ScheduledAction` with state represented by `TActionState`
12
- */
13
- export class ScheduledActionBuilderWithState<
14
- TActionState extends IActionState
15
- > {
16
- private readonly name: string;
17
- private readonly cachedStateFactories = new Map<
18
- string,
19
- CachedStateFactory
20
- >();
21
- private readonly stateConstructor: () => TActionState;
22
- private handler: ScheduledHandler<TActionState> = Noop.call;
23
-
24
- private whitelistProvider: ScheduledActionPropertyProvider<number[]> =
25
- () => [];
26
- private activeProvider: ScheduledActionPropertyProvider<boolean> = () =>
27
- true;
28
- private timeinHoursProvider: ScheduledActionPropertyProvider<HoursOfDay> =
29
- () => 0;
30
-
31
- /**
32
- * Builder for `ScheduledAction` with state represented by `TActionState`
33
- * @param name Action name, will be used for logging and storage
34
- * @param stateConstructor Function that creates default state object
35
- */
36
- constructor(name: string, stateConstructor: () => TActionState) {
37
- this.name = name;
38
- this.stateConstructor = stateConstructor;
39
- }
40
-
41
- /**
42
- * Sets whitelist for this action.
43
- * @param chatIds Chat ids to execute in.
44
- */
45
- in(chatIds: number[]) {
46
- this.whitelistProvider = () => chatIds;
47
-
48
- return this;
49
- }
50
-
51
- /**
52
- * Defines time for scheduled item execution.
53
- * @param time Time of day (0 - 23) to execute action.
54
- */
55
- runAt(time: HoursOfDay) {
56
- this.timeinHoursProvider = () => time;
57
-
58
- return this;
59
- }
60
-
61
- /** Defines action logic itself, will be executed on timer.
62
- * @param handler Callback that will be called on timer.
63
- */
64
- do(handler: ScheduledHandler<TActionState>) {
65
- this.handler = handler;
66
-
67
- return this;
68
- }
69
-
70
- /**
71
- * Defines process-wide cache, that is shared by all actions of this type (even in different bot instances).
72
- * Can be used for fetch request de-duping, etc.
73
- * @param key Key that will be used to retrieve value from cache.
74
- * @param itemFactory Callback that will be executed once to create cached value.
75
- * @param invalidationTimeoutInHours Timeout for cache invalidation.
76
- */
77
- withSharedCache(
78
- key: string,
79
- itemFactory: () => Promise<unknown>,
80
- invalidationTimeoutInHours: Hours = 20 as Hours
81
- ) {
82
- this.cachedStateFactories.set(
83
- key,
84
- new CachedStateFactory(itemFactory, invalidationTimeoutInHours)
85
- );
86
-
87
- return this;
88
- }
89
-
90
- /** If called during building, action is marked as disabled and never checked. */
91
- disabled() {
92
- this.activeProvider = () => false;
93
-
94
- return this;
95
- }
96
-
97
- /**
98
- * Configures action to use property value providers instead of static value to allow changes in runtime
99
- */
100
- withConfiguration(configuration: {
101
- timeinHoursProvider?: ScheduledActionPropertyProvider<HoursOfDay>;
102
- isActiveProvider?: ScheduledActionPropertyProvider<boolean>;
103
- chatsWhitelistProvider?: ScheduledActionPropertyProvider<number[]>;
104
- }) {
105
- if (configuration.chatsWhitelistProvider)
106
- this.whitelistProvider = configuration.chatsWhitelistProvider;
107
-
108
- if (configuration.timeinHoursProvider)
109
- this.timeinHoursProvider = configuration.timeinHoursProvider;
110
-
111
- if (configuration.isActiveProvider)
112
- this.activeProvider = configuration.isActiveProvider;
113
-
114
- return this;
115
- }
116
-
117
- /** Builds action */
118
- build() {
119
- return new ScheduledAction<TActionState>(
120
- this.name,
121
- this.handler,
122
- {
123
- chatsWhitelistProvider: this.whitelistProvider,
124
- isActiveProvider: this.activeProvider,
125
- timeinHoursProvider: this.timeinHoursProvider
126
- },
127
- this.cachedStateFactories,
128
- this.stateConstructor
129
- );
130
- }
131
- }
132
-
133
- /**
134
- * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
135
- */
136
- export class ScheduledActionBuilder extends ScheduledActionBuilderWithState<ActionStateBase> {
137
- /**
138
- * Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
139
- */
140
- constructor(name: string) {
141
- super(name, () => new ActionStateBase());
142
- }
143
- }
@@ -1,28 +0,0 @@
1
- type KeyedReadonlyCollection<K, V> = { get: (key: K) => V | undefined };
2
- type KeyedWriteableCollection<K, V> = KeyedReadonlyCollection<K, V> & {
3
- set: (key: K, value: V) => KeyedWriteableCollection<K, V>;
4
- };
5
-
6
- export function getOrSetIfNotExists<K, V>(
7
- map: KeyedWriteableCollection<K, V>,
8
- key: K,
9
- fallback: V
10
- ) {
11
- const existingValue = map.get(key);
12
- if (existingValue) return existingValue;
13
-
14
- map.set(key, fallback);
15
-
16
- return fallback;
17
- }
18
-
19
- export function getOrThrow<K, V>(
20
- map: KeyedReadonlyCollection<K, V>,
21
- key: K,
22
- error: string = 'Key not found in collection'
23
- ) {
24
- const existingValue = map.get(key);
25
- if (existingValue) return existingValue;
26
-
27
- throw new Error(error);
28
- }
@@ -1,20 +0,0 @@
1
- import { BotResponse } from '../types/response';
2
-
3
- /* eslint-disable @typescript-eslint/no-unused-vars */
4
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
5
- export class Noop {
6
- static readonly NoResponse: BotResponse[] = [];
7
- static true(arg1: unknown) {
8
- return true;
9
- }
10
- static false(arg1: unknown) {
11
- return false;
12
- }
13
- static emptyString() {
14
- return '';
15
- }
16
- static async call(arg1: unknown): Promise<void>;
17
- static async call(arg1: unknown, arg2?: unknown) {
18
- return Promise.resolve();
19
- }
20
- }
@@ -1,7 +0,0 @@
1
- export const typeSafeObjectFromEntries = <
2
- const T extends ReadonlyArray<readonly [PropertyKey, unknown]>
3
- >(
4
- entries: T
5
- ): { [K in T[number] as K[0]]: K[1] } => {
6
- return Object.fromEntries(entries) as { [K in T[number] as K[0]]: K[1] };
7
- };
@@ -1,13 +0,0 @@
1
- import { Hours, Milliseconds, Seconds } from '../types/timeValues';
2
-
3
- export function secondsToMilliseconds(value: Seconds): Milliseconds {
4
- return (value * 1000) as Milliseconds;
5
- }
6
-
7
- export function hoursToMilliseconds(value: Hours): Milliseconds {
8
- return (value * 60 * 60 * 1000) as Milliseconds;
9
- }
10
-
11
- export function hoursToSeconds(value: Hours): Seconds {
12
- return (value * 60 * 60) as Seconds;
13
- }
@@ -1,3 +0,0 @@
1
- export function toArray<TType>(value: TType | TType[]) {
2
- return Array.isArray(value) ? value : [value];
3
- }
@@ -1,11 +0,0 @@
1
- import { TraceId } from '../types/trace';
2
-
3
- export function createTrace(
4
- traceOwner: object | string,
5
- botName: string,
6
- traceName: string
7
- ) {
8
- return `${
9
- typeof traceOwner == 'string' ? traceOwner : traceOwner.constructor.name
10
- }:${botName}-${traceName}` as TraceId;
11
- }
package/src/index.ts DELETED
@@ -1,33 +0,0 @@
1
- export * from './main';
2
- export * from './helpers/builders/inlineQueryActionBuilder';
3
- export * from './helpers/builders/commandActionBuilder';
4
- export * from './helpers/builders/scheduledActionBuilder';
5
- export * from './types/actionState';
6
- export * from './entities/states/actionStateBase';
7
- export * from './types/messageTypes';
8
- export * from './helpers/timeConvertions';
9
- export * from './types/action';
10
- export * from './types/externalAliases';
11
- export * from './types/storage';
12
- export * from './types/scheduler';
13
- export { CommandAction } from './entities/actions/commandAction';
14
- export { InlineQueryAction } from './entities/actions/inlineQueryAction';
15
- export { ReplyCaptureAction } from './entities/actions/replyCaptureAction';
16
- export { ScheduledAction } from './entities/actions/scheduledAction';
17
- export { Hours, Milliseconds, Seconds } from './types/timeValues';
18
- export { ChatContext } from './entities/context/chatContext';
19
- export { InlineQueryContext } from './entities/context/inlineQueryContext';
20
- export { MessageContext } from './entities/context/messageContext';
21
- export { ReplyContext } from './entities/context/replyContext';
22
- export { ICaptureController } from './types/capture';
23
- export { ChatInfo } from './dtos/chatInfo';
24
- export { MessageInfo } from './dtos/messageInfo';
25
- export { UserInfo } from './dtos/userInfo';
26
- export {
27
- BotEventType,
28
- BotEventMap as BotEventArgumentsMap,
29
- TypedEventEmitter,
30
- Listener,
31
- EachListener
32
- } from './types/events';
33
- export { TraceId } from './types/trace';
package/src/main.ts DELETED
@@ -1,76 +0,0 @@
1
- import { IStorageClient } from './types/storage';
2
- import { CommandAction } from './entities/actions/commandAction';
3
- import { ScheduledAction } from './entities/actions/scheduledAction';
4
- import { BotInstance } from './entities/botInstance';
5
- import { Seconds } from './types/timeValues';
6
- import { IScheduler } from './types/scheduler';
7
- import { InlineQueryAction } from './entities/actions/inlineQueryAction';
8
- import { IActionState } from './types/actionState';
9
- import { TypedEventEmitter } from './types/events';
10
-
11
- class BotOrchestrator {
12
- bots: BotInstance[] = [];
13
-
14
- /**
15
- * Starts bot
16
- */
17
- async startBot(options: {
18
- /** Bot name, used in logging */
19
- name: string;
20
- /** Function that provides the bot token. */
21
- tokenProvider: () => Promise<string>;
22
- actions: {
23
- /** Collection of actions that will be executed as a response to message from used. Created using `CommandActionBuilder`.*/
24
- commands: CommandAction<IActionState>[];
25
- /** Collection of actions that will be executed on timer. Created using `ScheduledActionBuilder`.*/
26
- scheduled: ScheduledAction<IActionState>[];
27
- /** Collection of actions that will handle inline queries */
28
- inlineQueries: InlineQueryAction[];
29
- };
30
- /** Object containing chat name and chat id pairs. Used for logging and execution of scheduled action. */
31
- chats: Record<string, number>;
32
- /** 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.*/
33
- storagePath?: string;
34
- /** Period of time between execution of scheduled actions. */
35
- scheduledPeriod?: Seconds;
36
- services?: {
37
- /** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
38
- storageClient?: IStorageClient;
39
- /** Scheduler client for bot scheduling. If not provided, default `NodeTimeoutScheduler` will be used. */
40
- scheduler?: IScheduler;
41
- /** Event emitter instance. If not provided, a default `TypedEventEmitter` will be created. Pass a `TypedEventEmitter<YourCustomEvents>` to support custom events. */
42
- eventEmitter?: TypedEventEmitter<Record<string, unknown>>;
43
- };
44
- }) {
45
- const token = await options.tokenProvider();
46
-
47
- const bot = new BotInstance({
48
- name: options.name,
49
- actions: options.actions,
50
- chats: options.chats,
51
- services: {
52
- storageClient: options.services?.storageClient,
53
- scheduler: options.services?.scheduler,
54
- eventEmitter: options.services?.eventEmitter
55
- },
56
- storagePath: options.storagePath
57
- });
58
-
59
- await bot.start(token, options.actions, options.scheduledPeriod);
60
-
61
- this.bots.push(bot);
62
-
63
- return bot;
64
- }
65
-
66
- /**
67
- * Terminates all scheduled tasks, closes storage connections and stops all bots.
68
- */
69
- async stopBots() {
70
- for (const bot of this.bots) {
71
- await bot.stop();
72
- }
73
- }
74
- }
75
-
76
- export const botOrchestrator = new BotOrchestrator();
@@ -1,125 +0,0 @@
1
- import { hoursToSeconds } from '../helpers/timeConvertions';
2
- import { Seconds, Milliseconds, Hours } from '../types/timeValues';
3
- import { IScheduler } from '../types/scheduler';
4
- import { IStorageClient } from '../types/storage';
5
- import { TelegramApiService } from './telegramApi';
6
- import { InlineQueryAction } from '../entities/actions/inlineQueryAction';
7
- import { IActionState } from '../types/actionState';
8
- import { CommandAction } from '../entities/actions/commandAction';
9
- import { ScheduledAction } from '../entities/actions/scheduledAction';
10
- import { buildHelpCommand } from '../builtin/helpAction';
11
- import { CommandActionProcessor } from './actionProcessors/commandActionProcessor';
12
- import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryActionProcessor';
13
- import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
14
- import { TelegramBot } from '../types/externalAliases';
15
- import { Telegraf } from 'telegraf';
16
- import { TypedEventEmitter } from '../types/events';
17
-
18
- export class ActionProcessingService {
19
- private readonly eventEmitter: TypedEventEmitter;
20
- private readonly storage: IStorageClient;
21
- private readonly commandProcessor: CommandActionProcessor;
22
- private readonly scheduledProcessor: ScheduledActionProcessor;
23
- private readonly inlineQueryProcessor: InlineQueryActionProcessor;
24
-
25
- private readonly botName: string;
26
-
27
- private telegramBot!: TelegramBot;
28
-
29
- constructor(
30
- botName: string,
31
- chats: Record<string, number>,
32
- storage: IStorageClient,
33
- scheduler: IScheduler,
34
- eventEmitter: TypedEventEmitter
35
- ) {
36
- this.storage = storage;
37
- this.eventEmitter = eventEmitter;
38
-
39
- this.commandProcessor = new CommandActionProcessor(
40
- botName,
41
- storage,
42
- scheduler,
43
- this.eventEmitter
44
- );
45
- this.scheduledProcessor = new ScheduledActionProcessor(
46
- botName,
47
- chats,
48
- storage,
49
- scheduler,
50
- this.eventEmitter
51
- );
52
- this.inlineQueryProcessor = new InlineQueryActionProcessor(
53
- botName,
54
- storage,
55
- scheduler,
56
- this.eventEmitter
57
- );
58
-
59
- this.botName = botName;
60
- }
61
-
62
- async initialize(
63
- token: string,
64
- actions: {
65
- commands: CommandAction<IActionState>[];
66
- scheduled: ScheduledAction<IActionState>[];
67
- inlineQueries: InlineQueryAction[];
68
- },
69
- scheduledPeriod?: Seconds
70
- ) {
71
- this.telegramBot = new Telegraf(token);
72
- const api = new TelegramApiService(
73
- this.botName,
74
- this.telegramBot.telegram,
75
- this.storage,
76
- this.eventEmitter,
77
- (capture, id, chatInfo, traceId) => {
78
- this.commandProcessor.captureRegistrationCallback(
79
- capture,
80
- id,
81
- chatInfo,
82
- traceId
83
- );
84
- }
85
- );
86
-
87
- const botInfo = await this.telegramBot.telegram.getMe();
88
- const commandActions =
89
- actions.commands.length > 0 && botInfo.username
90
- ? [
91
- buildHelpCommand(
92
- actions.commands
93
- .map((x) => x.readmeFactory(botInfo.username))
94
- .filter((x) => !!x),
95
- botInfo.username
96
- ),
97
- ...actions.commands
98
- ]
99
- : [];
100
-
101
- this.commandProcessor.initialize(
102
- api,
103
- this.telegramBot,
104
- commandActions,
105
- botInfo
106
- );
107
- this.inlineQueryProcessor.initialize(
108
- api,
109
- this.telegramBot,
110
- actions.inlineQueries,
111
- 300 as Milliseconds
112
- );
113
- this.scheduledProcessor.initialize(
114
- api,
115
- actions.scheduled,
116
- scheduledPeriod ?? hoursToSeconds(1 as Hours)
117
- );
118
-
119
- void this.telegramBot.launch();
120
- }
121
-
122
- stop() {
123
- this.telegramBot.stop();
124
- }
125
- }
@@ -1,67 +0,0 @@
1
- import { IScheduler } from '../../types/scheduler';
2
- import { IStorageClient } from '../../types/storage';
3
- import { TelegramApiService } from '../telegramApi';
4
- import { IAction } from '../../types/action';
5
- import { BaseContextInternal } from '../../entities/context/baseContext';
6
- import { BotEventType, TypedEventEmitter } from '../../types/events';
7
-
8
- export abstract class BaseActionProcessor {
9
- protected readonly storage: IStorageClient;
10
- protected readonly scheduler: IScheduler;
11
- protected readonly eventEmitter: TypedEventEmitter;
12
-
13
- protected readonly botName: string;
14
-
15
- protected api!: TelegramApiService;
16
-
17
- constructor(
18
- botName: string,
19
- storage: IStorageClient,
20
- scheduler: IScheduler,
21
- eventEmitter: TypedEventEmitter
22
- ) {
23
- this.storage = storage;
24
- this.scheduler = scheduler;
25
- this.eventEmitter = eventEmitter;
26
-
27
- this.botName = botName;
28
- }
29
-
30
- private defaultErrorHandler(
31
- error: Error,
32
- ctx: BaseContextInternal<IAction>
33
- ) {
34
- console.error(error);
35
- this.eventEmitter.emit(BotEventType.error, {
36
- error,
37
- traceId: ctx.observability.traceId
38
- });
39
- }
40
-
41
- initializeDependencies(api: TelegramApiService) {
42
- this.api = api;
43
- }
44
-
45
- async executeAction<
46
- TAction extends IAction,
47
- TActionContext extends BaseContextInternal<TAction>
48
- >(
49
- action: TAction,
50
- ctx: TActionContext,
51
- errorHandler?: (error: Error, ctx: TActionContext) => void
52
- ) {
53
- try {
54
- const responses = await action.exec(ctx);
55
-
56
- this.api.enqueueBatchedResponses(responses);
57
- } catch (e) {
58
- const error = e as Error;
59
-
60
- if (errorHandler) {
61
- errorHandler(error, ctx);
62
- } else {
63
- this.defaultErrorHandler(error, ctx);
64
- }
65
- }
66
- }
67
- }