chz-telegram-bot 0.0.51 → 0.0.53
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 +5 -5
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/dtos/commandTriggerCheckResult.js +3 -6
- package/dist/dtos/incomingMessage.d.ts +8 -7
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +3 -2
- package/dist/dtos/responses/delay.d.ts +3 -2
- package/dist/dtos/responses/delay.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.d.ts +3 -2
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
- package/dist/dtos/responses/reaction.d.ts +3 -2
- package/dist/dtos/responses/reaction.d.ts.map +1 -1
- package/dist/dtos/responses/textMessage.d.ts +3 -2
- package/dist/dtos/responses/textMessage.d.ts.map +1 -1
- package/dist/dtos/responses/unpin.d.ts +3 -2
- package/dist/dtos/responses/unpin.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.d.ts +3 -2
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.d.ts +1 -0
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +38 -37
- package/dist/entities/actions/scheduledAction.d.ts +1 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +12 -16
- package/dist/entities/botInstance.d.ts +11 -3
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +26 -18
- package/dist/entities/context/chatContext.d.ts +12 -11
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +5 -12
- package/dist/entities/context/messageContext.d.ts +4 -2
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +4 -4
- package/dist/helpers/noop.d.ts +4 -2
- package/dist/helpers/noop.d.ts.map +1 -1
- package/dist/helpers/noop.js +3 -2
- package/dist/helpers/traceFactory.d.ts +3 -0
- package/dist/helpers/traceFactory.d.ts.map +1 -0
- package/dist/helpers/traceFactory.js +6 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/main.d.ts +10 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +6 -11
- package/dist/services/jsonFileStorage.d.ts +1 -2
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +3 -5
- package/dist/services/jsonLogger.d.ts +9 -0
- package/dist/services/jsonLogger.d.ts.map +1 -0
- package/dist/services/jsonLogger.js +28 -0
- package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
- package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
- package/dist/services/nodeTimeoutScheduler.js +34 -0
- package/dist/services/telegramApi.d.ts +7 -5
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +4 -4
- package/dist/types/commandCondition.d.ts +1 -1
- package/dist/types/commandCondition.d.ts.map +1 -1
- package/dist/types/logger.d.ts +7 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logger.js +2 -0
- package/dist/types/response.d.ts +2 -1
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/scheduler.d.ts +7 -0
- package/dist/types/scheduler.d.ts.map +1 -0
- package/dist/types/scheduler.js +2 -0
- package/dist/types/storage.d.ts +1 -2
- package/dist/types/storage.d.ts.map +1 -1
- package/dist/types/trace.d.ts +2 -0
- package/dist/types/trace.d.ts.map +1 -0
- package/dist/types/trace.js +2 -0
- package/dtos/commandTriggerCheckResult.ts +10 -10
- package/dtos/incomingMessage.ts +17 -7
- package/dtos/responses/delay.ts +3 -2
- package/dtos/responses/imageMessage.ts +3 -2
- package/dtos/responses/reaction.ts +3 -2
- package/dtos/responses/textMessage.ts +3 -2
- package/dtos/responses/unpin.ts +3 -2
- package/dtos/responses/videoMessage.ts +3 -2
- package/entities/actions/commandAction.ts +48 -41
- package/entities/actions/scheduledAction.ts +28 -31
- package/entities/botInstance.ts +57 -34
- package/entities/context/chatContext.ts +22 -17
- package/entities/context/messageContext.ts +10 -6
- package/helpers/noop.ts +5 -2
- package/helpers/traceFactory.ts +9 -0
- package/index.ts +4 -1
- package/main.ts +16 -15
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +4 -6
- package/services/{logger.ts → jsonLogger.ts} +7 -6
- package/services/{taskScheduler.ts → nodeTimeoutScheduler.ts} +15 -10
- package/services/telegramApi.ts +15 -8
- package/types/commandCondition.ts +1 -1
- package/types/logger.ts +26 -0
- package/types/response.ts +2 -1
- package/types/scheduler.ts +20 -0
- package/types/storage.ts +1 -2
- package/types/trace.ts +1 -0
- package/dtos/actionExecutionResult.ts +0 -11
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TelegramApiService = void 0;
|
|
4
|
-
const logger_1 = require("./logger");
|
|
5
4
|
const promises_1 = require("timers/promises");
|
|
6
5
|
const TELEGRAM_RATELIMIT_DELAY = 35;
|
|
7
6
|
class TelegramApiService {
|
|
8
|
-
constructor(botName, telegram, storage) {
|
|
9
|
-
this.isFlushing = false;
|
|
7
|
+
constructor(botName, telegram, storage, logger) {
|
|
10
8
|
this.messageQueue = [];
|
|
9
|
+
this.isFlushing = false;
|
|
11
10
|
this.telegram = telegram;
|
|
12
11
|
this.botName = botName;
|
|
13
12
|
this.storage = storage;
|
|
13
|
+
this.logger = logger;
|
|
14
14
|
}
|
|
15
15
|
enqueueBatchedResponses(responses) {
|
|
16
16
|
for (const response of responses) {
|
|
@@ -30,7 +30,7 @@ class TelegramApiService {
|
|
|
30
30
|
await (0, promises_1.setTimeout)(TELEGRAM_RATELIMIT_DELAY);
|
|
31
31
|
}
|
|
32
32
|
catch (error) {
|
|
33
|
-
|
|
33
|
+
this.logger.errorWithTraceId(this.botName, message.traceId, message.chatInfo.name, error, message);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
this.isFlushing = false;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { MessageContext } from '../entities/context/messageContext';
|
|
2
2
|
import { IActionState } from './actionState';
|
|
3
|
-
export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) =>
|
|
3
|
+
export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) => boolean;
|
|
4
4
|
//# sourceMappingURL=commandCondition.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC
|
|
1
|
+
{"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TraceId } from './trace';
|
|
2
|
+
export interface ILogger {
|
|
3
|
+
logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: any): void;
|
|
4
|
+
logWithTraceId(botName: string, traceId: TraceId, chatName: string, text: string): void;
|
|
5
|
+
errorWithTraceId<TData>(botName: string, traceId: TraceId, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../types/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,WAAW,OAAO;IACpB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACb,IAAI,CAAC;IAER,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS,GAC9B,IAAI,CAAC;CACX"}
|
package/dist/types/response.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { UnpinResponse } from '../dtos/responses/unpin';
|
|
|
7
7
|
import { VideoMessage } from '../dtos/responses/videoMessage';
|
|
8
8
|
import { IActionState } from './actionState';
|
|
9
9
|
import { IActionWithState } from './actionWithState';
|
|
10
|
+
import { TraceId } from './trace';
|
|
10
11
|
export declare const BotResponseTypes: {
|
|
11
12
|
readonly unpin: "unpin";
|
|
12
13
|
readonly text: "text";
|
|
@@ -19,7 +20,7 @@ export type BotResponse = UnpinResponse | Reaction | TextMessage | VideoMessage
|
|
|
19
20
|
export interface IChatResponse {
|
|
20
21
|
readonly kind: keyof typeof BotResponseTypes;
|
|
21
22
|
readonly chatInfo: ChatInfo;
|
|
22
|
-
readonly traceId:
|
|
23
|
+
readonly traceId: TraceId;
|
|
23
24
|
readonly action: IActionWithState<IActionState>;
|
|
24
25
|
}
|
|
25
26
|
export interface IReplyMessage<TType> extends IChatResponse {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../types/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../types/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,eAAO,MAAM,gBAAgB;;;;;;;CAOnB,CAAC;AAEX,MAAM,MAAM,WAAW,GACjB,aAAa,GACb,QAAQ,GACR,WAAW,GACX,YAAY,GACZ,aAAa,GACb,YAAY,CAAC;AAEnB,MAAM,WAAW,aAAa;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,gBAAgB,CAAC;IAC7C,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAE1B,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;CACnD;AAED,MAAM,WAAW,aAAa,CAAC,KAAK,CAAE,SAAQ,aAAa;IACvD,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC/B"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Milliseconds } from './timeValues';
|
|
2
|
+
export interface IScheduler {
|
|
3
|
+
stopAll(): void;
|
|
4
|
+
createTask(name: string, action: () => void, interval: Milliseconds, executeRightAway: boolean, ownerName: string): void;
|
|
5
|
+
createOnetimeTask(name: string, action: () => void, delay: Milliseconds, ownerName: string): void;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=scheduler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../types/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,WAAW,UAAU;IACvB,OAAO,IAAI,IAAI,CAAC;IAEhB,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM,GAClB,IAAI,CAAC;IAER,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,IAAI,EAClB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM,GAClB,IAAI,CAAC;CACX"}
|
package/dist/types/storage.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ActionExecutionResult } from '../dtos/actionExecutionResult';
|
|
2
1
|
import { IActionState } from './actionState';
|
|
3
2
|
import { ActionKey, IActionWithState } from './actionWithState';
|
|
4
3
|
export interface IStorageClient {
|
|
@@ -7,6 +6,6 @@ export interface IStorageClient {
|
|
|
7
6
|
load<TActionState extends IActionState>(key: ActionKey): Promise<Record<number, TActionState>>;
|
|
8
7
|
saveMetadata<TActionState extends IActionState>(actions: IActionWithState<TActionState>[], botName: string): Promise<void>;
|
|
9
8
|
getActionState<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number): Promise<TActionState>;
|
|
10
|
-
saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number,
|
|
9
|
+
saveActionExecutionResult<TActionState extends IActionState>(action: IActionWithState<TActionState>, chatId: number, state: TActionState): Promise<void>;
|
|
11
10
|
}
|
|
12
11
|
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC3B,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,SAAS,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,YAAY,SAAS,YAAY,EAC1C,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CAAC,YAAY,SAAS,YAAY,EACvD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY,GACpB,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../types/trace.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC"}
|
|
@@ -6,25 +6,25 @@ export class CommandTriggerCheckResult {
|
|
|
6
6
|
return new CommandTriggerCheckResult(false, [], false);
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
matchResults: RegExpExecArray[];
|
|
11
|
-
skipCooldown: boolean;
|
|
9
|
+
readonly shouldExecute: boolean;
|
|
10
|
+
readonly matchResults: RegExpExecArray[];
|
|
11
|
+
readonly skipCooldown: boolean;
|
|
12
12
|
|
|
13
13
|
constructor(
|
|
14
|
-
|
|
14
|
+
shouldExecute: boolean,
|
|
15
15
|
matchResults: RegExpExecArray[],
|
|
16
16
|
skipCooldown: boolean
|
|
17
17
|
) {
|
|
18
|
-
this.
|
|
18
|
+
this.shouldExecute = shouldExecute;
|
|
19
19
|
this.matchResults = matchResults;
|
|
20
20
|
this.skipCooldown = skipCooldown;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
mergeWith(other: CommandTriggerCheckResult) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
return new CommandTriggerCheckResult(
|
|
25
|
+
this.shouldExecute || other.shouldExecute,
|
|
26
|
+
this.matchResults.concat(other.matchResults),
|
|
27
|
+
this.skipCooldown || other.skipCooldown
|
|
28
|
+
);
|
|
29
29
|
}
|
|
30
30
|
}
|
package/dtos/incomingMessage.ts
CHANGED
|
@@ -2,14 +2,16 @@ import { Message, Update, User } from 'telegraf/types';
|
|
|
2
2
|
import { randomInt } from 'crypto';
|
|
3
3
|
import { MessageType, MessageTypeValue } from '../types/messageTypes';
|
|
4
4
|
import { ChatInfo } from './chatInfo';
|
|
5
|
+
import { createTrace } from '../helpers/traceFactory';
|
|
6
|
+
import { TraceId } from '../types/trace';
|
|
5
7
|
|
|
6
8
|
export class IncomingMessage {
|
|
7
|
-
message_id: number;
|
|
8
|
-
chatInfo: ChatInfo;
|
|
9
|
-
from: User | undefined;
|
|
10
|
-
text: string;
|
|
11
|
-
type: MessageTypeValue;
|
|
12
|
-
traceId
|
|
9
|
+
readonly message_id: number;
|
|
10
|
+
readonly chatInfo: ChatInfo;
|
|
11
|
+
readonly from: User | undefined;
|
|
12
|
+
readonly text: string;
|
|
13
|
+
readonly type: MessageTypeValue;
|
|
14
|
+
readonly traceId: TraceId;
|
|
13
15
|
|
|
14
16
|
private detectMessageType(
|
|
15
17
|
message: Update.New & (Update.NonChannel & Message)
|
|
@@ -29,7 +31,15 @@ export class IncomingMessage {
|
|
|
29
31
|
return MessageType.Unknown;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
constructor(
|
|
34
|
+
constructor(
|
|
35
|
+
ctxMessage: Update.New & (Update.NonChannel & Message),
|
|
36
|
+
botName: string
|
|
37
|
+
) {
|
|
38
|
+
this.traceId = createTrace(
|
|
39
|
+
this,
|
|
40
|
+
botName,
|
|
41
|
+
randomInt(10000, 99999).toString()
|
|
42
|
+
);
|
|
33
43
|
this.message_id = ctxMessage.message_id;
|
|
34
44
|
this.from = ctxMessage.from;
|
|
35
45
|
this.text = 'text' in ctxMessage ? ctxMessage.text : '';
|
package/dtos/responses/delay.ts
CHANGED
|
@@ -2,20 +2,21 @@ import { IActionState } from '../../types/actionState';
|
|
|
2
2
|
import { IActionWithState } from '../../types/actionWithState';
|
|
3
3
|
import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
4
4
|
import { Milliseconds } from '../../types/timeValues';
|
|
5
|
+
import { TraceId } from '../../types/trace';
|
|
5
6
|
import { ChatInfo } from '../chatInfo';
|
|
6
7
|
|
|
7
8
|
export class DelayResponse implements IChatResponse {
|
|
8
9
|
readonly kind = BotResponseTypes.delay;
|
|
9
10
|
|
|
10
11
|
readonly chatInfo: ChatInfo;
|
|
11
|
-
readonly traceId:
|
|
12
|
+
readonly traceId: TraceId;
|
|
12
13
|
readonly delay: Milliseconds;
|
|
13
14
|
readonly action: IActionWithState<IActionState>;
|
|
14
15
|
|
|
15
16
|
constructor(
|
|
16
17
|
delay: Milliseconds,
|
|
17
18
|
chatInfo: ChatInfo,
|
|
18
|
-
traceId:
|
|
19
|
+
traceId: TraceId,
|
|
19
20
|
action: IActionWithState<IActionState>
|
|
20
21
|
) {
|
|
21
22
|
this.chatInfo = chatInfo;
|
|
@@ -4,6 +4,7 @@ import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
5
|
import { IActionState } from '../../types/actionState';
|
|
6
6
|
import { ChatInfo } from '../chatInfo';
|
|
7
|
+
import { TraceId } from '../../types/trace';
|
|
7
8
|
|
|
8
9
|
export class ImageMessage implements IReplyMessage<InputFile> {
|
|
9
10
|
readonly kind = BotResponseTypes.image;
|
|
@@ -11,7 +12,7 @@ export class ImageMessage implements IReplyMessage<InputFile> {
|
|
|
11
12
|
readonly content: InputFile;
|
|
12
13
|
readonly chatInfo: ChatInfo;
|
|
13
14
|
readonly replyId: number | undefined;
|
|
14
|
-
readonly traceId:
|
|
15
|
+
readonly traceId: TraceId;
|
|
15
16
|
readonly disableWebPreview = false;
|
|
16
17
|
readonly shouldPin: boolean;
|
|
17
18
|
readonly action: IActionWithState<IActionState>;
|
|
@@ -20,7 +21,7 @@ export class ImageMessage implements IReplyMessage<InputFile> {
|
|
|
20
21
|
image: InputFile,
|
|
21
22
|
chatInfo: ChatInfo,
|
|
22
23
|
replyId: number | undefined,
|
|
23
|
-
traceId:
|
|
24
|
+
traceId: TraceId,
|
|
24
25
|
action: IActionWithState<IActionState>,
|
|
25
26
|
options?: MessageSendingOptions
|
|
26
27
|
) {
|
|
@@ -3,18 +3,19 @@ import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
|
3
3
|
import { IActionWithState } from '../../types/actionWithState';
|
|
4
4
|
import { IActionState } from '../../types/actionState';
|
|
5
5
|
import { ChatInfo } from '../chatInfo';
|
|
6
|
+
import { TraceId } from '../../types/trace';
|
|
6
7
|
|
|
7
8
|
export class Reaction implements IChatResponse {
|
|
8
9
|
readonly kind = BotResponseTypes.react;
|
|
9
10
|
|
|
10
11
|
readonly chatInfo: ChatInfo;
|
|
11
12
|
readonly messageId: number;
|
|
12
|
-
readonly traceId:
|
|
13
|
+
readonly traceId: TraceId;
|
|
13
14
|
readonly emoji: TelegramEmoji;
|
|
14
15
|
readonly action: IActionWithState<IActionState>;
|
|
15
16
|
|
|
16
17
|
constructor(
|
|
17
|
-
traceId:
|
|
18
|
+
traceId: TraceId,
|
|
18
19
|
chatInfo: ChatInfo,
|
|
19
20
|
messageId: number,
|
|
20
21
|
emoji: TelegramEmoji,
|
|
@@ -3,6 +3,7 @@ import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
|
3
3
|
import { IActionWithState } from '../../types/actionWithState';
|
|
4
4
|
import { IActionState } from '../../types/actionState';
|
|
5
5
|
import { ChatInfo } from '../chatInfo';
|
|
6
|
+
import { TraceId } from '../../types/trace';
|
|
6
7
|
|
|
7
8
|
export class TextMessage implements IReplyMessage<string> {
|
|
8
9
|
readonly kind = BotResponseTypes.text;
|
|
@@ -10,7 +11,7 @@ export class TextMessage implements IReplyMessage<string> {
|
|
|
10
11
|
readonly content: string;
|
|
11
12
|
readonly chatInfo: ChatInfo;
|
|
12
13
|
readonly replyId: number | undefined;
|
|
13
|
-
readonly traceId:
|
|
14
|
+
readonly traceId: TraceId;
|
|
14
15
|
readonly disableWebPreview: boolean;
|
|
15
16
|
readonly shouldPin: boolean;
|
|
16
17
|
readonly action: IActionWithState<IActionState>;
|
|
@@ -19,7 +20,7 @@ export class TextMessage implements IReplyMessage<string> {
|
|
|
19
20
|
text: string,
|
|
20
21
|
chatInfo: ChatInfo,
|
|
21
22
|
replyId: number | undefined,
|
|
22
|
-
traceId:
|
|
23
|
+
traceId: TraceId,
|
|
23
24
|
action: IActionWithState<IActionState>,
|
|
24
25
|
options?: TextMessageSendingOptions
|
|
25
26
|
) {
|
package/dtos/responses/unpin.ts
CHANGED
|
@@ -2,19 +2,20 @@ import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
|
2
2
|
import { IActionWithState } from '../../types/actionWithState';
|
|
3
3
|
import { IActionState } from '../../types/actionState';
|
|
4
4
|
import { ChatInfo } from '../chatInfo';
|
|
5
|
+
import { TraceId } from '../../types/trace';
|
|
5
6
|
|
|
6
7
|
export class UnpinResponse implements IChatResponse {
|
|
7
8
|
readonly kind = BotResponseTypes.unpin;
|
|
8
9
|
|
|
9
10
|
readonly messageId: number;
|
|
10
11
|
readonly chatInfo: ChatInfo;
|
|
11
|
-
readonly traceId:
|
|
12
|
+
readonly traceId: TraceId;
|
|
12
13
|
readonly action: IActionWithState<IActionState>;
|
|
13
14
|
|
|
14
15
|
constructor(
|
|
15
16
|
messageId: number,
|
|
16
17
|
chatInfo: ChatInfo,
|
|
17
|
-
traceId:
|
|
18
|
+
traceId: TraceId,
|
|
18
19
|
action: IActionWithState<IActionState>
|
|
19
20
|
) {
|
|
20
21
|
this.messageId = messageId;
|
|
@@ -4,6 +4,7 @@ import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
5
|
import { IActionState } from '../../types/actionState';
|
|
6
6
|
import { ChatInfo } from '../chatInfo';
|
|
7
|
+
import { TraceId } from '../../types/trace';
|
|
7
8
|
|
|
8
9
|
export class VideoMessage implements IReplyMessage<InputFile> {
|
|
9
10
|
readonly kind = BotResponseTypes.video;
|
|
@@ -11,7 +12,7 @@ export class VideoMessage implements IReplyMessage<InputFile> {
|
|
|
11
12
|
readonly content: InputFile;
|
|
12
13
|
readonly chatInfo: ChatInfo;
|
|
13
14
|
readonly replyId: number | undefined;
|
|
14
|
-
readonly traceId:
|
|
15
|
+
readonly traceId: TraceId;
|
|
15
16
|
readonly disableWebPreview = false;
|
|
16
17
|
readonly shouldPin: boolean;
|
|
17
18
|
readonly action: IActionWithState<IActionState>;
|
|
@@ -20,7 +21,7 @@ export class VideoMessage implements IReplyMessage<InputFile> {
|
|
|
20
21
|
video: InputFile,
|
|
21
22
|
chatInfo: ChatInfo,
|
|
22
23
|
replyId: number | undefined,
|
|
23
|
-
traceId:
|
|
24
|
+
traceId: TraceId,
|
|
24
25
|
action: IActionWithState<IActionState>,
|
|
25
26
|
options?: MessageSendingOptions
|
|
26
27
|
) {
|
|
@@ -8,9 +8,8 @@ import { IActionState } from '../../types/actionState';
|
|
|
8
8
|
import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
9
9
|
import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
|
|
10
10
|
import { MessageContext } from '../context/messageContext';
|
|
11
|
-
import { Logger } from '../../services/logger';
|
|
12
|
-
import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
|
|
13
11
|
import { CommandTrigger } from '../../types/commandTrigger';
|
|
12
|
+
import { Noop } from '../../helpers/noop';
|
|
14
13
|
|
|
15
14
|
export class CommandAction<TActionState extends IActionState>
|
|
16
15
|
implements IActionWithState<TActionState>
|
|
@@ -57,27 +56,23 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
57
56
|
);
|
|
58
57
|
|
|
59
58
|
if (!this.active || this.chatsBlacklist.includes(ctx.chatInfo.id))
|
|
60
|
-
return
|
|
61
|
-
|
|
62
|
-
const isConditionMet = await this.condition(ctx);
|
|
63
|
-
|
|
64
|
-
if (!isConditionMet) return [];
|
|
59
|
+
return Noop.NoResponse;
|
|
65
60
|
|
|
66
61
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
67
62
|
this,
|
|
68
63
|
ctx.chatInfo.id
|
|
69
64
|
);
|
|
70
65
|
|
|
71
|
-
const {
|
|
72
|
-
.map((x) => this.
|
|
66
|
+
const { shouldExecute, matchResults, skipCooldown } = this.triggers
|
|
67
|
+
.map((x) => this.checkIfShouldBeExecuted(ctx, x, state))
|
|
73
68
|
.reduce(
|
|
74
69
|
(acc, curr) => acc.mergeWith(curr),
|
|
75
70
|
CommandTriggerCheckResult.DoNotTrigger
|
|
76
71
|
);
|
|
77
72
|
|
|
78
|
-
if (!
|
|
73
|
+
if (!shouldExecute) return Noop.NoResponse;
|
|
79
74
|
|
|
80
|
-
|
|
75
|
+
ctx.logger.logWithTraceId(
|
|
81
76
|
ctx.botName,
|
|
82
77
|
ctx.traceId,
|
|
83
78
|
ctx.chatInfo.name,
|
|
@@ -95,27 +90,20 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
95
90
|
state.lastExecutedDate = moment().valueOf();
|
|
96
91
|
}
|
|
97
92
|
|
|
98
|
-
ctx.updateActions.forEach((action) => action(state));
|
|
99
|
-
|
|
100
93
|
await ctx.storage.saveActionExecutionResult(
|
|
101
94
|
this,
|
|
102
95
|
ctx.chatInfo.id,
|
|
103
|
-
|
|
96
|
+
state
|
|
104
97
|
);
|
|
105
98
|
|
|
106
|
-
ctx.isInitialized = false;
|
|
107
|
-
|
|
108
99
|
return ctx.responses;
|
|
109
100
|
}
|
|
110
101
|
|
|
111
|
-
private
|
|
102
|
+
private checkIfShouldBeExecuted(
|
|
112
103
|
ctx: MessageContext<TActionState>,
|
|
113
104
|
trigger: CommandTrigger,
|
|
114
105
|
state: IActionState
|
|
115
106
|
) {
|
|
116
|
-
let shouldTrigger = false;
|
|
117
|
-
const matchResults: RegExpExecArray[] = [];
|
|
118
|
-
|
|
119
107
|
if (!ctx.fromUserId)
|
|
120
108
|
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
121
109
|
|
|
@@ -135,28 +123,11 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
135
123
|
|
|
136
124
|
if (onCooldown) return CommandTriggerCheckResult.DoNotTrigger;
|
|
137
125
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
shouldTrigger = ctx.messageText.toLowerCase() == trigger;
|
|
142
|
-
} else {
|
|
143
|
-
trigger.lastIndex = 0;
|
|
144
|
-
|
|
145
|
-
const execResult = trigger.exec(ctx.messageText);
|
|
146
|
-
if (execResult != null) {
|
|
147
|
-
matchResults.push(execResult);
|
|
148
|
-
|
|
149
|
-
if (trigger.global) {
|
|
150
|
-
while (true) {
|
|
151
|
-
const nextResult = trigger.exec(ctx.messageText);
|
|
152
|
-
if (nextResult == null) break;
|
|
153
|
-
matchResults.push(nextResult);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
126
|
+
const isCustomConditionMet = this.condition(ctx);
|
|
127
|
+
if (!isCustomConditionMet)
|
|
128
|
+
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
157
129
|
|
|
158
|
-
|
|
159
|
-
}
|
|
130
|
+
const { shouldTrigger, matchResults } = this.checkTrigger(ctx, trigger);
|
|
160
131
|
|
|
161
132
|
return new CommandTriggerCheckResult(
|
|
162
133
|
shouldTrigger,
|
|
@@ -164,4 +135,40 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
164
135
|
false
|
|
165
136
|
);
|
|
166
137
|
}
|
|
138
|
+
|
|
139
|
+
private checkTrigger(
|
|
140
|
+
ctx: MessageContext<TActionState>,
|
|
141
|
+
trigger: CommandTrigger
|
|
142
|
+
) {
|
|
143
|
+
if (trigger == ctx.messageType)
|
|
144
|
+
return { shouldTrigger: true, matchResults: [] };
|
|
145
|
+
|
|
146
|
+
if (typeof trigger == 'string')
|
|
147
|
+
return {
|
|
148
|
+
shouldTrigger: ctx.messageText.toLowerCase() == trigger,
|
|
149
|
+
matchResults: []
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const matchResults: RegExpExecArray[] = [];
|
|
153
|
+
|
|
154
|
+
trigger.lastIndex = 0;
|
|
155
|
+
|
|
156
|
+
const execResult = trigger.exec(ctx.messageText);
|
|
157
|
+
if (execResult != null) {
|
|
158
|
+
matchResults.push(execResult);
|
|
159
|
+
|
|
160
|
+
if (trigger.global) {
|
|
161
|
+
while (true) {
|
|
162
|
+
const nextResult = trigger.exec(ctx.messageText);
|
|
163
|
+
if (nextResult == null) break;
|
|
164
|
+
matchResults.push(nextResult);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
shouldTrigger: matchResults.length > 0,
|
|
171
|
+
matchResults
|
|
172
|
+
};
|
|
173
|
+
}
|
|
167
174
|
}
|
|
@@ -7,9 +7,8 @@ import { IActionState } from '../../types/actionState';
|
|
|
7
7
|
import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
8
8
|
import { CachedStateFactory } from '../cachedStateFactory';
|
|
9
9
|
import { ChatContext } from '../context/chatContext';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import { Scheduler } from '../../services/taskScheduler';
|
|
10
|
+
import { Noop } from '../../helpers/noop';
|
|
11
|
+
import { IScheduler } from '../../types/scheduler';
|
|
13
12
|
|
|
14
13
|
export class ScheduledAction<TActionState extends IActionState>
|
|
15
14
|
implements IActionWithState<TActionState>
|
|
@@ -53,47 +52,45 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
53
52
|
);
|
|
54
53
|
|
|
55
54
|
if (!this.active || !this.chatsWhitelist.includes(ctx.chatInfo.id))
|
|
56
|
-
return
|
|
55
|
+
return Noop.NoResponse;
|
|
57
56
|
|
|
58
57
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
59
58
|
this,
|
|
60
59
|
ctx.chatInfo.id
|
|
61
60
|
);
|
|
62
|
-
const isAllowedToTrigger = this.shouldTrigger(state);
|
|
63
|
-
|
|
64
|
-
if (isAllowedToTrigger) {
|
|
65
|
-
Logger.logWithTraceId(
|
|
66
|
-
ctx.botName,
|
|
67
|
-
ctx.traceId,
|
|
68
|
-
ctx.chatInfo.name,
|
|
69
|
-
` - Executing [${this.name}] in ${ctx.chatInfo.id}`
|
|
70
|
-
);
|
|
71
61
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
<TResult>(key: string) =>
|
|
75
|
-
this.getCachedValue<TResult>(key, ctx.botName),
|
|
76
|
-
state
|
|
77
|
-
);
|
|
62
|
+
const isAllowedToTrigger = this.checkIfShouldBeExecuted(state);
|
|
63
|
+
if (!isAllowedToTrigger) return Noop.NoResponse;
|
|
78
64
|
|
|
79
|
-
|
|
65
|
+
ctx.logger.logWithTraceId(
|
|
66
|
+
ctx.botName,
|
|
67
|
+
ctx.traceId,
|
|
68
|
+
ctx.chatInfo.name,
|
|
69
|
+
` - Executing [${this.name}] in ${ctx.chatInfo.id}`
|
|
70
|
+
);
|
|
80
71
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
ctx.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
72
|
+
await this.handler(
|
|
73
|
+
ctx,
|
|
74
|
+
<TResult>(key: string) =>
|
|
75
|
+
this.getCachedValue<TResult>(key, ctx.botName, ctx.scheduler),
|
|
76
|
+
state
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
state.lastExecutedDate = moment().valueOf();
|
|
88
80
|
|
|
89
|
-
ctx.
|
|
81
|
+
await ctx.storage.saveActionExecutionResult(
|
|
82
|
+
this,
|
|
83
|
+
ctx.chatInfo.id,
|
|
84
|
+
state
|
|
85
|
+
);
|
|
90
86
|
|
|
91
87
|
return ctx.responses;
|
|
92
88
|
}
|
|
93
89
|
|
|
94
90
|
private async getCachedValue<TResult>(
|
|
95
91
|
key: string,
|
|
96
|
-
botName: string
|
|
92
|
+
botName: string,
|
|
93
|
+
scheduler: IScheduler
|
|
97
94
|
): Promise<TResult> {
|
|
98
95
|
if (!this.cachedStateFactories.has(key)) {
|
|
99
96
|
throw new Error(
|
|
@@ -122,7 +119,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
122
119
|
|
|
123
120
|
this.cachedState.set(key, value);
|
|
124
121
|
|
|
125
|
-
|
|
122
|
+
scheduler.createOnetimeTask(
|
|
126
123
|
`Drop cached value [${this.name} : ${key}]`,
|
|
127
124
|
() => this.cachedState.delete(key),
|
|
128
125
|
hoursToMilliseconds(
|
|
@@ -137,7 +134,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
137
134
|
}
|
|
138
135
|
}
|
|
139
136
|
|
|
140
|
-
private
|
|
137
|
+
private checkIfShouldBeExecuted(state: IActionState): boolean {
|
|
141
138
|
const startOfToday = moment().startOf('day').valueOf();
|
|
142
139
|
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
143
140
|
const currentTime = moment();
|