chz-telegram-bot 0.5.1 → 0.5.3
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/dtos/chatHistoryMessage.ts +9 -1
- package/entities/botInstance.ts +1 -1
- package/entities/context/messageContext.ts +3 -3
- package/entities/context/replyContext.ts +3 -3
- package/{eslint.config.js → eslint.config.ts} +8 -5
- package/index.ts +3 -0
- package/package.json +38 -37
- package/services/actionProcessors/commandActionProcessor.ts +7 -7
- package/services/jsonFileStorage.ts +1 -1
- package/services/nodeTimeoutScheduler.ts +2 -2
- package/tsconfig.json +2 -2
- package/bun.lock +0 -301
- package/dist/builtin/helpAction.d.ts +0 -2
- package/dist/builtin/helpAction.d.ts.map +0 -1
- package/dist/builtin/helpAction.js +0 -17
- package/dist/dtos/chatHistoryMessage.d.ts +0 -13
- package/dist/dtos/chatHistoryMessage.d.ts.map +0 -1
- package/dist/dtos/chatHistoryMessage.js +0 -20
- package/dist/dtos/chatInfo.d.ts +0 -17
- package/dist/dtos/chatInfo.d.ts.map +0 -1
- package/dist/dtos/chatInfo.js +0 -20
- package/dist/dtos/commandTriggerCheckResult.d.ts +0 -24
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +0 -1
- package/dist/dtos/commandTriggerCheckResult.js +0 -38
- package/dist/dtos/cooldownInfo.d.ts +0 -13
- package/dist/dtos/cooldownInfo.d.ts.map +0 -1
- package/dist/dtos/cooldownInfo.js +0 -16
- package/dist/dtos/incomingMessage.d.ts +0 -19
- package/dist/dtos/incomingMessage.d.ts.map +0 -1
- package/dist/dtos/incomingMessage.js +0 -67
- package/dist/dtos/incomingQuery.d.ts +0 -10
- package/dist/dtos/incomingQuery.d.ts.map +0 -1
- package/dist/dtos/incomingQuery.js +0 -18
- package/dist/dtos/messageInfo.d.ts +0 -22
- package/dist/dtos/messageInfo.d.ts.map +0 -1
- package/dist/dtos/messageInfo.js +0 -24
- package/dist/dtos/propertyProviderSets.d.ts +0 -16
- package/dist/dtos/propertyProviderSets.d.ts.map +0 -1
- package/dist/dtos/propertyProviderSets.js +0 -2
- package/dist/dtos/replyInfo.d.ts +0 -6
- package/dist/dtos/replyInfo.d.ts.map +0 -1
- package/dist/dtos/replyInfo.js +0 -12
- package/dist/dtos/responses/delay.d.ts +0 -16
- package/dist/dtos/responses/delay.d.ts.map +0 -1
- package/dist/dtos/responses/delay.js +0 -19
- package/dist/dtos/responses/imageMessage.d.ts +0 -22
- package/dist/dtos/responses/imageMessage.d.ts.map +0 -1
- package/dist/dtos/responses/imageMessage.js +0 -25
- package/dist/dtos/responses/inlineQueryResponse.d.ts +0 -13
- package/dist/dtos/responses/inlineQueryResponse.d.ts.map +0 -1
- package/dist/dtos/responses/inlineQueryResponse.js +0 -19
- package/dist/dtos/responses/reaction.d.ts +0 -16
- package/dist/dtos/responses/reaction.d.ts.map +0 -1
- package/dist/dtos/responses/reaction.js +0 -21
- package/dist/dtos/responses/textMessage.d.ts +0 -23
- package/dist/dtos/responses/textMessage.d.ts.map +0 -1
- package/dist/dtos/responses/textMessage.js +0 -28
- package/dist/dtos/responses/unpin.d.ts +0 -15
- package/dist/dtos/responses/unpin.d.ts.map +0 -1
- package/dist/dtos/responses/unpin.js +0 -19
- package/dist/dtos/responses/videoMessage.d.ts +0 -22
- package/dist/dtos/responses/videoMessage.d.ts.map +0 -1
- package/dist/dtos/responses/videoMessage.js +0 -25
- package/dist/dtos/userInfo.d.ts +0 -12
- package/dist/dtos/userInfo.d.ts.map +0 -1
- package/dist/dtos/userInfo.js +0 -16
- package/dist/entities/actions/commandAction.d.ts +0 -31
- package/dist/entities/actions/commandAction.d.ts.map +0 -1
- package/dist/entities/actions/commandAction.js +0 -146
- package/dist/entities/actions/inlineQueryAction.d.ts +0 -14
- package/dist/entities/actions/inlineQueryAction.d.ts.map +0 -1
- package/dist/entities/actions/inlineQueryAction.js +0 -47
- package/dist/entities/actions/replyCaptureAction.d.ts +0 -15
- package/dist/entities/actions/replyCaptureAction.d.ts.map +0 -1
- package/dist/entities/actions/replyCaptureAction.js +0 -62
- package/dist/entities/actions/scheduledAction.d.ts +0 -24
- package/dist/entities/actions/scheduledAction.d.ts.map +0 -1
- package/dist/entities/actions/scheduledAction.js +0 -79
- package/dist/entities/botInstance.d.ts +0 -37
- package/dist/entities/botInstance.d.ts.map +0 -1
- package/dist/entities/botInstance.js +0 -41
- package/dist/entities/cachedStateFactory.d.ts +0 -7
- package/dist/entities/cachedStateFactory.d.ts.map +0 -1
- package/dist/entities/cachedStateFactory.js +0 -12
- package/dist/entities/context/baseContext.d.ts +0 -39
- package/dist/entities/context/baseContext.d.ts.map +0 -1
- package/dist/entities/context/baseContext.js +0 -59
- package/dist/entities/context/chatContext.d.ts +0 -50
- package/dist/entities/context/chatContext.d.ts.map +0 -1
- package/dist/entities/context/chatContext.js +0 -69
- package/dist/entities/context/inlineQueryContext.d.ts +0 -27
- package/dist/entities/context/inlineQueryContext.d.ts.map +0 -1
- package/dist/entities/context/inlineQueryContext.js +0 -33
- package/dist/entities/context/messageContext.d.ts +0 -92
- package/dist/entities/context/messageContext.d.ts.map +0 -1
- package/dist/entities/context/messageContext.js +0 -120
- package/dist/entities/context/replyContext.d.ts +0 -89
- package/dist/entities/context/replyContext.d.ts.map +0 -1
- package/dist/entities/context/replyContext.js +0 -128
- package/dist/entities/states/actionStateBase.d.ts +0 -6
- package/dist/entities/states/actionStateBase.d.ts.map +0 -1
- package/dist/entities/states/actionStateBase.js +0 -8
- package/dist/entities/taskRecord.d.ts +0 -8
- package/dist/entities/taskRecord.d.ts.map +0 -1
- package/dist/entities/taskRecord.js +0 -14
- package/dist/helpers/builders/commandActionBuilder.d.ts +0 -100
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +0 -1
- package/dist/helpers/builders/commandActionBuilder.js +0 -151
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +0 -37
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +0 -1
- package/dist/helpers/builders/inlineQueryActionBuilder.js +0 -54
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +0 -68
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +0 -1
- package/dist/helpers/builders/scheduledActionBuilder.js +0 -100
- package/dist/helpers/mapUtils.d.ts +0 -10
- package/dist/helpers/mapUtils.d.ts.map +0 -1
- package/dist/helpers/mapUtils.js +0 -17
- package/dist/helpers/noop.d.ts +0 -9
- package/dist/helpers/noop.d.ts.map +0 -1
- package/dist/helpers/noop.js +0 -21
- package/dist/helpers/objectFromEntries.d.ts +0 -2
- package/dist/helpers/objectFromEntries.d.ts.map +0 -1
- package/dist/helpers/objectFromEntries.js +0 -7
- package/dist/helpers/timeConvertions.d.ts +0 -5
- package/dist/helpers/timeConvertions.d.ts.map +0 -1
- package/dist/helpers/timeConvertions.js +0 -14
- package/dist/helpers/toArray.d.ts +0 -2
- package/dist/helpers/toArray.d.ts.map +0 -1
- package/dist/helpers/toArray.js +0 -6
- package/dist/helpers/traceFactory.d.ts +0 -3
- package/dist/helpers/traceFactory.d.ts.map +0 -1
- package/dist/helpers/traceFactory.js +0 -6
- package/dist/index.d.ts +0 -24
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -41
- package/dist/main.d.ts +0 -52
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -37
- package/dist/services/actionProcessingService.d.ts +0 -25
- package/dist/services/actionProcessingService.d.ts.map +0 -1
- package/dist/services/actionProcessingService.js +0 -54
- package/dist/services/actionProcessors/baseProcessor.d.ts +0 -18
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +0 -1
- package/dist/services/actionProcessors/baseProcessor.js +0 -33
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +0 -20
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +0 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +0 -119
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +0 -11
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +0 -1
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +0 -65
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +0 -17
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +0 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.js +0 -63
- package/dist/services/jsonFileStorage.d.ts +0 -23
- package/dist/services/jsonFileStorage.d.ts.map +0 -1
- package/dist/services/jsonFileStorage.js +0 -113
- package/dist/services/jsonLogger.d.ts +0 -11
- package/dist/services/jsonLogger.d.ts.map +0 -1
- package/dist/services/jsonLogger.js +0 -70
- package/dist/services/nodeTimeoutScheduler.d.ts +0 -13
- package/dist/services/nodeTimeoutScheduler.d.ts.map +0 -1
- package/dist/services/nodeTimeoutScheduler.js +0 -35
- package/dist/services/responseProcessingQueue.d.ts +0 -12
- package/dist/services/responseProcessingQueue.d.ts.map +0 -1
- package/dist/services/responseProcessingQueue.js +0 -41
- package/dist/services/telegramApi.d.ts +0 -23
- package/dist/services/telegramApi.d.ts.map +0 -1
- package/dist/services/telegramApi.js +0 -137
- package/dist/types/action.d.ts +0 -14
- package/dist/types/action.d.ts.map +0 -1
- package/dist/types/action.js +0 -2
- package/dist/types/actionState.d.ts +0 -5
- package/dist/types/actionState.d.ts.map +0 -1
- package/dist/types/actionState.js +0 -2
- package/dist/types/cachedValueAccessor.d.ts +0 -2
- package/dist/types/cachedValueAccessor.d.ts.map +0 -1
- package/dist/types/cachedValueAccessor.js +0 -2
- package/dist/types/capture.d.ts +0 -24
- package/dist/types/capture.d.ts.map +0 -1
- package/dist/types/capture.js +0 -2
- package/dist/types/commandCondition.d.ts +0 -8
- package/dist/types/commandCondition.d.ts.map +0 -1
- package/dist/types/commandCondition.js +0 -2
- package/dist/types/commandTrigger.d.ts +0 -2
- package/dist/types/commandTrigger.d.ts.map +0 -1
- package/dist/types/commandTrigger.js +0 -2
- package/dist/types/externalAliases.d.ts +0 -11
- package/dist/types/externalAliases.d.ts.map +0 -1
- package/dist/types/externalAliases.js +0 -2
- package/dist/types/handlers.d.ts +0 -21
- package/dist/types/handlers.d.ts.map +0 -1
- package/dist/types/handlers.js +0 -2
- package/dist/types/inputFile.d.ts +0 -5
- package/dist/types/inputFile.d.ts.map +0 -1
- package/dist/types/inputFile.js +0 -2
- package/dist/types/logger.d.ts +0 -13
- package/dist/types/logger.d.ts.map +0 -1
- package/dist/types/logger.js +0 -2
- package/dist/types/messageSendingOptions.d.ts +0 -9
- package/dist/types/messageSendingOptions.d.ts.map +0 -1
- package/dist/types/messageSendingOptions.js +0 -2
- package/dist/types/messageTypes.d.ts +0 -20
- package/dist/types/messageTypes.d.ts.map +0 -1
- package/dist/types/messageTypes.js +0 -21
- package/dist/types/propertyProvider.d.ts +0 -8
- package/dist/types/propertyProvider.d.ts.map +0 -1
- package/dist/types/propertyProvider.js +0 -2
- package/dist/types/response.d.ts +0 -39
- package/dist/types/response.d.ts.map +0 -1
- package/dist/types/response.js +0 -12
- package/dist/types/scheduler.d.ts +0 -7
- package/dist/types/scheduler.d.ts.map +0 -1
- package/dist/types/scheduler.js +0 -2
- package/dist/types/storage.d.ts +0 -11
- package/dist/types/storage.d.ts.map +0 -1
- package/dist/types/storage.js +0 -2
- package/dist/types/timeValues.d.ts +0 -15
- package/dist/types/timeValues.d.ts.map +0 -1
- package/dist/types/timeValues.js +0 -2
- package/dist/types/trace.d.ts +0 -6
- package/dist/types/trace.d.ts.map +0 -1
- package/dist/types/trace.js +0 -2
|
@@ -1,20 +0,0 @@
|
|
|
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 { TraceId } from '../../types/trace';
|
|
6
|
-
import { ChatInfo } from '../../dtos/chatInfo';
|
|
7
|
-
import { BaseActionProcessor } from './baseProcessor';
|
|
8
|
-
import { BotInfo, TelegramBot } from '../../types/externalAliases';
|
|
9
|
-
export declare class CommandActionProcessor extends BaseActionProcessor {
|
|
10
|
-
private readonly replyCaptures;
|
|
11
|
-
private readonly chatHistory;
|
|
12
|
-
private botInfo;
|
|
13
|
-
private commands;
|
|
14
|
-
initialize(api: TelegramApiService, telegram: TelegramBot, commands: CommandAction<IActionState>[], verboseLoggingForIncomingMessage: boolean, botInfo: BotInfo): void;
|
|
15
|
-
captureRegistrationCallback(capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId): void;
|
|
16
|
-
private processMessage;
|
|
17
|
-
private initializeReplyCaptureContext;
|
|
18
|
-
private initializeMessageContext;
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=commandActionProcessor.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,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;AAInE,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,gCAAgC,EAAE,OAAO,EACzC,OAAO,EAAE,OAAO;IA2DpB,2BAA2B,CACvB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO;YAgCN,cAAc;IAoD5B,OAAO,CAAC,6BAA6B;IAiCrC,OAAO,CAAC,wBAAwB;CAmCnC"}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CommandActionProcessor = void 0;
|
|
4
|
-
const incomingMessage_1 = require("../../dtos/incomingMessage");
|
|
5
|
-
const replyCaptureAction_1 = require("../../entities/actions/replyCaptureAction");
|
|
6
|
-
const messageContext_1 = require("../../entities/context/messageContext");
|
|
7
|
-
const replyContext_1 = require("../../entities/context/replyContext");
|
|
8
|
-
const messageTypes_1 = require("../../types/messageTypes");
|
|
9
|
-
const objectFromEntries_1 = require("../../helpers/objectFromEntries");
|
|
10
|
-
const baseProcessor_1 = require("./baseProcessor");
|
|
11
|
-
const mapUtils_1 = require("../../helpers/mapUtils");
|
|
12
|
-
const messageInfo_1 = require("../../dtos/messageInfo");
|
|
13
|
-
const userInfo_1 = require("../../dtos/userInfo");
|
|
14
|
-
const chatHistoryMessage_1 = require("../../dtos/chatHistoryMessage");
|
|
15
|
-
const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
|
|
16
|
-
class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
17
|
-
replyCaptures = [];
|
|
18
|
-
chatHistory = new Map();
|
|
19
|
-
botInfo;
|
|
20
|
-
commands = (0, objectFromEntries_1.typeSafeObjectFromEntries)(Object.values(messageTypes_1.MessageType).map((x) => [
|
|
21
|
-
x,
|
|
22
|
-
[]
|
|
23
|
-
]));
|
|
24
|
-
initialize(api, telegram, commands, verboseLoggingForIncomingMessage, botInfo) {
|
|
25
|
-
this.botInfo = botInfo;
|
|
26
|
-
this.initializeDependencies(api);
|
|
27
|
-
for (const msgType of Object.values(messageTypes_1.MessageType)) {
|
|
28
|
-
if (msgType == messageTypes_1.MessageType.Text) {
|
|
29
|
-
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.find((x) => typeof x != 'string') !=
|
|
30
|
-
undefined ||
|
|
31
|
-
cmd.triggers.find((x) => typeof x == 'string' &&
|
|
32
|
-
!x.startsWith(messageTypes_1.INTERNAL_MESSAGE_TYPE_PREFIX)) ||
|
|
33
|
-
cmd.triggers.includes(messageTypes_1.MessageType.Text) ||
|
|
34
|
-
cmd.triggers.includes(messageTypes_1.MessageType.Any));
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.includes(msgType) ||
|
|
38
|
-
cmd.triggers.includes(messageTypes_1.MessageType.Any));
|
|
39
|
-
}
|
|
40
|
-
if (commands.length > 0) {
|
|
41
|
-
telegram.on('message', ({ message }) => {
|
|
42
|
-
const internalMessage = new incomingMessage_1.IncomingMessage(message, this.botName, (0, mapUtils_1.getOrSetIfNotExists)(this.chatHistory, message.chat.id, []));
|
|
43
|
-
const logger = this.logger.createScope(this.botName, internalMessage.traceId, internalMessage.chatInfo.name);
|
|
44
|
-
if (verboseLoggingForIncomingMessage) {
|
|
45
|
-
logger.logObjectWithTraceId(message);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
logger.logWithTraceId(`${internalMessage.from?.first_name ?? 'Unknown'} (${internalMessage.from?.id ?? 'Unknown'}): ${internalMessage.text || internalMessage.type}`);
|
|
49
|
-
}
|
|
50
|
-
void this.processMessage(internalMessage);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
captureRegistrationCallback(capture, parentMessageId, chatInfo, traceId) {
|
|
55
|
-
const replyAction = new replyCaptureAction_1.ReplyCaptureAction(parentMessageId, capture.action, capture.handler, capture.trigger, capture.abortController);
|
|
56
|
-
const logger = this.logger.createScope(this.botName, traceId, chatInfo.name);
|
|
57
|
-
logger.logWithTraceId(`Starting capturing replies to message ${parentMessageId} with action ${replyAction.key}`);
|
|
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
|
-
logger.logWithTraceId(`Stopping capturing replies to message ${parentMessageId} with action ${replyAction.key}`);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
async processMessage(msg) {
|
|
66
|
-
const chatHistoryArray = (0, mapUtils_1.getOrSetIfNotExists)(this.chatHistory, msg.chatInfo.id, []);
|
|
67
|
-
while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
|
|
68
|
-
chatHistoryArray.shift();
|
|
69
|
-
chatHistoryArray.push(new chatHistoryMessage_1.ChatHistoryMessage(msg.messageId, msg.from, msg.text, msg.type, msg.traceId, msg.replyToMessageId));
|
|
70
|
-
const ctx = new messageContext_1.MessageContextInternal(this.storage, this.scheduler);
|
|
71
|
-
const commandsToCheck = new Set(this.commands[msg.type]);
|
|
72
|
-
if (msg.type != messageTypes_1.MessageType.Text && msg.text != '') {
|
|
73
|
-
this.commands[messageTypes_1.MessageType.Text].forEach((x) => commandsToCheck.add(x));
|
|
74
|
-
}
|
|
75
|
-
for (const commandAction of commandsToCheck) {
|
|
76
|
-
this.initializeMessageContext(ctx, commandAction, msg);
|
|
77
|
-
await this.executeAction(commandAction, ctx);
|
|
78
|
-
}
|
|
79
|
-
if (this.replyCaptures.length != 0) {
|
|
80
|
-
const replyCtx = new replyContext_1.ReplyContextInternal(this.storage, this.scheduler);
|
|
81
|
-
for (const replyAction of this.replyCaptures) {
|
|
82
|
-
this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
|
|
83
|
-
await this.executeAction(replyAction, replyCtx);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
this.api.flushResponses();
|
|
87
|
-
}
|
|
88
|
-
initializeReplyCaptureContext(ctx, action, message) {
|
|
89
|
-
ctx.replyMessageId = message.replyToMessageId;
|
|
90
|
-
ctx.messageInfo = new messageInfo_1.MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
91
|
-
ctx.userInfo = new userInfo_1.UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
92
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
93
|
-
ctx.botName = this.botName;
|
|
94
|
-
ctx.action = action;
|
|
95
|
-
ctx.chatInfo = message.chatInfo;
|
|
96
|
-
ctx.traceId = message.traceId;
|
|
97
|
-
ctx.botInfo = this.botInfo;
|
|
98
|
-
ctx.isInitialized = true;
|
|
99
|
-
ctx.matchResults = [];
|
|
100
|
-
ctx.logger = this.logger.createScope(this.botName, message.traceId, message.chatInfo.name);
|
|
101
|
-
}
|
|
102
|
-
initializeMessageContext(ctx, action, message) {
|
|
103
|
-
ctx.messageInfo = new messageInfo_1.MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
104
|
-
ctx.userInfo = new userInfo_1.UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
105
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
106
|
-
ctx.matchResults = [];
|
|
107
|
-
ctx.startCooldown = true;
|
|
108
|
-
ctx.responses = [];
|
|
109
|
-
ctx.isInitialized = true;
|
|
110
|
-
ctx.botName = this.botName;
|
|
111
|
-
ctx.action = action;
|
|
112
|
-
ctx.chatInfo = message.chatInfo;
|
|
113
|
-
ctx.traceId = message.traceId;
|
|
114
|
-
ctx.botInfo = this.botInfo;
|
|
115
|
-
ctx.customCooldown = undefined;
|
|
116
|
-
ctx.logger = this.logger.createScope(this.botName, message.traceId, message.chatInfo.name);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
exports.CommandActionProcessor = CommandActionProcessor;
|
|
@@ -1,11 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inlineQueryActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/inlineQueryActionProcessor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAG7E,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;IAuGxB,OAAO,CAAC,4BAA4B;CAqBvC"}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InlineQueryActionProcessor = void 0;
|
|
4
|
-
const incomingQuery_1 = require("../../dtos/incomingQuery");
|
|
5
|
-
const inlineQueryContext_1 = require("../../entities/context/inlineQueryContext");
|
|
6
|
-
const traceFactory_1 = require("../../helpers/traceFactory");
|
|
7
|
-
const baseProcessor_1 = require("./baseProcessor");
|
|
8
|
-
class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
9
|
-
inlineQueries;
|
|
10
|
-
initialize(api, telegram, inlineQueries, period) {
|
|
11
|
-
this.initializeDependencies(api);
|
|
12
|
-
this.inlineQueries = inlineQueries;
|
|
13
|
-
let pendingInlineQueries = [];
|
|
14
|
-
const queriesInProcessing = new Map();
|
|
15
|
-
if (this.inlineQueries.length > 0) {
|
|
16
|
-
telegram.on('inline_query', ({ inlineQuery }) => {
|
|
17
|
-
const query = new incomingQuery_1.IncomingInlineQuery(inlineQuery.id, inlineQuery.query, inlineQuery.from.id, (0, traceFactory_1.createTrace)('InlineQuery', this.botName, inlineQuery.id));
|
|
18
|
-
const logger = this.logger.createScope(this.botName, query.traceId, 'Query');
|
|
19
|
-
logger.logWithTraceId(`${inlineQuery.from.username ?? 'Unknown'} (${inlineQuery.from.id}): Query for ${inlineQuery.query}`);
|
|
20
|
-
const queryBeingProcessed = queriesInProcessing.get(query.userId);
|
|
21
|
-
if (queryBeingProcessed) {
|
|
22
|
-
logger.logWithTraceId(`Aborting query ${queryBeingProcessed.queryId} (${queryBeingProcessed.query}): new query recieved from ${query.userId}`);
|
|
23
|
-
queryBeingProcessed.abortController.abort();
|
|
24
|
-
queriesInProcessing.delete(query.userId);
|
|
25
|
-
}
|
|
26
|
-
pendingInlineQueries = pendingInlineQueries.filter((q) => q.userId != query.userId);
|
|
27
|
-
pendingInlineQueries.push(query);
|
|
28
|
-
});
|
|
29
|
-
this.scheduler.createTask('InlineQueryProcessing', async () => {
|
|
30
|
-
const ctx = new inlineQueryContext_1.InlineQueryContextInternal(this.storage, this.scheduler);
|
|
31
|
-
const queriesToProcess = [...pendingInlineQueries];
|
|
32
|
-
pendingInlineQueries = [];
|
|
33
|
-
for (const inlineQuery of queriesToProcess) {
|
|
34
|
-
queriesInProcessing.set(inlineQuery.userId, inlineQuery);
|
|
35
|
-
for (const inlineQueryAction of this.inlineQueries) {
|
|
36
|
-
this.initializeInlineQueryContext(ctx, inlineQuery.query, inlineQuery.queryId, inlineQueryAction, inlineQuery.abortController.signal, inlineQuery.traceId);
|
|
37
|
-
await this.executeAction(inlineQueryAction, ctx, (error, ctx) => {
|
|
38
|
-
if (error.name == 'AbortError') {
|
|
39
|
-
ctx.logger.logWithTraceId(`Aborting query ${inlineQuery.queryId} (${inlineQuery.query}) successful.`);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
ctx.logger.errorWithTraceId(error, ctx);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
queriesInProcessing.delete(inlineQuery.userId);
|
|
47
|
-
}
|
|
48
|
-
this.api.flushResponses();
|
|
49
|
-
}, period, false, this.botName);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
initializeInlineQueryContext(ctx, queryText, queryId, action, abortSignal, traceId) {
|
|
53
|
-
ctx.queryText = queryText;
|
|
54
|
-
ctx.queryId = queryId;
|
|
55
|
-
ctx.botName = this.botName;
|
|
56
|
-
ctx.action = action;
|
|
57
|
-
ctx.traceId = traceId;
|
|
58
|
-
ctx.abortSignal = abortSignal;
|
|
59
|
-
ctx.isInitialized = true;
|
|
60
|
-
ctx.queryResults = [];
|
|
61
|
-
ctx.matchResults = [];
|
|
62
|
-
ctx.logger = this.logger.createScope(this.botName, traceId, 'Unknown');
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.InlineQueryActionProcessor = InlineQueryActionProcessor;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
2
|
-
import { IActionState } from '../../types/actionState';
|
|
3
|
-
import { ILogger } from '../../types/logger';
|
|
4
|
-
import { IScheduler } from '../../types/scheduler';
|
|
5
|
-
import { IStorageClient } from '../../types/storage';
|
|
6
|
-
import { Seconds } from '../../types/timeValues';
|
|
7
|
-
import { TelegramApiService } from '../telegramApi';
|
|
8
|
-
import { BaseActionProcessor } from './baseProcessor';
|
|
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, logger: ILogger);
|
|
13
|
-
initialize(api: TelegramApiService, scheduled: ScheduledAction<IActionState>[], period: Seconds): void;
|
|
14
|
-
private runScheduled;
|
|
15
|
-
private initializeChatContext;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=scheduledActionProcessor.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,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;AAEtD,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,MAAM,EAAE,OAAO;IAMnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YAgDL,YAAY;IA0B1B,OAAO,CAAC,qBAAqB;CAmBhC"}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ScheduledActionProcessor = void 0;
|
|
7
|
-
const moment_1 = __importDefault(require("moment"));
|
|
8
|
-
const chatInfo_1 = require("../../dtos/chatInfo");
|
|
9
|
-
const chatContext_1 = require("../../entities/context/chatContext");
|
|
10
|
-
const timeConvertions_1 = require("../../helpers/timeConvertions");
|
|
11
|
-
const traceFactory_1 = require("../../helpers/traceFactory");
|
|
12
|
-
const baseProcessor_1 = require("./baseProcessor");
|
|
13
|
-
class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
14
|
-
chats;
|
|
15
|
-
scheduled;
|
|
16
|
-
constructor(botName, chats, storage, scheduler, logger) {
|
|
17
|
-
super(botName, storage, scheduler, logger);
|
|
18
|
-
this.chats = chats;
|
|
19
|
-
}
|
|
20
|
-
initialize(api, scheduled, period) {
|
|
21
|
-
this.initializeDependencies(api);
|
|
22
|
-
this.scheduled = scheduled;
|
|
23
|
-
if (this.scheduled.length > 0) {
|
|
24
|
-
const now = (0, moment_1.default)();
|
|
25
|
-
if (now.minute() == 0 && now.second() == 0) {
|
|
26
|
-
this.scheduler.createTask('ScheduledProcessing', async () => {
|
|
27
|
-
await this.runScheduled();
|
|
28
|
-
}, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.botName);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
let nextExecutionTime = now.clone().startOf('hour');
|
|
32
|
-
if (now.minute() > 0 || now.second() > 0) {
|
|
33
|
-
nextExecutionTime = nextExecutionTime.add(1, 'hour');
|
|
34
|
-
}
|
|
35
|
-
const delay = nextExecutionTime.diff(now);
|
|
36
|
-
this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', () => {
|
|
37
|
-
this.scheduler.createTask('ScheduledProcessing', () => {
|
|
38
|
-
void this.runScheduled();
|
|
39
|
-
}, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.botName);
|
|
40
|
-
}, delay, this.botName);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
async runScheduled() {
|
|
44
|
-
const ctx = new chatContext_1.ChatContextInternal(this.storage, this.scheduler);
|
|
45
|
-
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
46
|
-
for (const scheduledAction of this.scheduled) {
|
|
47
|
-
this.initializeChatContext(ctx, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName, []), (0, traceFactory_1.createTrace)(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
|
|
48
|
-
await this.executeAction(scheduledAction, ctx);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
this.api.flushResponses();
|
|
52
|
-
}
|
|
53
|
-
initializeChatContext(ctx, action, chatInfo, traceId) {
|
|
54
|
-
ctx.responses = [];
|
|
55
|
-
ctx.isInitialized = true;
|
|
56
|
-
ctx.botName = this.botName;
|
|
57
|
-
ctx.action = action;
|
|
58
|
-
ctx.chatInfo = chatInfo;
|
|
59
|
-
ctx.traceId = traceId;
|
|
60
|
-
ctx.logger = this.logger.createScope(this.botName, traceId, chatInfo.name);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
exports.ScheduledActionProcessor = ScheduledActionProcessor;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { IStorageClient } from '../types/storage';
|
|
2
|
-
import { IActionState } from '../types/actionState';
|
|
3
|
-
import { IActionWithState, ActionKey } from '../types/action';
|
|
4
|
-
export declare class JsonFileStorage implements IStorageClient {
|
|
5
|
-
private readonly filePaths;
|
|
6
|
-
private readonly locks;
|
|
7
|
-
private readonly cache;
|
|
8
|
-
private readonly storagePath;
|
|
9
|
-
private readonly botName;
|
|
10
|
-
constructor(botName: string, actions: IActionWithState<IActionState>[], path?: string);
|
|
11
|
-
private backfillEmptyActionStates;
|
|
12
|
-
private lock;
|
|
13
|
-
private tryGetFromCache;
|
|
14
|
-
private loadFromFile;
|
|
15
|
-
private updateCacheAndSaveToFile;
|
|
16
|
-
load<TActionState extends IActionState>(key: ActionKey): Promise<Record<number, TActionState | undefined>>;
|
|
17
|
-
saveMetadata(actions: IActionWithState<IActionState>[]): Promise<void>;
|
|
18
|
-
getActionState<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number): Promise<TActionState | (TActionState & undefined)>;
|
|
19
|
-
saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, state: TActionState): Promise<void>;
|
|
20
|
-
close(): Promise<void>;
|
|
21
|
-
updateStateFor<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, update: (state: TActionState) => Promise<void> | void): Promise<void>;
|
|
22
|
-
}
|
|
23
|
-
//# sourceMappingURL=jsonFileStorage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAO9D,qBAAa,eAAgB,YAAW,cAAc;IAClD,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,IAAI,CAAC,EAAE,MAAM;IAqBjB,OAAO,CAAC,yBAAyB;YAanB,IAAI;IAYlB,OAAO,CAAC,eAAe;YAIT,YAAY;YA6BZ,wBAAwB;IAehC,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;IAWZ,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"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JsonFileStorage = void 0;
|
|
4
|
-
const fs_1 = require("fs");
|
|
5
|
-
const promises_1 = require("fs/promises");
|
|
6
|
-
const async_sema_1 = require("async-sema");
|
|
7
|
-
const mapUtils_1 = require("../helpers/mapUtils");
|
|
8
|
-
function buildPath(storagePath, botName, actionKey) {
|
|
9
|
-
return `${storagePath}/${botName}/${actionKey.replaceAll(':', '/')}.json`;
|
|
10
|
-
}
|
|
11
|
-
class JsonFileStorage {
|
|
12
|
-
filePaths = new Map();
|
|
13
|
-
locks = new Map();
|
|
14
|
-
cache;
|
|
15
|
-
storagePath;
|
|
16
|
-
botName;
|
|
17
|
-
constructor(botName, actions, path) {
|
|
18
|
-
this.cache = new Map();
|
|
19
|
-
this.botName = botName;
|
|
20
|
-
this.storagePath = path ?? 'storage';
|
|
21
|
-
if (!(0, fs_1.existsSync)(`${this.storagePath}/${this.botName}/`)) {
|
|
22
|
-
(0, fs_1.mkdirSync)(`${this.storagePath}/${this.botName}/`, {
|
|
23
|
-
recursive: true
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
for (const action of actions) {
|
|
27
|
-
this.locks.set(action.key, new async_sema_1.Sema(1));
|
|
28
|
-
this.filePaths.set(action.key, buildPath(this.storagePath, this.botName, action.key));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
backfillEmptyActionStates(action, data) {
|
|
32
|
-
for (const [stringKey, value] of Object.entries(data)) {
|
|
33
|
-
if (value)
|
|
34
|
-
continue;
|
|
35
|
-
data[parseInt(stringKey)] = action.stateConstructor();
|
|
36
|
-
}
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
async lock(key, action) {
|
|
40
|
-
const lock = (0, mapUtils_1.getOrSetIfNotExists)(this.locks, key, new async_sema_1.Sema(1));
|
|
41
|
-
await lock.acquire();
|
|
42
|
-
try {
|
|
43
|
-
return await action();
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
lock.release();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
tryGetFromCache(key) {
|
|
50
|
-
return this.cache.get(key);
|
|
51
|
-
}
|
|
52
|
-
async loadFromFile(key) {
|
|
53
|
-
const targetPath = (0, mapUtils_1.getOrSetIfNotExists)(this.filePaths, key, buildPath(this.storagePath, this.botName, key));
|
|
54
|
-
const fileContent = await (0, promises_1.readFile)(targetPath, {
|
|
55
|
-
encoding: 'utf-8',
|
|
56
|
-
flag: 'a+'
|
|
57
|
-
});
|
|
58
|
-
if (fileContent) {
|
|
59
|
-
const data = JSON.parse(fileContent);
|
|
60
|
-
this.cache.set(key, data);
|
|
61
|
-
}
|
|
62
|
-
return (this.cache.get(key) ?? {});
|
|
63
|
-
}
|
|
64
|
-
async updateCacheAndSaveToFile(data, key) {
|
|
65
|
-
this.cache.set(key, data);
|
|
66
|
-
const targetPath = (0, mapUtils_1.getOrSetIfNotExists)(this.filePaths, key, buildPath(this.storagePath, this.botName, key));
|
|
67
|
-
await (0, promises_1.writeFile)(targetPath, JSON.stringify(data), { flag: 'w+' });
|
|
68
|
-
}
|
|
69
|
-
async load(key) {
|
|
70
|
-
return (this.tryGetFromCache(key) ??
|
|
71
|
-
(await this.lock(key, async () => {
|
|
72
|
-
return await this.loadFromFile(key);
|
|
73
|
-
})));
|
|
74
|
-
}
|
|
75
|
-
async saveMetadata(actions) {
|
|
76
|
-
const targetPath = `${this.storagePath}/${this.botName}/Metadata-${this.botName}.json`;
|
|
77
|
-
await (0, promises_1.writeFile)(targetPath, JSON.stringify(actions), {
|
|
78
|
-
flag: 'w+'
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
async getActionState(action, chatId) {
|
|
82
|
-
const value = this.tryGetFromCache(action.key) ??
|
|
83
|
-
(await this.lock(action.key, async () => {
|
|
84
|
-
return await this.loadFromFile(action.key);
|
|
85
|
-
}));
|
|
86
|
-
return Object.assign(action.stateConstructor(), value[chatId]);
|
|
87
|
-
}
|
|
88
|
-
async saveActionExecutionResult(action, chatId, state) {
|
|
89
|
-
await this.lock(action.key, async () => {
|
|
90
|
-
const data = this.tryGetFromCache(action.key) ??
|
|
91
|
-
(await this.loadFromFile(action.key));
|
|
92
|
-
data[chatId] = state;
|
|
93
|
-
if (this.backfillEmptyActionStates(action, data))
|
|
94
|
-
await this.updateCacheAndSaveToFile(data, action.key);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
async close() {
|
|
98
|
-
for (const lock of this.locks.values()) {
|
|
99
|
-
await lock.acquire();
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
async updateStateFor(action, chatId, update) {
|
|
103
|
-
await this.lock(action.key, async () => {
|
|
104
|
-
const data = this.tryGetFromCache(action.key) ??
|
|
105
|
-
(await this.loadFromFile(action.key));
|
|
106
|
-
const state = Object.assign(action.stateConstructor(), data[chatId]);
|
|
107
|
-
await update(state);
|
|
108
|
-
if (this.backfillEmptyActionStates(action, data))
|
|
109
|
-
await this.updateCacheAndSaveToFile(data, action.key);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
exports.JsonFileStorage = JsonFileStorage;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ILogger, IScopedLogger } from '../types/logger';
|
|
2
|
-
import { TraceId } from '../types/trace';
|
|
3
|
-
export declare class JsonLogger implements ILogger {
|
|
4
|
-
private serializeError;
|
|
5
|
-
private getCircularReplacer;
|
|
6
|
-
createScope(botName: string, traceId: TraceId, chatName: string): IScopedLogger;
|
|
7
|
-
logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: unknown): void;
|
|
8
|
-
logWithTraceId(botName: string, traceId: TraceId, chatName: string, text: string): void;
|
|
9
|
-
errorWithTraceId(botName: string, traceId: TraceId, chatName: string, errorObj: unknown, extraData?: unknown): void;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=jsonLogger.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsonLogger.d.ts","sourceRoot":"","sources":["../../services/jsonLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,qBAAa,UAAW,YAAW,OAAO;IACtC,OAAO,CAAC,cAAc;IAkBtB,OAAO,CAAC,mBAAmB;IAc3B,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAiBtD,aAAa;IAGtB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO;IAoBjB,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM;IAOhB,gBAAgB,CACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,OAAO;CAe1B"}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JsonLogger = void 0;
|
|
4
|
-
class JsonLogger {
|
|
5
|
-
serializeError(error) {
|
|
6
|
-
if (error instanceof Error) {
|
|
7
|
-
const plainObject = {
|
|
8
|
-
name: error.name,
|
|
9
|
-
message: error.message,
|
|
10
|
-
stack: error.stack
|
|
11
|
-
};
|
|
12
|
-
for (const [key, value] of Object.entries(error)) {
|
|
13
|
-
plainObject[key] = value;
|
|
14
|
-
}
|
|
15
|
-
return JSON.stringify(plainObject);
|
|
16
|
-
}
|
|
17
|
-
return JSON.stringify({ error });
|
|
18
|
-
}
|
|
19
|
-
getCircularReplacer() {
|
|
20
|
-
const cache = new Set();
|
|
21
|
-
return (_, value) => {
|
|
22
|
-
if (typeof value === 'object' && value !== null) {
|
|
23
|
-
if (cache.has(value)) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
cache.add(value);
|
|
27
|
-
}
|
|
28
|
-
return value;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
createScope(botName, traceId, chatName) {
|
|
32
|
-
return {
|
|
33
|
-
logObjectWithTraceId: (data) => {
|
|
34
|
-
this.logObjectWithTraceId(botName, traceId, chatName, data);
|
|
35
|
-
},
|
|
36
|
-
logWithTraceId: (text) => {
|
|
37
|
-
this.logWithTraceId(botName, traceId, chatName, text);
|
|
38
|
-
},
|
|
39
|
-
errorWithTraceId: (errorObj, extraData) => {
|
|
40
|
-
this.errorWithTraceId(botName, traceId, chatName, errorObj, extraData);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
logObjectWithTraceId(botName, traceId, chatName, data) {
|
|
45
|
-
const enrichedData = typeof data == 'object'
|
|
46
|
-
? {
|
|
47
|
-
...data,
|
|
48
|
-
botName,
|
|
49
|
-
traceId,
|
|
50
|
-
chatName
|
|
51
|
-
}
|
|
52
|
-
: {
|
|
53
|
-
botName,
|
|
54
|
-
traceId,
|
|
55
|
-
chatName,
|
|
56
|
-
data
|
|
57
|
-
};
|
|
58
|
-
console.log(enrichedData);
|
|
59
|
-
}
|
|
60
|
-
logWithTraceId(botName, traceId, chatName, text) {
|
|
61
|
-
console.log(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`);
|
|
62
|
-
}
|
|
63
|
-
errorWithTraceId(botName, traceId, chatName, errorObj, extraData) {
|
|
64
|
-
const dataString = extraData
|
|
65
|
-
? `,"extraData":${JSON.stringify(extraData, this.getCircularReplacer())}`
|
|
66
|
-
: '';
|
|
67
|
-
console.error(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","error":${this.serializeError(errorObj)}${dataString}}`);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
exports.JsonLogger = JsonLogger;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { TaskRecord } from '../entities/taskRecord';
|
|
2
|
-
import { ILogger } from '../types/logger';
|
|
3
|
-
import { IScheduler } from '../types/scheduler';
|
|
4
|
-
import { Milliseconds } from '../types/timeValues';
|
|
5
|
-
export declare class NodeTimeoutScheduler implements IScheduler {
|
|
6
|
-
private readonly logger;
|
|
7
|
-
readonly activeTasks: TaskRecord[];
|
|
8
|
-
constructor(logger: ILogger);
|
|
9
|
-
stopAll(): void;
|
|
10
|
-
createTask(name: string, action: () => unknown, interval: Milliseconds, executeRightAway: boolean, ownerName: string): void;
|
|
11
|
-
createOnetimeTask(name: string, action: () => unknown, delay: Milliseconds, ownerName: string): void;
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=nodeTimeoutScheduler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nodeTimeoutScheduler.d.ts","sourceRoot":"","sources":["../../services/nodeTimeoutScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,qBAAa,oBAAqB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAClC,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAM;gBAE5B,MAAM,EAAE,OAAO;IAI3B,OAAO;IAMP,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,OAAO,EACrB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM;IAmBrB,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,OAAO,EACrB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM;CAoBxB"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NodeTimeoutScheduler = void 0;
|
|
4
|
-
const taskRecord_1 = require("../entities/taskRecord");
|
|
5
|
-
const traceFactory_1 = require("../helpers/traceFactory");
|
|
6
|
-
class NodeTimeoutScheduler {
|
|
7
|
-
logger;
|
|
8
|
-
activeTasks = [];
|
|
9
|
-
constructor(logger) {
|
|
10
|
-
this.logger = logger;
|
|
11
|
-
}
|
|
12
|
-
stopAll() {
|
|
13
|
-
this.activeTasks.forEach((task) => {
|
|
14
|
-
clearInterval(task.taskId);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
createTask(name, action, interval, executeRightAway, ownerName) {
|
|
18
|
-
const taskId = setInterval(action, interval);
|
|
19
|
-
const task = new taskRecord_1.TaskRecord(name, taskId, interval);
|
|
20
|
-
if (executeRightAway) {
|
|
21
|
-
setImmediate(action);
|
|
22
|
-
}
|
|
23
|
-
this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Created task ${name}, that will run every ${interval}ms.`);
|
|
24
|
-
this.activeTasks.push(task);
|
|
25
|
-
}
|
|
26
|
-
createOnetimeTask(name, action, delay, ownerName) {
|
|
27
|
-
const actionWrapper = () => {
|
|
28
|
-
this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Executing delayed oneshot ${name}`);
|
|
29
|
-
action();
|
|
30
|
-
};
|
|
31
|
-
setTimeout(actionWrapper, delay);
|
|
32
|
-
this.logger.logWithTraceId(ownerName, (0, traceFactory_1.createTrace)(this, ownerName, name), 'System', `Created oneshot task ${name}, that will run in ${delay}ms.`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.NodeTimeoutScheduler = NodeTimeoutScheduler;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type QueueItem = {
|
|
2
|
-
priority: number;
|
|
3
|
-
callback: () => Promise<void>;
|
|
4
|
-
};
|
|
5
|
-
export declare class ResponseProcessingQueue {
|
|
6
|
-
rateLimiter: () => Promise<void>;
|
|
7
|
-
items: QueueItem[];
|
|
8
|
-
isFlushing: boolean;
|
|
9
|
-
enqueue(item: QueueItem): void;
|
|
10
|
-
flushReadyItems(): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=responseProcessingQueue.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAQF,qBAAa,uBAAuB;IAChC,WAAW,sBAAwD;IACnE,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAkBxB"}
|