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
package/dist/main.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import { BotInstance } from './entities/botInstance';
|
|
3
|
+
class BotOrchestrator {
|
|
4
|
+
bots = [];
|
|
5
|
+
/**
|
|
6
|
+
* Starts bot
|
|
7
|
+
*/
|
|
8
|
+
async startBot(options) {
|
|
9
|
+
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
10
|
+
const bot = new BotInstance({
|
|
11
|
+
name: options.name,
|
|
12
|
+
actions: options.actions,
|
|
13
|
+
chats: options.chats,
|
|
14
|
+
services: {
|
|
15
|
+
storageClient: options.services?.storageClient,
|
|
16
|
+
scheduler: options.services?.scheduler
|
|
17
|
+
},
|
|
18
|
+
storagePath: options.storagePath
|
|
19
|
+
});
|
|
20
|
+
await bot.start(token, options.actions, options.scheduledPeriod);
|
|
21
|
+
this.bots.push(bot);
|
|
22
|
+
return bot;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Terminates all scheduled tasks, closes storage connections and stops all bots.
|
|
26
|
+
*/
|
|
27
|
+
async stopBots() {
|
|
28
|
+
for (const bot of this.bots) {
|
|
29
|
+
await bot.stop();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export const botOrchestrator = new BotOrchestrator();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Seconds } from '../types/timeValues';
|
|
2
|
+
import { IScheduler } from '../types/scheduler';
|
|
3
|
+
import { IStorageClient } from '../types/storage';
|
|
4
|
+
import { InlineQueryAction } from '../entities/actions/inlineQueryAction';
|
|
5
|
+
import { IActionState } from '../types/actionState';
|
|
6
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
7
|
+
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
8
|
+
import { TypedEventEmitter } from '../types/events';
|
|
9
|
+
export declare class ActionProcessingService {
|
|
10
|
+
private readonly eventEmitter;
|
|
11
|
+
private readonly storage;
|
|
12
|
+
private readonly commandProcessor;
|
|
13
|
+
private readonly scheduledProcessor;
|
|
14
|
+
private readonly inlineQueryProcessor;
|
|
15
|
+
private readonly botName;
|
|
16
|
+
private telegramBot;
|
|
17
|
+
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler, eventEmitter: TypedEventEmitter);
|
|
18
|
+
initialize(token: string, actions: {
|
|
19
|
+
commands: CommandAction<IActionState>[];
|
|
20
|
+
scheduled: ScheduledAction<IActionState>[];
|
|
21
|
+
inlineQueries: InlineQueryAction[];
|
|
22
|
+
}, scheduledPeriod?: Seconds): Promise<void>;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=actionProcessingService.d.ts.map
|
|
@@ -0,0 +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,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"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { hoursToSeconds } from '../helpers/timeConvertions';
|
|
2
|
+
import { TelegramApiService } from './telegramApi';
|
|
3
|
+
import { buildHelpCommand } from '../builtin/helpAction';
|
|
4
|
+
import { CommandActionProcessor } from './actionProcessors/commandActionProcessor';
|
|
5
|
+
import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryActionProcessor';
|
|
6
|
+
import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
|
|
7
|
+
import { Telegraf } from 'telegraf';
|
|
8
|
+
export class ActionProcessingService {
|
|
9
|
+
eventEmitter;
|
|
10
|
+
storage;
|
|
11
|
+
commandProcessor;
|
|
12
|
+
scheduledProcessor;
|
|
13
|
+
inlineQueryProcessor;
|
|
14
|
+
botName;
|
|
15
|
+
telegramBot;
|
|
16
|
+
constructor(botName, chats, storage, scheduler, eventEmitter) {
|
|
17
|
+
this.storage = storage;
|
|
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
|
+
this.botName = botName;
|
|
23
|
+
}
|
|
24
|
+
async initialize(token, actions, scheduledPeriod) {
|
|
25
|
+
this.telegramBot = new Telegraf(token);
|
|
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
|
+
});
|
|
29
|
+
const botInfo = await this.telegramBot.telegram.getMe();
|
|
30
|
+
const commandActions = actions.commands.length > 0 && botInfo.username
|
|
31
|
+
? [
|
|
32
|
+
buildHelpCommand(actions.commands
|
|
33
|
+
.map((x) => x.readmeFactory(botInfo.username))
|
|
34
|
+
.filter((x) => !!x), botInfo.username),
|
|
35
|
+
...actions.commands
|
|
36
|
+
]
|
|
37
|
+
: [];
|
|
38
|
+
this.commandProcessor.initialize(api, this.telegramBot, commandActions, botInfo);
|
|
39
|
+
this.inlineQueryProcessor.initialize(api, this.telegramBot, actions.inlineQueries, 300);
|
|
40
|
+
this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? hoursToSeconds(1));
|
|
41
|
+
void this.telegramBot.launch();
|
|
42
|
+
void this.storage.saveMetadata([
|
|
43
|
+
...actions.scheduled,
|
|
44
|
+
...commandActions
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
stop() {
|
|
48
|
+
this.telegramBot.stop();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 { TypedEventEmitter } from '../../types/events';
|
|
7
|
+
export declare abstract class BaseActionProcessor {
|
|
8
|
+
protected readonly storage: IStorageClient;
|
|
9
|
+
protected readonly scheduler: IScheduler;
|
|
10
|
+
protected readonly eventEmitter: TypedEventEmitter;
|
|
11
|
+
protected readonly botName: string;
|
|
12
|
+
protected api: TelegramApiService;
|
|
13
|
+
constructor(botName: string, storage: IStorageClient, scheduler: IScheduler, eventEmitter: TypedEventEmitter);
|
|
14
|
+
private defaultErrorHandler;
|
|
15
|
+
initializeDependencies(api: TelegramApiService): void;
|
|
16
|
+
executeAction<TAction extends IAction, TActionContext extends BaseContextInternal<TAction>>(action: TAction, ctx: TActionContext, errorHandler?: (error: Error, ctx: TActionContext) => void): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=baseProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"baseProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/baseProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAgB,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAErE,8BAAsB,mBAAmB;IACrC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IAC3C,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC;IACzC,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IAEnD,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEnC,SAAS,CAAC,GAAG,EAAG,kBAAkB,CAAC;gBAG/B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IASnC,OAAO,CAAC,mBAAmB;IAI3B,sBAAsB,CAAC,GAAG,EAAE,kBAAkB;IAIxC,aAAa,CACf,OAAO,SAAS,OAAO,EACvB,cAAc,SAAS,mBAAmB,CAAC,OAAO,CAAC,EAEnD,MAAM,EAAE,OAAO,EACf,GAAG,EAAE,cAAc,EACnB,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,cAAc,KAAK,IAAI;CAajE"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BotEventType } from '../../types/events';
|
|
2
|
+
export class BaseActionProcessor {
|
|
3
|
+
storage;
|
|
4
|
+
scheduler;
|
|
5
|
+
eventEmitter;
|
|
6
|
+
botName;
|
|
7
|
+
api;
|
|
8
|
+
constructor(botName, storage, scheduler, eventEmitter) {
|
|
9
|
+
this.storage = storage;
|
|
10
|
+
this.scheduler = scheduler;
|
|
11
|
+
this.eventEmitter = eventEmitter;
|
|
12
|
+
this.botName = botName;
|
|
13
|
+
}
|
|
14
|
+
defaultErrorHandler(error) {
|
|
15
|
+
this.eventEmitter.emit(BotEventType.error, { error });
|
|
16
|
+
}
|
|
17
|
+
initializeDependencies(api) {
|
|
18
|
+
this.api = api;
|
|
19
|
+
}
|
|
20
|
+
async executeAction(action, ctx, errorHandler) {
|
|
21
|
+
try {
|
|
22
|
+
const responses = await action.exec(ctx);
|
|
23
|
+
this.api.enqueueBatchedResponses(responses);
|
|
24
|
+
ctx.isInitialized = false;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
(errorHandler ?? this.defaultErrorHandler)(error, ctx);
|
|
28
|
+
this.eventEmitter.emit(BotEventType.error, {
|
|
29
|
+
error: error
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CommandAction } from '../../entities/actions/commandAction';
|
|
2
|
+
import { IActionState } from '../../types/actionState';
|
|
3
|
+
import { TelegramApiService } from '../telegramApi';
|
|
4
|
+
import { IReplyCapture } from '../../types/capture';
|
|
5
|
+
import { ChatInfo } from '../../dtos/chatInfo';
|
|
6
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
7
|
+
import { BotInfo, TelegramBot } from '../../types/externalAliases';
|
|
8
|
+
export declare class CommandActionProcessor extends BaseActionProcessor {
|
|
9
|
+
private readonly replyCaptures;
|
|
10
|
+
private readonly chatHistory;
|
|
11
|
+
private botInfo;
|
|
12
|
+
private commands;
|
|
13
|
+
initialize(api: TelegramApiService, telegram: TelegramBot, commands: CommandAction<IActionState>[], botInfo: BotInfo): void;
|
|
14
|
+
captureRegistrationCallback(capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo): void;
|
|
15
|
+
private processMessage;
|
|
16
|
+
private initializeReplyCaptureContext;
|
|
17
|
+
private initializeMessageContext;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=commandActionProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commandActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/commandActionProcessor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAIrE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAM/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAKtD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAKnE,qBAAa,sBAAuB,SAAQ,mBAAmB;IAC3D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0C;IACxE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA2C;IACvE,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO,CAAC,QAAQ,CAKd;IAEF,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,WAAW,EACrB,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,EACvC,OAAO,EAAE,OAAO;IAoDpB,2BAA2B,CACvB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ;YA4BR,cAAc;IAkE5B,OAAO,CAAC,6BAA6B;IA2BrC,OAAO,CAAC,wBAAwB;CA6BnC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { IncomingMessage } from '../../dtos/incomingMessage';
|
|
2
|
+
import { ReplyCaptureAction } from '../../entities/actions/replyCaptureAction';
|
|
3
|
+
import { MessageContextInternal } from '../../entities/context/messageContext';
|
|
4
|
+
import { ReplyContextInternal } from '../../entities/context/replyContext';
|
|
5
|
+
import { INTERNAL_MESSAGE_TYPE_PREFIX, MessageType } from '../../types/messageTypes';
|
|
6
|
+
import { typeSafeObjectFromEntries } from '../../helpers/objectFromEntries';
|
|
7
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
8
|
+
import { getOrSetIfNotExists } from '../../helpers/mapUtils';
|
|
9
|
+
import { MessageInfo } from '../../dtos/messageInfo';
|
|
10
|
+
import { UserInfo } from '../../dtos/userInfo';
|
|
11
|
+
import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
|
|
12
|
+
import { BotEventType } from '../../types/events';
|
|
13
|
+
const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
|
|
14
|
+
export class CommandActionProcessor extends BaseActionProcessor {
|
|
15
|
+
replyCaptures = [];
|
|
16
|
+
chatHistory = new Map();
|
|
17
|
+
botInfo;
|
|
18
|
+
commands = typeSafeObjectFromEntries(Object.values(MessageType).map((x) => [
|
|
19
|
+
x,
|
|
20
|
+
[]
|
|
21
|
+
]));
|
|
22
|
+
initialize(api, telegram, commands, botInfo) {
|
|
23
|
+
this.botInfo = botInfo;
|
|
24
|
+
this.initializeDependencies(api);
|
|
25
|
+
for (const msgType of Object.values(MessageType)) {
|
|
26
|
+
if (msgType == MessageType.Text) {
|
|
27
|
+
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.some((x) => typeof x != 'string') ||
|
|
28
|
+
cmd.triggers.some((x) => typeof x == 'string' &&
|
|
29
|
+
!x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)) ||
|
|
30
|
+
cmd.triggers.includes(MessageType.Text) ||
|
|
31
|
+
cmd.triggers.includes(MessageType.Any));
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.includes(msgType) ||
|
|
35
|
+
cmd.triggers.includes(MessageType.Any));
|
|
36
|
+
}
|
|
37
|
+
if (commands.length > 0) {
|
|
38
|
+
telegram.on('message', async ({ message }) => {
|
|
39
|
+
const internalMessage = new IncomingMessage(message, this.botName, getOrSetIfNotExists(this.chatHistory, message.chat.id, []));
|
|
40
|
+
this.eventEmitter.emit(BotEventType.messageRecieved, {
|
|
41
|
+
botInfo: this.botInfo,
|
|
42
|
+
message: internalMessage
|
|
43
|
+
});
|
|
44
|
+
await this.processMessage(internalMessage);
|
|
45
|
+
this.eventEmitter.emit(BotEventType.messageProcessingFinished, {
|
|
46
|
+
botInfo: this.botInfo,
|
|
47
|
+
message: internalMessage
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
captureRegistrationCallback(capture, parentMessageId, chatInfo) {
|
|
53
|
+
const replyAction = new ReplyCaptureAction(parentMessageId, capture.action, capture.handler, capture.trigger, capture.abortController);
|
|
54
|
+
this.eventEmitter.emit(BotEventType.commandActionCaptureStarted, {
|
|
55
|
+
parentMessageId,
|
|
56
|
+
chatInfo
|
|
57
|
+
});
|
|
58
|
+
this.replyCaptures.push(replyAction);
|
|
59
|
+
capture.abortController.signal.addEventListener('abort', () => {
|
|
60
|
+
const index = this.replyCaptures.indexOf(replyAction);
|
|
61
|
+
this.replyCaptures.splice(index, 1);
|
|
62
|
+
this.eventEmitter.emit(BotEventType.commandActionCaptureAborted, {
|
|
63
|
+
parentMessageId,
|
|
64
|
+
chatInfo
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async processMessage(msg) {
|
|
69
|
+
this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
|
|
70
|
+
botInfo: this.botInfo,
|
|
71
|
+
message: msg
|
|
72
|
+
});
|
|
73
|
+
const chatHistoryArray = getOrSetIfNotExists(this.chatHistory, msg.chatInfo.id, []);
|
|
74
|
+
while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
|
|
75
|
+
chatHistoryArray.shift();
|
|
76
|
+
chatHistoryArray.push(new ChatHistoryMessage(msg.messageId, msg.from, msg.text, msg.type, msg.traceId, msg.replyToMessageId, msg.updateObject.date));
|
|
77
|
+
const ctx = new MessageContextInternal(this.storage, this.scheduler, this.eventEmitter);
|
|
78
|
+
const commandsToCheck = new Set(this.commands[msg.type]);
|
|
79
|
+
if (msg.type != MessageType.Text && msg.text != '') {
|
|
80
|
+
for (const command of this.commands[MessageType.Text]) {
|
|
81
|
+
commandsToCheck.add(command);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.eventEmitter.emit(BotEventType.beforeActionsExecuting, {
|
|
85
|
+
botInfo: this.botInfo,
|
|
86
|
+
message: msg,
|
|
87
|
+
commands: commandsToCheck
|
|
88
|
+
});
|
|
89
|
+
for (const commandAction of commandsToCheck) {
|
|
90
|
+
this.initializeMessageContext(ctx, commandAction, msg);
|
|
91
|
+
await this.executeAction(commandAction, ctx);
|
|
92
|
+
}
|
|
93
|
+
if (this.replyCaptures.length != 0) {
|
|
94
|
+
const replyCtx = new ReplyContextInternal(this.storage, this.scheduler, this.eventEmitter);
|
|
95
|
+
for (const replyAction of this.replyCaptures) {
|
|
96
|
+
this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
|
|
97
|
+
await this.executeAction(replyAction, replyCtx);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.api.flushResponses();
|
|
101
|
+
}
|
|
102
|
+
initializeReplyCaptureContext(ctx, action, message) {
|
|
103
|
+
ctx.replyMessageId = message.replyToMessageId;
|
|
104
|
+
ctx.messageInfo = new MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
105
|
+
ctx.userInfo = new UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
106
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
107
|
+
ctx.botName = this.botName;
|
|
108
|
+
ctx.action = action;
|
|
109
|
+
ctx.chatInfo = message.chatInfo;
|
|
110
|
+
ctx.traceId = message.traceId;
|
|
111
|
+
ctx.botInfo = this.botInfo;
|
|
112
|
+
ctx.isInitialized = true;
|
|
113
|
+
ctx.matchResults = [];
|
|
114
|
+
}
|
|
115
|
+
initializeMessageContext(ctx, action, message) {
|
|
116
|
+
ctx.messageInfo = new MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
117
|
+
ctx.userInfo = new UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
118
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
119
|
+
ctx.matchResults = [];
|
|
120
|
+
ctx.startCooldown = true;
|
|
121
|
+
ctx.responses = [];
|
|
122
|
+
ctx.isInitialized = true;
|
|
123
|
+
ctx.botName = this.botName;
|
|
124
|
+
ctx.action = action;
|
|
125
|
+
ctx.chatInfo = message.chatInfo;
|
|
126
|
+
ctx.traceId = message.traceId;
|
|
127
|
+
ctx.botInfo = this.botInfo;
|
|
128
|
+
ctx.customCooldown = undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
|
|
2
|
+
import { TelegramBot } from '../../types/externalAliases';
|
|
3
|
+
import { Milliseconds } from '../../types/timeValues';
|
|
4
|
+
import { TelegramApiService } from '../telegramApi';
|
|
5
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
6
|
+
export declare class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
7
|
+
private inlineQueries;
|
|
8
|
+
initialize(api: TelegramApiService, telegram: TelegramBot, inlineQueries: InlineQueryAction[], period: Milliseconds): void;
|
|
9
|
+
private initializeInlineQueryContext;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=inlineQueryActionProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inlineQueryActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/inlineQueryActionProcessor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAI7E,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,qBAAa,0BAA2B,SAAQ,mBAAmB;IAC/D,OAAO,CAAC,aAAa,CAAuB;IAE5C,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,WAAW,EACrB,aAAa,EAAE,iBAAiB,EAAE,EAClC,MAAM,EAAE,YAAY;IA4GxB,OAAO,CAAC,4BAA4B;CAmBvC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { IncomingInlineQuery } from '../../dtos/incomingQuery';
|
|
2
|
+
import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
|
|
3
|
+
import { createTrace } from '../../helpers/traceFactory';
|
|
4
|
+
import { BotEventType } from '../../types/events';
|
|
5
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
6
|
+
export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
7
|
+
inlineQueries;
|
|
8
|
+
initialize(api, telegram, inlineQueries, period) {
|
|
9
|
+
this.initializeDependencies(api);
|
|
10
|
+
this.inlineQueries = inlineQueries;
|
|
11
|
+
let pendingInlineQueries = [];
|
|
12
|
+
const queriesInProcessing = new Map();
|
|
13
|
+
if (this.inlineQueries.length > 0) {
|
|
14
|
+
telegram.on('inline_query', ({ inlineQuery }) => {
|
|
15
|
+
const query = new IncomingInlineQuery(inlineQuery.id, inlineQuery.query, inlineQuery.from.id, createTrace('InlineQuery', this.botName, inlineQuery.id));
|
|
16
|
+
this.eventEmitter.emit(BotEventType.inlineProcessingStarted, {
|
|
17
|
+
query
|
|
18
|
+
});
|
|
19
|
+
const queryBeingProcessed = queriesInProcessing.get(query.userId);
|
|
20
|
+
if (queryBeingProcessed) {
|
|
21
|
+
this.eventEmitter.emit(BotEventType.inlineProcessingAborting, {
|
|
22
|
+
newQuery: query,
|
|
23
|
+
abortedQuery: queryBeingProcessed
|
|
24
|
+
});
|
|
25
|
+
queryBeingProcessed.abortController.abort();
|
|
26
|
+
queriesInProcessing.delete(query.userId);
|
|
27
|
+
}
|
|
28
|
+
pendingInlineQueries = pendingInlineQueries.filter((q) => q.userId != query.userId);
|
|
29
|
+
pendingInlineQueries.push(query);
|
|
30
|
+
});
|
|
31
|
+
this.scheduler.createTask('InlineQueryProcessing', async () => {
|
|
32
|
+
const ctx = new InlineQueryContextInternal(this.storage, this.scheduler, this.eventEmitter);
|
|
33
|
+
const queriesToProcess = [...pendingInlineQueries];
|
|
34
|
+
pendingInlineQueries = [];
|
|
35
|
+
for (const inlineQuery of queriesToProcess) {
|
|
36
|
+
queriesInProcessing.set(inlineQuery.userId, inlineQuery);
|
|
37
|
+
for (const inlineQueryAction of this.inlineQueries) {
|
|
38
|
+
this.initializeInlineQueryContext(ctx, inlineQuery.query, inlineQuery.queryId, inlineQueryAction, inlineQuery.abortController.signal, inlineQuery.traceId);
|
|
39
|
+
await this.executeAction(inlineQueryAction, ctx, (error, _) => {
|
|
40
|
+
if (error.name == 'AbortError') {
|
|
41
|
+
this.eventEmitter.emit(BotEventType.inlineProcessingAborted, {
|
|
42
|
+
abortedQuery: inlineQuery
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.eventEmitter.emit(BotEventType.error, {
|
|
47
|
+
error
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
queriesInProcessing.delete(inlineQuery.userId);
|
|
53
|
+
}
|
|
54
|
+
this.api.flushResponses();
|
|
55
|
+
}, period, false, this.botName);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
initializeInlineQueryContext(ctx, queryText, queryId, action, abortSignal, traceId) {
|
|
59
|
+
ctx.queryText = queryText;
|
|
60
|
+
ctx.queryId = queryId;
|
|
61
|
+
ctx.botName = this.botName;
|
|
62
|
+
ctx.action = action;
|
|
63
|
+
ctx.traceId = traceId;
|
|
64
|
+
ctx.abortSignal = abortSignal;
|
|
65
|
+
ctx.isInitialized = true;
|
|
66
|
+
ctx.queryResults = [];
|
|
67
|
+
ctx.matchResults = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
2
|
+
import { IActionState } from '../../types/actionState';
|
|
3
|
+
import { IScheduler } from '../../types/scheduler';
|
|
4
|
+
import { IStorageClient } from '../../types/storage';
|
|
5
|
+
import { Seconds } from '../../types/timeValues';
|
|
6
|
+
import { TelegramApiService } from '../telegramApi';
|
|
7
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
8
|
+
import { TypedEventEmitter } from '../../types/events';
|
|
9
|
+
export declare class ScheduledActionProcessor extends BaseActionProcessor {
|
|
10
|
+
private readonly chats;
|
|
11
|
+
private scheduled;
|
|
12
|
+
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler, eventEmitter: TypedEventEmitter);
|
|
13
|
+
initialize(api: TelegramApiService, scheduled: ScheduledAction<IActionState>[], period: Seconds): void;
|
|
14
|
+
private runScheduled;
|
|
15
|
+
private initializeChatContext;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=scheduledActionProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduledActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/scheduledActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAgB,MAAM,wBAAwB,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAEvD,qBAAa,wBAAyB,SAAQ,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAE/C,OAAO,CAAC,SAAS,CAAmC;gBAGhD,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IAMnC,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YAgDL,YAAY;IA2B1B,OAAO,CAAC,qBAAqB;CAahC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import { ChatInfo } from '../../dtos/chatInfo';
|
|
3
|
+
import { ChatContextInternal } from '../../entities/context/chatContext';
|
|
4
|
+
import { secondsToMilliseconds } from '../../helpers/timeConvertions';
|
|
5
|
+
import { createTrace } from '../../helpers/traceFactory';
|
|
6
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
7
|
+
export class ScheduledActionProcessor extends BaseActionProcessor {
|
|
8
|
+
chats;
|
|
9
|
+
scheduled;
|
|
10
|
+
constructor(botName, chats, storage, scheduler, eventEmitter) {
|
|
11
|
+
super(botName, storage, scheduler, eventEmitter);
|
|
12
|
+
this.chats = chats;
|
|
13
|
+
}
|
|
14
|
+
initialize(api, scheduled, period) {
|
|
15
|
+
this.initializeDependencies(api);
|
|
16
|
+
this.scheduled = scheduled;
|
|
17
|
+
if (this.scheduled.length > 0) {
|
|
18
|
+
const now = moment();
|
|
19
|
+
if (now.minute() == 0 && now.second() == 0) {
|
|
20
|
+
this.scheduler.createTask('ScheduledProcessing', async () => {
|
|
21
|
+
await this.runScheduled();
|
|
22
|
+
}, secondsToMilliseconds(period), true, this.botName);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let nextExecutionTime = now.clone().startOf('hour');
|
|
26
|
+
if (now.minute() > 0 || now.second() > 0) {
|
|
27
|
+
nextExecutionTime = nextExecutionTime.add(1, 'hour');
|
|
28
|
+
}
|
|
29
|
+
const delay = nextExecutionTime.diff(now);
|
|
30
|
+
this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', () => {
|
|
31
|
+
this.scheduler.createTask('ScheduledProcessing', () => {
|
|
32
|
+
void this.runScheduled();
|
|
33
|
+
}, secondsToMilliseconds(period), true, this.botName);
|
|
34
|
+
}, delay, this.botName);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async runScheduled() {
|
|
38
|
+
const ctx = new ChatContextInternal(this.storage, this.scheduler, this.eventEmitter);
|
|
39
|
+
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
40
|
+
for (const scheduledAction of this.scheduled) {
|
|
41
|
+
this.initializeChatContext(ctx, scheduledAction, new ChatInfo(chatId, chatName, []), createTrace(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
|
|
42
|
+
await this.executeAction(scheduledAction, ctx);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.api.flushResponses();
|
|
46
|
+
}
|
|
47
|
+
initializeChatContext(ctx, action, chatInfo, traceId) {
|
|
48
|
+
ctx.responses = [];
|
|
49
|
+
ctx.isInitialized = true;
|
|
50
|
+
ctx.botName = this.botName;
|
|
51
|
+
ctx.action = action;
|
|
52
|
+
ctx.chatInfo = chatInfo;
|
|
53
|
+
ctx.traceId = traceId;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { IStorageClient } from '../types/storage';
|
|
2
|
+
import { IActionState } from '../types/actionState';
|
|
3
|
+
import { IActionWithState, ActionKey } from '../types/action';
|
|
4
|
+
import { TypedEventEmitter } from '../types/events';
|
|
5
|
+
export declare class JsonFileStorage implements IStorageClient {
|
|
6
|
+
private readonly eventEmitter;
|
|
7
|
+
private readonly filePaths;
|
|
8
|
+
private readonly locks;
|
|
9
|
+
private readonly cache;
|
|
10
|
+
private readonly storagePath;
|
|
11
|
+
private readonly botName;
|
|
12
|
+
constructor(botName: string, actions: IActionWithState<IActionState>[], eventEmitter: TypedEventEmitter, path?: string);
|
|
13
|
+
private backfillEmptyActionStates;
|
|
14
|
+
private lock;
|
|
15
|
+
private tryGetFromCache;
|
|
16
|
+
private loadFromFile;
|
|
17
|
+
private updateCacheAndSaveToFile;
|
|
18
|
+
load<TActionState extends IActionState>(key: ActionKey): Promise<Record<number, TActionState | undefined>>;
|
|
19
|
+
saveMetadata(actions: IActionWithState<IActionState>[]): Promise<void>;
|
|
20
|
+
getActionState<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number): Promise<TActionState | (TActionState & undefined)>;
|
|
21
|
+
saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, state: TActionState): Promise<void>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
updateStateFor<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, update: (state: TActionState) => Promise<void> | void): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=jsonFileStorage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../services/jsonFileStorage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAMlE,qBAAa,eAAgB,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoB;IACjD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgC;IAC1D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmC;IACzD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA4C;IAClE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG7B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,YAAY,EAAE,iBAAiB,EAC/B,IAAI,CAAC,EAAE,MAAM;IAsBjB,OAAO,CAAC,yBAAyB;YAanB,IAAI;IAelB,OAAO,CAAC,eAAe;YAIT,YAAY;YA6BZ,wBAAwB;IAuBhC,IAAI,CAAC,YAAY,SAAS,YAAY,EAAE,GAAG,EAAE,SAAS;IAStD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE;IAQtD,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM;IAsBZ,yBAAyB,CAAC,YAAY,SAAS,YAAY,EAC7D,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY;IAcjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;CAkB5D"}
|