chz-telegram-bot 0.0.49 → 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 +11 -21
- 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 +17 -65
- 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,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;
|
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
|
|
|
@@ -47,25 +33,19 @@ export class TelegramApiService {
|
|
|
47
33
|
|
|
48
34
|
this.isFlushing = true;
|
|
49
35
|
|
|
50
|
-
console.log(
|
|
51
|
-
`Flushing ${this.messageQueue.length} messages for ${this.botName}`
|
|
52
|
-
);
|
|
53
36
|
while (this.messageQueue.length) {
|
|
54
|
-
const message = this.messageQueue.
|
|
37
|
+
const message = this.messageQueue.shift();
|
|
55
38
|
|
|
56
39
|
if (!message) break;
|
|
57
40
|
|
|
58
41
|
try {
|
|
59
|
-
console.log(
|
|
60
|
-
`Sending message ${message.kind} to ${message.chatId} for ${this.botName}`
|
|
61
|
-
);
|
|
62
42
|
await this.processResponse(message);
|
|
63
43
|
await setTimeout(TELEGRAM_RATELIMIT_DELAY);
|
|
64
44
|
} catch (error) {
|
|
65
45
|
Logger.errorWithTraceId(
|
|
66
46
|
this.botName,
|
|
67
47
|
message.traceId,
|
|
68
|
-
|
|
48
|
+
message.chatInfo.name,
|
|
69
49
|
error,
|
|
70
50
|
message
|
|
71
51
|
);
|
|
@@ -81,14 +61,14 @@ export class TelegramApiService {
|
|
|
81
61
|
) {
|
|
82
62
|
if (response.shouldPin) {
|
|
83
63
|
await this.telegram.pinChatMessage(
|
|
84
|
-
response.
|
|
64
|
+
response.chatInfo.id,
|
|
85
65
|
sentMessage.message_id,
|
|
86
66
|
{ disable_notification: true }
|
|
87
67
|
);
|
|
88
68
|
|
|
89
69
|
await this.storage.updateStateFor(
|
|
90
70
|
response.action,
|
|
91
|
-
response.
|
|
71
|
+
response.chatInfo.id,
|
|
92
72
|
async (state) => {
|
|
93
73
|
state.pinnedMessages.push(sentMessage.message_id);
|
|
94
74
|
}
|
|
@@ -102,7 +82,7 @@ export class TelegramApiService {
|
|
|
102
82
|
switch (response.kind) {
|
|
103
83
|
case 'text':
|
|
104
84
|
sentMessage = await this.telegram.sendMessage(
|
|
105
|
-
response.
|
|
85
|
+
response.chatInfo.id,
|
|
106
86
|
response.content,
|
|
107
87
|
{
|
|
108
88
|
reply_to_message_id: response.replyId,
|
|
@@ -116,7 +96,7 @@ export class TelegramApiService {
|
|
|
116
96
|
break;
|
|
117
97
|
case 'image':
|
|
118
98
|
sentMessage = await this.telegram.sendPhoto(
|
|
119
|
-
response.
|
|
99
|
+
response.chatInfo.id,
|
|
120
100
|
response.content,
|
|
121
101
|
response.replyId
|
|
122
102
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -128,7 +108,7 @@ export class TelegramApiService {
|
|
|
128
108
|
break;
|
|
129
109
|
case 'video':
|
|
130
110
|
sentMessage = await this.telegram.sendVideo(
|
|
131
|
-
response.
|
|
111
|
+
response.chatInfo.id,
|
|
132
112
|
response.content,
|
|
133
113
|
response.replyId
|
|
134
114
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -140,7 +120,7 @@ export class TelegramApiService {
|
|
|
140
120
|
break;
|
|
141
121
|
case 'react':
|
|
142
122
|
await this.telegram.setMessageReaction(
|
|
143
|
-
response.
|
|
123
|
+
response.chatInfo.id,
|
|
144
124
|
response.messageId,
|
|
145
125
|
[
|
|
146
126
|
{
|
|
@@ -154,13 +134,13 @@ export class TelegramApiService {
|
|
|
154
134
|
return;
|
|
155
135
|
case 'unpin':
|
|
156
136
|
await this.telegram.unpinChatMessage(
|
|
157
|
-
response.
|
|
137
|
+
response.chatInfo.id,
|
|
158
138
|
response.messageId
|
|
159
139
|
);
|
|
160
140
|
|
|
161
141
|
await this.storage.updateStateFor(
|
|
162
142
|
response.action,
|
|
163
|
-
response.
|
|
143
|
+
response.chatInfo.id,
|
|
164
144
|
async (state) => {
|
|
165
145
|
state.pinnedMessages = state.pinnedMessages.filter(
|
|
166
146
|
(x) => x != response.messageId
|
|
@@ -174,32 +154,4 @@ export class TelegramApiService {
|
|
|
174
154
|
}
|
|
175
155
|
}
|
|
176
156
|
}
|
|
177
|
-
|
|
178
|
-
initializeContextForMessage<TActionState extends IActionState>(
|
|
179
|
-
ctx: MessageContext<TActionState>,
|
|
180
|
-
incomingMessage: IncomingMessage,
|
|
181
|
-
command: CommandAction<TActionState>
|
|
182
|
-
) {
|
|
183
|
-
return ctx.initializeMessageContext(
|
|
184
|
-
this.botName,
|
|
185
|
-
command,
|
|
186
|
-
incomingMessage,
|
|
187
|
-
this.storage
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
initializeContextForChat<TActionState extends IActionState>(
|
|
192
|
-
ctx: ChatContext<TActionState>,
|
|
193
|
-
chatId: number,
|
|
194
|
-
scheduledAction: ScheduledAction<TActionState>
|
|
195
|
-
) {
|
|
196
|
-
return ctx.initializeChatContext(
|
|
197
|
-
this.botName,
|
|
198
|
-
scheduledAction,
|
|
199
|
-
chatId,
|
|
200
|
-
this.chats[chatId],
|
|
201
|
-
`Scheduled:${scheduledAction.key}:${chatId}`,
|
|
202
|
-
this.storage
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
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
|