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.
- package/README.md +41 -31
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +11 -1
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/inlineQueryAction.js +9 -1
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.js +9 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +24 -4
- package/dist/entities/botInstance.d.ts +3 -4
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +13 -11
- package/dist/entities/context/baseContext.d.ts +4 -4
- package/dist/entities/context/baseContext.d.ts.map +1 -1
- package/dist/entities/context/baseContext.js +3 -2
- package/dist/eslint.config.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/main.d.ts +0 -3
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +1 -2
- package/dist/services/actionProcessingService.d.ts +4 -4
- package/dist/services/actionProcessingService.d.ts.map +1 -1
- package/dist/services/actionProcessingService.js +10 -10
- package/dist/services/actionProcessors/baseProcessor.d.ts +3 -3
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/baseProcessor.js +9 -5
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +32 -18
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +16 -8
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +2 -2
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.js +4 -4
- package/dist/services/jsonFileStorage.d.ts +3 -1
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +26 -2
- package/dist/services/nodeTimeoutScheduler.d.ts +3 -3
- package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -1
- package/dist/services/nodeTimeoutScheduler.js +27 -8
- package/dist/services/telegramApi.d.ts +4 -4
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +39 -9
- 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/logger.d.ts +0 -12
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/logger.js +1 -1
- 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 +1 -1
- 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 +7 -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/dist/services/jsonLogger.d.ts +0 -11
- package/dist/services/jsonLogger.d.ts.map +0 -1
- package/dist/services/jsonLogger.js +0 -66
- package/services/jsonLogger.ts +0 -112
- package/types/logger.ts +0 -39
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# chz-bot
|
|
1
|
+
# chz-telegram-bot
|
|
2
2
|
|
|
3
3
|
[](https://deepwiki.com/AlexSolari/botFramework)
|
|
4
4
|
|
|
@@ -8,19 +8,19 @@ botFramework is a TypeScript library that provides a structured approach to buil
|
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
11
|
+
- **Type-Safe Command Building**: Fully TypeScript-supported command builders
|
|
12
|
+
- **Stateful Actions**: Built-in state management for commands, scheduled actions, and inline queries
|
|
13
|
+
- **Flexible Triggering**: Support for exact matches, regex patterns, and message types
|
|
14
|
+
- **Scheduled Tasks**: Time-based actions with customizable execution schedules
|
|
15
|
+
- **Access Control**: Built-in user and chat-based permissions
|
|
16
|
+
- **Cooldown Management**: Configurable cooldown periods for commands
|
|
17
|
+
- **Cached Values**: Process-wide caching system for optimizing resource usage
|
|
18
|
+
- **Custom State Types**: Extensible state system for complex bot logic
|
|
19
|
+
- **Comprehensive Logging**: Built-in logging system with trace IDs
|
|
20
|
+
- **Persistent Storage**: JSON-based file storage with automatic state management
|
|
21
|
+
- **Inline Query Support**: Handle inline queries with type-safe builders
|
|
22
|
+
- **Response Queue**: Managed response processing queue for reliable message delivery
|
|
23
|
+
- **Rich Media Support**: Built-in support for text, images, videos, reactions, and inline results
|
|
24
24
|
|
|
25
25
|
## Installation
|
|
26
26
|
|
|
@@ -85,8 +85,11 @@ async function main() {
|
|
|
85
85
|
const bot = await botOrchestrator.startBot({
|
|
86
86
|
name: 'MyFirstBot',
|
|
87
87
|
tokenFilePath: './token.txt',
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
actions: {
|
|
89
|
+
commands,
|
|
90
|
+
scheduled: [], // Add scheduled actions if needed
|
|
91
|
+
inlineQueries: []
|
|
92
|
+
},
|
|
90
93
|
chats: {
|
|
91
94
|
MyChat: -1001234567890 // Replace with your chat ID
|
|
92
95
|
},
|
|
@@ -96,6 +99,15 @@ async function main() {
|
|
|
96
99
|
verboseLoggingForIncomingMessage: false
|
|
97
100
|
});
|
|
98
101
|
|
|
102
|
+
// Add logging
|
|
103
|
+
bot.eventEmitter.onEach(
|
|
104
|
+
(e: string, timestamp: number, data: unknown) => {
|
|
105
|
+
console.log(
|
|
106
|
+
`${new Date(timestamp).toISOString()} - ${e} - ${JSON.stringify(data)}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
99
111
|
// Proper cleanup on shutdown
|
|
100
112
|
const cleanup = async (signal: string) => {
|
|
101
113
|
console.log(`Received ${signal}, cleaning up...`);
|
|
@@ -199,23 +211,21 @@ This will result in `Message 1` being sent, followed by `Message 2` after a 5 se
|
|
|
199
211
|
|
|
200
212
|
When starting a bot, you can provide the following configuration:
|
|
201
213
|
|
|
202
|
-
| Option | Type
|
|
203
|
-
| ----------------- |
|
|
204
|
-
| `name` | `string`
|
|
205
|
-
| `tokenFilePath` | `string`
|
|
206
|
-
| `
|
|
207
|
-
| `
|
|
208
|
-
| `
|
|
209
|
-
| `
|
|
210
|
-
| `
|
|
211
|
-
| `services` | | No | Custom services to be used instead of default ones |
|
|
214
|
+
| Option | Type | Required | Description |
|
|
215
|
+
| ----------------- | ------------------------------------------------------------------------------------------------- | --------------------------- | -------------------------------------------------------------------------------------------- |
|
|
216
|
+
| `name` | `string` | Yes | Bot name used in logging |
|
|
217
|
+
| `tokenFilePath` | `string` | Yes | Path to file containing Telegram Bot token |
|
|
218
|
+
| `actions` | `{ commands: CommandAction[], scheduled: ScheduledAction[], inlineQueries: InlineQueryAction[] }` | Yes (can be empty) | Collection of actions grouped under `actions` — `commands`, `scheduled`, and `inlineQueries` |
|
|
219
|
+
| `chats` | `Record<string, number>` | Yes | Object containing chat name-id pairs. Used for logging and execution of scheduled action. |
|
|
220
|
+
| `storagePath` | `string` | No | Custom storage path for default JsonFileStorage client |
|
|
221
|
+
| `scheduledPeriod` | `Seconds` | No (will default to 1 hour) | Period between scheduled action executions |
|
|
222
|
+
| `services` | | No | Custom services to be used instead of default ones |
|
|
212
223
|
|
|
213
224
|
Services object should have following structure:
|
|
214
225
|
| Option | Type | Required | Description |
|
|
215
226
|
|------------------|--------------------------|----------|---------------------------------------------------------------|
|
|
216
|
-
| `storageClient` | `IStorageClient` | No (will default to `JsonFileStorage`) |
|
|
217
|
-
| `
|
|
218
|
-
| `scheduler` | `IScheduler` | No (will default to `NodeTimeoutScheduler`) | Scheduler used to scheduled action|
|
|
227
|
+
| `storageClient` | `IStorageClient` | No (will default to `JsonFileStorage`) | Persistence state provider |
|
|
228
|
+
| `scheduler` | `IScheduler` | No (will default to `NodeTimeoutScheduler`) | Scheduler used to schedule actions |
|
|
219
229
|
|
|
220
230
|
## Advanced Usage
|
|
221
231
|
|
|
@@ -287,8 +297,8 @@ All responses are queued and processed in order, ensuring proper sequencing of m
|
|
|
287
297
|
To properly terminate your bot and clean up resources:
|
|
288
298
|
|
|
289
299
|
```typescript
|
|
290
|
-
import {
|
|
300
|
+
import { botOrchestrator } from 'chz-telegram-bot';
|
|
291
301
|
|
|
292
302
|
// Call when your application is shutting down
|
|
293
|
-
await stopBots();
|
|
303
|
+
await botOrchestrator.stopBots();
|
|
294
304
|
```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAGhE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MA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,MAAM,CAAC;IAElD,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IAEpC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IAC/C,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IACnD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAEpD,OAAO,CAAC,kBAAkB,CAAsB;gBAG5C,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,sBAAsB,EACjC,gCAAgC,EAAE,MAAM,EACxC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY,EACpC,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM;IAsBxC,IAAI,CACN,GAAG,EAAE,sBAAsB,CAAC,YAAY,CAAC,GAC1C,OAAO,CAAC,WAAW,EAAE,CAAC;IAwFzB,OAAO,CAAC,uBAAuB;IA6D/B,OAAO,CAAC,YAAY;CAuCvB"}
|
|
@@ -8,6 +8,7 @@ import { Sema as Semaphore } from 'async-sema';
|
|
|
8
8
|
import { getOrSetIfNotExists } from '../../helpers/mapUtils';
|
|
9
9
|
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
10
10
|
import { ReplyInfo } from '../../dtos/replyInfo';
|
|
11
|
+
import { BotEventType } from '../../types/events';
|
|
11
12
|
export class CommandAction {
|
|
12
13
|
cooldownInfoProvider;
|
|
13
14
|
isActiveProvider;
|
|
@@ -64,7 +65,11 @@ export class CommandAction {
|
|
|
64
65
|
}
|
|
65
66
|
return Noop.NoResponse;
|
|
66
67
|
}
|
|
67
|
-
ctx.
|
|
68
|
+
ctx.eventEmitter.emit(BotEventType.commandActionExecuting, {
|
|
69
|
+
action: this,
|
|
70
|
+
ctx,
|
|
71
|
+
state
|
|
72
|
+
});
|
|
68
73
|
ctx.matchResults = matchResults;
|
|
69
74
|
await this.handler(ctx, state);
|
|
70
75
|
if (skipCooldown) {
|
|
@@ -75,6 +80,11 @@ export class CommandAction {
|
|
|
75
80
|
state.lastExecutedDate = moment().valueOf();
|
|
76
81
|
}
|
|
77
82
|
await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, state);
|
|
83
|
+
ctx.eventEmitter.emit(BotEventType.commandActionExecuted, {
|
|
84
|
+
action: this,
|
|
85
|
+
ctx,
|
|
86
|
+
state
|
|
87
|
+
});
|
|
78
88
|
return ctx.responses;
|
|
79
89
|
}
|
|
80
90
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inlineQueryAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/inlineQueryAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"inlineQueryAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/inlineQueryAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAG5E,qBAAa,iBAAkB,YAAW,OAAO;IAC7C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,gBAAgB,EAAE,4BAA4B,CAAC,OAAO,CAAC,CAAC;IACjE,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAGrB,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,4BAA4B,CAAC,OAAO,CAAC,EACrD,OAAO,EAAE,MAAM;IAUb,IAAI,CAAC,GAAG,EAAE,0BAA0B;CA8C7C"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Noop } from '../../helpers/noop';
|
|
2
|
+
import { BotEventType } from '../../types/events';
|
|
2
3
|
export class InlineQueryAction {
|
|
3
4
|
key;
|
|
4
5
|
isActiveProvider;
|
|
@@ -36,8 +37,15 @@ export class InlineQueryAction {
|
|
|
36
37
|
if (matchResults.length == 0)
|
|
37
38
|
return Noop.NoResponse;
|
|
38
39
|
ctx.matchResults = matchResults;
|
|
39
|
-
ctx.
|
|
40
|
+
ctx.eventEmitter.emit(BotEventType.inlineActionExecuting, {
|
|
41
|
+
action: this,
|
|
42
|
+
ctx
|
|
43
|
+
});
|
|
40
44
|
await this.handler(ctx);
|
|
45
|
+
ctx.eventEmitter.emit(BotEventType.inlineActionExecuted, {
|
|
46
|
+
action: this,
|
|
47
|
+
ctx
|
|
48
|
+
});
|
|
41
49
|
return ctx.responses;
|
|
42
50
|
}
|
|
43
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replyCaptureAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/replyCaptureAction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"replyCaptureAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/replyCaptureAction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAG/D,qBAAa,kBAAkB,CAAC,kBAAkB,SAAS,YAAY,CACnE,YAAW,OAAO;IAElB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,CACd,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;gBAGtC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,OAAO,EACrB,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,EAClB,QAAQ,EAAE,cAAc,EAAE,EAC1B,eAAe,EAAE,eAAe;IAY9B,IAAI,CAAC,GAAG,EAAE,oBAAoB,CAAC,kBAAkB,CAAC;IA8BxD,OAAO,CAAC,uBAAuB;CA4ClC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
|
|
2
2
|
import { Noop } from '../../helpers/noop';
|
|
3
|
+
import { BotEventType } from '../../types/events';
|
|
3
4
|
export class ReplyCaptureAction {
|
|
4
5
|
parentMessageId;
|
|
5
6
|
key;
|
|
@@ -23,9 +24,16 @@ export class ReplyCaptureAction {
|
|
|
23
24
|
.reduce((acc, curr) => acc.mergeWith(curr), CommandTriggerCheckResult.DoNotTrigger('Other'));
|
|
24
25
|
if (!shouldExecute)
|
|
25
26
|
return Noop.NoResponse;
|
|
26
|
-
ctx.
|
|
27
|
+
ctx.eventEmitter.emit(BotEventType.replyActionExecuting, {
|
|
28
|
+
action: this,
|
|
29
|
+
ctx
|
|
30
|
+
});
|
|
27
31
|
ctx.matchResults = matchResults;
|
|
28
32
|
await this.handler(ctx);
|
|
33
|
+
ctx.eventEmitter.emit(BotEventType.replyActionExecuted, {
|
|
34
|
+
action: this,
|
|
35
|
+
ctx
|
|
36
|
+
});
|
|
29
37
|
return ctx.responses;
|
|
30
38
|
}
|
|
31
39
|
checkIfShouldBeExecuted(ctx, trigger) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACv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pers/noop';
|
|
5
5
|
import { getOrSetIfNotExists, getOrThrow } from '../../helpers/mapUtils';
|
|
6
|
+
import { BotEventType } from '../../types/events';
|
|
6
7
|
export class ScheduledAction {
|
|
7
8
|
static locks = new Map();
|
|
8
9
|
name;
|
|
@@ -34,13 +35,22 @@ export class ScheduledAction {
|
|
|
34
35
|
const isAllowedToTrigger = this.checkIfShouldBeExecuted(state, ctx);
|
|
35
36
|
if (!isAllowedToTrigger)
|
|
36
37
|
return Noop.NoResponse;
|
|
37
|
-
ctx.
|
|
38
|
-
|
|
38
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionExecuting, {
|
|
39
|
+
action: this,
|
|
40
|
+
ctx,
|
|
41
|
+
state
|
|
42
|
+
});
|
|
43
|
+
await this.handler(ctx, (key) => this.getCachedValue(key, ctx), state);
|
|
39
44
|
state.lastExecutedDate = moment().valueOf();
|
|
40
45
|
await ctx.storage.saveActionExecutionResult(this, ctx.chatInfo.id, state);
|
|
46
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionExecuted, {
|
|
47
|
+
action: this,
|
|
48
|
+
ctx,
|
|
49
|
+
state
|
|
50
|
+
});
|
|
41
51
|
return ctx.responses;
|
|
42
52
|
}
|
|
43
|
-
async getCachedValue(key,
|
|
53
|
+
async getCachedValue(key, ctx) {
|
|
44
54
|
const cachedItemFactory = getOrThrow(this.cachedStateFactories, key, `No shared cache was set up for the key [${key}] in action '${this.name}'`);
|
|
45
55
|
const semaphoreKey = `${this.key}_cached:${key}`;
|
|
46
56
|
const semaphore = getOrSetIfNotExists(ScheduledAction.locks, semaphoreKey, new Semaphore(1));
|
|
@@ -49,12 +59,22 @@ export class ScheduledAction {
|
|
|
49
59
|
if (this.cachedState.has(key)) {
|
|
50
60
|
return this.cachedState.get(key);
|
|
51
61
|
}
|
|
62
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionCacheValueCreating, {
|
|
63
|
+
action: this,
|
|
64
|
+
ctx,
|
|
65
|
+
key
|
|
66
|
+
});
|
|
52
67
|
const value = await cachedItemFactory.getValue();
|
|
53
68
|
this.cachedState.set(key, value);
|
|
54
|
-
scheduler.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), hoursToMilliseconds(cachedItemFactory.invalidationTimeoutInHours), botName);
|
|
69
|
+
ctx.scheduler.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), hoursToMilliseconds(cachedItemFactory.invalidationTimeoutInHours), ctx.botName);
|
|
55
70
|
return value;
|
|
56
71
|
}
|
|
57
72
|
finally {
|
|
73
|
+
ctx.eventEmitter.emit(BotEventType.scheduledActionCacheValueReturned, {
|
|
74
|
+
action: this,
|
|
75
|
+
ctx,
|
|
76
|
+
key
|
|
77
|
+
});
|
|
58
78
|
semaphore.release();
|
|
59
79
|
}
|
|
60
80
|
}
|
|
@@ -3,15 +3,15 @@ import { IStorageClient } from '../types/storage';
|
|
|
3
3
|
import { IActionState } from '../types/actionState';
|
|
4
4
|
import { CommandAction } from './actions/commandAction';
|
|
5
5
|
import { ScheduledAction } from './actions/scheduledAction';
|
|
6
|
-
import { ILogger } from '../types/logger';
|
|
7
6
|
import { IScheduler } from '../types/scheduler';
|
|
8
7
|
import { InlineQueryAction } from './actions/inlineQueryAction';
|
|
8
|
+
import { TypedEventEmitter } from '../types/events';
|
|
9
9
|
export declare class BotInstance {
|
|
10
10
|
private readonly storage;
|
|
11
11
|
private readonly scheduler;
|
|
12
|
-
private readonly logger;
|
|
13
12
|
private readonly actionProcessingService;
|
|
14
13
|
readonly name: string;
|
|
14
|
+
readonly eventEmitter: TypedEventEmitter;
|
|
15
15
|
constructor(options: {
|
|
16
16
|
name: string;
|
|
17
17
|
actions: {
|
|
@@ -23,7 +23,6 @@ export declare class BotInstance {
|
|
|
23
23
|
storagePath?: string;
|
|
24
24
|
services?: {
|
|
25
25
|
storageClient?: IStorageClient;
|
|
26
|
-
logger?: ILogger;
|
|
27
26
|
scheduler?: IScheduler;
|
|
28
27
|
};
|
|
29
28
|
});
|
|
@@ -31,7 +30,7 @@ export declare class BotInstance {
|
|
|
31
30
|
commands: CommandAction<IActionState>[];
|
|
32
31
|
scheduled: ScheduledAction<IActionState>[];
|
|
33
32
|
inlineQueries: InlineQueryAction[];
|
|
34
|
-
}, scheduledPeriod?: Seconds
|
|
33
|
+
}, scheduledPeriod?: Seconds): Promise<void>;
|
|
35
34
|
stop(): Promise<void>;
|
|
36
35
|
}
|
|
37
36
|
//# sourceMappingURL=botInstance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAElE,qBAAa,WAAW;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAA0B;IAElE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,oBAA2B;gBAEpC,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE;YACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;YACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;YAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;SACtC,CAAC;QACF,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE;YACP,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IA4BK,KAAK,CACP,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;QACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;KACtC,EACD,eAAe,CAAC,EAAE,OAAO;IAavB,IAAI;CASb"}
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
2
|
-
import { JsonLogger } from '../services/jsonLogger';
|
|
3
2
|
import { NodeTimeoutScheduler } from '../services/nodeTimeoutScheduler';
|
|
4
|
-
import { createTrace } from '../helpers/traceFactory';
|
|
5
3
|
import { ActionProcessingService } from '../services/actionProcessingService';
|
|
4
|
+
import { BotEventType, TypedEventEmitter } from '../types/events';
|
|
6
5
|
export class BotInstance {
|
|
7
6
|
storage;
|
|
8
7
|
scheduler;
|
|
9
|
-
logger;
|
|
10
8
|
actionProcessingService;
|
|
11
9
|
name;
|
|
10
|
+
eventEmitter = new TypedEventEmitter();
|
|
12
11
|
constructor(options) {
|
|
13
12
|
const actions = [
|
|
14
13
|
...options.actions.commands,
|
|
15
14
|
...options.actions.scheduled
|
|
16
15
|
];
|
|
17
16
|
this.name = options.name;
|
|
18
|
-
this.logger = options.services?.logger ?? new JsonLogger();
|
|
19
17
|
this.scheduler =
|
|
20
18
|
options.services?.scheduler ??
|
|
21
|
-
new NodeTimeoutScheduler(this.
|
|
19
|
+
new NodeTimeoutScheduler(this.eventEmitter);
|
|
22
20
|
this.storage =
|
|
23
21
|
options.services?.storageClient ??
|
|
24
|
-
new JsonFileStorage(options.name, actions, options.storagePath);
|
|
25
|
-
this.actionProcessingService = new ActionProcessingService(this.name, options.chats, this.storage, this.scheduler, this.
|
|
22
|
+
new JsonFileStorage(options.name, actions, this.eventEmitter, options.storagePath);
|
|
23
|
+
this.actionProcessingService = new ActionProcessingService(this.name, options.chats, this.storage, this.scheduler, this.eventEmitter);
|
|
26
24
|
}
|
|
27
|
-
async start(token, actions, scheduledPeriod
|
|
28
|
-
this.
|
|
29
|
-
|
|
25
|
+
async start(token, actions, scheduledPeriod) {
|
|
26
|
+
this.eventEmitter.emit(BotEventType.botStarting, {
|
|
27
|
+
botName: this.name
|
|
28
|
+
});
|
|
29
|
+
await this.actionProcessingService.initialize(token, actions, scheduledPeriod);
|
|
30
30
|
}
|
|
31
31
|
async stop() {
|
|
32
|
-
this.
|
|
32
|
+
this.eventEmitter.emit(BotEventType.botStopping, {
|
|
33
|
+
botName: this.name
|
|
34
|
+
});
|
|
33
35
|
this.scheduler.stopAll();
|
|
34
36
|
await this.storage.close();
|
|
35
37
|
this.actionProcessingService.stop();
|
|
@@ -2,12 +2,12 @@ import { ChatInfo } from '../../dtos/chatInfo';
|
|
|
2
2
|
import { IAction, IActionWithState } from '../../types/action';
|
|
3
3
|
import { IActionState } from '../../types/actionState';
|
|
4
4
|
import { ICaptureController } from '../../types/capture';
|
|
5
|
-
import {
|
|
5
|
+
import { TypedEventEmitter } from '../../types/events';
|
|
6
6
|
import { BotResponse, IReplyResponse } from '../../types/response';
|
|
7
7
|
import { IScheduler } from '../../types/scheduler';
|
|
8
8
|
import { IStorageClient } from '../../types/storage';
|
|
9
9
|
import { TraceId } from '../../types/trace';
|
|
10
|
-
export type BaseContextPropertiesToOmit = 'action' | 'isInitialized' | 'storage' | 'scheduler' | '
|
|
10
|
+
export type BaseContextPropertiesToOmit = 'action' | 'isInitialized' | 'storage' | 'scheduler' | 'eventEmitter' | 'responses' | 'traceId';
|
|
11
11
|
export declare abstract class BaseContextInternal<TAction extends IAction> {
|
|
12
12
|
isInitialized: boolean;
|
|
13
13
|
private _responses;
|
|
@@ -16,7 +16,7 @@ export declare abstract class BaseContextInternal<TAction extends IAction> {
|
|
|
16
16
|
readonly storage: IStorageClient;
|
|
17
17
|
/** Scheduler instance for the bot executing this action */
|
|
18
18
|
readonly scheduler: IScheduler;
|
|
19
|
-
|
|
19
|
+
readonly eventEmitter: TypedEventEmitter;
|
|
20
20
|
/** Trace id of a action execution. */
|
|
21
21
|
traceId: TraceId;
|
|
22
22
|
/** Name of a bot that executes this action. */
|
|
@@ -27,7 +27,7 @@ export declare abstract class BaseContextInternal<TAction extends IAction> {
|
|
|
27
27
|
/** Ordered collection of responses to be processed */
|
|
28
28
|
get responses(): BotResponse[];
|
|
29
29
|
set responses(value: BotResponse[]);
|
|
30
|
-
constructor(storage: IStorageClient, scheduler: IScheduler);
|
|
30
|
+
constructor(storage: IStorageClient, scheduler: IScheduler, eventEmitter: TypedEventEmitter);
|
|
31
31
|
protected createCaptureController(response: IReplyResponse): ICaptureController;
|
|
32
32
|
/**
|
|
33
33
|
* Loads state of another action for current chat.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseContext.d.ts","sourceRoot":"","sources":["../../../entities/context/baseContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE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O,CAAC;IAClB,+CAA+C;IAC/C,OAAO,EAAG,MAAM,CAAC;IACjB,wBAAwB;IACxB,QAAQ,EAAG,QAAQ,CAAC;IACpB,IAAI,SAAS,8BAEZ;IAED,uDAAuD;IACvD,IAAW,SAAS,IAAI,WAAW,EAAE,CAEpC;IACD,IAAW,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,EAExC;gBAGG,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IAOnC,SAAS,CAAC,uBAAuB,CAC7B,QAAQ,EAAE,cAAc,GACzB,kBAAkB;IAmBrB;;;;OAIG;IACG,WAAW,CAAC,mBAAmB,SAAS,YAAY,EACtD,MAAM,EAAE,gBAAgB,CAAC,mBAAmB,CAAC;CAWpD"}
|
|
@@ -6,7 +6,7 @@ export class BaseContextInternal {
|
|
|
6
6
|
storage;
|
|
7
7
|
/** Scheduler instance for the bot executing this action */
|
|
8
8
|
scheduler;
|
|
9
|
-
|
|
9
|
+
eventEmitter;
|
|
10
10
|
/** Trace id of a action execution. */
|
|
11
11
|
traceId;
|
|
12
12
|
/** Name of a bot that executes this action. */
|
|
@@ -23,9 +23,10 @@ export class BaseContextInternal {
|
|
|
23
23
|
set responses(value) {
|
|
24
24
|
this._responses = value;
|
|
25
25
|
}
|
|
26
|
-
constructor(storage, scheduler) {
|
|
26
|
+
constructor(storage, scheduler, eventEmitter) {
|
|
27
27
|
this.storage = storage;
|
|
28
28
|
this.scheduler = scheduler;
|
|
29
|
+
this.eventEmitter = eventEmitter;
|
|
29
30
|
}
|
|
30
31
|
createCaptureController(response) {
|
|
31
32
|
return {
|
package/dist/eslint.config.d.ts
CHANGED
package/dist/index.d.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,4 +23,5 @@ 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';
|
|
27
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,6CAA6C,CAAC;AAC5D,cAAc,yCAAyC,CAAC;AACxD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,6CAA6C,CAAC;AAC5D,cAAc,yCAAyC,CAAC;AACxD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
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';
|
|
@@ -18,3 +17,4 @@ export { ScheduledAction } from './entities/actions/scheduledAction';
|
|
|
18
17
|
export { ChatInfo } from './dtos/chatInfo';
|
|
19
18
|
export { MessageInfo } from './dtos/messageInfo';
|
|
20
19
|
export { UserInfo } from './dtos/userInfo';
|
|
20
|
+
export { BotEventType } from './types/events';
|
package/dist/main.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { ScheduledAction } from './entities/actions/scheduledAction';
|
|
|
4
4
|
import { BotInstance } from './entities/botInstance';
|
|
5
5
|
import { Seconds } from './types/timeValues';
|
|
6
6
|
import { IScheduler } from './types/scheduler';
|
|
7
|
-
import { ILogger } from './types/logger';
|
|
8
7
|
import { InlineQueryAction } from './entities/actions/inlineQueryAction';
|
|
9
8
|
import { IActionState } from './types/actionState';
|
|
10
9
|
declare class BotOrchestrator {
|
|
@@ -36,8 +35,6 @@ declare class BotOrchestrator {
|
|
|
36
35
|
services?: {
|
|
37
36
|
/** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
|
|
38
37
|
storageClient?: IStorageClient;
|
|
39
|
-
/** Logger client for bot logging. If not provided, default `JsonFileStorage` will be used. */
|
|
40
|
-
logger?: ILogger;
|
|
41
38
|
/** Scheduler client for bot scheduling. If not provided, default `NodeTimeoutScheduler` will be used. */
|
|
42
39
|
scheduler?: IScheduler;
|
|
43
40
|
};
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,cAAM,eAAe;IACjB,IAAI,EAAE,WAAW,EAAE,CAAM;IAEzB;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE;QACpB,gCAAgC;QAChC,IAAI,EAAE,MAAM,CAAC;QACb,kDAAkD;QAClD,aAAa,EAAE,MAAM,CAAC;QACtB,OAAO,EAAE;YACL,2HAA2H;YAC3H,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;YACxC,mGAAmG;YACnG,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;YAC3C,4DAA4D;YAC5D,aAAa,EAAE,iBAAiB,EAAE,CAAC;SACtC,CAAC;QACF,yGAAyG;QACzG,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,2KAA2K;QAC3K,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,6DAA6D;QAC7D,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,+EAA+E;QAC/E,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,qGAAqG;YACrG,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,yGAAyG;YACzG,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAoBD;;OAEG;IACG,QAAQ;CAKjB;AAED,eAAO,MAAM,eAAe,iBAAwB,CAAC"}
|
package/dist/main.js
CHANGED
|
@@ -13,12 +13,11 @@ class BotOrchestrator {
|
|
|
13
13
|
chats: options.chats,
|
|
14
14
|
services: {
|
|
15
15
|
storageClient: options.services?.storageClient,
|
|
16
|
-
logger: options.services?.logger,
|
|
17
16
|
scheduler: options.services?.scheduler
|
|
18
17
|
},
|
|
19
18
|
storagePath: options.storagePath
|
|
20
19
|
});
|
|
21
|
-
await bot.start(token, options.actions, options.scheduledPeriod
|
|
20
|
+
await bot.start(token, options.actions, options.scheduledPeriod);
|
|
22
21
|
this.bots.push(bot);
|
|
23
22
|
return bot;
|
|
24
23
|
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { Seconds } from '../types/timeValues';
|
|
2
|
-
import { ILogger } from '../types/logger';
|
|
3
2
|
import { IScheduler } from '../types/scheduler';
|
|
4
3
|
import { IStorageClient } from '../types/storage';
|
|
5
4
|
import { InlineQueryAction } from '../entities/actions/inlineQueryAction';
|
|
6
5
|
import { IActionState } from '../types/actionState';
|
|
7
6
|
import { CommandAction } from '../entities/actions/commandAction';
|
|
8
7
|
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
8
|
+
import { TypedEventEmitter } from '../types/events';
|
|
9
9
|
export declare class ActionProcessingService {
|
|
10
|
+
private readonly eventEmitter;
|
|
10
11
|
private readonly storage;
|
|
11
|
-
private readonly logger;
|
|
12
12
|
private readonly commandProcessor;
|
|
13
13
|
private readonly scheduledProcessor;
|
|
14
14
|
private readonly inlineQueryProcessor;
|
|
15
15
|
private readonly botName;
|
|
16
16
|
private telegramBot;
|
|
17
|
-
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler,
|
|
17
|
+
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler, eventEmitter: TypedEventEmitter);
|
|
18
18
|
initialize(token: string, actions: {
|
|
19
19
|
commands: CommandAction<IActionState>[];
|
|
20
20
|
scheduled: ScheduledAction<IActionState>[];
|
|
21
21
|
inlineQueries: InlineQueryAction[];
|
|
22
|
-
}, scheduledPeriod?: Seconds
|
|
22
|
+
}, scheduledPeriod?: Seconds): Promise<void>;
|
|
23
23
|
stop(): void;
|
|
24
24
|
}
|
|
25
25
|
//# sourceMappingURL=actionProcessingService.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actionProcessingService.d.ts","sourceRoot":"","sources":["../../services/actionProcessingService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAuB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"actionProcessingService.d.ts","sourceRoot":"","sources":["../../services/actionProcessingService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAuB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAOtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,qBAAa,uBAAuB;IAChC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoB;IACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAyB;IAC1D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA2B;IAC9D,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA6B;IAElE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC,OAAO,CAAC,WAAW,CAAe;gBAG9B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IA4B7B,UAAU,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;QACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;KACtC,EACD,eAAe,CAAC,EAAE,OAAO;IAyD7B,IAAI;CAGP"}
|
|
@@ -6,25 +6,25 @@ import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryAction
|
|
|
6
6
|
import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
|
|
7
7
|
import { Telegraf } from 'telegraf';
|
|
8
8
|
export class ActionProcessingService {
|
|
9
|
+
eventEmitter;
|
|
9
10
|
storage;
|
|
10
|
-
logger;
|
|
11
11
|
commandProcessor;
|
|
12
12
|
scheduledProcessor;
|
|
13
13
|
inlineQueryProcessor;
|
|
14
14
|
botName;
|
|
15
15
|
telegramBot;
|
|
16
|
-
constructor(botName, chats, storage, scheduler,
|
|
16
|
+
constructor(botName, chats, storage, scheduler, eventEmitter) {
|
|
17
17
|
this.storage = storage;
|
|
18
|
-
this.
|
|
19
|
-
this.commandProcessor = new CommandActionProcessor(botName, storage, scheduler,
|
|
20
|
-
this.scheduledProcessor = new ScheduledActionProcessor(botName, chats, storage, scheduler,
|
|
21
|
-
this.inlineQueryProcessor = new InlineQueryActionProcessor(botName, storage, scheduler,
|
|
18
|
+
this.eventEmitter = eventEmitter;
|
|
19
|
+
this.commandProcessor = new CommandActionProcessor(botName, storage, scheduler, this.eventEmitter);
|
|
20
|
+
this.scheduledProcessor = new ScheduledActionProcessor(botName, chats, storage, scheduler, this.eventEmitter);
|
|
21
|
+
this.inlineQueryProcessor = new InlineQueryActionProcessor(botName, storage, scheduler, this.eventEmitter);
|
|
22
22
|
this.botName = botName;
|
|
23
23
|
}
|
|
24
|
-
async initialize(token, actions, scheduledPeriod
|
|
24
|
+
async initialize(token, actions, scheduledPeriod) {
|
|
25
25
|
this.telegramBot = new Telegraf(token);
|
|
26
|
-
const api = new TelegramApiService(this.botName, this.telegramBot.telegram, this.storage, this.
|
|
27
|
-
this.commandProcessor.captureRegistrationCallback(capture, id, chatInfo
|
|
26
|
+
const api = new TelegramApiService(this.botName, this.telegramBot.telegram, this.storage, this.eventEmitter, (capture, id, chatInfo) => {
|
|
27
|
+
this.commandProcessor.captureRegistrationCallback(capture, id, chatInfo);
|
|
28
28
|
});
|
|
29
29
|
const botInfo = await this.telegramBot.telegram.getMe();
|
|
30
30
|
const commandActions = actions.commands.length > 0 && botInfo.username
|
|
@@ -35,7 +35,7 @@ export class ActionProcessingService {
|
|
|
35
35
|
...actions.commands
|
|
36
36
|
]
|
|
37
37
|
: [];
|
|
38
|
-
this.commandProcessor.initialize(api, this.telegramBot, commandActions,
|
|
38
|
+
this.commandProcessor.initialize(api, this.telegramBot, commandActions, botInfo);
|
|
39
39
|
this.inlineQueryProcessor.initialize(api, this.telegramBot, actions.inlineQueries, 300);
|
|
40
40
|
this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? hoursToSeconds(1));
|
|
41
41
|
void this.telegramBot.launch();
|