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
package/package.json
CHANGED
|
@@ -3,15 +3,15 @@ import { dirname } from 'path';
|
|
|
3
3
|
import { mkdir, readFile, writeFile } from 'fs/promises';
|
|
4
4
|
import { Sema as Semaphore } from 'async-sema';
|
|
5
5
|
import { IStorageClient } from '../types/storage';
|
|
6
|
-
import { ActionExecutionResult } from '../
|
|
6
|
+
import { ActionExecutionResult } from '../dtos/actionExecutionResult';
|
|
7
7
|
import { IActionState } from '../types/actionState';
|
|
8
8
|
import { IActionWithState, ActionKey } from '../types/actionWithState';
|
|
9
9
|
|
|
10
10
|
export class JsonFileStorage implements IStorageClient {
|
|
11
|
-
private locks = new Map<ActionKey, Semaphore>();
|
|
12
|
-
private cache: Map<string, Record<number, IActionState>>;
|
|
13
|
-
private storagePath: string;
|
|
14
|
-
private botName: string;
|
|
11
|
+
private readonly locks = new Map<ActionKey, Semaphore>();
|
|
12
|
+
private readonly cache: Map<string, Record<number, IActionState>>;
|
|
13
|
+
private readonly storagePath: string;
|
|
14
|
+
private readonly botName: string;
|
|
15
15
|
|
|
16
16
|
constructor(
|
|
17
17
|
botName: string,
|
package/services/telegramApi.ts
CHANGED
|
@@ -1,38 +1,24 @@
|
|
|
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
3
|
import { Logger } from './logger';
|
|
7
|
-
import { IncomingMessage } from '../entities/incomingMessage';
|
|
8
4
|
import { BotResponse, IReplyMessage } from '../types/response';
|
|
9
5
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
10
6
|
import { setTimeout } from 'timers/promises';
|
|
11
7
|
import { Milliseconds } from '../types/timeValues';
|
|
12
|
-
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
13
|
-
import { IActionState } from '../types/actionState';
|
|
14
|
-
import { CommandAction } from '../entities/actions/commandAction';
|
|
15
8
|
|
|
16
9
|
const TELEGRAM_RATELIMIT_DELAY = 35 as Milliseconds;
|
|
17
10
|
|
|
18
11
|
export class TelegramApiService {
|
|
19
12
|
isFlushing = false;
|
|
20
|
-
messageQueue: BotResponse[] = [];
|
|
21
|
-
|
|
22
|
-
botName: string;
|
|
23
|
-
telegram: Telegram;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
botName: string,
|
|
29
|
-
telegram: Telegram,
|
|
30
|
-
storage: IStorageClient,
|
|
31
|
-
chats: Record<string, number>
|
|
32
|
-
) {
|
|
13
|
+
readonly messageQueue: BotResponse[] = [];
|
|
14
|
+
|
|
15
|
+
readonly botName: string;
|
|
16
|
+
readonly telegram: Telegram;
|
|
17
|
+
readonly storage: IStorageClient;
|
|
18
|
+
|
|
19
|
+
constructor(botName: string, telegram: Telegram, storage: IStorageClient) {
|
|
33
20
|
this.telegram = telegram;
|
|
34
21
|
this.botName = botName;
|
|
35
|
-
this.chats = inverseRecord(chats);
|
|
36
22
|
this.storage = storage;
|
|
37
23
|
}
|
|
38
24
|
|
|
@@ -59,7 +45,7 @@ export class TelegramApiService {
|
|
|
59
45
|
Logger.errorWithTraceId(
|
|
60
46
|
this.botName,
|
|
61
47
|
message.traceId,
|
|
62
|
-
|
|
48
|
+
message.chatInfo.name,
|
|
63
49
|
error,
|
|
64
50
|
message
|
|
65
51
|
);
|
|
@@ -75,14 +61,14 @@ export class TelegramApiService {
|
|
|
75
61
|
) {
|
|
76
62
|
if (response.shouldPin) {
|
|
77
63
|
await this.telegram.pinChatMessage(
|
|
78
|
-
response.
|
|
64
|
+
response.chatInfo.id,
|
|
79
65
|
sentMessage.message_id,
|
|
80
66
|
{ disable_notification: true }
|
|
81
67
|
);
|
|
82
68
|
|
|
83
69
|
await this.storage.updateStateFor(
|
|
84
70
|
response.action,
|
|
85
|
-
response.
|
|
71
|
+
response.chatInfo.id,
|
|
86
72
|
async (state) => {
|
|
87
73
|
state.pinnedMessages.push(sentMessage.message_id);
|
|
88
74
|
}
|
|
@@ -96,7 +82,7 @@ export class TelegramApiService {
|
|
|
96
82
|
switch (response.kind) {
|
|
97
83
|
case 'text':
|
|
98
84
|
sentMessage = await this.telegram.sendMessage(
|
|
99
|
-
response.
|
|
85
|
+
response.chatInfo.id,
|
|
100
86
|
response.content,
|
|
101
87
|
{
|
|
102
88
|
reply_to_message_id: response.replyId,
|
|
@@ -110,7 +96,7 @@ export class TelegramApiService {
|
|
|
110
96
|
break;
|
|
111
97
|
case 'image':
|
|
112
98
|
sentMessage = await this.telegram.sendPhoto(
|
|
113
|
-
response.
|
|
99
|
+
response.chatInfo.id,
|
|
114
100
|
response.content,
|
|
115
101
|
response.replyId
|
|
116
102
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -122,7 +108,7 @@ export class TelegramApiService {
|
|
|
122
108
|
break;
|
|
123
109
|
case 'video':
|
|
124
110
|
sentMessage = await this.telegram.sendVideo(
|
|
125
|
-
response.
|
|
111
|
+
response.chatInfo.id,
|
|
126
112
|
response.content,
|
|
127
113
|
response.replyId
|
|
128
114
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -134,7 +120,7 @@ export class TelegramApiService {
|
|
|
134
120
|
break;
|
|
135
121
|
case 'react':
|
|
136
122
|
await this.telegram.setMessageReaction(
|
|
137
|
-
response.
|
|
123
|
+
response.chatInfo.id,
|
|
138
124
|
response.messageId,
|
|
139
125
|
[
|
|
140
126
|
{
|
|
@@ -148,13 +134,13 @@ export class TelegramApiService {
|
|
|
148
134
|
return;
|
|
149
135
|
case 'unpin':
|
|
150
136
|
await this.telegram.unpinChatMessage(
|
|
151
|
-
response.
|
|
137
|
+
response.chatInfo.id,
|
|
152
138
|
response.messageId
|
|
153
139
|
);
|
|
154
140
|
|
|
155
141
|
await this.storage.updateStateFor(
|
|
156
142
|
response.action,
|
|
157
|
-
response.
|
|
143
|
+
response.chatInfo.id,
|
|
158
144
|
async (state) => {
|
|
159
145
|
state.pinnedMessages = state.pinnedMessages.filter(
|
|
160
146
|
(x) => x != response.messageId
|
|
@@ -168,32 +154,4 @@ export class TelegramApiService {
|
|
|
168
154
|
}
|
|
169
155
|
}
|
|
170
156
|
}
|
|
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
157
|
}
|
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/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
|
}
|
package/types/storage.ts
CHANGED
package/helpers/inverseRecord.ts
DELETED
|
File without changes
|