chz-telegram-bot 0.5.1 → 0.5.2
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/builtin/helpAction.js +3 -6
- package/dist/dtos/chatHistoryMessage.js +1 -5
- package/dist/dtos/chatInfo.js +1 -5
- package/dist/dtos/commandTriggerCheckResult.js +1 -5
- package/dist/dtos/cooldownInfo.js +1 -5
- package/dist/dtos/incomingMessage.js +21 -25
- package/dist/dtos/incomingQuery.js +1 -5
- package/dist/dtos/messageInfo.js +1 -5
- package/dist/dtos/propertyProviderSets.js +1 -2
- package/dist/dtos/replyInfo.js +1 -5
- package/dist/dtos/responses/delay.js +3 -7
- package/dist/dtos/responses/imageMessage.js +3 -7
- package/dist/dtos/responses/inlineQueryResponse.js +3 -7
- package/dist/dtos/responses/reaction.js +3 -7
- package/dist/dtos/responses/textMessage.js +3 -7
- package/dist/dtos/responses/unpin.js +3 -7
- package/dist/dtos/responses/videoMessage.js +3 -7
- package/dist/dtos/userInfo.js +1 -5
- package/dist/entities/actions/commandAction.js +32 -39
- package/dist/entities/actions/inlineQueryAction.js +4 -8
- package/dist/entities/actions/replyCaptureAction.js +10 -14
- package/dist/entities/actions/scheduledAction.js +16 -23
- package/dist/entities/botInstance.js +13 -17
- package/dist/entities/cachedStateFactory.js +1 -5
- package/dist/entities/context/baseContext.d.ts +1 -1
- package/dist/entities/context/baseContext.d.ts.map +1 -1
- package/dist/entities/context/baseContext.js +1 -5
- package/dist/entities/context/chatContext.js +13 -17
- package/dist/entities/context/inlineQueryContext.js +4 -8
- package/dist/entities/context/messageContext.js +15 -19
- package/dist/entities/context/replyContext.js +15 -19
- package/dist/entities/states/actionStateBase.js +1 -5
- package/dist/entities/taskRecord.js +1 -5
- 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.js +15 -20
- package/dist/helpers/builders/inlineQueryActionBuilder.js +5 -9
- package/dist/helpers/builders/scheduledActionBuilder.js +10 -15
- package/dist/helpers/mapUtils.js +2 -6
- package/dist/helpers/noop.js +1 -5
- package/dist/helpers/objectFromEntries.js +1 -5
- package/dist/helpers/timeConvertions.js +3 -8
- package/dist/helpers/toArray.js +1 -4
- package/dist/helpers/traceFactory.js +1 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -41
- package/dist/main.js +5 -8
- package/dist/services/actionProcessingService.js +15 -19
- package/dist/services/actionProcessors/baseProcessor.js +1 -5
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +35 -38
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +7 -11
- package/dist/services/actionProcessors/scheduledActionProcessor.js +12 -19
- package/dist/services/jsonFileStorage.js +15 -19
- package/dist/services/jsonLogger.js +1 -5
- package/dist/services/nodeTimeoutScheduler.js +9 -13
- package/dist/services/responseProcessingQueue.js +3 -7
- package/dist/services/telegramApi.js +5 -9
- package/dist/types/action.js +1 -2
- package/dist/types/actionState.js +1 -2
- package/dist/types/cachedValueAccessor.js +1 -2
- package/dist/types/capture.js +1 -2
- package/dist/types/commandCondition.js +1 -2
- package/dist/types/commandTrigger.js +1 -2
- package/dist/types/externalAliases.js +1 -2
- package/dist/types/handlers.js +1 -2
- package/dist/types/inputFile.js +1 -2
- package/dist/types/logger.js +1 -2
- package/dist/types/messageSendingOptions.js +1 -2
- package/dist/types/messageTypes.js +17 -20
- package/dist/types/propertyProvider.js +1 -2
- package/dist/types/response.js +1 -4
- package/dist/types/scheduler.js +1 -2
- package/dist/types/storage.js +1 -2
- package/dist/types/timeValues.js +1 -2
- package/dist/types/trace.js +1 -2
- 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 +2 -1
- package/services/actionProcessors/commandActionProcessor.ts +5 -6
- package/services/jsonFileStorage.ts +1 -1
- package/services/nodeTimeoutScheduler.ts +2 -2
- package/tsconfig.json +2 -2
- package/bun.lock +0 -301
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const cachedStateFactory_1 = require("../../entities/cachedStateFactory");
|
|
6
|
-
const actionStateBase_1 = require("../../entities/states/actionStateBase");
|
|
7
|
-
const noop_1 = require("../noop");
|
|
1
|
+
import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
2
|
+
import { CachedStateFactory } from '../../entities/cachedStateFactory';
|
|
3
|
+
import { ActionStateBase } from '../../entities/states/actionStateBase';
|
|
4
|
+
import { Noop } from '../noop';
|
|
8
5
|
/**
|
|
9
6
|
* Builder for `ScheduledAction` with state represented by `TActionState`
|
|
10
7
|
*/
|
|
11
|
-
class ScheduledActionBuilderWithState {
|
|
8
|
+
export class ScheduledActionBuilderWithState {
|
|
12
9
|
name;
|
|
13
10
|
cachedStateFactories = new Map();
|
|
14
11
|
stateConstructor;
|
|
15
|
-
handler =
|
|
12
|
+
handler = Noop.call;
|
|
16
13
|
whitelistProvider = () => [];
|
|
17
14
|
activeProvider = () => true;
|
|
18
15
|
timeinHoursProvider = () => 0;
|
|
@@ -56,7 +53,7 @@ class ScheduledActionBuilderWithState {
|
|
|
56
53
|
* @param invalidationTimeoutInHours Timeout for cache invalidation.
|
|
57
54
|
*/
|
|
58
55
|
withSharedCache(key, itemFactory, invalidationTimeoutInHours = 20) {
|
|
59
|
-
this.cachedStateFactories.set(key, new
|
|
56
|
+
this.cachedStateFactories.set(key, new CachedStateFactory(itemFactory, invalidationTimeoutInHours));
|
|
60
57
|
return this;
|
|
61
58
|
}
|
|
62
59
|
/** If called during building, action is marked as disabled and never checked. */
|
|
@@ -78,23 +75,21 @@ class ScheduledActionBuilderWithState {
|
|
|
78
75
|
}
|
|
79
76
|
/** Builds action */
|
|
80
77
|
build() {
|
|
81
|
-
return new
|
|
78
|
+
return new ScheduledAction(this.name, this.handler, {
|
|
82
79
|
chatsWhitelistProvider: this.whitelistProvider,
|
|
83
80
|
isActiveProvider: this.activeProvider,
|
|
84
81
|
timeinHoursProvider: this.timeinHoursProvider
|
|
85
82
|
}, this.cachedStateFactories, this.stateConstructor);
|
|
86
83
|
}
|
|
87
84
|
}
|
|
88
|
-
exports.ScheduledActionBuilderWithState = ScheduledActionBuilderWithState;
|
|
89
85
|
/**
|
|
90
86
|
* Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
|
|
91
87
|
*/
|
|
92
|
-
class ScheduledActionBuilder extends ScheduledActionBuilderWithState {
|
|
88
|
+
export class ScheduledActionBuilder extends ScheduledActionBuilderWithState {
|
|
93
89
|
/**
|
|
94
90
|
* Builder for `ScheduledAction` with state represented by default state (containing only last execution date).
|
|
95
91
|
*/
|
|
96
92
|
constructor(name) {
|
|
97
|
-
super(name, () => new
|
|
93
|
+
super(name, () => new ActionStateBase());
|
|
98
94
|
}
|
|
99
95
|
}
|
|
100
|
-
exports.ScheduledActionBuilder = ScheduledActionBuilder;
|
package/dist/helpers/mapUtils.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getOrSetIfNotExists = getOrSetIfNotExists;
|
|
4
|
-
exports.getOrThrow = getOrThrow;
|
|
5
|
-
function getOrSetIfNotExists(map, key, fallback) {
|
|
1
|
+
export function getOrSetIfNotExists(map, key, fallback) {
|
|
6
2
|
const existingValue = map.get(key);
|
|
7
3
|
if (existingValue)
|
|
8
4
|
return existingValue;
|
|
9
5
|
map.set(key, fallback);
|
|
10
6
|
return fallback;
|
|
11
7
|
}
|
|
12
|
-
function getOrThrow(map, key, error = 'Key not found in collection') {
|
|
8
|
+
export function getOrThrow(map, key, error = 'Key not found in collection') {
|
|
13
9
|
const existingValue = map.get(key);
|
|
14
10
|
if (existingValue)
|
|
15
11
|
return existingValue;
|
package/dist/helpers/noop.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Noop = void 0;
|
|
4
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5
2
|
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
6
|
-
class Noop {
|
|
3
|
+
export class Noop {
|
|
7
4
|
static NoResponse = [];
|
|
8
5
|
static true(arg1) {
|
|
9
6
|
return true;
|
|
@@ -18,4 +15,3 @@ class Noop {
|
|
|
18
15
|
return Promise.resolve();
|
|
19
16
|
}
|
|
20
17
|
}
|
|
21
|
-
exports.Noop = Noop;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.typeSafeObjectFromEntries = void 0;
|
|
4
|
-
const typeSafeObjectFromEntries = (entries) => {
|
|
1
|
+
export const typeSafeObjectFromEntries = (entries) => {
|
|
5
2
|
return Object.fromEntries(entries);
|
|
6
3
|
};
|
|
7
|
-
exports.typeSafeObjectFromEntries = typeSafeObjectFromEntries;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.secondsToMilliseconds = secondsToMilliseconds;
|
|
4
|
-
exports.hoursToMilliseconds = hoursToMilliseconds;
|
|
5
|
-
exports.hoursToSeconds = hoursToSeconds;
|
|
6
|
-
function secondsToMilliseconds(value) {
|
|
1
|
+
export function secondsToMilliseconds(value) {
|
|
7
2
|
return (value * 1000);
|
|
8
3
|
}
|
|
9
|
-
function hoursToMilliseconds(value) {
|
|
4
|
+
export function hoursToMilliseconds(value) {
|
|
10
5
|
return (value * 60 * 60 * 1000);
|
|
11
6
|
}
|
|
12
|
-
function hoursToSeconds(value) {
|
|
7
|
+
export function hoursToSeconds(value) {
|
|
13
8
|
return (value * 60 * 60);
|
|
14
9
|
}
|
package/dist/helpers/toArray.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTrace = createTrace;
|
|
4
|
-
function createTrace(traceOwner, botName, traceName) {
|
|
1
|
+
export function createTrace(traceOwner, botName, traceName) {
|
|
5
2
|
return `${typeof traceOwner == 'string' ? traceOwner : traceOwner.constructor.name}:${botName}-${traceName}`;
|
|
6
3
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ export * from './types/messageTypes';
|
|
|
8
8
|
export * from './helpers/timeConvertions';
|
|
9
9
|
export * from './types/action';
|
|
10
10
|
export * from './types/externalAliases';
|
|
11
|
+
export * from './types/storage';
|
|
12
|
+
export * from './types/logger';
|
|
13
|
+
export * from './types/scheduler';
|
|
11
14
|
export { CommandAction } from './entities/actions/commandAction';
|
|
12
15
|
export { InlineQueryAction } from './entities/actions/inlineQueryAction';
|
|
13
16
|
export { ReplyCaptureAction } from './entities/actions/replyCaptureAction';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,6CAA6C,CAAC;AAC5D,cAAc,yCAAyC,CAAC;AACxD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,u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,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,41 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
__exportStar(require("./helpers/builders/scheduledActionBuilder"), exports);
|
|
22
|
-
__exportStar(require("./types/actionState"), exports);
|
|
23
|
-
__exportStar(require("./entities/states/actionStateBase"), exports);
|
|
24
|
-
__exportStar(require("./types/messageTypes"), exports);
|
|
25
|
-
__exportStar(require("./helpers/timeConvertions"), exports);
|
|
26
|
-
__exportStar(require("./types/action"), exports);
|
|
27
|
-
__exportStar(require("./types/externalAliases"), exports);
|
|
28
|
-
var commandAction_1 = require("./entities/actions/commandAction");
|
|
29
|
-
Object.defineProperty(exports, "CommandAction", { enumerable: true, get: function () { return commandAction_1.CommandAction; } });
|
|
30
|
-
var inlineQueryAction_1 = require("./entities/actions/inlineQueryAction");
|
|
31
|
-
Object.defineProperty(exports, "InlineQueryAction", { enumerable: true, get: function () { return inlineQueryAction_1.InlineQueryAction; } });
|
|
32
|
-
var replyCaptureAction_1 = require("./entities/actions/replyCaptureAction");
|
|
33
|
-
Object.defineProperty(exports, "ReplyCaptureAction", { enumerable: true, get: function () { return replyCaptureAction_1.ReplyCaptureAction; } });
|
|
34
|
-
var scheduledAction_1 = require("./entities/actions/scheduledAction");
|
|
35
|
-
Object.defineProperty(exports, "ScheduledAction", { enumerable: true, get: function () { return scheduledAction_1.ScheduledAction; } });
|
|
36
|
-
var chatInfo_1 = require("./dtos/chatInfo");
|
|
37
|
-
Object.defineProperty(exports, "ChatInfo", { enumerable: true, get: function () { return chatInfo_1.ChatInfo; } });
|
|
38
|
-
var messageInfo_1 = require("./dtos/messageInfo");
|
|
39
|
-
Object.defineProperty(exports, "MessageInfo", { enumerable: true, get: function () { return messageInfo_1.MessageInfo; } });
|
|
40
|
-
var userInfo_1 = require("./dtos/userInfo");
|
|
41
|
-
Object.defineProperty(exports, "UserInfo", { enumerable: true, get: function () { return userInfo_1.UserInfo; } });
|
|
1
|
+
export * from './main';
|
|
2
|
+
export * from './helpers/builders/inlineQueryActionBuilder';
|
|
3
|
+
export * from './helpers/builders/commandActionBuilder';
|
|
4
|
+
export * from './helpers/builders/scheduledActionBuilder';
|
|
5
|
+
export * from './types/actionState';
|
|
6
|
+
export * from './entities/states/actionStateBase';
|
|
7
|
+
export * from './types/messageTypes';
|
|
8
|
+
export * from './helpers/timeConvertions';
|
|
9
|
+
export * from './types/action';
|
|
10
|
+
export * from './types/externalAliases';
|
|
11
|
+
export * from './types/storage';
|
|
12
|
+
export * from './types/logger';
|
|
13
|
+
export * from './types/scheduler';
|
|
14
|
+
export { CommandAction } from './entities/actions/commandAction';
|
|
15
|
+
export { InlineQueryAction } from './entities/actions/inlineQueryAction';
|
|
16
|
+
export { ReplyCaptureAction } from './entities/actions/replyCaptureAction';
|
|
17
|
+
export { ScheduledAction } from './entities/actions/scheduledAction';
|
|
18
|
+
export { ChatInfo } from './dtos/chatInfo';
|
|
19
|
+
export { MessageInfo } from './dtos/messageInfo';
|
|
20
|
+
export { UserInfo } from './dtos/userInfo';
|
package/dist/main.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.botOrchestrator = void 0;
|
|
4
|
-
const promises_1 = require("fs/promises");
|
|
5
|
-
const botInstance_1 = require("./entities/botInstance");
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import { BotInstance } from './entities/botInstance';
|
|
6
3
|
class BotOrchestrator {
|
|
7
4
|
bots = [];
|
|
8
5
|
/**
|
|
9
6
|
* Starts bot
|
|
10
7
|
*/
|
|
11
8
|
async startBot(options) {
|
|
12
|
-
const token = await
|
|
13
|
-
const bot = new
|
|
9
|
+
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
10
|
+
const bot = new BotInstance({
|
|
14
11
|
name: options.name,
|
|
15
12
|
actions: options.actions,
|
|
16
13
|
chats: options.chats,
|
|
@@ -34,4 +31,4 @@ class BotOrchestrator {
|
|
|
34
31
|
}
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
|
-
|
|
34
|
+
export const botOrchestrator = new BotOrchestrator();
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const scheduledActionProcessor_1 = require("./actionProcessors/scheduledActionProcessor");
|
|
10
|
-
const telegraf_1 = require("telegraf");
|
|
11
|
-
class ActionProcessingService {
|
|
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 {
|
|
12
9
|
storage;
|
|
13
10
|
logger;
|
|
14
11
|
commandProcessor;
|
|
@@ -19,20 +16,20 @@ class ActionProcessingService {
|
|
|
19
16
|
constructor(botName, chats, storage, scheduler, logger) {
|
|
20
17
|
this.storage = storage;
|
|
21
18
|
this.logger = logger;
|
|
22
|
-
this.commandProcessor = new
|
|
23
|
-
this.scheduledProcessor = new
|
|
24
|
-
this.inlineQueryProcessor = new
|
|
19
|
+
this.commandProcessor = new CommandActionProcessor(botName, storage, scheduler, logger);
|
|
20
|
+
this.scheduledProcessor = new ScheduledActionProcessor(botName, chats, storage, scheduler, logger);
|
|
21
|
+
this.inlineQueryProcessor = new InlineQueryActionProcessor(botName, storage, scheduler, logger);
|
|
25
22
|
this.botName = botName;
|
|
26
23
|
}
|
|
27
24
|
async initialize(token, actions, scheduledPeriod, verboseLoggingForIncomingMessage) {
|
|
28
|
-
this.telegramBot = new
|
|
29
|
-
const api = new
|
|
25
|
+
this.telegramBot = new Telegraf(token);
|
|
26
|
+
const api = new TelegramApiService(this.botName, this.telegramBot.telegram, this.storage, this.logger, (capture, id, chatInfo, traceId) => {
|
|
30
27
|
this.commandProcessor.captureRegistrationCallback(capture, id, chatInfo, traceId);
|
|
31
28
|
});
|
|
32
29
|
const botInfo = await this.telegramBot.telegram.getMe();
|
|
33
30
|
const commandActions = actions.commands.length > 0 && botInfo.username
|
|
34
31
|
? [
|
|
35
|
-
|
|
32
|
+
buildHelpCommand(actions.commands
|
|
36
33
|
.map((x) => x.readmeFactory(botInfo.username))
|
|
37
34
|
.filter((x) => !!x), botInfo.username),
|
|
38
35
|
...actions.commands
|
|
@@ -40,7 +37,7 @@ class ActionProcessingService {
|
|
|
40
37
|
: [];
|
|
41
38
|
this.commandProcessor.initialize(api, this.telegramBot, commandActions, verboseLoggingForIncomingMessage ?? false, botInfo);
|
|
42
39
|
this.inlineQueryProcessor.initialize(api, this.telegramBot, actions.inlineQueries, 300);
|
|
43
|
-
this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ??
|
|
40
|
+
this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? hoursToSeconds(1));
|
|
44
41
|
void this.telegramBot.launch();
|
|
45
42
|
void this.storage.saveMetadata([
|
|
46
43
|
...actions.scheduled,
|
|
@@ -51,4 +48,3 @@ class ActionProcessingService {
|
|
|
51
48
|
this.telegramBot.stop();
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
|
-
exports.ActionProcessingService = ActionProcessingService;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseActionProcessor = void 0;
|
|
4
|
-
class BaseActionProcessor {
|
|
1
|
+
export class BaseActionProcessor {
|
|
5
2
|
storage;
|
|
6
3
|
scheduler;
|
|
7
4
|
logger;
|
|
@@ -30,4 +27,3 @@ class BaseActionProcessor {
|
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
|
-
exports.BaseActionProcessor = BaseActionProcessor;
|
|
@@ -1 +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,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,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C,EAAE,OAAO,EACzC,OAAO,EAAE,OAAO;IA0DpB,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,45 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const messageInfo_1 = require("../../dtos/messageInfo");
|
|
13
|
-
const userInfo_1 = require("../../dtos/userInfo");
|
|
14
|
-
const chatHistoryMessage_1 = require("../../dtos/chatHistoryMessage");
|
|
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';
|
|
15
12
|
const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
|
|
16
|
-
class CommandActionProcessor extends
|
|
13
|
+
export class CommandActionProcessor extends BaseActionProcessor {
|
|
17
14
|
replyCaptures = [];
|
|
18
15
|
chatHistory = new Map();
|
|
19
16
|
botInfo;
|
|
20
|
-
commands =
|
|
17
|
+
commands = typeSafeObjectFromEntries(Object.values(MessageType).map((x) => [
|
|
21
18
|
x,
|
|
22
19
|
[]
|
|
23
20
|
]));
|
|
24
21
|
initialize(api, telegram, commands, verboseLoggingForIncomingMessage, botInfo) {
|
|
25
22
|
this.botInfo = botInfo;
|
|
26
23
|
this.initializeDependencies(api);
|
|
27
|
-
for (const msgType of Object.values(
|
|
28
|
-
if (msgType ==
|
|
29
|
-
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
cmd.triggers.includes(
|
|
34
|
-
cmd.triggers.includes(messageTypes_1.MessageType.Any));
|
|
24
|
+
for (const msgType of Object.values(MessageType)) {
|
|
25
|
+
if (msgType == MessageType.Text) {
|
|
26
|
+
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.some((x) => typeof x != 'string') ||
|
|
27
|
+
cmd.triggers.some((x) => typeof x == 'string' &&
|
|
28
|
+
!x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)) ||
|
|
29
|
+
cmd.triggers.includes(MessageType.Text) ||
|
|
30
|
+
cmd.triggers.includes(MessageType.Any));
|
|
35
31
|
continue;
|
|
36
32
|
}
|
|
37
33
|
this.commands[msgType] = commands.filter((cmd) => cmd.triggers.includes(msgType) ||
|
|
38
|
-
cmd.triggers.includes(
|
|
34
|
+
cmd.triggers.includes(MessageType.Any));
|
|
39
35
|
}
|
|
40
36
|
if (commands.length > 0) {
|
|
41
37
|
telegram.on('message', ({ message }) => {
|
|
42
|
-
const internalMessage = new
|
|
38
|
+
const internalMessage = new IncomingMessage(message, this.botName, getOrSetIfNotExists(this.chatHistory, message.chat.id, []));
|
|
43
39
|
const logger = this.logger.createScope(this.botName, internalMessage.traceId, internalMessage.chatInfo.name);
|
|
44
40
|
if (verboseLoggingForIncomingMessage) {
|
|
45
41
|
logger.logObjectWithTraceId(message);
|
|
@@ -52,7 +48,7 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
52
48
|
}
|
|
53
49
|
}
|
|
54
50
|
captureRegistrationCallback(capture, parentMessageId, chatInfo, traceId) {
|
|
55
|
-
const replyAction = new
|
|
51
|
+
const replyAction = new ReplyCaptureAction(parentMessageId, capture.action, capture.handler, capture.trigger, capture.abortController);
|
|
56
52
|
const logger = this.logger.createScope(this.botName, traceId, chatInfo.name);
|
|
57
53
|
logger.logWithTraceId(`Starting capturing replies to message ${parentMessageId} with action ${replyAction.key}`);
|
|
58
54
|
this.replyCaptures.push(replyAction);
|
|
@@ -63,21 +59,23 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
63
59
|
});
|
|
64
60
|
}
|
|
65
61
|
async processMessage(msg) {
|
|
66
|
-
const chatHistoryArray =
|
|
62
|
+
const chatHistoryArray = getOrSetIfNotExists(this.chatHistory, msg.chatInfo.id, []);
|
|
67
63
|
while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
|
|
68
64
|
chatHistoryArray.shift();
|
|
69
|
-
chatHistoryArray.push(new
|
|
70
|
-
const ctx = new
|
|
65
|
+
chatHistoryArray.push(new ChatHistoryMessage(msg.messageId, msg.from, msg.text, msg.type, msg.traceId, msg.replyToMessageId));
|
|
66
|
+
const ctx = new MessageContextInternal(this.storage, this.scheduler);
|
|
71
67
|
const commandsToCheck = new Set(this.commands[msg.type]);
|
|
72
|
-
if (msg.type !=
|
|
73
|
-
this.commands[
|
|
68
|
+
if (msg.type != MessageType.Text && msg.text != '') {
|
|
69
|
+
for (const command of this.commands[MessageType.Text]) {
|
|
70
|
+
commandsToCheck.add(command);
|
|
71
|
+
}
|
|
74
72
|
}
|
|
75
73
|
for (const commandAction of commandsToCheck) {
|
|
76
74
|
this.initializeMessageContext(ctx, commandAction, msg);
|
|
77
75
|
await this.executeAction(commandAction, ctx);
|
|
78
76
|
}
|
|
79
77
|
if (this.replyCaptures.length != 0) {
|
|
80
|
-
const replyCtx = new
|
|
78
|
+
const replyCtx = new ReplyContextInternal(this.storage, this.scheduler);
|
|
81
79
|
for (const replyAction of this.replyCaptures) {
|
|
82
80
|
this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
|
|
83
81
|
await this.executeAction(replyAction, replyCtx);
|
|
@@ -87,8 +85,8 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
87
85
|
}
|
|
88
86
|
initializeReplyCaptureContext(ctx, action, message) {
|
|
89
87
|
ctx.replyMessageId = message.replyToMessageId;
|
|
90
|
-
ctx.messageInfo = new
|
|
91
|
-
ctx.userInfo = new
|
|
88
|
+
ctx.messageInfo = new MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
89
|
+
ctx.userInfo = new UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
92
90
|
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
93
91
|
ctx.botName = this.botName;
|
|
94
92
|
ctx.action = action;
|
|
@@ -100,8 +98,8 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
100
98
|
ctx.logger = this.logger.createScope(this.botName, message.traceId, message.chatInfo.name);
|
|
101
99
|
}
|
|
102
100
|
initializeMessageContext(ctx, action, message) {
|
|
103
|
-
ctx.messageInfo = new
|
|
104
|
-
ctx.userInfo = new
|
|
101
|
+
ctx.messageInfo = new MessageInfo(message.messageId, message.text, message.type, message.updateObject);
|
|
102
|
+
ctx.userInfo = new UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
|
|
105
103
|
(message.from?.last_name ? ` ${message.from.last_name}` : ''));
|
|
106
104
|
ctx.matchResults = [];
|
|
107
105
|
ctx.startCooldown = true;
|
|
@@ -116,4 +114,3 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
116
114
|
ctx.logger = this.logger.createScope(this.botName, message.traceId, message.chatInfo.name);
|
|
117
115
|
}
|
|
118
116
|
}
|
|
119
|
-
exports.CommandActionProcessor = CommandActionProcessor;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const traceFactory_1 = require("../../helpers/traceFactory");
|
|
7
|
-
const baseProcessor_1 = require("./baseProcessor");
|
|
8
|
-
class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
1
|
+
import { IncomingInlineQuery } from '../../dtos/incomingQuery';
|
|
2
|
+
import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
|
|
3
|
+
import { createTrace } from '../../helpers/traceFactory';
|
|
4
|
+
import { BaseActionProcessor } from './baseProcessor';
|
|
5
|
+
export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
9
6
|
inlineQueries;
|
|
10
7
|
initialize(api, telegram, inlineQueries, period) {
|
|
11
8
|
this.initializeDependencies(api);
|
|
@@ -14,7 +11,7 @@ class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
14
11
|
const queriesInProcessing = new Map();
|
|
15
12
|
if (this.inlineQueries.length > 0) {
|
|
16
13
|
telegram.on('inline_query', ({ inlineQuery }) => {
|
|
17
|
-
const query = new
|
|
14
|
+
const query = new IncomingInlineQuery(inlineQuery.id, inlineQuery.query, inlineQuery.from.id, createTrace('InlineQuery', this.botName, inlineQuery.id));
|
|
18
15
|
const logger = this.logger.createScope(this.botName, query.traceId, 'Query');
|
|
19
16
|
logger.logWithTraceId(`${inlineQuery.from.username ?? 'Unknown'} (${inlineQuery.from.id}): Query for ${inlineQuery.query}`);
|
|
20
17
|
const queryBeingProcessed = queriesInProcessing.get(query.userId);
|
|
@@ -27,7 +24,7 @@ class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
27
24
|
pendingInlineQueries.push(query);
|
|
28
25
|
});
|
|
29
26
|
this.scheduler.createTask('InlineQueryProcessing', async () => {
|
|
30
|
-
const ctx = new
|
|
27
|
+
const ctx = new InlineQueryContextInternal(this.storage, this.scheduler);
|
|
31
28
|
const queriesToProcess = [...pendingInlineQueries];
|
|
32
29
|
pendingInlineQueries = [];
|
|
33
30
|
for (const inlineQuery of queriesToProcess) {
|
|
@@ -62,4 +59,3 @@ class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
62
59
|
ctx.logger = this.logger.createScope(this.botName, traceId, 'Unknown');
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
|
-
exports.InlineQueryActionProcessor = InlineQueryActionProcessor;
|
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 {
|
|
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 {
|
|
14
8
|
chats;
|
|
15
9
|
scheduled;
|
|
16
10
|
constructor(botName, chats, storage, scheduler, logger) {
|
|
@@ -21,11 +15,11 @@ class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
21
15
|
this.initializeDependencies(api);
|
|
22
16
|
this.scheduled = scheduled;
|
|
23
17
|
if (this.scheduled.length > 0) {
|
|
24
|
-
const now = (
|
|
18
|
+
const now = moment();
|
|
25
19
|
if (now.minute() == 0 && now.second() == 0) {
|
|
26
20
|
this.scheduler.createTask('ScheduledProcessing', async () => {
|
|
27
21
|
await this.runScheduled();
|
|
28
|
-
},
|
|
22
|
+
}, secondsToMilliseconds(period), true, this.botName);
|
|
29
23
|
return;
|
|
30
24
|
}
|
|
31
25
|
let nextExecutionTime = now.clone().startOf('hour');
|
|
@@ -36,15 +30,15 @@ class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
36
30
|
this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', () => {
|
|
37
31
|
this.scheduler.createTask('ScheduledProcessing', () => {
|
|
38
32
|
void this.runScheduled();
|
|
39
|
-
},
|
|
33
|
+
}, secondsToMilliseconds(period), true, this.botName);
|
|
40
34
|
}, delay, this.botName);
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
37
|
async runScheduled() {
|
|
44
|
-
const ctx = new
|
|
38
|
+
const ctx = new ChatContextInternal(this.storage, this.scheduler);
|
|
45
39
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
46
40
|
for (const scheduledAction of this.scheduled) {
|
|
47
|
-
this.initializeChatContext(ctx, scheduledAction, new
|
|
41
|
+
this.initializeChatContext(ctx, scheduledAction, new ChatInfo(chatId, chatName, []), createTrace(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
|
|
48
42
|
await this.executeAction(scheduledAction, ctx);
|
|
49
43
|
}
|
|
50
44
|
}
|
|
@@ -60,4 +54,3 @@ class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
|
|
|
60
54
|
ctx.logger = this.logger.createScope(this.botName, traceId, chatInfo.name);
|
|
61
55
|
}
|
|
62
56
|
}
|
|
63
|
-
exports.ScheduledActionProcessor = ScheduledActionProcessor;
|