chz-telegram-bot 0.0.50 → 0.0.52
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 +20 -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 +11 -10
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +41 -40
- package/dist/entities/actions/scheduledAction.d.ts +11 -11
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +14 -18
- package/dist/entities/botInstance.d.ts +16 -8
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +26 -18
- package/dist/entities/cachedStateFactory.d.ts +2 -2
- package/dist/entities/cachedStateFactory.d.ts.map +1 -1
- package/dist/entities/context/chatContext.d.ts +13 -14
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +16 -24
- package/dist/entities/context/messageContext.d.ts +5 -3
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +13 -13
- package/dist/entities/taskRecord.d.ts +3 -3
- package/dist/entities/taskRecord.d.ts.map +1 -1
- 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/index.d.ts +3 -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 +5 -6
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +3 -5
- package/dist/services/jsonLogger.d.ts +8 -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 +33 -0
- package/dist/services/taskScheduler.d.ts +1 -1
- package/dist/services/taskScheduler.d.ts.map +1 -1
- package/dist/services/telegramApi.d.ts +7 -14
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +12 -20
- package/dist/types/actionWithState.d.ts +2 -2
- package/dist/types/actionWithState.d.ts.map +1 -1
- package/dist/types/commandCondition.d.ts +1 -1
- package/dist/types/commandCondition.d.ts.map +1 -1
- package/dist/types/logger.d.ts +6 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logger.js +2 -0
- package/dist/types/response.d.ts +15 -14
- 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/dtos/chatInfo.ts +11 -0
- package/{entities → dtos}/commandTriggerCheckResult.ts +10 -10
- package/{entities → dtos}/incomingMessage.ts +13 -12
- 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 +64 -56
- package/entities/actions/scheduledAction.ts +40 -43
- package/entities/botInstance.ts +64 -35
- package/entities/cachedStateFactory.ts +2 -2
- package/entities/context/chatContext.ts +40 -34
- package/entities/context/messageContext.ts +21 -18
- package/entities/taskRecord.ts +3 -3
- package/helpers/noop.ts +5 -2
- package/index.ts +3 -1
- package/main.ts +16 -15
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +8 -10
- package/services/{logger.ts → jsonLogger.ts} +3 -3
- package/services/{taskScheduler.ts → nodeTimeoutScheduler.ts} +12 -8
- package/services/telegramApi.ts +19 -54
- package/types/actionWithState.ts +2 -2
- package/types/commandCondition.ts +1 -1
- package/types/logger.ts +24 -0
- package/types/response.ts +15 -14
- package/types/scheduler.ts +20 -0
- package/types/storage.ts +1 -2
- package/entities/actionExecutionResult.ts +0 -11
- package/helpers/inverseRecord.ts +0 -7
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { TaskRecord } from '../entities/taskRecord';
|
|
2
|
+
import { ILogger } from '../types/logger';
|
|
3
|
+
import { IScheduler } from '../types/scheduler';
|
|
2
4
|
import { Milliseconds } from '../types/timeValues';
|
|
3
|
-
import { Logger } from './logger';
|
|
4
5
|
|
|
5
|
-
class
|
|
6
|
-
|
|
6
|
+
export class NodeTimeoutScheduler implements IScheduler {
|
|
7
|
+
private readonly logger!: ILogger;
|
|
8
|
+
readonly activeTasks: TaskRecord[] = [];
|
|
9
|
+
|
|
10
|
+
constructor(logger: ILogger) {
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
}
|
|
7
13
|
|
|
8
14
|
stopAll() {
|
|
9
15
|
this.activeTasks.forEach((task) => {
|
|
@@ -25,7 +31,7 @@ class TaskScheduler {
|
|
|
25
31
|
setImmediate(action);
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
this.logger.logWithTraceId(
|
|
29
35
|
ownerName,
|
|
30
36
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
31
37
|
'System',
|
|
@@ -42,7 +48,7 @@ class TaskScheduler {
|
|
|
42
48
|
ownerName: string
|
|
43
49
|
) {
|
|
44
50
|
const actionWrapper = () => {
|
|
45
|
-
|
|
51
|
+
this.logger.logWithTraceId(
|
|
46
52
|
ownerName,
|
|
47
53
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
48
54
|
'System',
|
|
@@ -52,7 +58,7 @@ class TaskScheduler {
|
|
|
52
58
|
};
|
|
53
59
|
const taskId = setTimeout(actionWrapper, delay);
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
this.logger.logWithTraceId(
|
|
56
62
|
ownerName,
|
|
57
63
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
58
64
|
'System',
|
|
@@ -60,5 +66,3 @@ class TaskScheduler {
|
|
|
60
66
|
);
|
|
61
67
|
}
|
|
62
68
|
}
|
|
63
|
-
|
|
64
|
-
export const Scheduler = new TaskScheduler();
|
package/services/telegramApi.ts
CHANGED
|
@@ -1,39 +1,32 @@
|
|
|
1
1
|
import { Message } from 'telegraf/types';
|
|
2
|
-
import { ChatContext } from '../entities/context/chatContext';
|
|
3
|
-
import { MessageContext } from '../entities/context/messageContext';
|
|
4
|
-
import { inverseRecord as inverseRecord } from '../helpers/inverseRecord';
|
|
5
2
|
import { IStorageClient } from '../types/storage';
|
|
6
|
-
import { Logger } from './logger';
|
|
7
|
-
import { IncomingMessage } from '../entities/incomingMessage';
|
|
8
3
|
import { BotResponse, IReplyMessage } from '../types/response';
|
|
9
4
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
10
5
|
import { setTimeout } from 'timers/promises';
|
|
11
6
|
import { Milliseconds } from '../types/timeValues';
|
|
12
|
-
import {
|
|
13
|
-
import { IActionState } from '../types/actionState';
|
|
14
|
-
import { CommandAction } from '../entities/actions/commandAction';
|
|
7
|
+
import { ILogger } from '../types/logger';
|
|
15
8
|
|
|
16
9
|
const TELEGRAM_RATELIMIT_DELAY = 35 as Milliseconds;
|
|
17
10
|
|
|
18
11
|
export class TelegramApiService {
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
private readonly telegram: Telegram;
|
|
13
|
+
private readonly storage: IStorageClient;
|
|
14
|
+
private readonly logger: ILogger;
|
|
21
15
|
|
|
22
|
-
botName: string;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
storage: IStorageClient;
|
|
16
|
+
private readonly botName: string;
|
|
17
|
+
private readonly messageQueue: BotResponse[] = [];
|
|
18
|
+
isFlushing = false;
|
|
26
19
|
|
|
27
20
|
constructor(
|
|
28
21
|
botName: string,
|
|
29
22
|
telegram: Telegram,
|
|
30
23
|
storage: IStorageClient,
|
|
31
|
-
|
|
24
|
+
logger: ILogger
|
|
32
25
|
) {
|
|
33
26
|
this.telegram = telegram;
|
|
34
27
|
this.botName = botName;
|
|
35
|
-
this.chats = inverseRecord(chats);
|
|
36
28
|
this.storage = storage;
|
|
29
|
+
this.logger = logger;
|
|
37
30
|
}
|
|
38
31
|
|
|
39
32
|
enqueueBatchedResponses(responses: BotResponse[]) {
|
|
@@ -56,10 +49,10 @@ export class TelegramApiService {
|
|
|
56
49
|
await this.processResponse(message);
|
|
57
50
|
await setTimeout(TELEGRAM_RATELIMIT_DELAY);
|
|
58
51
|
} catch (error) {
|
|
59
|
-
|
|
52
|
+
this.logger.errorWithTraceId(
|
|
60
53
|
this.botName,
|
|
61
54
|
message.traceId,
|
|
62
|
-
|
|
55
|
+
message.chatInfo.name,
|
|
63
56
|
error,
|
|
64
57
|
message
|
|
65
58
|
);
|
|
@@ -75,14 +68,14 @@ export class TelegramApiService {
|
|
|
75
68
|
) {
|
|
76
69
|
if (response.shouldPin) {
|
|
77
70
|
await this.telegram.pinChatMessage(
|
|
78
|
-
response.
|
|
71
|
+
response.chatInfo.id,
|
|
79
72
|
sentMessage.message_id,
|
|
80
73
|
{ disable_notification: true }
|
|
81
74
|
);
|
|
82
75
|
|
|
83
76
|
await this.storage.updateStateFor(
|
|
84
77
|
response.action,
|
|
85
|
-
response.
|
|
78
|
+
response.chatInfo.id,
|
|
86
79
|
async (state) => {
|
|
87
80
|
state.pinnedMessages.push(sentMessage.message_id);
|
|
88
81
|
}
|
|
@@ -96,7 +89,7 @@ export class TelegramApiService {
|
|
|
96
89
|
switch (response.kind) {
|
|
97
90
|
case 'text':
|
|
98
91
|
sentMessage = await this.telegram.sendMessage(
|
|
99
|
-
response.
|
|
92
|
+
response.chatInfo.id,
|
|
100
93
|
response.content,
|
|
101
94
|
{
|
|
102
95
|
reply_to_message_id: response.replyId,
|
|
@@ -110,7 +103,7 @@ export class TelegramApiService {
|
|
|
110
103
|
break;
|
|
111
104
|
case 'image':
|
|
112
105
|
sentMessage = await this.telegram.sendPhoto(
|
|
113
|
-
response.
|
|
106
|
+
response.chatInfo.id,
|
|
114
107
|
response.content,
|
|
115
108
|
response.replyId
|
|
116
109
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -122,7 +115,7 @@ export class TelegramApiService {
|
|
|
122
115
|
break;
|
|
123
116
|
case 'video':
|
|
124
117
|
sentMessage = await this.telegram.sendVideo(
|
|
125
|
-
response.
|
|
118
|
+
response.chatInfo.id,
|
|
126
119
|
response.content,
|
|
127
120
|
response.replyId
|
|
128
121
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -134,7 +127,7 @@ export class TelegramApiService {
|
|
|
134
127
|
break;
|
|
135
128
|
case 'react':
|
|
136
129
|
await this.telegram.setMessageReaction(
|
|
137
|
-
response.
|
|
130
|
+
response.chatInfo.id,
|
|
138
131
|
response.messageId,
|
|
139
132
|
[
|
|
140
133
|
{
|
|
@@ -148,13 +141,13 @@ export class TelegramApiService {
|
|
|
148
141
|
return;
|
|
149
142
|
case 'unpin':
|
|
150
143
|
await this.telegram.unpinChatMessage(
|
|
151
|
-
response.
|
|
144
|
+
response.chatInfo.id,
|
|
152
145
|
response.messageId
|
|
153
146
|
);
|
|
154
147
|
|
|
155
148
|
await this.storage.updateStateFor(
|
|
156
149
|
response.action,
|
|
157
|
-
response.
|
|
150
|
+
response.chatInfo.id,
|
|
158
151
|
async (state) => {
|
|
159
152
|
state.pinnedMessages = state.pinnedMessages.filter(
|
|
160
153
|
(x) => x != response.messageId
|
|
@@ -168,32 +161,4 @@ export class TelegramApiService {
|
|
|
168
161
|
}
|
|
169
162
|
}
|
|
170
163
|
}
|
|
171
|
-
|
|
172
|
-
initializeContextForMessage<TActionState extends IActionState>(
|
|
173
|
-
ctx: MessageContext<TActionState>,
|
|
174
|
-
incomingMessage: IncomingMessage,
|
|
175
|
-
command: CommandAction<TActionState>
|
|
176
|
-
) {
|
|
177
|
-
return ctx.initializeMessageContext(
|
|
178
|
-
this.botName,
|
|
179
|
-
command,
|
|
180
|
-
incomingMessage,
|
|
181
|
-
this.storage
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
initializeContextForChat<TActionState extends IActionState>(
|
|
186
|
-
ctx: ChatContext<TActionState>,
|
|
187
|
-
chatId: number,
|
|
188
|
-
scheduledAction: ScheduledAction<TActionState>
|
|
189
|
-
) {
|
|
190
|
-
return ctx.initializeChatContext(
|
|
191
|
-
this.botName,
|
|
192
|
-
scheduledAction,
|
|
193
|
-
chatId,
|
|
194
|
-
this.chats[chatId],
|
|
195
|
-
`Scheduled:${scheduledAction.key}:${chatId}`,
|
|
196
|
-
this.storage
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
164
|
}
|
package/types/actionWithState.ts
CHANGED
|
@@ -3,6 +3,6 @@ import { IActionState } from './actionState';
|
|
|
3
3
|
export type ActionKey = string & { __brand: 'actionKey' };
|
|
4
4
|
|
|
5
5
|
export interface IActionWithState<TActionState extends IActionState> {
|
|
6
|
-
key: ActionKey;
|
|
7
|
-
stateConstructor: () => TActionState;
|
|
6
|
+
readonly key: ActionKey;
|
|
7
|
+
readonly stateConstructor: () => TActionState;
|
|
8
8
|
}
|
package/types/logger.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ILogger {
|
|
2
|
+
logObjectWithTraceId(
|
|
3
|
+
botName: string,
|
|
4
|
+
traceId: string | number,
|
|
5
|
+
chatName: string,
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
data: any
|
|
8
|
+
): void;
|
|
9
|
+
|
|
10
|
+
logWithTraceId(
|
|
11
|
+
botName: string,
|
|
12
|
+
traceId: string | number,
|
|
13
|
+
chatName: string,
|
|
14
|
+
text: string
|
|
15
|
+
): void;
|
|
16
|
+
|
|
17
|
+
errorWithTraceId<TData>(
|
|
18
|
+
botName: string,
|
|
19
|
+
traceId: string | number,
|
|
20
|
+
chatName: string,
|
|
21
|
+
errorObj: unknown,
|
|
22
|
+
extraData?: TData | undefined
|
|
23
|
+
): void;
|
|
24
|
+
}
|
package/types/response.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { ChatInfo } from '../dtos/chatInfo';
|
|
2
|
+
import { DelayResponse } from '../dtos/responses/delay';
|
|
3
|
+
import { ImageMessage } from '../dtos/responses/imageMessage';
|
|
4
|
+
import { Reaction } from '../dtos/responses/reaction';
|
|
5
|
+
import { TextMessage } from '../dtos/responses/textMessage';
|
|
6
|
+
import { UnpinResponse } from '../dtos/responses/unpin';
|
|
7
|
+
import { VideoMessage } from '../dtos/responses/videoMessage';
|
|
7
8
|
import { IActionState } from './actionState';
|
|
8
9
|
import { IActionWithState } from './actionWithState';
|
|
9
10
|
|
|
@@ -25,16 +26,16 @@ export type BotResponse =
|
|
|
25
26
|
| ImageMessage;
|
|
26
27
|
|
|
27
28
|
export interface IChatResponse {
|
|
28
|
-
kind: keyof typeof BotResponseTypes;
|
|
29
|
-
|
|
30
|
-
traceId: number | string;
|
|
29
|
+
readonly kind: keyof typeof BotResponseTypes;
|
|
30
|
+
readonly chatInfo: ChatInfo;
|
|
31
|
+
readonly traceId: number | string;
|
|
31
32
|
|
|
32
|
-
action: IActionWithState<IActionState>;
|
|
33
|
+
readonly action: IActionWithState<IActionState>;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export interface IReplyMessage<TType> extends IChatResponse {
|
|
36
|
-
content: TType;
|
|
37
|
-
replyId: number | undefined;
|
|
38
|
-
disableWebPreview: boolean;
|
|
39
|
-
shouldPin: boolean;
|
|
37
|
+
readonly content: TType;
|
|
38
|
+
readonly replyId: number | undefined;
|
|
39
|
+
readonly disableWebPreview: boolean;
|
|
40
|
+
readonly shouldPin: boolean;
|
|
40
41
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Milliseconds } from './timeValues';
|
|
2
|
+
|
|
3
|
+
export interface IScheduler {
|
|
4
|
+
stopAll(): void;
|
|
5
|
+
|
|
6
|
+
createTask(
|
|
7
|
+
name: string,
|
|
8
|
+
action: () => void,
|
|
9
|
+
interval: Milliseconds,
|
|
10
|
+
executeRightAway: boolean,
|
|
11
|
+
ownerName: string
|
|
12
|
+
): void;
|
|
13
|
+
|
|
14
|
+
createOnetimeTask(
|
|
15
|
+
name: string,
|
|
16
|
+
action: () => void,
|
|
17
|
+
delay: Milliseconds,
|
|
18
|
+
ownerName: string
|
|
19
|
+
): void;
|
|
20
|
+
}
|
package/types/storage.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ActionExecutionResult } from '../entities/actionExecutionResult';
|
|
2
1
|
import { IActionState } from './actionState';
|
|
3
2
|
import { ActionKey, IActionWithState } from './actionWithState';
|
|
4
3
|
|
|
@@ -23,6 +22,6 @@ export interface IStorageClient {
|
|
|
23
22
|
saveActionExecutionResult<TActionState extends IActionState>(
|
|
24
23
|
action: IActionWithState<TActionState>,
|
|
25
24
|
chatId: number,
|
|
26
|
-
|
|
25
|
+
state: TActionState
|
|
27
26
|
): Promise<void>;
|
|
28
27
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { IActionState } from '../types/actionState';
|
|
2
|
-
|
|
3
|
-
export class ActionExecutionResult<TActionState extends IActionState> {
|
|
4
|
-
data: TActionState;
|
|
5
|
-
shouldUpdate: boolean;
|
|
6
|
-
|
|
7
|
-
constructor(data: TActionState, shouldUpdate: boolean) {
|
|
8
|
-
this.data = data;
|
|
9
|
-
this.shouldUpdate = shouldUpdate;
|
|
10
|
-
}
|
|
11
|
-
}
|