chz-telegram-bot 0.0.43 → 0.0.45
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/entities/actions/commandAction.ts +2 -0
- package/entities/actions/scheduledAction.ts +14 -3
- package/entities/botInstance.ts +8 -5
- package/entities/context/chatContext.ts +17 -8
- package/entities/context/messageContext.ts +21 -14
- package/entities/incomingMessage.ts +1 -0
- package/package.json +29 -29
- package/services/telegramApi.ts +6 -4
- package/dist/entities/actionExecutionResult.d.ts +0 -7
- package/dist/entities/actionExecutionResult.d.ts.map +0 -1
- package/dist/entities/actionExecutionResult.js +0 -10
- package/dist/entities/actions/commandAction.d.ts +0 -23
- package/dist/entities/actions/commandAction.d.ts.map +0 -1
- package/dist/entities/actions/commandAction.js +0 -89
- package/dist/entities/actions/scheduledAction.d.ts +0 -24
- package/dist/entities/actions/scheduledAction.d.ts.map +0 -1
- package/dist/entities/actions/scheduledAction.js +0 -70
- package/dist/entities/botInstance.d.ts +0 -31
- package/dist/entities/botInstance.d.ts.map +0 -1
- package/dist/entities/botInstance.js +0 -103
- package/dist/entities/cachedStateFactory.d.ts +0 -7
- package/dist/entities/cachedStateFactory.d.ts.map +0 -1
- package/dist/entities/cachedStateFactory.js +0 -10
- package/dist/entities/commandTriggerCheckResult.d.ts +0 -10
- package/dist/entities/commandTriggerCheckResult.d.ts.map +0 -1
- package/dist/entities/commandTriggerCheckResult.js +0 -23
- package/dist/entities/context/chatContext.d.ts +0 -52
- package/dist/entities/context/chatContext.d.ts.map +0 -1
- package/dist/entities/context/chatContext.js +0 -64
- package/dist/entities/context/messageContext.d.ts +0 -59
- package/dist/entities/context/messageContext.d.ts.map +0 -1
- package/dist/entities/context/messageContext.js +0 -77
- package/dist/entities/incomingMessage.d.ts +0 -14
- package/dist/entities/incomingMessage.d.ts.map +0 -1
- package/dist/entities/incomingMessage.js +0 -43
- package/dist/entities/responses/imageMessage.d.ts +0 -16
- package/dist/entities/responses/imageMessage.d.ts.map +0 -1
- package/dist/entities/responses/imageMessage.js +0 -17
- package/dist/entities/responses/reaction.d.ts +0 -13
- package/dist/entities/responses/reaction.d.ts.map +0 -1
- package/dist/entities/responses/reaction.js +0 -15
- package/dist/entities/responses/textMessage.d.ts +0 -15
- package/dist/entities/responses/textMessage.d.ts.map +0 -1
- package/dist/entities/responses/textMessage.js +0 -17
- package/dist/entities/responses/unpin.d.ts +0 -11
- package/dist/entities/responses/unpin.d.ts.map +0 -1
- package/dist/entities/responses/unpin.js +0 -14
- package/dist/entities/responses/videoMessage.d.ts +0 -16
- package/dist/entities/responses/videoMessage.d.ts.map +0 -1
- package/dist/entities/responses/videoMessage.js +0 -17
- package/dist/entities/states/actionStateBase.d.ts +0 -6
- package/dist/entities/states/actionStateBase.d.ts.map +0 -1
- package/dist/entities/states/actionStateBase.js +0 -10
- package/dist/entities/taskRecord.d.ts +0 -8
- package/dist/entities/taskRecord.d.ts.map +0 -1
- package/dist/entities/taskRecord.js +0 -11
- package/dist/helpers/builders/commandActionBuilder.d.ts +0 -69
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +0 -1
- package/dist/helpers/builders/commandActionBuilder.js +0 -96
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +0 -60
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +0 -1
- package/dist/helpers/builders/scheduledActionBuilder.js +0 -82
- package/dist/helpers/inverseRecord.d.ts +0 -2
- package/dist/helpers/inverseRecord.d.ts.map +0 -1
- package/dist/helpers/inverseRecord.js +0 -6
- package/dist/helpers/noop.d.ts +0 -6
- package/dist/helpers/noop.d.ts.map +0 -1
- package/dist/helpers/noop.js +0 -14
- package/dist/helpers/reverseMap.d.ts +0 -2
- package/dist/helpers/reverseMap.d.ts.map +0 -1
- package/dist/helpers/reverseMap.js +0 -6
- package/dist/helpers/reverseRecord.d.ts +0 -2
- package/dist/helpers/reverseRecord.d.ts.map +0 -1
- package/dist/helpers/reverseRecord.js +0 -6
- package/dist/helpers/timeConvertions.d.ts +0 -5
- package/dist/helpers/timeConvertions.d.ts.map +0 -1
- package/dist/helpers/timeConvertions.js +0 -14
- package/dist/helpers/toArray.d.ts +0 -2
- package/dist/helpers/toArray.d.ts.map +0 -1
- package/dist/helpers/toArray.js +0 -6
- package/dist/index.d.ts +0 -10
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -29
- package/dist/main.d.ts +0 -35
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -43
- package/dist/services/jsonFileStorage.d.ts +0 -22
- package/dist/services/jsonFileStorage.d.ts.map +0 -1
- package/dist/services/jsonFileStorage.js +0 -102
- package/dist/services/logger.d.ts +0 -9
- package/dist/services/logger.d.ts.map +0 -1
- package/dist/services/logger.js +0 -28
- package/dist/services/taskScheduler.d.ts +0 -11
- package/dist/services/taskScheduler.d.ts.map +0 -1
- package/dist/services/taskScheduler.js +0 -33
- package/dist/services/telegramApi.d.ts +0 -36
- package/dist/services/telegramApi.d.ts.map +0 -1
- package/dist/services/telegramApi.js +0 -102
- package/dist/types/actionState.d.ts +0 -5
- package/dist/types/actionState.d.ts.map +0 -1
- package/dist/types/actionState.js +0 -2
- package/dist/types/actionWithState.d.ts +0 -9
- package/dist/types/actionWithState.d.ts.map +0 -1
- package/dist/types/actionWithState.js +0 -2
- package/dist/types/cachedValueAccessor.d.ts +0 -2
- package/dist/types/cachedValueAccessor.d.ts.map +0 -1
- package/dist/types/cachedValueAccessor.js +0 -2
- package/dist/types/commandCondition.d.ts +0 -4
- package/dist/types/commandCondition.d.ts.map +0 -1
- package/dist/types/commandCondition.js +0 -2
- package/dist/types/commandTrigger.d.ts +0 -3
- package/dist/types/commandTrigger.d.ts.map +0 -1
- package/dist/types/commandTrigger.js +0 -2
- package/dist/types/daysOfTheWeek.d.ts +0 -10
- package/dist/types/daysOfTheWeek.d.ts.map +0 -1
- package/dist/types/daysOfTheWeek.js +0 -13
- package/dist/types/handlers.d.ts +0 -17
- package/dist/types/handlers.d.ts.map +0 -1
- package/dist/types/handlers.js +0 -2
- package/dist/types/messageSendingOptions.d.ts +0 -7
- package/dist/types/messageSendingOptions.d.ts.map +0 -1
- package/dist/types/messageSendingOptions.js +0 -2
- package/dist/types/messageTypes.d.ts +0 -16
- package/dist/types/messageTypes.d.ts.map +0 -1
- package/dist/types/messageTypes.js +0 -17
- package/dist/types/replyMessage.d.ts +0 -8
- package/dist/types/replyMessage.d.ts.map +0 -1
- package/dist/types/replyMessage.js +0 -2
- package/dist/types/response.d.ts +0 -27
- package/dist/types/response.d.ts.map +0 -1
- package/dist/types/response.js +0 -10
- package/dist/types/storage.d.ts +0 -12
- package/dist/types/storage.d.ts.map +0 -1
- package/dist/types/storage.js +0 -2
- package/dist/types/timeValues.d.ts +0 -11
- package/dist/types/timeValues.d.ts.map +0 -1
- package/dist/types/timeValues.js +0 -2
|
@@ -51,6 +51,8 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async exec(ctx: MessageContext<TActionState>) {
|
|
54
|
+
if (!ctx.isInitialized) throw new Error('Context is not initialized');
|
|
55
|
+
|
|
54
56
|
if (!this.active || this.chatsBlacklist.includes(ctx.chatId)) return;
|
|
55
57
|
|
|
56
58
|
const isConditionMet = await this.condition(ctx);
|
|
@@ -14,7 +14,7 @@ import { Scheduler } from '../../services/taskScheduler';
|
|
|
14
14
|
export class ScheduledAction<TActionState extends IActionState>
|
|
15
15
|
implements IActionWithState
|
|
16
16
|
{
|
|
17
|
-
static
|
|
17
|
+
static locks = new Map<string, Semaphore>();
|
|
18
18
|
|
|
19
19
|
name: string;
|
|
20
20
|
timeinHours: HoursOfDay;
|
|
@@ -47,6 +47,8 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
async exec(ctx: ChatContext<TActionState>) {
|
|
50
|
+
if (!ctx.isInitialized) throw new Error('Context is not initialized');
|
|
51
|
+
|
|
50
52
|
if (!this.active || !this.chatsWhitelist.includes(ctx.chatId)) return;
|
|
51
53
|
|
|
52
54
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
@@ -91,7 +93,16 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
91
93
|
);
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
96
|
+
const semaphoreKey = `${this.key}_cached:${key}`;
|
|
97
|
+
let semaphore: Semaphore;
|
|
98
|
+
if (ScheduledAction.locks.has(semaphoreKey)) {
|
|
99
|
+
semaphore = ScheduledAction.locks.get(semaphoreKey)!;
|
|
100
|
+
} else {
|
|
101
|
+
semaphore = new Semaphore(1);
|
|
102
|
+
ScheduledAction.locks.set(semaphoreKey, semaphore);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await semaphore.acquire();
|
|
95
106
|
|
|
96
107
|
try {
|
|
97
108
|
if (this.cachedState.has(key)) {
|
|
@@ -114,7 +125,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
114
125
|
|
|
115
126
|
return value as TResult;
|
|
116
127
|
} finally {
|
|
117
|
-
|
|
128
|
+
semaphore.release();
|
|
118
129
|
}
|
|
119
130
|
}
|
|
120
131
|
|
package/entities/botInstance.ts
CHANGED
|
@@ -14,6 +14,8 @@ import { Logger } from '../services/logger';
|
|
|
14
14
|
import { Scheduler } from '../services/taskScheduler';
|
|
15
15
|
import { IncomingMessage } from './incomingMessage';
|
|
16
16
|
import moment from 'moment';
|
|
17
|
+
import { ChatContext } from './context/chatContext';
|
|
18
|
+
import { MessageContext } from './context/messageContext';
|
|
17
19
|
|
|
18
20
|
export class BotInstance {
|
|
19
21
|
name: string;
|
|
@@ -160,12 +162,11 @@ export class BotInstance {
|
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
private async runScheduled() {
|
|
165
|
+
const ctx = new ChatContext<IActionState>();
|
|
166
|
+
|
|
163
167
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
164
168
|
for (const scheduledAction of this.scheduled) {
|
|
165
|
-
|
|
166
|
-
chatId,
|
|
167
|
-
scheduledAction
|
|
168
|
-
);
|
|
169
|
+
this.api.initializeContextForChat(ctx, chatId, scheduledAction);
|
|
169
170
|
|
|
170
171
|
try {
|
|
171
172
|
await scheduledAction.exec(ctx);
|
|
@@ -185,8 +186,10 @@ export class BotInstance {
|
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
private async processMessage(msg: IncomingMessage) {
|
|
189
|
+
const ctx = new MessageContext<IActionState>();
|
|
190
|
+
|
|
188
191
|
for (const commandAction of this.commands) {
|
|
189
|
-
|
|
192
|
+
this.api.initializeContextForMessage(ctx, msg, commandAction);
|
|
190
193
|
|
|
191
194
|
try {
|
|
192
195
|
await commandAction.exec(ctx);
|
|
@@ -15,21 +15,25 @@ import { IActionWithState } from '../../types/actionWithState';
|
|
|
15
15
|
* Context of action executed in chat.
|
|
16
16
|
*/
|
|
17
17
|
export class ChatContext<TActionState> {
|
|
18
|
-
protected action
|
|
19
|
-
protected interactions
|
|
18
|
+
protected action!: IActionWithState;
|
|
19
|
+
protected interactions!: IBotApiInteractions;
|
|
20
20
|
updateActions: Array<(state: TActionState) => void> = [];
|
|
21
21
|
/** Trace id of a action execution. */
|
|
22
|
-
traceId
|
|
22
|
+
traceId!: number | string;
|
|
23
23
|
/** Name of a bot that executes this action. */
|
|
24
|
-
botName
|
|
24
|
+
botName!: string;
|
|
25
25
|
/** Id of a chat that action is executed in. */
|
|
26
|
-
chatId
|
|
26
|
+
chatId!: number;
|
|
27
27
|
/** Name of a chat that action is executed in. */
|
|
28
|
-
chatName
|
|
28
|
+
chatName!: string;
|
|
29
29
|
/** Storage client instance for this bot. */
|
|
30
|
-
storage
|
|
30
|
+
storage!: IStorageClient;
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
isInitialized = false;
|
|
33
|
+
|
|
34
|
+
constructor() {}
|
|
35
|
+
|
|
36
|
+
initializeChatContext(
|
|
33
37
|
botName: string,
|
|
34
38
|
action: IActionWithState,
|
|
35
39
|
interactions: IBotApiInteractions,
|
|
@@ -45,6 +49,11 @@ export class ChatContext<TActionState> {
|
|
|
45
49
|
this.chatName = chatName;
|
|
46
50
|
this.traceId = traceId;
|
|
47
51
|
this.storage = storage;
|
|
52
|
+
|
|
53
|
+
this.updateActions = [];
|
|
54
|
+
this.isInitialized = true;
|
|
55
|
+
|
|
56
|
+
return this;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
59
|
/**
|
|
@@ -24,9 +24,9 @@ export class MessageContext<
|
|
|
24
24
|
TActionState extends IActionState
|
|
25
25
|
> extends ChatContext<TActionState> {
|
|
26
26
|
/** Id of a message that triggered this action. */
|
|
27
|
-
messageId
|
|
27
|
+
messageId!: number;
|
|
28
28
|
/** Text of a message that triggered this action. */
|
|
29
|
-
messageText
|
|
29
|
+
messageText!: string;
|
|
30
30
|
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
31
31
|
matchResults: RegExpMatchArray[] = [];
|
|
32
32
|
/** Id of a user that sent a message that triggered this action. */
|
|
@@ -34,18 +34,33 @@ export class MessageContext<
|
|
|
34
34
|
/** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
|
|
35
35
|
startCooldown: boolean = true;
|
|
36
36
|
/** Name of a user that sent a message that triggered this action. */
|
|
37
|
-
fromUserName
|
|
37
|
+
fromUserName!: string;
|
|
38
38
|
/** Type of message being received */
|
|
39
|
-
messageType
|
|
39
|
+
messageType!: MessageTypeValue;
|
|
40
40
|
|
|
41
|
-
constructor(
|
|
41
|
+
constructor() {
|
|
42
|
+
super();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
initializeMessageContext(
|
|
42
46
|
botName: string,
|
|
43
47
|
action: IActionWithState,
|
|
44
48
|
interactions: IBotApiInteractions,
|
|
45
49
|
message: IncomingMessage,
|
|
46
50
|
storage: IStorageClient
|
|
47
51
|
) {
|
|
48
|
-
|
|
52
|
+
this.messageId = message.message_id;
|
|
53
|
+
this.messageText = message.text ?? '';
|
|
54
|
+
this.messageType = message.type;
|
|
55
|
+
this.fromUserId = message.from?.id;
|
|
56
|
+
this.fromUserName =
|
|
57
|
+
(message.from?.first_name ?? 'Unknown user') +
|
|
58
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
59
|
+
|
|
60
|
+
this.matchResults = [];
|
|
61
|
+
this.startCooldown = true;
|
|
62
|
+
|
|
63
|
+
return this.initializeChatContext(
|
|
49
64
|
botName,
|
|
50
65
|
action,
|
|
51
66
|
interactions,
|
|
@@ -54,14 +69,6 @@ export class MessageContext<
|
|
|
54
69
|
message.traceId,
|
|
55
70
|
storage
|
|
56
71
|
);
|
|
57
|
-
|
|
58
|
-
this.messageId = message.message_id;
|
|
59
|
-
this.messageText = message.text ?? '';
|
|
60
|
-
this.messageType = message.type;
|
|
61
|
-
this.fromUserId = message.from?.id;
|
|
62
|
-
this.fromUserName =
|
|
63
|
-
(message.from?.first_name ?? 'Unknown user') +
|
|
64
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
/**
|
|
@@ -14,6 +14,7 @@ export class IncomingMessage {
|
|
|
14
14
|
private detectMessageType(
|
|
15
15
|
message: Update.New & (Update.NonChannel & Message)
|
|
16
16
|
) {
|
|
17
|
+
if ('text' in message) return MessageType.Text;
|
|
17
18
|
if ('photo' in message) return MessageType.Photo;
|
|
18
19
|
if ('sticker' in message) return MessageType.Sticker;
|
|
19
20
|
if ('animation' in message) return MessageType.Animation;
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "chz-telegram-bot",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"dependencies": {
|
|
6
|
-
"async-sema": "^3.1.1",
|
|
7
|
-
"moment": "^2.29.4",
|
|
8
|
-
"telegraf": "^4.16.3"
|
|
9
|
-
},
|
|
10
|
-
"main": "dist/index.js",
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
12
|
-
"devDependencies": {
|
|
13
|
-
"@eslint/js": "^9.10.0",
|
|
14
|
-
"@types/markdown-escape": "^1.1.3",
|
|
15
|
-
"@types/node": "^22.5.5",
|
|
16
|
-
"eslint": "^9.10.0",
|
|
17
|
-
"typescript": "^5.6.2",
|
|
18
|
-
"typescript-eslint": "^8.5.0"
|
|
19
|
-
},
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc",
|
|
22
|
-
"lint": "npx eslint && tsc --noEmit"
|
|
23
|
-
},
|
|
24
|
-
"overrides": {
|
|
25
|
-
"telegraf": {
|
|
26
|
-
"node-fetch": "3.3.2"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "chz-telegram-bot",
|
|
3
|
+
"version": "0.0.45",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"async-sema": "^3.1.1",
|
|
7
|
+
"moment": "^2.29.4",
|
|
8
|
+
"telegraf": "^4.16.3"
|
|
9
|
+
},
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@eslint/js": "^9.10.0",
|
|
14
|
+
"@types/markdown-escape": "^1.1.3",
|
|
15
|
+
"@types/node": "^22.5.5",
|
|
16
|
+
"eslint": "^9.10.0",
|
|
17
|
+
"typescript": "^5.6.2",
|
|
18
|
+
"typescript-eslint": "^8.5.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"lint": "npx eslint && tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"overrides": {
|
|
25
|
+
"telegraf": {
|
|
26
|
+
"node-fetch": "3.3.2"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/services/telegramApi.ts
CHANGED
|
@@ -175,11 +175,12 @@ export class TelegramApiService {
|
|
|
175
175
|
this.messageQueue.push(response);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
initializeContextForMessage<TActionState extends IActionState>(
|
|
179
|
+
ctx: MessageContext<TActionState>,
|
|
179
180
|
incomingMessage: IncomingMessage,
|
|
180
181
|
command: CommandAction<TActionState>
|
|
181
182
|
) {
|
|
182
|
-
return
|
|
183
|
+
return ctx.initializeMessageContext(
|
|
183
184
|
this.botName,
|
|
184
185
|
command,
|
|
185
186
|
this.interactions,
|
|
@@ -188,11 +189,12 @@ export class TelegramApiService {
|
|
|
188
189
|
);
|
|
189
190
|
}
|
|
190
191
|
|
|
191
|
-
|
|
192
|
+
initializeContextForChat<TActionState extends IActionState>(
|
|
193
|
+
ctx: ChatContext<TActionState>,
|
|
192
194
|
chatId: number,
|
|
193
195
|
scheduledAction: ScheduledAction<TActionState>
|
|
194
196
|
) {
|
|
195
|
-
return
|
|
197
|
+
return ctx.initializeChatContext(
|
|
196
198
|
this.botName,
|
|
197
199
|
scheduledAction,
|
|
198
200
|
this.interactions,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"actionExecutionResult.d.ts","sourceRoot":"","sources":["../../entities/actionExecutionResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,qBAAa,qBAAqB;IAC9B,IAAI,EAAE,YAAY,CAAC;IACnB,YAAY,EAAE,OAAO,CAAC;gBAEV,IAAI,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO;CAIxD"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ActionExecutionResult = void 0;
|
|
4
|
-
class ActionExecutionResult {
|
|
5
|
-
constructor(data, shouldUpdate) {
|
|
6
|
-
this.data = data;
|
|
7
|
-
this.shouldUpdate = shouldUpdate;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.ActionExecutionResult = ActionExecutionResult;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { CommandHandler } from '../../types/handlers';
|
|
2
|
-
import { CommandCondition } from '../../types/commandCondition';
|
|
3
|
-
import { Seconds } from '../../types/timeValues';
|
|
4
|
-
import { IActionState } from '../../types/actionState';
|
|
5
|
-
import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
6
|
-
import { MessageContext } from '../context/messageContext';
|
|
7
|
-
import { CommandTrigger } from '../../types/commandTrigger';
|
|
8
|
-
export declare class CommandAction<TActionState extends IActionState> implements IActionWithState {
|
|
9
|
-
triggers: CommandTrigger[];
|
|
10
|
-
handler: CommandHandler<TActionState>;
|
|
11
|
-
name: string;
|
|
12
|
-
cooldownInSeconds: Seconds;
|
|
13
|
-
active: boolean;
|
|
14
|
-
chatsBlacklist: number[];
|
|
15
|
-
allowedUsers: number[];
|
|
16
|
-
condition: CommandCondition<TActionState>;
|
|
17
|
-
stateConstructor: () => TActionState;
|
|
18
|
-
key: ActionKey;
|
|
19
|
-
constructor(trigger: CommandTrigger | CommandTrigger[], handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: number[], allowedUsers: number[], condition: CommandCondition<TActionState>, stateConstructor: () => TActionState);
|
|
20
|
-
exec(ctx: MessageContext<TActionState>): Promise<void>;
|
|
21
|
-
private checkTrigger;
|
|
22
|
-
}
|
|
23
|
-
//# sourceMappingURL=commandAction.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAG3D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAE5D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB;IAE3B,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,EAAE,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,GAAG,EAAE,SAAS,CAAC;gBAGX,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,EACjB,cAAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY;IAelC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAgD5C,OAAO,CAAC,YAAY;CAwDvB"}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CommandAction = void 0;
|
|
7
|
-
const moment_1 = __importDefault(require("moment"));
|
|
8
|
-
const timeConvertions_1 = require("../../helpers/timeConvertions");
|
|
9
|
-
const toArray_1 = require("../../helpers/toArray");
|
|
10
|
-
const commandTriggerCheckResult_1 = require("../commandTriggerCheckResult");
|
|
11
|
-
const logger_1 = require("../../services/logger");
|
|
12
|
-
const actionExecutionResult_1 = require("../actionExecutionResult");
|
|
13
|
-
class CommandAction {
|
|
14
|
-
constructor(trigger, handler, name, active, cooldown, chatsBlacklist, allowedUsers, condition, stateConstructor) {
|
|
15
|
-
this.triggers = (0, toArray_1.toArray)(trigger);
|
|
16
|
-
this.handler = handler;
|
|
17
|
-
this.name = name;
|
|
18
|
-
this.cooldownInSeconds = cooldown;
|
|
19
|
-
this.active = active;
|
|
20
|
-
this.chatsBlacklist = chatsBlacklist;
|
|
21
|
-
this.allowedUsers = allowedUsers;
|
|
22
|
-
this.condition = condition;
|
|
23
|
-
this.stateConstructor = stateConstructor;
|
|
24
|
-
this.key = `command:${this.name.replace('.', '-')}`;
|
|
25
|
-
}
|
|
26
|
-
async exec(ctx) {
|
|
27
|
-
if (!this.active || this.chatsBlacklist.includes(ctx.chatId))
|
|
28
|
-
return;
|
|
29
|
-
const isConditionMet = await this.condition(ctx);
|
|
30
|
-
if (!isConditionMet)
|
|
31
|
-
return;
|
|
32
|
-
const state = await ctx.storage.getActionState(this, ctx.chatId);
|
|
33
|
-
const { shouldTrigger, matchResults, skipCooldown } = this.triggers
|
|
34
|
-
.map((x) => this.checkTrigger(ctx, x, state))
|
|
35
|
-
.reduce((acc, curr) => acc.mergeWith(curr), commandTriggerCheckResult_1.CommandTriggerCheckResult.DoNotTrigger);
|
|
36
|
-
if (!shouldTrigger)
|
|
37
|
-
return;
|
|
38
|
-
logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
|
|
39
|
-
ctx.matchResults = matchResults;
|
|
40
|
-
await this.handler(ctx, state);
|
|
41
|
-
if (skipCooldown) {
|
|
42
|
-
ctx.startCooldown = false;
|
|
43
|
-
}
|
|
44
|
-
if (ctx.startCooldown) {
|
|
45
|
-
state.lastExecutedDate = (0, moment_1.default)().valueOf();
|
|
46
|
-
}
|
|
47
|
-
ctx.updateActions.forEach((action) => action(state));
|
|
48
|
-
await ctx.storage.saveActionExecutionResult(this, ctx.chatId, new actionExecutionResult_1.ActionExecutionResult(state, ctx.startCooldown && shouldTrigger));
|
|
49
|
-
}
|
|
50
|
-
checkTrigger(ctx, trigger, state) {
|
|
51
|
-
let shouldTrigger = false;
|
|
52
|
-
const matchResults = [];
|
|
53
|
-
if (!ctx.fromUserId)
|
|
54
|
-
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
55
|
-
const isUserAllowed = this.allowedUsers.length == 0 ||
|
|
56
|
-
this.allowedUsers.includes(ctx.fromUserId);
|
|
57
|
-
if (!isUserAllowed)
|
|
58
|
-
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
59
|
-
const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
|
|
60
|
-
const cooldownInMilliseconds = (0, timeConvertions_1.secondsToMilliseconds)(this.cooldownInSeconds);
|
|
61
|
-
const onCooldown = (0, moment_1.default)().diff(lastExecutedDate) < cooldownInMilliseconds;
|
|
62
|
-
if (onCooldown)
|
|
63
|
-
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DoNotTrigger;
|
|
64
|
-
if (trigger == ctx.messageType) {
|
|
65
|
-
shouldTrigger = true;
|
|
66
|
-
}
|
|
67
|
-
else if (typeof trigger == 'string') {
|
|
68
|
-
shouldTrigger = ctx.messageText.toLowerCase() == trigger;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
trigger.lastIndex = 0;
|
|
72
|
-
const execResult = trigger.exec(ctx.messageText);
|
|
73
|
-
if (execResult != null) {
|
|
74
|
-
matchResults.push(execResult);
|
|
75
|
-
if (trigger.global) {
|
|
76
|
-
while (true) {
|
|
77
|
-
const nextResult = trigger.exec(ctx.messageText);
|
|
78
|
-
if (nextResult == null)
|
|
79
|
-
break;
|
|
80
|
-
matchResults.push(nextResult);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
shouldTrigger = matchResults.length > 0;
|
|
85
|
-
}
|
|
86
|
-
return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults, false);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
exports.CommandAction = CommandAction;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Sema as Semaphore } from 'async-sema';
|
|
2
|
-
import { ScheduledHandler } from '../../types/handlers';
|
|
3
|
-
import { HoursOfDay } from '../../types/timeValues';
|
|
4
|
-
import { IActionState } from '../../types/actionState';
|
|
5
|
-
import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
6
|
-
import { CachedStateFactory } from '../cachedStateFactory';
|
|
7
|
-
import { ChatContext } from '../context/chatContext';
|
|
8
|
-
export declare class ScheduledAction<TActionState extends IActionState> implements IActionWithState {
|
|
9
|
-
static semaphore: Semaphore;
|
|
10
|
-
name: string;
|
|
11
|
-
timeinHours: HoursOfDay;
|
|
12
|
-
active: boolean;
|
|
13
|
-
chatsWhitelist: number[];
|
|
14
|
-
key: ActionKey;
|
|
15
|
-
cachedState: Map<string, unknown>;
|
|
16
|
-
stateConstructor: () => TActionState;
|
|
17
|
-
cachedStateFactories: Map<string, CachedStateFactory>;
|
|
18
|
-
handler: ScheduledHandler<TActionState>;
|
|
19
|
-
constructor(name: string, handler: ScheduledHandler<TActionState>, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>, stateConstructor: () => TActionState);
|
|
20
|
-
exec(ctx: ChatContext<TActionState>): Promise<void>;
|
|
21
|
-
private getCachedValue;
|
|
22
|
-
private shouldTrigger;
|
|
23
|
-
}
|
|
24
|
-
//# sourceMappingURL=scheduledAction.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAKrD,qBAAa,eAAe,CAAC,YAAY,SAAS,YAAY,CAC1D,YAAW,gBAAgB;IAE3B,MAAM,CAAC,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,SAAS,CAAC;IAEf,WAAW,uBAA8B;IACzC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAGpC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAYlC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC;YAmC3B,cAAc;IAqC5B,OAAO,CAAC,aAAa;CAaxB"}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ScheduledAction = void 0;
|
|
7
|
-
const moment_1 = __importDefault(require("moment"));
|
|
8
|
-
const async_sema_1 = require("async-sema");
|
|
9
|
-
const timeConvertions_1 = require("../../helpers/timeConvertions");
|
|
10
|
-
const actionExecutionResult_1 = require("../actionExecutionResult");
|
|
11
|
-
const logger_1 = require("../../services/logger");
|
|
12
|
-
const taskScheduler_1 = require("../../services/taskScheduler");
|
|
13
|
-
class ScheduledAction {
|
|
14
|
-
constructor(name, handler, timeinHours, active, whitelist, cachedStateFactories, stateConstructor) {
|
|
15
|
-
this.cachedState = new Map();
|
|
16
|
-
this.name = name;
|
|
17
|
-
this.handler = handler;
|
|
18
|
-
this.timeinHours = timeinHours;
|
|
19
|
-
this.active = active;
|
|
20
|
-
this.chatsWhitelist = whitelist;
|
|
21
|
-
this.cachedStateFactories = cachedStateFactories;
|
|
22
|
-
this.key = `scheduled:${this.name.replace('.', '-')}`;
|
|
23
|
-
this.stateConstructor = stateConstructor;
|
|
24
|
-
}
|
|
25
|
-
async exec(ctx) {
|
|
26
|
-
if (!this.active || !this.chatsWhitelist.includes(ctx.chatId))
|
|
27
|
-
return;
|
|
28
|
-
const state = await ctx.storage.getActionState(this, ctx.chatId);
|
|
29
|
-
const isAllowedToTrigger = this.shouldTrigger(state);
|
|
30
|
-
if (isAllowedToTrigger) {
|
|
31
|
-
logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
|
|
32
|
-
await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName), state);
|
|
33
|
-
state.lastExecutedDate = (0, moment_1.default)().valueOf();
|
|
34
|
-
ctx.updateActions.forEach((action) => action(state));
|
|
35
|
-
await ctx.storage.saveActionExecutionResult(this, ctx.chatId, new actionExecutionResult_1.ActionExecutionResult(state, isAllowedToTrigger));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
async getCachedValue(key, botName) {
|
|
39
|
-
if (!this.cachedStateFactories.has(key)) {
|
|
40
|
-
throw new Error(`No shared cache was set up for the key [${key}] in action '${this.name}'`);
|
|
41
|
-
}
|
|
42
|
-
await ScheduledAction.semaphore.acquire();
|
|
43
|
-
try {
|
|
44
|
-
if (this.cachedState.has(key)) {
|
|
45
|
-
return this.cachedState.get(key);
|
|
46
|
-
}
|
|
47
|
-
const cachedItemFactory = this.cachedStateFactories.get(key);
|
|
48
|
-
const value = await cachedItemFactory.getValue();
|
|
49
|
-
this.cachedState.set(key, value);
|
|
50
|
-
taskScheduler_1.Scheduler.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), (0, timeConvertions_1.hoursToMilliseconds)(cachedItemFactory.invalidationTimeoutInHours), botName);
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
finally {
|
|
54
|
-
ScheduledAction.semaphore.release();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
shouldTrigger(state) {
|
|
58
|
-
const startOfToday = (0, moment_1.default)().startOf('day').valueOf();
|
|
59
|
-
const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
|
|
60
|
-
const currentTime = (0, moment_1.default)();
|
|
61
|
-
const scheduledTime = (0, moment_1.default)()
|
|
62
|
-
.startOf('day')
|
|
63
|
-
.add(this.timeinHours, 'hours');
|
|
64
|
-
const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
|
|
65
|
-
const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
|
|
66
|
-
return isAllowedToTrigger && !hasTriggeredToday;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
exports.ScheduledAction = ScheduledAction;
|
|
70
|
-
ScheduledAction.semaphore = new async_sema_1.Sema(1);
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Seconds } from '../types/timeValues';
|
|
2
|
-
import { IStorageClient } from '../types/storage';
|
|
3
|
-
import { IActionState } from '../types/actionState';
|
|
4
|
-
import { CommandAction } from './actions/commandAction';
|
|
5
|
-
import { ScheduledAction } from './actions/scheduledAction';
|
|
6
|
-
export declare class BotInstance {
|
|
7
|
-
name: string;
|
|
8
|
-
private api;
|
|
9
|
-
private telegraf;
|
|
10
|
-
private commands;
|
|
11
|
-
private scheduled;
|
|
12
|
-
private chats;
|
|
13
|
-
storage: IStorageClient;
|
|
14
|
-
constructor(options: {
|
|
15
|
-
name: string;
|
|
16
|
-
token: string;
|
|
17
|
-
commands: CommandAction<IActionState>[];
|
|
18
|
-
scheduled: ScheduledAction<IActionState>[];
|
|
19
|
-
chats: Record<string, number>;
|
|
20
|
-
storageClient?: IStorageClient;
|
|
21
|
-
storagePath?: string;
|
|
22
|
-
scheduledPeriod?: Seconds;
|
|
23
|
-
verboseLoggingForIncomingMessage?: boolean;
|
|
24
|
-
});
|
|
25
|
-
private initializeScheduledProcessing;
|
|
26
|
-
private initializeMessageProcessing;
|
|
27
|
-
stop(code: string): Promise<void>;
|
|
28
|
-
private runScheduled;
|
|
29
|
-
private processMessage;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=botInstance.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAM5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;KAC9C;IAqCD,OAAO,CAAC,6BAA6B;IA2CrC,OAAO,CAAC,2BAA2B;IAiC7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAyBZ,cAAc;CAmB/B"}
|