chz-telegram-bot 0.0.50 → 0.0.51
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/actionExecutionResult.d.ts +7 -0
- package/dist/dtos/actionExecutionResult.d.ts.map +1 -0
- package/dist/dtos/actionExecutionResult.js +10 -0
- package/dist/dtos/chatInfo.d.ts +8 -0
- package/dist/dtos/chatInfo.d.ts.map +1 -0
- package/dist/dtos/chatInfo.js +10 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts +10 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
- package/dist/dtos/commandTriggerCheckResult.js +23 -0
- package/dist/dtos/incomingMessage.d.ts +14 -0
- package/dist/dtos/incomingMessage.d.ts.map +1 -0
- package/dist/dtos/incomingMessage.js +44 -0
- package/dist/dtos/responses/delay.d.ts +14 -0
- package/dist/dtos/responses/delay.d.ts.map +1 -0
- package/dist/dtos/responses/delay.js +14 -0
- package/dist/dtos/responses/imageMessage.d.ts +18 -0
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
- package/dist/dtos/responses/imageMessage.js +17 -0
- package/dist/dtos/responses/reaction.d.ts +15 -0
- package/dist/dtos/responses/reaction.d.ts.map +1 -0
- package/dist/dtos/responses/reaction.js +15 -0
- package/dist/dtos/responses/textMessage.d.ts +17 -0
- package/dist/dtos/responses/textMessage.d.ts.map +1 -0
- package/dist/dtos/responses/textMessage.js +17 -0
- package/dist/dtos/responses/unpin.d.ts +13 -0
- package/dist/dtos/responses/unpin.d.ts.map +1 -0
- package/dist/dtos/responses/unpin.js +14 -0
- package/dist/dtos/responses/videoMessage.d.ts +18 -0
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
- package/dist/dtos/responses/videoMessage.js +17 -0
- package/dist/entities/actions/commandAction.d.ts +10 -10
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +6 -6
- package/dist/entities/actions/scheduledAction.d.ts +10 -10
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +5 -5
- package/dist/entities/botInstance.d.ts +7 -7
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +8 -7
- package/dist/entities/cachedStateFactory.d.ts +2 -2
- package/dist/entities/cachedStateFactory.d.ts.map +1 -1
- package/dist/entities/context/chatContext.d.ts +4 -5
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +12 -13
- package/dist/entities/context/messageContext.d.ts +1 -1
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +10 -10
- package/dist/entities/taskRecord.d.ts +3 -3
- package/dist/entities/taskRecord.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.d.ts +5 -5
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/taskScheduler.d.ts +1 -1
- package/dist/services/taskScheduler.d.ts.map +1 -1
- package/dist/services/telegramApi.d.ts +5 -14
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +10 -18
- package/dist/types/actionWithState.d.ts +2 -2
- package/dist/types/actionWithState.d.ts.map +1 -1
- package/dist/types/response.d.ts +15 -14
- 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/{entities → dtos}/actionExecutionResult.ts +2 -2
- package/dtos/chatInfo.ts +11 -0
- package/{entities → dtos}/incomingMessage.ts +8 -7
- package/{entities → dtos}/responses/delay.ts +8 -7
- package/{entities → dtos}/responses/imageMessage.ts +11 -10
- package/{entities → dtos}/responses/reaction.ts +9 -8
- package/{entities → dtos}/responses/textMessage.ts +11 -10
- package/{entities → dtos}/responses/unpin.ts +8 -7
- package/{entities → dtos}/responses/videoMessage.ts +11 -10
- package/entities/actions/commandAction.ts +18 -17
- package/entities/actions/scheduledAction.ts +16 -16
- package/entities/botInstance.ts +26 -15
- package/entities/cachedStateFactory.ts +2 -2
- package/entities/context/chatContext.ts +20 -18
- package/entities/context/messageContext.ts +11 -12
- package/entities/taskRecord.ts +3 -3
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +5 -5
- package/services/taskScheduler.ts +1 -1
- package/services/telegramApi.ts +16 -58
- package/types/actionWithState.ts +2 -2
- package/types/response.ts +15 -14
- package/types/storage.ts +1 -1
- package/helpers/inverseRecord.ts +0 -7
- /package/{entities → dtos}/commandTriggerCheckResult.ts +0 -0
|
@@ -2,22 +2,23 @@ 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 { ChatInfo } from '../chatInfo';
|
|
5
6
|
|
|
6
7
|
export class DelayResponse implements IChatResponse {
|
|
7
|
-
kind = BotResponseTypes.delay;
|
|
8
|
+
readonly kind = BotResponseTypes.delay;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
traceId: number | string;
|
|
11
|
-
delay: Milliseconds;
|
|
12
|
-
action: IActionWithState<IActionState>;
|
|
10
|
+
readonly chatInfo: ChatInfo;
|
|
11
|
+
readonly traceId: number | string;
|
|
12
|
+
readonly delay: Milliseconds;
|
|
13
|
+
readonly action: IActionWithState<IActionState>;
|
|
13
14
|
|
|
14
15
|
constructor(
|
|
15
16
|
delay: Milliseconds,
|
|
16
|
-
|
|
17
|
+
chatInfo: ChatInfo,
|
|
17
18
|
traceId: number | string,
|
|
18
19
|
action: IActionWithState<IActionState>
|
|
19
20
|
) {
|
|
20
|
-
this.
|
|
21
|
+
this.chatInfo = chatInfo;
|
|
21
22
|
this.delay = delay;
|
|
22
23
|
this.traceId = traceId;
|
|
23
24
|
this.action = action;
|
|
@@ -3,28 +3,29 @@ import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
|
3
3
|
import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
5
|
import { IActionState } from '../../types/actionState';
|
|
6
|
+
import { ChatInfo } from '../chatInfo';
|
|
6
7
|
|
|
7
8
|
export class ImageMessage implements IReplyMessage<InputFile> {
|
|
8
|
-
kind = BotResponseTypes.image;
|
|
9
|
+
readonly kind = BotResponseTypes.image;
|
|
9
10
|
|
|
10
|
-
content: InputFile;
|
|
11
|
-
|
|
12
|
-
replyId: number | undefined;
|
|
13
|
-
traceId: string | number;
|
|
14
|
-
disableWebPreview = false;
|
|
15
|
-
shouldPin: boolean;
|
|
16
|
-
action: IActionWithState<IActionState>;
|
|
11
|
+
readonly content: InputFile;
|
|
12
|
+
readonly chatInfo: ChatInfo;
|
|
13
|
+
readonly replyId: number | undefined;
|
|
14
|
+
readonly traceId: string | number;
|
|
15
|
+
readonly disableWebPreview = false;
|
|
16
|
+
readonly shouldPin: boolean;
|
|
17
|
+
readonly action: IActionWithState<IActionState>;
|
|
17
18
|
|
|
18
19
|
constructor(
|
|
19
20
|
image: InputFile,
|
|
20
|
-
|
|
21
|
+
chatInfo: ChatInfo,
|
|
21
22
|
replyId: number | undefined,
|
|
22
23
|
traceId: number | string,
|
|
23
24
|
action: IActionWithState<IActionState>,
|
|
24
25
|
options?: MessageSendingOptions
|
|
25
26
|
) {
|
|
26
27
|
this.content = image;
|
|
27
|
-
this.
|
|
28
|
+
this.chatInfo = chatInfo;
|
|
28
29
|
this.replyId = replyId;
|
|
29
30
|
this.traceId = traceId;
|
|
30
31
|
this.shouldPin = options?.pin ?? false;
|
|
@@ -2,24 +2,25 @@ import { TelegramEmoji } from 'telegraf/types';
|
|
|
2
2
|
import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
3
3
|
import { IActionWithState } from '../../types/actionWithState';
|
|
4
4
|
import { IActionState } from '../../types/actionState';
|
|
5
|
+
import { ChatInfo } from '../chatInfo';
|
|
5
6
|
|
|
6
7
|
export class Reaction implements IChatResponse {
|
|
7
|
-
kind = BotResponseTypes.react;
|
|
8
|
+
readonly kind = BotResponseTypes.react;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
messageId: number;
|
|
11
|
-
traceId: number | string;
|
|
12
|
-
emoji: TelegramEmoji;
|
|
13
|
-
action: IActionWithState<IActionState>;
|
|
10
|
+
readonly chatInfo: ChatInfo;
|
|
11
|
+
readonly messageId: number;
|
|
12
|
+
readonly traceId: number | string;
|
|
13
|
+
readonly emoji: TelegramEmoji;
|
|
14
|
+
readonly action: IActionWithState<IActionState>;
|
|
14
15
|
|
|
15
16
|
constructor(
|
|
16
17
|
traceId: number | string,
|
|
17
|
-
|
|
18
|
+
chatInfo: ChatInfo,
|
|
18
19
|
messageId: number,
|
|
19
20
|
emoji: TelegramEmoji,
|
|
20
21
|
action: IActionWithState<IActionState>
|
|
21
22
|
) {
|
|
22
|
-
this.
|
|
23
|
+
this.chatInfo = chatInfo;
|
|
23
24
|
this.messageId = messageId;
|
|
24
25
|
this.emoji = emoji;
|
|
25
26
|
this.traceId = traceId;
|
|
@@ -2,28 +2,29 @@ import { TextMessageSendingOptions } from '../../types/messageSendingOptions';
|
|
|
2
2
|
import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
3
3
|
import { IActionWithState } from '../../types/actionWithState';
|
|
4
4
|
import { IActionState } from '../../types/actionState';
|
|
5
|
+
import { ChatInfo } from '../chatInfo';
|
|
5
6
|
|
|
6
7
|
export class TextMessage implements IReplyMessage<string> {
|
|
7
|
-
kind = BotResponseTypes.text;
|
|
8
|
+
readonly kind = BotResponseTypes.text;
|
|
8
9
|
|
|
9
|
-
content: string;
|
|
10
|
-
|
|
11
|
-
replyId: number | undefined;
|
|
12
|
-
traceId: string | number;
|
|
13
|
-
disableWebPreview: boolean;
|
|
14
|
-
shouldPin: boolean;
|
|
15
|
-
action: IActionWithState<IActionState>;
|
|
10
|
+
readonly content: string;
|
|
11
|
+
readonly chatInfo: ChatInfo;
|
|
12
|
+
readonly replyId: number | undefined;
|
|
13
|
+
readonly traceId: string | number;
|
|
14
|
+
readonly disableWebPreview: boolean;
|
|
15
|
+
readonly shouldPin: boolean;
|
|
16
|
+
readonly action: IActionWithState<IActionState>;
|
|
16
17
|
|
|
17
18
|
constructor(
|
|
18
19
|
text: string,
|
|
19
|
-
|
|
20
|
+
chatInfo: ChatInfo,
|
|
20
21
|
replyId: number | undefined,
|
|
21
22
|
traceId: string | number,
|
|
22
23
|
action: IActionWithState<IActionState>,
|
|
23
24
|
options?: TextMessageSendingOptions
|
|
24
25
|
) {
|
|
25
26
|
this.content = text;
|
|
26
|
-
this.
|
|
27
|
+
this.chatInfo = chatInfo;
|
|
27
28
|
this.replyId = replyId;
|
|
28
29
|
this.traceId = traceId;
|
|
29
30
|
this.disableWebPreview = options?.disableWebPreview ?? false;
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
2
2
|
import { IActionWithState } from '../../types/actionWithState';
|
|
3
3
|
import { IActionState } from '../../types/actionState';
|
|
4
|
+
import { ChatInfo } from '../chatInfo';
|
|
4
5
|
|
|
5
6
|
export class UnpinResponse implements IChatResponse {
|
|
6
|
-
kind = BotResponseTypes.unpin;
|
|
7
|
+
readonly kind = BotResponseTypes.unpin;
|
|
7
8
|
|
|
8
|
-
messageId: number;
|
|
9
|
-
|
|
10
|
-
traceId: number | string;
|
|
11
|
-
action: IActionWithState<IActionState>;
|
|
9
|
+
readonly messageId: number;
|
|
10
|
+
readonly chatInfo: ChatInfo;
|
|
11
|
+
readonly traceId: number | string;
|
|
12
|
+
readonly action: IActionWithState<IActionState>;
|
|
12
13
|
|
|
13
14
|
constructor(
|
|
14
15
|
messageId: number,
|
|
15
|
-
|
|
16
|
+
chatInfo: ChatInfo,
|
|
16
17
|
traceId: number | string,
|
|
17
18
|
action: IActionWithState<IActionState>
|
|
18
19
|
) {
|
|
19
20
|
this.messageId = messageId;
|
|
20
|
-
this.
|
|
21
|
+
this.chatInfo = chatInfo;
|
|
21
22
|
this.traceId = traceId;
|
|
22
23
|
this.action = action;
|
|
23
24
|
}
|
|
@@ -3,28 +3,29 @@ import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
|
3
3
|
import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
5
|
import { IActionState } from '../../types/actionState';
|
|
6
|
+
import { ChatInfo } from '../chatInfo';
|
|
6
7
|
|
|
7
8
|
export class VideoMessage implements IReplyMessage<InputFile> {
|
|
8
|
-
kind = BotResponseTypes.video;
|
|
9
|
+
readonly kind = BotResponseTypes.video;
|
|
9
10
|
|
|
10
|
-
content: InputFile;
|
|
11
|
-
|
|
12
|
-
replyId: number | undefined;
|
|
13
|
-
traceId: string | number;
|
|
14
|
-
disableWebPreview = false;
|
|
15
|
-
shouldPin: boolean;
|
|
16
|
-
action: IActionWithState<IActionState>;
|
|
11
|
+
readonly content: InputFile;
|
|
12
|
+
readonly chatInfo: ChatInfo;
|
|
13
|
+
readonly replyId: number | undefined;
|
|
14
|
+
readonly traceId: string | number;
|
|
15
|
+
readonly disableWebPreview = false;
|
|
16
|
+
readonly shouldPin: boolean;
|
|
17
|
+
readonly action: IActionWithState<IActionState>;
|
|
17
18
|
|
|
18
19
|
constructor(
|
|
19
20
|
video: InputFile,
|
|
20
|
-
|
|
21
|
+
chatInfo: ChatInfo,
|
|
21
22
|
replyId: number | undefined,
|
|
22
23
|
traceId: number | string,
|
|
23
24
|
action: IActionWithState<IActionState>,
|
|
24
25
|
options?: MessageSendingOptions
|
|
25
26
|
) {
|
|
26
27
|
this.content = video;
|
|
27
|
-
this.
|
|
28
|
+
this.chatInfo = chatInfo;
|
|
28
29
|
this.replyId = replyId;
|
|
29
30
|
this.traceId = traceId;
|
|
30
31
|
this.shouldPin = options?.pin ?? false;
|
|
@@ -6,25 +6,25 @@ import { secondsToMilliseconds } from '../../helpers/timeConvertions';
|
|
|
6
6
|
import { toArray } from '../../helpers/toArray';
|
|
7
7
|
import { IActionState } from '../../types/actionState';
|
|
8
8
|
import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
9
|
-
import { CommandTriggerCheckResult } from '
|
|
9
|
+
import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
|
|
10
10
|
import { MessageContext } from '../context/messageContext';
|
|
11
11
|
import { Logger } from '../../services/logger';
|
|
12
|
-
import { ActionExecutionResult } from '
|
|
12
|
+
import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
|
|
13
13
|
import { CommandTrigger } from '../../types/commandTrigger';
|
|
14
14
|
|
|
15
15
|
export class CommandAction<TActionState extends IActionState>
|
|
16
16
|
implements IActionWithState<TActionState>
|
|
17
17
|
{
|
|
18
|
-
triggers: CommandTrigger[];
|
|
19
|
-
handler: CommandHandler<TActionState>;
|
|
20
|
-
name: string;
|
|
21
|
-
cooldownInSeconds: Seconds;
|
|
22
|
-
active: boolean;
|
|
23
|
-
chatsBlacklist: number[];
|
|
24
|
-
allowedUsers: number[];
|
|
25
|
-
condition: CommandCondition<TActionState>;
|
|
26
|
-
stateConstructor: () => TActionState;
|
|
27
|
-
key: ActionKey;
|
|
18
|
+
readonly triggers: CommandTrigger[];
|
|
19
|
+
readonly handler: CommandHandler<TActionState>;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly cooldownInSeconds: Seconds;
|
|
22
|
+
readonly active: boolean;
|
|
23
|
+
readonly chatsBlacklist: number[];
|
|
24
|
+
readonly allowedUsers: number[];
|
|
25
|
+
readonly condition: CommandCondition<TActionState>;
|
|
26
|
+
readonly stateConstructor: () => TActionState;
|
|
27
|
+
readonly key: ActionKey;
|
|
28
28
|
|
|
29
29
|
constructor(
|
|
30
30
|
trigger: CommandTrigger | CommandTrigger[],
|
|
@@ -56,7 +56,8 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
56
56
|
`Context for ${this.key} is not initialized or already consumed`
|
|
57
57
|
);
|
|
58
58
|
|
|
59
|
-
if (!this.active || this.chatsBlacklist.includes(ctx.
|
|
59
|
+
if (!this.active || this.chatsBlacklist.includes(ctx.chatInfo.id))
|
|
60
|
+
return [];
|
|
60
61
|
|
|
61
62
|
const isConditionMet = await this.condition(ctx);
|
|
62
63
|
|
|
@@ -64,7 +65,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
64
65
|
|
|
65
66
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
66
67
|
this,
|
|
67
|
-
ctx.
|
|
68
|
+
ctx.chatInfo.id
|
|
68
69
|
);
|
|
69
70
|
|
|
70
71
|
const { shouldTrigger, matchResults, skipCooldown } = this.triggers
|
|
@@ -79,8 +80,8 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
79
80
|
Logger.logWithTraceId(
|
|
80
81
|
ctx.botName,
|
|
81
82
|
ctx.traceId,
|
|
82
|
-
ctx.
|
|
83
|
-
` - Executing [${this.name}] in ${ctx.
|
|
83
|
+
ctx.chatInfo.name,
|
|
84
|
+
` - Executing [${this.name}] in ${ctx.chatInfo.id}`
|
|
84
85
|
);
|
|
85
86
|
ctx.matchResults = matchResults;
|
|
86
87
|
|
|
@@ -98,7 +99,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
98
99
|
|
|
99
100
|
await ctx.storage.saveActionExecutionResult(
|
|
100
101
|
this,
|
|
101
|
-
ctx.
|
|
102
|
+
ctx.chatInfo.id,
|
|
102
103
|
new ActionExecutionResult(state, ctx.startCooldown && shouldTrigger)
|
|
103
104
|
);
|
|
104
105
|
|
|
@@ -7,25 +7,25 @@ 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 { ActionExecutionResult } from '
|
|
10
|
+
import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
|
|
11
11
|
import { Logger } from '../../services/logger';
|
|
12
12
|
import { Scheduler } from '../../services/taskScheduler';
|
|
13
13
|
|
|
14
14
|
export class ScheduledAction<TActionState extends IActionState>
|
|
15
15
|
implements IActionWithState<TActionState>
|
|
16
16
|
{
|
|
17
|
-
static locks = new Map<string, Semaphore>();
|
|
17
|
+
static readonly locks = new Map<string, Semaphore>();
|
|
18
18
|
|
|
19
|
-
name: string;
|
|
20
|
-
timeinHours: HoursOfDay;
|
|
21
|
-
active: boolean;
|
|
22
|
-
chatsWhitelist: number[];
|
|
23
|
-
key: ActionKey;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly timeinHours: HoursOfDay;
|
|
21
|
+
readonly active: boolean;
|
|
22
|
+
readonly chatsWhitelist: number[];
|
|
23
|
+
readonly key: ActionKey;
|
|
24
24
|
|
|
25
|
-
cachedState = new Map<string, unknown>();
|
|
26
|
-
stateConstructor: () => TActionState;
|
|
27
|
-
cachedStateFactories: Map<string, CachedStateFactory>;
|
|
28
|
-
handler: ScheduledHandler<TActionState>;
|
|
25
|
+
readonly cachedState = new Map<string, unknown>();
|
|
26
|
+
readonly stateConstructor: () => TActionState;
|
|
27
|
+
readonly cachedStateFactories: Map<string, CachedStateFactory>;
|
|
28
|
+
readonly handler: ScheduledHandler<TActionState>;
|
|
29
29
|
|
|
30
30
|
constructor(
|
|
31
31
|
name: string,
|
|
@@ -52,12 +52,12 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
52
52
|
`Context for ${this.key} is not initialized or already consumed`
|
|
53
53
|
);
|
|
54
54
|
|
|
55
|
-
if (!this.active || !this.chatsWhitelist.includes(ctx.
|
|
55
|
+
if (!this.active || !this.chatsWhitelist.includes(ctx.chatInfo.id))
|
|
56
56
|
return [];
|
|
57
57
|
|
|
58
58
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
59
59
|
this,
|
|
60
|
-
ctx.
|
|
60
|
+
ctx.chatInfo.id
|
|
61
61
|
);
|
|
62
62
|
const isAllowedToTrigger = this.shouldTrigger(state);
|
|
63
63
|
|
|
@@ -65,8 +65,8 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
65
65
|
Logger.logWithTraceId(
|
|
66
66
|
ctx.botName,
|
|
67
67
|
ctx.traceId,
|
|
68
|
-
ctx.
|
|
69
|
-
` - Executing [${this.name}] in ${ctx.
|
|
68
|
+
ctx.chatInfo.name,
|
|
69
|
+
` - Executing [${this.name}] in ${ctx.chatInfo.id}`
|
|
70
70
|
);
|
|
71
71
|
|
|
72
72
|
await this.handler(
|
|
@@ -81,7 +81,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
81
81
|
ctx.updateActions.forEach((action) => action(state));
|
|
82
82
|
await ctx.storage.saveActionExecutionResult(
|
|
83
83
|
this,
|
|
84
|
-
ctx.
|
|
84
|
+
ctx.chatInfo.id,
|
|
85
85
|
new ActionExecutionResult(state, isAllowedToTrigger)
|
|
86
86
|
);
|
|
87
87
|
}
|
package/entities/botInstance.ts
CHANGED
|
@@ -12,19 +12,20 @@ import { CommandAction } from './actions/commandAction';
|
|
|
12
12
|
import { ScheduledAction } from './actions/scheduledAction';
|
|
13
13
|
import { Logger } from '../services/logger';
|
|
14
14
|
import { Scheduler } from '../services/taskScheduler';
|
|
15
|
-
import { IncomingMessage } from '
|
|
15
|
+
import { IncomingMessage } from '../dtos/incomingMessage';
|
|
16
16
|
import moment from 'moment';
|
|
17
17
|
import { ChatContext } from './context/chatContext';
|
|
18
18
|
import { MessageContext } from './context/messageContext';
|
|
19
|
+
import { ChatInfo } from '../dtos/chatInfo';
|
|
19
20
|
|
|
20
21
|
export class BotInstance {
|
|
21
|
-
name: string;
|
|
22
|
-
private api: TelegramApiService;
|
|
23
|
-
private telegraf: Telegraf;
|
|
24
|
-
private commands: CommandAction<IActionState>[];
|
|
25
|
-
private scheduled: ScheduledAction<IActionState>[];
|
|
26
|
-
private chats: Record<string, number>;
|
|
27
|
-
storage: IStorageClient;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
private readonly api: TelegramApiService;
|
|
24
|
+
private readonly telegraf: Telegraf;
|
|
25
|
+
private readonly commands: CommandAction<IActionState>[];
|
|
26
|
+
private readonly scheduled: ScheduledAction<IActionState>[];
|
|
27
|
+
private readonly chats: Record<string, number>;
|
|
28
|
+
readonly storage: IStorageClient;
|
|
28
29
|
|
|
29
30
|
constructor(options: {
|
|
30
31
|
name: string;
|
|
@@ -57,8 +58,7 @@ export class BotInstance {
|
|
|
57
58
|
this.api = new TelegramApiService(
|
|
58
59
|
this.name,
|
|
59
60
|
this.telegraf.telegram,
|
|
60
|
-
this.storage
|
|
61
|
-
this.chats
|
|
61
|
+
this.storage
|
|
62
62
|
);
|
|
63
63
|
|
|
64
64
|
this.initializeMessageProcessing(
|
|
@@ -132,14 +132,14 @@ export class BotInstance {
|
|
|
132
132
|
Logger.logObjectWithTraceId(
|
|
133
133
|
this.name,
|
|
134
134
|
msg.traceId,
|
|
135
|
-
msg.
|
|
135
|
+
msg.chatInfo.name,
|
|
136
136
|
ctx.update.message
|
|
137
137
|
);
|
|
138
138
|
} else {
|
|
139
139
|
Logger.logWithTraceId(
|
|
140
140
|
this.name,
|
|
141
141
|
msg.traceId,
|
|
142
|
-
msg.
|
|
142
|
+
msg.chatInfo.name,
|
|
143
143
|
`${messageFromName} (${messageFromId}): ${messageContent}`
|
|
144
144
|
);
|
|
145
145
|
}
|
|
@@ -166,7 +166,13 @@ export class BotInstance {
|
|
|
166
166
|
|
|
167
167
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
168
168
|
for (const scheduledAction of this.scheduled) {
|
|
169
|
-
|
|
169
|
+
ctx.initializeChatContext(
|
|
170
|
+
this.name,
|
|
171
|
+
scheduledAction,
|
|
172
|
+
new ChatInfo(chatId, chatName),
|
|
173
|
+
`Scheduled:${scheduledAction.key}:${chatId}`,
|
|
174
|
+
this.storage
|
|
175
|
+
);
|
|
170
176
|
|
|
171
177
|
try {
|
|
172
178
|
const responses = await scheduledAction.exec(ctx);
|
|
@@ -190,7 +196,12 @@ export class BotInstance {
|
|
|
190
196
|
const ctx = new MessageContext<IActionState>();
|
|
191
197
|
|
|
192
198
|
for (const commandAction of this.commands) {
|
|
193
|
-
|
|
199
|
+
ctx.initializeMessageContext(
|
|
200
|
+
this.name,
|
|
201
|
+
commandAction,
|
|
202
|
+
msg,
|
|
203
|
+
this.storage
|
|
204
|
+
);
|
|
194
205
|
|
|
195
206
|
try {
|
|
196
207
|
const responses = await commandAction.exec(ctx);
|
|
@@ -199,7 +210,7 @@ export class BotInstance {
|
|
|
199
210
|
Logger.errorWithTraceId(
|
|
200
211
|
ctx.botName,
|
|
201
212
|
ctx.traceId,
|
|
202
|
-
ctx.
|
|
213
|
+
ctx.chatInfo.name,
|
|
203
214
|
error,
|
|
204
215
|
ctx
|
|
205
216
|
);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Hours } from '../types/timeValues';
|
|
2
2
|
|
|
3
3
|
export class CachedStateFactory {
|
|
4
|
-
getValue: () => Promise<unknown>;
|
|
5
|
-
invalidationTimeoutInHours: Hours;
|
|
4
|
+
readonly getValue: () => Promise<unknown>;
|
|
5
|
+
readonly invalidationTimeoutInHours: Hours;
|
|
6
6
|
|
|
7
7
|
constructor(
|
|
8
8
|
itemFactory: () => Promise<unknown>,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { resolve } from 'path';
|
|
2
2
|
import { IStorageClient } from '../../types/storage';
|
|
3
|
-
import { ImageMessage } from '
|
|
4
|
-
import { TextMessage } from '
|
|
5
|
-
import { VideoMessage } from '
|
|
6
|
-
import { UnpinResponse } from '
|
|
3
|
+
import { ImageMessage } from '../../dtos/responses/imageMessage';
|
|
4
|
+
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
5
|
+
import { VideoMessage } from '../../dtos/responses/videoMessage';
|
|
6
|
+
import { UnpinResponse } from '../../dtos/responses/unpin';
|
|
7
7
|
import {
|
|
8
8
|
MessageSendingOptions,
|
|
9
9
|
TextMessageSendingOptions
|
|
@@ -12,7 +12,8 @@ import { IActionWithState } from '../../types/actionWithState';
|
|
|
12
12
|
import { IActionState } from '../../types/actionState';
|
|
13
13
|
import { BotResponse } from '../../types/response';
|
|
14
14
|
import { Milliseconds } from '../../types/timeValues';
|
|
15
|
-
import { DelayResponse } from '
|
|
15
|
+
import { DelayResponse } from '../../dtos/responses/delay';
|
|
16
|
+
import { ChatInfo } from '../../dtos/chatInfo';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Context of action executed in chat.
|
|
@@ -24,10 +25,8 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
24
25
|
traceId!: number | string;
|
|
25
26
|
/** Name of a bot that executes this action. */
|
|
26
27
|
botName!: string;
|
|
27
|
-
/**
|
|
28
|
-
|
|
29
|
-
/** Name of a chat that action is executed in. */
|
|
30
|
-
chatName!: string;
|
|
28
|
+
/** Chat information. */
|
|
29
|
+
chatInfo!: ChatInfo;
|
|
31
30
|
/** Storage client instance for this bot. */
|
|
32
31
|
storage!: IStorageClient;
|
|
33
32
|
/** Ordered collection of responses to be processed */
|
|
@@ -40,15 +39,13 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
40
39
|
initializeChatContext(
|
|
41
40
|
botName: string,
|
|
42
41
|
action: IActionWithState<TActionState>,
|
|
43
|
-
|
|
44
|
-
chatName: string,
|
|
42
|
+
chatInfo: ChatInfo,
|
|
45
43
|
traceId: number | string,
|
|
46
44
|
storage: IStorageClient
|
|
47
45
|
) {
|
|
48
46
|
this.botName = botName;
|
|
49
47
|
this.action = action;
|
|
50
|
-
this.
|
|
51
|
-
this.chatName = chatName;
|
|
48
|
+
this.chatInfo = chatInfo;
|
|
52
49
|
this.traceId = traceId;
|
|
53
50
|
this.storage = storage;
|
|
54
51
|
|
|
@@ -77,7 +74,7 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
77
74
|
this.responses.push(
|
|
78
75
|
new TextMessage(
|
|
79
76
|
text,
|
|
80
|
-
this.
|
|
77
|
+
this.chatInfo,
|
|
81
78
|
undefined,
|
|
82
79
|
this.traceId,
|
|
83
80
|
this.action,
|
|
@@ -97,7 +94,7 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
97
94
|
this.responses.push(
|
|
98
95
|
new ImageMessage(
|
|
99
96
|
{ source: resolve(filePath) },
|
|
100
|
-
this.
|
|
97
|
+
this.chatInfo,
|
|
101
98
|
undefined,
|
|
102
99
|
this.traceId,
|
|
103
100
|
this.action,
|
|
@@ -117,7 +114,7 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
117
114
|
this.responses.push(
|
|
118
115
|
new VideoMessage(
|
|
119
116
|
{ source: resolve(filePath) },
|
|
120
|
-
this.
|
|
117
|
+
this.chatInfo,
|
|
121
118
|
undefined,
|
|
122
119
|
this.traceId,
|
|
123
120
|
this.action,
|
|
@@ -133,7 +130,12 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
133
130
|
*/
|
|
134
131
|
unpinMessage(messageId: number) {
|
|
135
132
|
this.responses.push(
|
|
136
|
-
new UnpinResponse(
|
|
133
|
+
new UnpinResponse(
|
|
134
|
+
messageId,
|
|
135
|
+
this.chatInfo,
|
|
136
|
+
this.traceId,
|
|
137
|
+
this.action
|
|
138
|
+
)
|
|
137
139
|
);
|
|
138
140
|
}
|
|
139
141
|
|
|
@@ -143,7 +145,7 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
143
145
|
*/
|
|
144
146
|
delayNextResponse(delay: Milliseconds) {
|
|
145
147
|
this.responses.push(
|
|
146
|
-
new DelayResponse(delay, this.
|
|
148
|
+
new DelayResponse(delay, this.chatInfo, this.traceId, this.action)
|
|
147
149
|
);
|
|
148
150
|
}
|
|
149
151
|
}
|
|
@@ -2,13 +2,13 @@ import { resolve } from 'path';
|
|
|
2
2
|
import { TelegramEmoji } from 'telegraf/types';
|
|
3
3
|
import { IStorageClient } from '../../types/storage';
|
|
4
4
|
import { IActionState } from '../../types/actionState';
|
|
5
|
-
import { ImageMessage } from '
|
|
6
|
-
import { Reaction } from '
|
|
7
|
-
import { TextMessage } from '
|
|
8
|
-
import { VideoMessage } from '
|
|
5
|
+
import { ImageMessage } from '../../dtos/responses/imageMessage';
|
|
6
|
+
import { Reaction } from '../../dtos/responses/reaction';
|
|
7
|
+
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
8
|
+
import { VideoMessage } from '../../dtos/responses/videoMessage';
|
|
9
9
|
import { ActionStateBase } from '../states/actionStateBase';
|
|
10
10
|
import { ChatContext } from './chatContext';
|
|
11
|
-
import { IncomingMessage } from '
|
|
11
|
+
import { IncomingMessage } from '../../dtos/incomingMessage';
|
|
12
12
|
import {
|
|
13
13
|
MessageSendingOptions,
|
|
14
14
|
TextMessageSendingOptions
|
|
@@ -60,8 +60,7 @@ export class MessageContext<
|
|
|
60
60
|
return this.initializeChatContext(
|
|
61
61
|
botName,
|
|
62
62
|
action,
|
|
63
|
-
message.
|
|
64
|
-
message.chatName,
|
|
63
|
+
message.chatInfo,
|
|
65
64
|
message.traceId,
|
|
66
65
|
storage
|
|
67
66
|
);
|
|
@@ -80,7 +79,7 @@ export class MessageContext<
|
|
|
80
79
|
'-'
|
|
81
80
|
)}` as ActionKey;
|
|
82
81
|
const allStates = await this.storage.load(storageKey);
|
|
83
|
-
const stateForChat = allStates[this.
|
|
82
|
+
const stateForChat = allStates[this.chatInfo.id];
|
|
84
83
|
|
|
85
84
|
if (!stateForChat) {
|
|
86
85
|
return new ActionStateBase() as TAnotherActionState;
|
|
@@ -99,7 +98,7 @@ export class MessageContext<
|
|
|
99
98
|
this.responses.push(
|
|
100
99
|
new TextMessage(
|
|
101
100
|
text,
|
|
102
|
-
this.
|
|
101
|
+
this.chatInfo,
|
|
103
102
|
this.messageId,
|
|
104
103
|
this.traceId,
|
|
105
104
|
this.action,
|
|
@@ -119,7 +118,7 @@ export class MessageContext<
|
|
|
119
118
|
this.responses.push(
|
|
120
119
|
new ImageMessage(
|
|
121
120
|
{ source: resolve(filePath) },
|
|
122
|
-
this.
|
|
121
|
+
this.chatInfo,
|
|
123
122
|
this.messageId,
|
|
124
123
|
this.traceId,
|
|
125
124
|
this.action,
|
|
@@ -139,7 +138,7 @@ export class MessageContext<
|
|
|
139
138
|
this.responses.push(
|
|
140
139
|
new VideoMessage(
|
|
141
140
|
{ source: resolve(filePath) },
|
|
142
|
-
this.
|
|
141
|
+
this.chatInfo,
|
|
143
142
|
this.messageId,
|
|
144
143
|
this.traceId,
|
|
145
144
|
this.action,
|
|
@@ -157,7 +156,7 @@ export class MessageContext<
|
|
|
157
156
|
this.responses.push(
|
|
158
157
|
new Reaction(
|
|
159
158
|
this.traceId,
|
|
160
|
-
this.
|
|
159
|
+
this.chatInfo,
|
|
161
160
|
this.messageId,
|
|
162
161
|
emoji,
|
|
163
162
|
this.action
|
package/entities/taskRecord.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Milliseconds } from '../types/timeValues';
|
|
2
2
|
|
|
3
3
|
export class TaskRecord {
|
|
4
|
-
name: string;
|
|
5
|
-
taskId: NodeJS.Timeout;
|
|
6
|
-
interval: Milliseconds;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly taskId: NodeJS.Timeout;
|
|
6
|
+
readonly interval: Milliseconds;
|
|
7
7
|
|
|
8
8
|
constructor(name: string, taskId: NodeJS.Timeout, interval: Milliseconds) {
|
|
9
9
|
this.name = name;
|