chz-telegram-bot 0.2.1 → 0.3.0
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/dist/dtos/commandTriggerCheckResult.d.ts +1 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/dtos/commandTriggerCheckResult.js +3 -0
- package/dist/dtos/incomingMessage.d.ts +2 -1
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +5 -1
- package/dist/dtos/incomingQuery.d.ts +1 -0
- package/dist/dtos/incomingQuery.d.ts.map +1 -1
- package/dist/dtos/incomingQuery.js +1 -0
- package/dist/dtos/replyInfo.d.ts.map +1 -0
- package/dist/dtos/responses/delay.d.ts +1 -1
- package/dist/dtos/responses/delay.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.d.ts +8 -7
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.js +1 -0
- package/dist/dtos/responses/reaction.d.ts +3 -4
- package/dist/dtos/responses/reaction.d.ts.map +1 -1
- package/dist/dtos/responses/textMessage.d.ts +8 -7
- package/dist/dtos/responses/textMessage.d.ts.map +1 -1
- package/dist/dtos/responses/textMessage.js +1 -0
- package/dist/dtos/responses/unpin.d.ts +1 -1
- package/dist/dtos/responses/unpin.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.d.ts +8 -7
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.js +1 -0
- package/dist/entities/actions/commandAction.d.ts +1 -1
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +6 -11
- package/dist/entities/actions/inlineQueryAction.d.ts +2 -3
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- 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 +56 -0
- package/dist/entities/actions/scheduledAction.d.ts +1 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/botInstance.d.ts +7 -36
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +5 -162
- package/dist/entities/context/chatContext.d.ts +7 -5
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +21 -3
- package/dist/entities/context/inlineQueryContext.d.ts +7 -3
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/inlineQueryContext.js +0 -11
- package/dist/entities/context/messageContext.d.ts +6 -6
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +10 -4
- package/dist/entities/context/replyContext.d.ts +113 -0
- package/dist/entities/context/replyContext.d.ts.map +1 -0
- package/dist/entities/context/replyContext.js +108 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +5 -9
- package/dist/services/actionProcessingService.d.ts +25 -0
- package/dist/services/actionProcessingService.d.ts.map +1 -0
- package/dist/services/actionProcessingService.js +40 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +28 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.js +116 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +20 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +74 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +21 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.js +69 -0
- package/dist/services/jsonFileStorage.d.ts +1 -1
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/telegramApi.d.ts +5 -1
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +19 -23
- package/dist/types/{actionWithState.d.ts → action.d.ts} +5 -3
- package/dist/types/action.d.ts.map +1 -0
- package/dist/types/capture.d.ts +14 -0
- package/dist/types/capture.d.ts.map +1 -0
- package/dist/types/response.d.ts +9 -6
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/storage.d.ts +1 -1
- package/dist/types/storage.d.ts.map +1 -1
- package/dtos/commandTriggerCheckResult.ts +3 -0
- package/dtos/incomingMessage.ts +7 -2
- package/dtos/incomingQuery.ts +2 -0
- package/dtos/responses/delay.ts +1 -1
- package/dtos/responses/imageMessage.ts +11 -7
- package/dtos/responses/reaction.ts +3 -4
- package/dtos/responses/textMessage.ts +11 -7
- package/dtos/responses/unpin.ts +1 -1
- package/dtos/responses/videoMessage.ts +11 -7
- package/entities/actions/commandAction.ts +11 -18
- package/entities/actions/inlineQueryAction.ts +2 -3
- package/entities/actions/replyCaptureAction.ts +104 -0
- package/entities/actions/scheduledAction.ts +1 -1
- package/entities/botInstance.ts +19 -347
- package/entities/context/chatContext.ts +59 -29
- package/entities/context/inlineQueryContext.ts +7 -22
- package/entities/context/messageContext.ts +36 -29
- package/entities/context/replyContext.ts +225 -0
- package/main.ts +11 -10
- package/package.json +1 -1
- package/services/actionProcessingService.ts +123 -0
- package/services/actionProcessors/commandActionProcessor.ts +242 -0
- package/services/actionProcessors/inlineQueryActionProcessor.ts +176 -0
- package/services/actionProcessors/scheduledActionProcessor.ts +145 -0
- package/services/jsonFileStorage.ts +1 -1
- package/services/telegramApi.ts +45 -31
- package/types/{statefulAction.ts → action.ts} +6 -2
- package/types/capture.ts +21 -0
- package/types/response.ts +10 -6
- package/types/storage.ts +1 -1
- package/dist/types/actionWithState.d.ts.map +0 -1
- package/dist/types/replyInfo.d.ts.map +0 -1
- package/dist/types/statefulAction.d.ts +0 -9
- package/dist/types/statefulAction.d.ts.map +0 -1
- package/dist/types/statelessAction.d.ts +0 -5
- package/dist/types/statelessAction.d.ts.map +0 -1
- package/dist/types/statelessAction.js +0 -2
- package/types/statelessAction.ts +0 -5
- /package/dist/{types → dtos}/replyInfo.d.ts +0 -0
- /package/dist/{types → dtos}/replyInfo.js +0 -0
- /package/dist/types/{actionWithState.js → action.js} +0 -0
- /package/dist/types/{statefulAction.js → capture.js} +0 -0
- /package/{types → dtos}/replyInfo.ts +0 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReplyContext = void 0;
|
|
4
|
+
const replyInfo_1 = require("../../dtos/replyInfo");
|
|
5
|
+
const imageMessage_1 = require("../../dtos/responses/imageMessage");
|
|
6
|
+
const reaction_1 = require("../../dtos/responses/reaction");
|
|
7
|
+
const textMessage_1 = require("../../dtos/responses/textMessage");
|
|
8
|
+
const videoMessage_1 = require("../../dtos/responses/videoMessage");
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
class ReplyContext {
|
|
11
|
+
constructor(storage, logger, scheduler) {
|
|
12
|
+
/** Ordered collection of responses to be processed */
|
|
13
|
+
this.responses = [];
|
|
14
|
+
this.isInitialized = false;
|
|
15
|
+
/**
|
|
16
|
+
* Collection of actions that can be done as a reply to a message that triggered this action
|
|
17
|
+
*/
|
|
18
|
+
this.reply = {
|
|
19
|
+
/**
|
|
20
|
+
* Collection of actions that can be done as a reply to a message, quoting the part that triggered this action
|
|
21
|
+
* If regex is matched, first match will be quoted
|
|
22
|
+
*/
|
|
23
|
+
andQuote: {
|
|
24
|
+
/**
|
|
25
|
+
* Reply with text message to message that triggered this action after action execution is finished.
|
|
26
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
27
|
+
* @param text Message contents.
|
|
28
|
+
* @param options Message sending option.
|
|
29
|
+
*/
|
|
30
|
+
withText: (text, options) => this.replyWithText(text, true, options),
|
|
31
|
+
/**
|
|
32
|
+
* Reply with image message to message that triggered this action after action execution is finished.
|
|
33
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
34
|
+
* @param text Message contents.
|
|
35
|
+
* @param options Message sending option.
|
|
36
|
+
*/
|
|
37
|
+
withImage: (name, options) => this.replyWithImage(name, true, options),
|
|
38
|
+
/**
|
|
39
|
+
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
40
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
41
|
+
* @param text Message contents.
|
|
42
|
+
* @param options Message sending option.
|
|
43
|
+
*/
|
|
44
|
+
withVideo: (name, options) => this.replyWithVideo(name, true, options)
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* Reply with text message to message that triggered this action after action execution is finished.
|
|
48
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
49
|
+
* @param text Message contents.
|
|
50
|
+
* @param options Message sending option.
|
|
51
|
+
*/
|
|
52
|
+
withText: (text, options) => this.replyWithText(text, false, options),
|
|
53
|
+
/**
|
|
54
|
+
* Reply with image message to message that triggered this action after action execution is finished.
|
|
55
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
56
|
+
* @param text Message contents.
|
|
57
|
+
* @param options Message sending option.
|
|
58
|
+
*/
|
|
59
|
+
withImage: (name, options) => this.replyWithImage(name, false, options),
|
|
60
|
+
/**
|
|
61
|
+
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
62
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
63
|
+
* @param text Message contents.
|
|
64
|
+
* @param options Message sending option.
|
|
65
|
+
*/
|
|
66
|
+
withVideo: (name, options) => this.replyWithVideo(name, false, options),
|
|
67
|
+
/**
|
|
68
|
+
* React to the message that triggered this action after action execution is finished.
|
|
69
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
70
|
+
* @param emoji Telegram emoji to react with.
|
|
71
|
+
*/
|
|
72
|
+
withReaction: (emoji) => {
|
|
73
|
+
this.responses.push(new reaction_1.Reaction(this.traceId, this.chatInfo, this.messageId, emoji, this.action));
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
this.storage = storage;
|
|
77
|
+
this.logger = logger;
|
|
78
|
+
this.scheduler = scheduler;
|
|
79
|
+
}
|
|
80
|
+
replyWithText(text, quote, options) {
|
|
81
|
+
const quotedPart = this.matchResults.length != 0
|
|
82
|
+
? this.matchResults[0][1]
|
|
83
|
+
: this.messageText;
|
|
84
|
+
const response = new textMessage_1.TextMessage(text, this.chatInfo, this.traceId, this.action, new replyInfo_1.ReplyInfo(this.messageId, quote ? quotedPart : undefined), options);
|
|
85
|
+
this.responses.push(response);
|
|
86
|
+
}
|
|
87
|
+
replyWithImage(name, quote, options) {
|
|
88
|
+
const quotedPart = this.matchResults.length != 0
|
|
89
|
+
? this.matchResults[0][1]
|
|
90
|
+
: this.messageText;
|
|
91
|
+
const response = new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(`./content/${name}.png`) }, this.chatInfo, this.traceId, this.action, new replyInfo_1.ReplyInfo(this.messageId, quote ? quotedPart : undefined), options);
|
|
92
|
+
this.responses.push(response);
|
|
93
|
+
}
|
|
94
|
+
replyWithVideo(name, quote, options) {
|
|
95
|
+
const quotedPart = this.matchResults.length != 0
|
|
96
|
+
? this.matchResults[0][1]
|
|
97
|
+
: this.messageText;
|
|
98
|
+
const response = new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(`./content/${name}.mp4`) }, this.chatInfo, this.traceId, this.action, new replyInfo_1.ReplyInfo(this.messageId, quote ? quotedPart : undefined), options);
|
|
99
|
+
this.responses.push(response);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Stops capturing replies and removes this action
|
|
103
|
+
*/
|
|
104
|
+
stopCapture() {
|
|
105
|
+
this.action.abortController.abort();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|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,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,8FAA8F;YAC9F,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,yGAAyG;YACzG,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IA0BD;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM;CAKhC;AAED,eAAO,MAAM,eAAe,iBAAwB,CAAC"}
|
package/dist/main.js
CHANGED
|
@@ -12,22 +12,18 @@ class BotOrchestrator {
|
|
|
12
12
|
*/
|
|
13
13
|
async startBot(options) {
|
|
14
14
|
const token = await (0, promises_1.readFile)(options.tokenFilePath, 'utf8');
|
|
15
|
-
const
|
|
15
|
+
const bot = new botInstance_1.BotInstance({
|
|
16
16
|
name: options.name,
|
|
17
|
-
token,
|
|
18
17
|
actions: options.actions,
|
|
19
18
|
chats: options.chats,
|
|
20
|
-
storagePath: options.storagePath,
|
|
21
|
-
scheduledPeriod: options.scheduledPeriod,
|
|
22
|
-
verboseLoggingForIncomingMessage: options.verboseLoggingForIncomingMessage,
|
|
23
19
|
services: {
|
|
24
20
|
storageClient: options.services?.storageClient,
|
|
25
21
|
logger: options.services?.logger,
|
|
26
22
|
scheduler: options.services?.scheduler
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
await bot.start(
|
|
23
|
+
},
|
|
24
|
+
storagePath: options.storagePath
|
|
25
|
+
});
|
|
26
|
+
await bot.start(token, options.actions, options.scheduledPeriod, options.verboseLoggingForIncomingMessage);
|
|
31
27
|
this.bots.push(bot);
|
|
32
28
|
return bot;
|
|
33
29
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Seconds } from '../types/timeValues';
|
|
2
|
+
import { ILogger } from '../types/logger';
|
|
3
|
+
import { IScheduler } from '../types/scheduler';
|
|
4
|
+
import { IStorageClient } from '../types/storage';
|
|
5
|
+
import { InlineQueryAction } from '../entities/actions/inlineQueryAction';
|
|
6
|
+
import { IActionState } from '../types/actionState';
|
|
7
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
8
|
+
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
9
|
+
export declare class ActionProcessingService {
|
|
10
|
+
private readonly storage;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly commandProcessor;
|
|
13
|
+
private readonly scheduledProcessor;
|
|
14
|
+
private readonly inlineQueryProcessor;
|
|
15
|
+
private readonly botName;
|
|
16
|
+
private telegraf;
|
|
17
|
+
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler, logger: ILogger);
|
|
18
|
+
initialize(token: string, actions: {
|
|
19
|
+
commands: CommandAction<IActionState>[];
|
|
20
|
+
scheduled: ScheduledAction<IActionState>[];
|
|
21
|
+
inlineQueries: InlineQueryAction[];
|
|
22
|
+
}, scheduledPeriod?: Seconds, verboseLoggingForIncomingMessage?: boolean): Promise<void>;
|
|
23
|
+
stop(code: string): 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;AAEnE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,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;AAMtE,qBAAa,uBAAuB;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,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,QAAQ,CAAY;gBAGxB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,OAAO;IA4Bb,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,EACzB,gCAAgC,CAAC,EAAE,OAAO;IAkD9C,IAAI,CAAC,IAAI,EAAE,MAAM;CAGpB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ActionProcessingService = void 0;
|
|
4
|
+
const timeConvertions_1 = require("../helpers/timeConvertions");
|
|
5
|
+
const telegraf_1 = require("telegraf");
|
|
6
|
+
const telegramApi_1 = require("./telegramApi");
|
|
7
|
+
const helpAction_1 = require("../builtin/helpAction");
|
|
8
|
+
const commandActionProcessor_1 = require("./actionProcessors/commandActionProcessor");
|
|
9
|
+
const inlineQueryActionProcessor_1 = require("./actionProcessors/inlineQueryActionProcessor");
|
|
10
|
+
const scheduledActionProcessor_1 = require("./actionProcessors/scheduledActionProcessor");
|
|
11
|
+
class ActionProcessingService {
|
|
12
|
+
constructor(botName, chats, storage, scheduler, logger) {
|
|
13
|
+
this.storage = storage;
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
this.commandProcessor = new commandActionProcessor_1.CommandActionProcessor(botName, storage, scheduler, logger);
|
|
16
|
+
this.scheduledProcessor = new scheduledActionProcessor_1.ScheduledActionProcessor(botName, chats, storage, scheduler, logger);
|
|
17
|
+
this.inlineQueryProcessor = new inlineQueryActionProcessor_1.InlineQueryActionProcessor(botName, storage, scheduler, logger);
|
|
18
|
+
this.botName = botName;
|
|
19
|
+
}
|
|
20
|
+
async initialize(token, actions, scheduledPeriod, verboseLoggingForIncomingMessage) {
|
|
21
|
+
this.telegraf = new telegraf_1.Telegraf(token);
|
|
22
|
+
const api = new telegramApi_1.TelegramApiService(this.botName, this.telegraf.telegram, this.storage, this.logger, (capture, id, chatInfo, traceId) => this.commandProcessor.captureRegistrationCallback(capture, id, chatInfo, traceId));
|
|
23
|
+
const botInfo = await this.telegraf.telegram.getMe();
|
|
24
|
+
this.commandProcessor.initialize(api, this.telegraf, actions.commands.length > 0
|
|
25
|
+
? [
|
|
26
|
+
(0, helpAction_1.buildHelpCommand)(actions.commands
|
|
27
|
+
.map((x) => x.readmeFactory(botInfo.username))
|
|
28
|
+
.filter((x) => !!x), botInfo.username),
|
|
29
|
+
...actions.commands
|
|
30
|
+
]
|
|
31
|
+
: [], verboseLoggingForIncomingMessage ?? false);
|
|
32
|
+
this.inlineQueryProcessor.initialize(api, this.telegraf, actions.inlineQueries, 300);
|
|
33
|
+
this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
|
|
34
|
+
this.telegraf.launch();
|
|
35
|
+
}
|
|
36
|
+
stop(code) {
|
|
37
|
+
this.telegraf.stop(code);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ActionProcessingService = ActionProcessingService;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Telegraf } from 'telegraf';
|
|
2
|
+
import { CommandAction } from '../../entities/actions/commandAction';
|
|
3
|
+
import { IActionState } from '../../types/actionState';
|
|
4
|
+
import { ILogger } from '../../types/logger';
|
|
5
|
+
import { IScheduler } from '../../types/scheduler';
|
|
6
|
+
import { IStorageClient } from '../../types/storage';
|
|
7
|
+
import { TelegramApiService } from '../telegramApi';
|
|
8
|
+
import { IReplyCapture } from '../../types/capture';
|
|
9
|
+
import { TraceId } from '../../types/trace';
|
|
10
|
+
import { ChatInfo } from '../../dtos/chatInfo';
|
|
11
|
+
export declare class CommandActionProcessor {
|
|
12
|
+
private readonly storage;
|
|
13
|
+
private readonly scheduler;
|
|
14
|
+
private readonly logger;
|
|
15
|
+
private readonly botName;
|
|
16
|
+
private readonly replyCaptures;
|
|
17
|
+
private api;
|
|
18
|
+
private telegraf;
|
|
19
|
+
private commands;
|
|
20
|
+
private permanentTriggersToBeProcessed;
|
|
21
|
+
constructor(botName: string, storage: IStorageClient, scheduler: IScheduler, logger: ILogger);
|
|
22
|
+
initialize(api: TelegramApiService, telegraf: Telegraf, commands: CommandAction<IActionState>[], verboseLoggingForIncomingMessage: boolean): void;
|
|
23
|
+
captureRegistrationCallback(capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId): void;
|
|
24
|
+
private processMessage;
|
|
25
|
+
private initializeReplyCaptureContext;
|
|
26
|
+
private initializeMessageContext;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=commandActionProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commandActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/commandActionProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ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IAoD5B,OAAO,CAAC,6BAA6B;IAuBrC,OAAO,CAAC,wBAAwB;CAwBnC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
class CommandActionProcessor {
|
|
10
|
+
constructor(botName, storage, scheduler, logger) {
|
|
11
|
+
this.storage = storage;
|
|
12
|
+
this.scheduler = scheduler;
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
this.botName = botName;
|
|
15
|
+
this.replyCaptures = [];
|
|
16
|
+
}
|
|
17
|
+
initialize(api, telegraf, commands, verboseLoggingForIncomingMessage) {
|
|
18
|
+
this.api = api;
|
|
19
|
+
this.telegraf = telegraf;
|
|
20
|
+
this.commands = commands;
|
|
21
|
+
if (this.commands.length > 0) {
|
|
22
|
+
this.permanentTriggersToBeProcessed = new Set(this.commands
|
|
23
|
+
.flatMap((x) => x.triggers)
|
|
24
|
+
.map((x) => typeof x == 'string'
|
|
25
|
+
? x.startsWith(messageTypes_1.INTERNAL_MESSAGE_TYPE_PREFIX)
|
|
26
|
+
? x
|
|
27
|
+
: messageTypes_1.MessageType.Text
|
|
28
|
+
: messageTypes_1.MessageType.Text));
|
|
29
|
+
this.telegraf.on('message', async (ctx) => {
|
|
30
|
+
const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.botName);
|
|
31
|
+
if (verboseLoggingForIncomingMessage) {
|
|
32
|
+
this.logger.logObjectWithTraceId(this.botName, msg.traceId, msg.chatInfo.name, ctx.update.message);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.logger.logWithTraceId(this.botName, msg.traceId, msg.chatInfo.name, `${msg.from?.first_name ?? 'Unknown'} (${msg.from?.id ?? 'Unknown'}): ${msg.text || `<non-text message: ${msg.type}>`}`);
|
|
36
|
+
}
|
|
37
|
+
if (this.permanentTriggersToBeProcessed.has(msg.type) ||
|
|
38
|
+
this.replyCaptures.find((x) => x.parentMessageId == msg.replyToMessageId))
|
|
39
|
+
await this.processMessage(msg);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
captureRegistrationCallback(capture, parentMessageId, chatInfo, traceId) {
|
|
44
|
+
const replyAction = new replyCaptureAction_1.ReplyCaptureAction(parentMessageId, capture.action, capture.handler, capture.trigger, capture.abortController);
|
|
45
|
+
this.logger.logWithTraceId(this.botName, traceId, chatInfo.name, `Starting capturing replies to message ${parentMessageId} with action ${replyAction.key}`);
|
|
46
|
+
this.replyCaptures.push(replyAction);
|
|
47
|
+
capture.abortController.signal.addEventListener('abort', () => {
|
|
48
|
+
const index = this.replyCaptures.indexOf(replyAction);
|
|
49
|
+
this.replyCaptures.splice(index, 1);
|
|
50
|
+
this.logger.logWithTraceId(this.botName, traceId, chatInfo.name, `Stopping capturing replies to message ${parentMessageId} with action ${replyAction.key}`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async processMessage(msg) {
|
|
54
|
+
const ctx = new messageContext_1.MessageContext(this.storage, this.logger, this.scheduler);
|
|
55
|
+
for (const commandAction of this.commands) {
|
|
56
|
+
this.initializeMessageContext(ctx, commandAction, msg);
|
|
57
|
+
try {
|
|
58
|
+
const responses = await commandAction.exec(ctx);
|
|
59
|
+
this.api.enqueueBatchedResponses(responses);
|
|
60
|
+
ctx.isInitialized = false;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
this.logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatInfo.name, error, ctx);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const replyCtx = new replyContext_1.ReplyContext(this.storage, this.logger, this.scheduler);
|
|
67
|
+
for (const replyAction of this.replyCaptures) {
|
|
68
|
+
this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
|
|
69
|
+
try {
|
|
70
|
+
const responses = await replyAction.exec(replyCtx);
|
|
71
|
+
this.api.enqueueBatchedResponses(responses);
|
|
72
|
+
replyCtx.isInitialized = false;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.logger.errorWithTraceId(replyCtx.botName, replyCtx.traceId, replyCtx.chatInfo.name, error, replyCtx);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
this.api.flushResponses();
|
|
79
|
+
}
|
|
80
|
+
initializeReplyCaptureContext(ctx, action, message) {
|
|
81
|
+
ctx.replyMessageId = message.replyToMessageId;
|
|
82
|
+
ctx.messageId = message.messageId;
|
|
83
|
+
ctx.messageText = message.text ?? '';
|
|
84
|
+
ctx.messageType = message.type;
|
|
85
|
+
ctx.fromUserId = message.from?.id;
|
|
86
|
+
ctx.fromUserName =
|
|
87
|
+
(message.from?.first_name ?? 'Unknown user') +
|
|
88
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
89
|
+
ctx.messageUpdateObject = message.updateObject;
|
|
90
|
+
ctx.botName = this.botName;
|
|
91
|
+
ctx.action = action;
|
|
92
|
+
ctx.chatInfo = message.chatInfo;
|
|
93
|
+
ctx.traceId = message.traceId;
|
|
94
|
+
ctx.isInitialized = true;
|
|
95
|
+
ctx.matchResults = [];
|
|
96
|
+
}
|
|
97
|
+
initializeMessageContext(ctx, action, message) {
|
|
98
|
+
ctx.messageId = message.messageId;
|
|
99
|
+
ctx.messageText = message.text ?? '';
|
|
100
|
+
ctx.messageType = message.type;
|
|
101
|
+
ctx.fromUserId = message.from?.id;
|
|
102
|
+
ctx.fromUserName =
|
|
103
|
+
(message.from?.first_name ?? 'Unknown user') +
|
|
104
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
105
|
+
ctx.messageUpdateObject = message.updateObject;
|
|
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
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.CommandActionProcessor = CommandActionProcessor;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Telegraf } from 'telegraf';
|
|
2
|
+
import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
|
|
3
|
+
import { ILogger } from '../../types/logger';
|
|
4
|
+
import { IScheduler } from '../../types/scheduler';
|
|
5
|
+
import { IStorageClient } from '../../types/storage';
|
|
6
|
+
import { Milliseconds } from '../../types/timeValues';
|
|
7
|
+
import { TelegramApiService } from '../telegramApi';
|
|
8
|
+
export declare class InlineQueryActionProcessor {
|
|
9
|
+
private readonly storage;
|
|
10
|
+
private readonly scheduler;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly botName;
|
|
13
|
+
private api;
|
|
14
|
+
private telegraf;
|
|
15
|
+
private inlineQueries;
|
|
16
|
+
constructor(botName: string, storage: IStorageClient, scheduler: IScheduler, logger: ILogger);
|
|
17
|
+
initialize(api: TelegramApiService, telegraf: Telegraf, inlineQueries: InlineQueryAction[], period: Milliseconds): void;
|
|
18
|
+
private initializeInlineQueryContext;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=inlineQueryActionProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inlineQueryActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/inlineQueryActionProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAG7E,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,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,qBAAa,0BAA0B;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC,OAAO,CAAC,GAAG,CAAsB;IACjC,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,aAAa,CAAuB;gBAGxC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,OAAO;IASnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,QAAQ,EAClB,aAAa,EAAE,iBAAiB,EAAE,EAClC,MAAM,EAAE,YAAY;IAoHxB,OAAO,CAAC,4BAA4B;CAmBvC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
class InlineQueryActionProcessor {
|
|
8
|
+
constructor(botName, storage, scheduler, logger) {
|
|
9
|
+
this.storage = storage;
|
|
10
|
+
this.scheduler = scheduler;
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
this.botName = botName;
|
|
13
|
+
}
|
|
14
|
+
initialize(api, telegraf, inlineQueries, period) {
|
|
15
|
+
this.api = api;
|
|
16
|
+
this.telegraf = telegraf;
|
|
17
|
+
this.inlineQueries = inlineQueries;
|
|
18
|
+
let pendingInlineQueries = [];
|
|
19
|
+
const queriesInProcessing = new Map();
|
|
20
|
+
if (this.inlineQueries.length > 0) {
|
|
21
|
+
this.telegraf.on('inline_query', async (ctx) => {
|
|
22
|
+
const query = new incomingQuery_1.IncomingInlineQuery(ctx.inlineQuery.id, ctx.inlineQuery.query, ctx.inlineQuery.from.id, (0, traceFactory_1.createTrace)('InlineQuery', this.botName, ctx.inlineQuery.id));
|
|
23
|
+
this.logger.logWithTraceId(this.botName, query.traceId, 'Query', `${ctx.inlineQuery.from.username} (${ctx.inlineQuery.from.id}): Query for ${ctx.inlineQuery.query}`);
|
|
24
|
+
const queryBeingProcessed = queriesInProcessing.get(query.userId);
|
|
25
|
+
if (queryBeingProcessed) {
|
|
26
|
+
this.logger.logWithTraceId(this.botName, query.traceId, 'Query', `Aborting query ${queryBeingProcessed.queryId} (${queryBeingProcessed.query}): new query recieved from ${query.userId}`);
|
|
27
|
+
queryBeingProcessed.abortController.abort();
|
|
28
|
+
queriesInProcessing.delete(query.userId);
|
|
29
|
+
}
|
|
30
|
+
pendingInlineQueries = pendingInlineQueries.filter((q) => q.userId != query.userId);
|
|
31
|
+
pendingInlineQueries.push(query);
|
|
32
|
+
});
|
|
33
|
+
this.scheduler.createTask('InlineQueryProcessing', async () => {
|
|
34
|
+
const ctx = new inlineQueryContext_1.InlineQueryContext(this.storage, this.logger, this.scheduler);
|
|
35
|
+
const queriesToProcess = [...pendingInlineQueries];
|
|
36
|
+
pendingInlineQueries = [];
|
|
37
|
+
for (const inlineQuery of queriesToProcess) {
|
|
38
|
+
queriesInProcessing.set(inlineQuery.userId, inlineQuery);
|
|
39
|
+
for (const inlineQueryAction of this.inlineQueries) {
|
|
40
|
+
this.initializeInlineQueryContext(ctx, inlineQuery.query, inlineQuery.queryId, inlineQueryAction, inlineQuery.abortController.signal, inlineQuery.traceId);
|
|
41
|
+
try {
|
|
42
|
+
const responses = await inlineQueryAction.exec(ctx);
|
|
43
|
+
this.api.enqueueBatchedResponses(responses);
|
|
44
|
+
ctx.isInitialized = false;
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
const error = err;
|
|
48
|
+
if (error.name == 'AbortError') {
|
|
49
|
+
this.logger.logWithTraceId(this.botName, inlineQuery.traceId, 'Query', `Aborting query ${inlineQuery.queryId} (${inlineQuery.query}) successful.`);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.logger.errorWithTraceId(ctx.botName, ctx.traceId, 'Unknown', error, ctx);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
queriesInProcessing.delete(inlineQuery.userId);
|
|
57
|
+
}
|
|
58
|
+
this.api.flushResponses();
|
|
59
|
+
}, period, false, this.botName);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
initializeInlineQueryContext(ctx, queryText, queryId, action, abortSignal, traceId) {
|
|
63
|
+
ctx.queryText = queryText;
|
|
64
|
+
ctx.queryId = queryId;
|
|
65
|
+
ctx.botName = this.botName;
|
|
66
|
+
ctx.action = action;
|
|
67
|
+
ctx.traceId = traceId;
|
|
68
|
+
ctx.abortSignal = abortSignal;
|
|
69
|
+
ctx.isInitialized = true;
|
|
70
|
+
ctx.queryResults = [];
|
|
71
|
+
ctx.matchResults = [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.InlineQueryActionProcessor = InlineQueryActionProcessor;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export declare class ScheduledActionProcessor {
|
|
9
|
+
private readonly storage;
|
|
10
|
+
private readonly scheduler;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly botName;
|
|
13
|
+
private readonly chats;
|
|
14
|
+
private api;
|
|
15
|
+
private scheduled;
|
|
16
|
+
constructor(botName: string, chats: Record<string, number>, storage: IStorageClient, scheduler: IScheduler, logger: ILogger);
|
|
17
|
+
initialize(api: TelegramApiService, scheduled: ScheduledAction<IActionState>[], period: Seconds): void;
|
|
18
|
+
private runScheduled;
|
|
19
|
+
private initializeChatContext;
|
|
20
|
+
}
|
|
21
|
+
//# 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;AAKzE,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;AAEpD,qBAAa,wBAAwB;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAE/C,OAAO,CAAC,GAAG,CAAsB;IACjC,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;IAUnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YAgDL,YAAY;IAuC1B,OAAO,CAAC,qBAAqB;CAahC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
class ScheduledActionProcessor {
|
|
13
|
+
constructor(botName, chats, storage, scheduler, logger) {
|
|
14
|
+
this.storage = storage;
|
|
15
|
+
this.scheduler = scheduler;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
this.botName = botName;
|
|
18
|
+
this.chats = chats;
|
|
19
|
+
}
|
|
20
|
+
initialize(api, scheduled, period) {
|
|
21
|
+
this.api = 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', async () => {
|
|
37
|
+
this.scheduler.createTask('ScheduledProcessing', async () => {
|
|
38
|
+
await 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.ChatContext(this.storage, this.logger, 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
|
+
try {
|
|
49
|
+
const responses = await scheduledAction.exec(ctx);
|
|
50
|
+
this.api.enqueueBatchedResponses(responses);
|
|
51
|
+
ctx.isInitialized = false;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
this.logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this.api.flushResponses();
|
|
59
|
+
}
|
|
60
|
+
initializeChatContext(ctx, action, chatInfo, traceId) {
|
|
61
|
+
ctx.responses = [];
|
|
62
|
+
ctx.isInitialized = true;
|
|
63
|
+
ctx.botName = this.botName;
|
|
64
|
+
ctx.action = action;
|
|
65
|
+
ctx.chatInfo = chatInfo;
|
|
66
|
+
ctx.traceId = traceId;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.ScheduledActionProcessor = ScheduledActionProcessor;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IStorageClient } from '../types/storage';
|
|
2
2
|
import { IActionState } from '../types/actionState';
|
|
3
|
-
import { IActionWithState, ActionKey } from '../types/
|
|
3
|
+
import { IActionWithState, ActionKey } from '../types/action';
|
|
4
4
|
export declare class JsonFileStorage implements IStorageClient {
|
|
5
5
|
private readonly locks;
|
|
6
6
|
private readonly cache;
|
|
@@ -1 +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,
|
|
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,qBAAa,eAAgB,YAAW,cAAc;IAClD,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;YAiBH,IAAI;YAcJ,YAAY;YAqBZ,IAAI;IAWlB,OAAO,CAAC,eAAe;IAOjB,IAAI,CAAC,YAAY,SAAS,YAAY,EAAE,GAAG,EAAE,SAAS;IAMtD,YAAY,CACd,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,OAAO,EAAE,MAAM;IAWb,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM;IAYZ,yBAAyB,CAAC,YAAY,SAAS,YAAY,EAC7D,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY;IAWjB,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;CAcrD"}
|
|
@@ -2,13 +2,17 @@ import { IStorageClient } from '../types/storage';
|
|
|
2
2
|
import { BotResponse } from '../types/response';
|
|
3
3
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
4
4
|
import { ILogger } from '../types/logger';
|
|
5
|
+
import { IReplyCapture } from '../types/capture';
|
|
6
|
+
import { TraceId } from '../types/trace';
|
|
7
|
+
import { ChatInfo } from '../dtos/chatInfo';
|
|
5
8
|
export declare class TelegramApiService {
|
|
6
9
|
private readonly queue;
|
|
7
10
|
private readonly telegram;
|
|
8
11
|
private readonly storage;
|
|
9
12
|
private readonly logger;
|
|
13
|
+
private readonly captureRegistrationCallback;
|
|
10
14
|
private readonly botName;
|
|
11
|
-
constructor(botName: string, telegram: Telegram, storage: IStorageClient, logger: ILogger);
|
|
15
|
+
constructor(botName: string, telegram: Telegram, storage: IStorageClient, logger: ILogger, captureRegistrationCallback: (capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId) => void);
|
|
12
16
|
enqueueBatchedResponses(responses: BotResponse[]): void;
|
|
13
17
|
flushResponses(): void;
|
|
14
18
|
private pinIfShould;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGjD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAKlC;IAEV,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG7B,OAAO,EAAE,MAAM,EACf,Q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