chz-telegram-bot 0.5.4 → 0.6.8
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.
- package/README.md +41 -31
- package/dist/builtin/helpAction.d.ts +2 -0
- package/dist/builtin/helpAction.d.ts.map +1 -0
- package/dist/builtin/helpAction.js +14 -0
- package/dist/dtos/chatHistoryMessage.d.ts +35 -0
- package/dist/dtos/chatHistoryMessage.d.ts.map +1 -0
- package/dist/dtos/chatHistoryMessage.js +32 -0
- package/dist/dtos/chatInfo.d.ts +17 -0
- package/dist/dtos/chatInfo.d.ts.map +1 -0
- package/dist/dtos/chatInfo.js +16 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts +24 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
- package/dist/dtos/commandTriggerCheckResult.js +34 -0
- package/dist/dtos/cooldownInfo.d.ts +13 -0
- package/dist/dtos/cooldownInfo.d.ts.map +1 -0
- package/dist/dtos/cooldownInfo.js +12 -0
- package/dist/dtos/incomingMessage.d.ts +19 -0
- package/dist/dtos/incomingMessage.d.ts.map +1 -0
- package/dist/dtos/incomingMessage.js +63 -0
- package/dist/dtos/incomingQuery.d.ts +10 -0
- package/dist/dtos/incomingQuery.d.ts.map +1 -0
- package/dist/dtos/incomingQuery.js +14 -0
- package/dist/dtos/messageInfo.d.ts +22 -0
- package/dist/dtos/messageInfo.d.ts.map +1 -0
- package/dist/dtos/messageInfo.js +20 -0
- package/dist/dtos/propertyProviderSets.d.ts +16 -0
- package/dist/dtos/propertyProviderSets.d.ts.map +1 -0
- package/dist/dtos/propertyProviderSets.js +1 -0
- package/dist/dtos/replyInfo.d.ts +6 -0
- package/dist/dtos/replyInfo.d.ts.map +1 -0
- package/dist/dtos/replyInfo.js +8 -0
- package/dist/dtos/responses/delay.d.ts +16 -0
- package/dist/dtos/responses/delay.d.ts.map +1 -0
- package/dist/dtos/responses/delay.js +15 -0
- package/dist/dtos/responses/imageMessage.d.ts +22 -0
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
- package/dist/dtos/responses/imageMessage.js +21 -0
- package/dist/dtos/responses/inlineQueryResponse.d.ts +13 -0
- package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -0
- package/dist/dtos/responses/inlineQueryResponse.js +15 -0
- package/dist/dtos/responses/reaction.d.ts +16 -0
- package/dist/dtos/responses/reaction.d.ts.map +1 -0
- package/dist/dtos/responses/reaction.js +17 -0
- package/dist/dtos/responses/textMessage.d.ts +23 -0
- package/dist/dtos/responses/textMessage.d.ts.map +1 -0
- package/dist/dtos/responses/textMessage.js +24 -0
- package/dist/dtos/responses/unpin.d.ts +15 -0
- package/dist/dtos/responses/unpin.d.ts.map +1 -0
- package/dist/dtos/responses/unpin.js +15 -0
- package/dist/dtos/responses/videoMessage.d.ts +22 -0
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
- package/dist/dtos/responses/videoMessage.js +21 -0
- package/dist/dtos/userInfo.d.ts +12 -0
- package/dist/dtos/userInfo.d.ts.map +1 -0
- package/dist/dtos/userInfo.js +12 -0
- package/dist/entities/actions/commandAction.d.ts +31 -0
- package/dist/entities/actions/commandAction.d.ts.map +1 -0
- package/dist/entities/actions/commandAction.js +149 -0
- package/dist/entities/actions/inlineQueryAction.d.ts +14 -0
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -0
- package/dist/entities/actions/inlineQueryAction.js +51 -0
- package/dist/entities/actions/replyCaptureAction.d.ts +15 -0
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -0
- package/dist/entities/actions/replyCaptureAction.js +66 -0
- package/dist/entities/actions/scheduledAction.d.ts +24 -0
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -0
- package/dist/entities/actions/scheduledAction.js +92 -0
- package/dist/entities/botInstance.d.ts +36 -0
- package/dist/entities/botInstance.d.ts.map +1 -0
- package/dist/entities/botInstance.js +39 -0
- package/dist/entities/cachedStateFactory.d.ts +7 -0
- package/dist/entities/cachedStateFactory.d.ts.map +1 -0
- package/dist/entities/cachedStateFactory.js +8 -0
- package/dist/entities/context/baseContext.d.ts +39 -0
- package/dist/entities/context/baseContext.d.ts.map +1 -0
- package/dist/entities/context/baseContext.js +56 -0
- package/dist/entities/context/chatContext.d.ts +50 -0
- package/dist/entities/context/chatContext.d.ts.map +1 -0
- package/dist/entities/context/chatContext.js +65 -0
- package/dist/entities/context/inlineQueryContext.d.ts +27 -0
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -0
- package/dist/entities/context/inlineQueryContext.js +29 -0
- package/dist/entities/context/messageContext.d.ts +92 -0
- package/dist/entities/context/messageContext.d.ts.map +1 -0
- package/dist/entities/context/messageContext.js +116 -0
- package/dist/entities/context/replyContext.d.ts +89 -0
- package/dist/entities/context/replyContext.d.ts.map +1 -0
- package/dist/entities/context/replyContext.js +124 -0
- package/dist/entities/states/actionStateBase.d.ts +6 -0
- package/dist/entities/states/actionStateBase.d.ts.map +1 -0
- package/dist/entities/states/actionStateBase.js +4 -0
- package/dist/entities/taskRecord.d.ts +8 -0
- package/dist/entities/taskRecord.d.ts.map +1 -0
- package/dist/entities/taskRecord.js +10 -0
- package/dist/eslint.config.d.ts +3 -0
- package/dist/eslint.config.d.ts.map +1 -0
- package/dist/eslint.config.js +51 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts +100 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/commandActionBuilder.js +146 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +37 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.js +50 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +68 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/scheduledActionBuilder.js +95 -0
- package/dist/helpers/mapUtils.d.ts +10 -0
- package/dist/helpers/mapUtils.d.ts.map +1 -0
- package/dist/helpers/mapUtils.js +13 -0
- package/dist/helpers/noop.d.ts +9 -0
- package/dist/helpers/noop.d.ts.map +1 -0
- package/dist/helpers/noop.js +17 -0
- package/dist/helpers/objectFromEntries.d.ts +2 -0
- package/dist/helpers/objectFromEntries.d.ts.map +1 -0
- package/dist/helpers/objectFromEntries.js +3 -0
- package/dist/helpers/timeConvertions.d.ts +5 -0
- package/dist/helpers/timeConvertions.d.ts.map +1 -0
- package/dist/helpers/timeConvertions.js +9 -0
- package/dist/helpers/toArray.d.ts +2 -0
- package/dist/helpers/toArray.d.ts.map +1 -0
- package/dist/helpers/toArray.js +3 -0
- package/dist/helpers/traceFactory.d.ts +3 -0
- package/dist/helpers/traceFactory.d.ts.map +1 -0
- package/dist/helpers/traceFactory.js +3 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/main.d.ts +49 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +33 -0
- package/dist/services/actionProcessingService.d.ts +25 -0
- package/dist/services/actionProcessingService.d.ts.map +1 -0
- package/dist/services/actionProcessingService.js +50 -0
- package/dist/services/actionProcessors/baseProcessor.d.ts +18 -0
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/baseProcessor.js +33 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +19 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.js +130 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +11 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +69 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +17 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.js +55 -0
- package/dist/services/jsonFileStorage.d.ts +25 -0
- package/dist/services/jsonFileStorage.d.ts.map +1 -0
- package/dist/services/jsonFileStorage.js +133 -0
- package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
- package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
- package/dist/services/nodeTimeoutScheduler.js +50 -0
- package/dist/services/responseProcessingQueue.d.ts +12 -0
- package/dist/services/responseProcessingQueue.d.ts.map +1 -0
- package/dist/services/responseProcessingQueue.js +37 -0
- package/dist/services/telegramApi.d.ts +23 -0
- package/dist/services/telegramApi.d.ts.map +1 -0
- package/dist/services/telegramApi.js +163 -0
- package/dist/types/action.d.ts +14 -0
- package/dist/types/action.d.ts.map +1 -0
- package/dist/types/action.js +1 -0
- package/dist/types/actionState.d.ts +5 -0
- package/dist/types/actionState.d.ts.map +1 -0
- package/dist/types/actionState.js +1 -0
- package/dist/types/cachedValueAccessor.d.ts +2 -0
- package/dist/types/cachedValueAccessor.d.ts.map +1 -0
- package/dist/types/cachedValueAccessor.js +1 -0
- package/dist/types/capture.d.ts +24 -0
- package/dist/types/capture.d.ts.map +1 -0
- package/dist/types/capture.js +1 -0
- package/dist/types/commandCondition.d.ts +8 -0
- package/dist/types/commandCondition.d.ts.map +1 -0
- package/dist/types/commandCondition.js +1 -0
- package/dist/types/commandTrigger.d.ts +2 -0
- package/dist/types/commandTrigger.d.ts.map +1 -0
- package/dist/types/commandTrigger.js +1 -0
- package/dist/types/events.d.ts +193 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +69 -0
- package/dist/types/externalAliases.d.ts +11 -0
- package/dist/types/externalAliases.d.ts.map +1 -0
- package/dist/types/externalAliases.js +1 -0
- package/dist/types/handlers.d.ts +21 -0
- package/dist/types/handlers.d.ts.map +1 -0
- package/dist/types/handlers.js +1 -0
- package/dist/types/inputFile.d.ts +5 -0
- package/dist/types/inputFile.d.ts.map +1 -0
- package/dist/types/inputFile.js +1 -0
- package/dist/types/logger.d.ts +1 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logger.js +1 -0
- package/dist/types/messageSendingOptions.d.ts +9 -0
- package/dist/types/messageSendingOptions.d.ts.map +1 -0
- package/dist/types/messageSendingOptions.js +1 -0
- package/dist/types/messageTypes.d.ts +20 -0
- package/dist/types/messageTypes.d.ts.map +1 -0
- package/dist/types/messageTypes.js +18 -0
- package/dist/types/propertyProvider.d.ts +8 -0
- package/dist/types/propertyProvider.d.ts.map +1 -0
- package/dist/types/propertyProvider.js +1 -0
- package/dist/types/response.d.ts +39 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +9 -0
- package/dist/types/scheduler.d.ts +7 -0
- package/dist/types/scheduler.d.ts.map +1 -0
- package/dist/types/scheduler.js +1 -0
- package/dist/types/storage.d.ts +11 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/storage.js +1 -0
- package/dist/types/timeValues.d.ts +15 -0
- package/dist/types/timeValues.d.ts.map +1 -0
- package/dist/types/timeValues.js +1 -0
- package/dist/types/trace.d.ts +6 -0
- package/dist/types/trace.d.ts.map +1 -0
- package/dist/types/trace.js +1 -0
- package/entities/actions/commandAction.ts +11 -3
- package/entities/actions/inlineQueryAction.ts +9 -1
- package/entities/actions/replyCaptureAction.ts +9 -3
- package/entities/actions/scheduledAction.ts +31 -10
- package/entities/botInstance.ts +18 -25
- package/entities/context/baseContext.ts +9 -4
- package/index.ts +1 -1
- package/main.ts +1 -10
- package/package.json +38 -38
- package/services/actionProcessingService.ts +11 -15
- package/services/actionProcessors/baseProcessor.ts +9 -9
- package/services/actionProcessors/commandActionProcessor.ts +35 -46
- package/services/actionProcessors/inlineQueryActionProcessor.ts +24 -20
- package/services/actionProcessors/scheduledActionProcessor.ts +5 -10
- package/services/jsonFileStorage.ts +27 -1
- package/services/nodeTimeoutScheduler.ts +22 -22
- package/services/telegramApi.ts +53 -23
- package/types/events.ts +285 -0
- package/services/jsonLogger.ts +0 -112
- package/types/logger.ts +0 -39
|
@@ -4,6 +4,7 @@ import { IActionState } from '../../types/actionState';
|
|
|
4
4
|
import { CommandTrigger } from '../../types/commandTrigger';
|
|
5
5
|
import { ActionKey, IAction } from '../../types/action';
|
|
6
6
|
import { ReplyContextInternal } from '../context/replyContext';
|
|
7
|
+
import { BotEventType } from '../../types/events';
|
|
7
8
|
|
|
8
9
|
export class ReplyCaptureAction<TParentActionState extends IActionState>
|
|
9
10
|
implements IAction
|
|
@@ -50,13 +51,18 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
|
|
|
50
51
|
|
|
51
52
|
if (!shouldExecute) return Noop.NoResponse;
|
|
52
53
|
|
|
53
|
-
ctx.
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
ctx.eventEmitter.emit(BotEventType.replyActionExecuting, {
|
|
55
|
+
action: this,
|
|
56
|
+
ctx
|
|
57
|
+
});
|
|
56
58
|
ctx.matchResults = matchResults;
|
|
57
59
|
|
|
58
60
|
await this.handler(ctx);
|
|
59
61
|
|
|
62
|
+
ctx.eventEmitter.emit(BotEventType.replyActionExecuted, {
|
|
63
|
+
action: this,
|
|
64
|
+
ctx
|
|
65
|
+
});
|
|
60
66
|
return ctx.responses;
|
|
61
67
|
}
|
|
62
68
|
|
|
@@ -8,10 +8,10 @@ import { IActionWithState, ActionKey } from '../../types/action';
|
|
|
8
8
|
import { CachedStateFactory } from '../cachedStateFactory';
|
|
9
9
|
import { ChatContextInternal } from '../context/chatContext';
|
|
10
10
|
import { Noop } from '../../helpers/noop';
|
|
11
|
-
import { IScheduler } from '../../types/scheduler';
|
|
12
11
|
import { getOrSetIfNotExists, getOrThrow } from '../../helpers/mapUtils';
|
|
13
12
|
import { ScheduledActionPropertyProvider } from '../../types/propertyProvider';
|
|
14
13
|
import { ScheduledActionProviders } from '../../dtos/propertyProviderSets';
|
|
14
|
+
import { BotEventType } from '../../types/events';
|
|
15
15
|
|
|
16
16
|
export class ScheduledAction<TActionState extends IActionState>
|
|
17
17
|
implements IActionWithState<TActionState>
|
|
@@ -71,14 +71,15 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
71
71
|
const isAllowedToTrigger = this.checkIfShouldBeExecuted(state, ctx);
|
|
72
72
|
if (!isAllowedToTrigger) return Noop.NoResponse;
|
|
73
73
|
|
|
74
|
-
ctx.
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionExecuting, {
|
|
75
|
+
action: this,
|
|
76
|
+
ctx,
|
|
77
|
+
state
|
|
78
|
+
});
|
|
77
79
|
|
|
78
80
|
await this.handler(
|
|
79
81
|
ctx,
|
|
80
|
-
<TResult>(key: string) =>
|
|
81
|
-
this.getCachedValue<TResult>(key, ctx.botName, ctx.scheduler),
|
|
82
|
+
<TResult>(key: string) => this.getCachedValue<TResult>(key, ctx),
|
|
82
83
|
state
|
|
83
84
|
);
|
|
84
85
|
|
|
@@ -90,13 +91,17 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
90
91
|
state
|
|
91
92
|
);
|
|
92
93
|
|
|
94
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionExecuted, {
|
|
95
|
+
action: this,
|
|
96
|
+
ctx,
|
|
97
|
+
state
|
|
98
|
+
});
|
|
93
99
|
return ctx.responses;
|
|
94
100
|
}
|
|
95
101
|
|
|
96
102
|
private async getCachedValue<TResult>(
|
|
97
103
|
key: string,
|
|
98
|
-
|
|
99
|
-
scheduler: IScheduler
|
|
104
|
+
ctx: ChatContextInternal<TActionState>
|
|
100
105
|
): Promise<TResult> {
|
|
101
106
|
const cachedItemFactory = getOrThrow(
|
|
102
107
|
this.cachedStateFactories,
|
|
@@ -118,21 +123,37 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
118
123
|
return this.cachedState.get(key) as TResult;
|
|
119
124
|
}
|
|
120
125
|
|
|
126
|
+
ctx.eventEmitter.emit(
|
|
127
|
+
BotEventType.scheduledActionCacheValueCreating,
|
|
128
|
+
{
|
|
129
|
+
action: this,
|
|
130
|
+
ctx,
|
|
131
|
+
key
|
|
132
|
+
}
|
|
133
|
+
);
|
|
121
134
|
const value = await cachedItemFactory.getValue();
|
|
122
135
|
|
|
123
136
|
this.cachedState.set(key, value);
|
|
124
137
|
|
|
125
|
-
scheduler.createOnetimeTask(
|
|
138
|
+
ctx.scheduler.createOnetimeTask(
|
|
126
139
|
`Drop cached value [${this.name} : ${key}]`,
|
|
127
140
|
() => this.cachedState.delete(key),
|
|
128
141
|
hoursToMilliseconds(
|
|
129
142
|
cachedItemFactory.invalidationTimeoutInHours
|
|
130
143
|
),
|
|
131
|
-
botName
|
|
144
|
+
ctx.botName
|
|
132
145
|
);
|
|
133
146
|
|
|
134
147
|
return value as TResult;
|
|
135
148
|
} finally {
|
|
149
|
+
ctx.eventEmitter.emit(
|
|
150
|
+
BotEventType.scheduledActionCacheValueReturned,
|
|
151
|
+
{
|
|
152
|
+
action: this,
|
|
153
|
+
ctx,
|
|
154
|
+
key
|
|
155
|
+
}
|
|
156
|
+
);
|
|
136
157
|
semaphore.release();
|
|
137
158
|
}
|
|
138
159
|
}
|
package/entities/botInstance.ts
CHANGED
|
@@ -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.
|
|
44
|
+
new NodeTimeoutScheduler(this.eventEmitter);
|
|
49
45
|
this.storage =
|
|
50
46
|
options.services?.storageClient ??
|
|
51
|
-
new JsonFileStorage(
|
|
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.
|
|
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.
|
|
72
|
-
this.name
|
|
73
|
-
|
|
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.
|
|
88
|
-
this.name
|
|
89
|
-
|
|
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 {
|
|
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
|
-
| '
|
|
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
|
-
|
|
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(
|
|
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
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "chz-telegram-bot",
|
|
3
|
-
"description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
|
|
4
|
-
"author": {
|
|
5
|
-
"name": "Alex Halanin",
|
|
6
|
-
"url": "https://github.com/AlexSolari"
|
|
7
|
-
},
|
|
8
|
-
"license": "MIT",
|
|
9
|
-
"keywords": [
|
|
10
|
-
"telegram",
|
|
11
|
-
"telegram bot"
|
|
12
|
-
],
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "https://github.com/AlexSolari/botFramework.git"
|
|
16
|
-
},
|
|
17
|
-
"version": "0.
|
|
18
|
-
"type": "module",
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"async-sema": "^3.1.1",
|
|
21
|
-
"moment": "^2.29.4",
|
|
22
|
-
"telegraf": "^4.16.3"
|
|
23
|
-
},
|
|
24
|
-
"main": "dist/index.js",
|
|
25
|
-
"types": "dist/index.d.ts",
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"@eslint/js": "^9.29.0",
|
|
28
|
-
"@types/node": "^22.5.5",
|
|
29
|
-
"eslint": "^9.29.0",
|
|
30
|
-
"jiti": "^2.6.1",
|
|
31
|
-
"typescript": "^5.9.0-beta",
|
|
32
|
-
"typescript-eslint": "^8.34.1"
|
|
33
|
-
},
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "tsc",
|
|
36
|
-
"lint": "npx eslint && tsc --noEmit"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "chz-telegram-bot",
|
|
3
|
+
"description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Alex Halanin",
|
|
6
|
+
"url": "https://github.com/AlexSolari"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"telegram",
|
|
11
|
+
"telegram bot"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/AlexSolari/botFramework.git"
|
|
16
|
+
},
|
|
17
|
+
"version": "0.6.8",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"async-sema": "^3.1.1",
|
|
21
|
+
"moment": "^2.29.4",
|
|
22
|
+
"telegraf": "^4.16.3"
|
|
23
|
+
},
|
|
24
|
+
"main": "dist/index.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eslint/js": "^9.29.0",
|
|
28
|
+
"@types/node": "^22.5.5",
|
|
29
|
+
"eslint": "^9.29.0",
|
|
30
|
+
"jiti": "^2.6.1",
|
|
31
|
+
"typescript": "^5.9.0-beta",
|
|
32
|
+
"typescript-eslint": "^8.34.1"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"lint": "npx eslint && tsc --noEmit"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -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
|
-
|
|
34
|
+
eventEmitter: TypedEventEmitter
|
|
36
35
|
) {
|
|
37
36
|
this.storage = storage;
|
|
38
|
-
this.
|
|
37
|
+
this.eventEmitter = eventEmitter;
|
|
39
38
|
|
|
40
39
|
this.commandProcessor = new CommandActionProcessor(
|
|
41
40
|
botName,
|
|
42
41
|
storage,
|
|
43
42
|
scheduler,
|
|
44
|
-
|
|
43
|
+
this.eventEmitter
|
|
45
44
|
);
|
|
46
45
|
this.scheduledProcessor = new ScheduledActionProcessor(
|
|
47
46
|
botName,
|
|
48
47
|
chats,
|
|
49
48
|
storage,
|
|
50
49
|
scheduler,
|
|
51
|
-
|
|
50
|
+
this.eventEmitter
|
|
52
51
|
);
|
|
53
52
|
this.inlineQueryProcessor = new InlineQueryActionProcessor(
|
|
54
53
|
botName,
|
|
55
54
|
storage,
|
|
56
55
|
scheduler,
|
|
57
|
-
|
|
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.
|
|
79
|
-
(capture, id, chatInfo
|
|
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
|
|
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
|
-
|
|
21
|
+
eventEmitter: TypedEventEmitter
|
|
22
22
|
) {
|
|
23
23
|
this.storage = storage;
|
|
24
24
|
this.scheduler = scheduler;
|
|
25
|
-
this.
|
|
25
|
+
this.eventEmitter = eventEmitter;
|
|
26
26
|
|
|
27
27
|
this.botName = botName;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
private defaultErrorHandler
|
|
31
|
-
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
|
}
|