chz-telegram-bot 0.0.46 → 0.0.48
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/entities/actionExecutionResult.d.ts +3 -3
- package/dist/entities/actionExecutionResult.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.d.ts +2 -2
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +6 -4
- package/dist/entities/actions/scheduledAction.d.ts +2 -2
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +4 -2
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +4 -2
- package/dist/entities/context/chatContext.d.ts +21 -9
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +24 -10
- package/dist/entities/context/messageContext.d.ts +9 -6
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +14 -10
- package/dist/entities/responses/delay.d.ts +13 -0
- package/dist/entities/responses/delay.d.ts.map +1 -0
- package/dist/entities/responses/delay.js +14 -0
- package/dist/entities/responses/imageMessage.d.ts +3 -2
- package/dist/entities/responses/imageMessage.d.ts.map +1 -1
- package/dist/entities/responses/reaction.d.ts +3 -2
- package/dist/entities/responses/reaction.d.ts.map +1 -1
- package/dist/entities/responses/textMessage.d.ts +3 -2
- package/dist/entities/responses/textMessage.d.ts.map +1 -1
- package/dist/entities/responses/unpin.d.ts +3 -2
- package/dist/entities/responses/unpin.d.ts.map +1 -1
- package/dist/entities/responses/videoMessage.d.ts +3 -2
- package/dist/entities/responses/videoMessage.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/jsonFileStorage.js +1 -1
- package/dist/services/telegramApi.d.ts +1 -12
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +7 -10
- package/dist/types/actionWithState.d.ts +2 -2
- package/dist/types/actionWithState.d.ts.map +1 -1
- package/dist/types/response.d.ts +5 -2
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/response.js +2 -1
- package/dist/types/storage.d.ts +4 -4
- package/dist/types/storage.d.ts.map +1 -1
- package/entities/actionExecutionResult.ts +3 -3
- package/entities/actions/commandAction.ts +12 -5
- package/entities/actions/scheduledAction.ts +11 -3
- package/entities/botInstance.ts +4 -2
- package/entities/context/chatContext.ts +32 -15
- package/entities/context/messageContext.ts +13 -13
- package/entities/responses/delay.ts +25 -0
- package/entities/responses/imageMessage.ts +3 -2
- package/entities/responses/reaction.ts +3 -2
- package/entities/responses/textMessage.ts +3 -2
- package/entities/responses/unpin.ts +3 -2
- package/entities/responses/videoMessage.ts +3 -2
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +22 -13
- package/services/telegramApi.ts +5 -23
- package/types/actionWithState.ts +2 -2
- package/types/response.ts +6 -2
- package/types/storage.ts +9 -6
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TextMessageSendingOptions } from '../../types/messageSendingOptions';
|
|
2
2
|
import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
3
3
|
import { IActionWithState } from '../../types/actionWithState';
|
|
4
|
+
import { IActionState } from '../../types/actionState';
|
|
4
5
|
|
|
5
6
|
export class TextMessage implements IReplyMessage<string> {
|
|
6
7
|
kind = BotResponseTypes.text;
|
|
@@ -11,14 +12,14 @@ export class TextMessage implements IReplyMessage<string> {
|
|
|
11
12
|
traceId: string | number;
|
|
12
13
|
disableWebPreview: boolean;
|
|
13
14
|
shouldPin: boolean;
|
|
14
|
-
action: IActionWithState
|
|
15
|
+
action: IActionWithState<IActionState>;
|
|
15
16
|
|
|
16
17
|
constructor(
|
|
17
18
|
text: string,
|
|
18
19
|
chatId: number,
|
|
19
20
|
replyId: number | undefined,
|
|
20
21
|
traceId: string | number,
|
|
21
|
-
action: IActionWithState
|
|
22
|
+
action: IActionWithState<IActionState>,
|
|
22
23
|
options?: TextMessageSendingOptions
|
|
23
24
|
) {
|
|
24
25
|
this.content = text;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
2
2
|
import { IActionWithState } from '../../types/actionWithState';
|
|
3
|
+
import { IActionState } from '../../types/actionState';
|
|
3
4
|
|
|
4
5
|
export class UnpinResponse implements IChatResponse {
|
|
5
6
|
kind = BotResponseTypes.unpin;
|
|
@@ -7,13 +8,13 @@ export class UnpinResponse implements IChatResponse {
|
|
|
7
8
|
messageId: number;
|
|
8
9
|
chatId: number;
|
|
9
10
|
traceId: number | string;
|
|
10
|
-
action: IActionWithState
|
|
11
|
+
action: IActionWithState<IActionState>;
|
|
11
12
|
|
|
12
13
|
constructor(
|
|
13
14
|
messageId: number,
|
|
14
15
|
chatId: number,
|
|
15
16
|
traceId: number | string,
|
|
16
|
-
action: IActionWithState
|
|
17
|
+
action: IActionWithState<IActionState>
|
|
17
18
|
) {
|
|
18
19
|
this.messageId = messageId;
|
|
19
20
|
this.chatId = chatId;
|
|
@@ -2,6 +2,7 @@ import { InputFile } from 'telegraf/types';
|
|
|
2
2
|
import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
3
3
|
import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
|
+
import { IActionState } from '../../types/actionState';
|
|
5
6
|
|
|
6
7
|
export class VideoMessage implements IReplyMessage<InputFile> {
|
|
7
8
|
kind = BotResponseTypes.video;
|
|
@@ -12,14 +13,14 @@ export class VideoMessage implements IReplyMessage<InputFile> {
|
|
|
12
13
|
traceId: string | number;
|
|
13
14
|
disableWebPreview = false;
|
|
14
15
|
shouldPin: boolean;
|
|
15
|
-
action: IActionWithState
|
|
16
|
+
action: IActionWithState<IActionState>;
|
|
16
17
|
|
|
17
18
|
constructor(
|
|
18
19
|
video: InputFile,
|
|
19
20
|
chatId: number,
|
|
20
21
|
replyId: number | undefined,
|
|
21
22
|
traceId: number | string,
|
|
22
|
-
action: IActionWithState
|
|
23
|
+
action: IActionWithState<IActionState>,
|
|
23
24
|
options?: MessageSendingOptions
|
|
24
25
|
) {
|
|
25
26
|
this.content = video;
|
package/package.json
CHANGED
|
@@ -3,7 +3,6 @@ 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 { ActionStateBase } from '../entities/states/actionStateBase';
|
|
7
6
|
import { ActionExecutionResult } from '../entities/actionExecutionResult';
|
|
8
7
|
import { IActionState } from '../types/actionState';
|
|
9
8
|
import { IActionWithState, ActionKey } from '../types/actionWithState';
|
|
@@ -14,7 +13,11 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
14
13
|
private storagePath: string;
|
|
15
14
|
private botName: string;
|
|
16
15
|
|
|
17
|
-
constructor(
|
|
16
|
+
constructor(
|
|
17
|
+
botName: string,
|
|
18
|
+
actions: IActionWithState<IActionState>[],
|
|
19
|
+
path?: string
|
|
20
|
+
) {
|
|
18
21
|
this.cache = new Map<string, Record<number, IActionState>>();
|
|
19
22
|
this.botName = botName;
|
|
20
23
|
this.storagePath = path ?? 'storage';
|
|
@@ -67,8 +70,11 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
67
70
|
return (this.cache.get(key) ?? {}) as Record<number, TActionState>;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
private async save
|
|
71
|
-
|
|
73
|
+
private async save<TActionState extends IActionState>(
|
|
74
|
+
data: Record<number, TActionState>,
|
|
75
|
+
key: ActionKey
|
|
76
|
+
) {
|
|
77
|
+
this.cache.set(key, data);
|
|
72
78
|
|
|
73
79
|
const targetPath = this.buidPathFromKey(key);
|
|
74
80
|
const folderName = dirname(targetPath);
|
|
@@ -93,7 +99,10 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
93
99
|
});
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
async saveMetadata(
|
|
102
|
+
async saveMetadata(
|
|
103
|
+
actions: IActionWithState<IActionState>[],
|
|
104
|
+
botName: string
|
|
105
|
+
) {
|
|
97
106
|
const targetPath = this.buidPathFromKey(
|
|
98
107
|
`Metadata-${botName}` as ActionKey
|
|
99
108
|
);
|
|
@@ -104,7 +113,7 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
async getActionState<TActionState extends IActionState>(
|
|
107
|
-
action: IActionWithState
|
|
116
|
+
action: IActionWithState<TActionState>,
|
|
108
117
|
chatId: number
|
|
109
118
|
) {
|
|
110
119
|
return await this.lock(action.key, async () => {
|
|
@@ -117,13 +126,13 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
117
126
|
});
|
|
118
127
|
}
|
|
119
128
|
|
|
120
|
-
async saveActionExecutionResult(
|
|
121
|
-
action: IActionWithState
|
|
129
|
+
async saveActionExecutionResult<TActionState extends IActionState>(
|
|
130
|
+
action: IActionWithState<TActionState>,
|
|
122
131
|
chatId: number,
|
|
123
|
-
transactionResult: ActionExecutionResult
|
|
132
|
+
transactionResult: ActionExecutionResult<TActionState>
|
|
124
133
|
) {
|
|
125
134
|
await this.lock(action.key, async () => {
|
|
126
|
-
const data = await this.loadInternal(action.key);
|
|
135
|
+
const data = await this.loadInternal<TActionState>(action.key);
|
|
127
136
|
|
|
128
137
|
if (transactionResult.shouldUpdate) {
|
|
129
138
|
data[chatId] = transactionResult.data;
|
|
@@ -139,16 +148,16 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
139
148
|
}
|
|
140
149
|
|
|
141
150
|
async updateStateFor<TActionState extends IActionState>(
|
|
142
|
-
action: IActionWithState
|
|
151
|
+
action: IActionWithState<TActionState>,
|
|
143
152
|
chatId: number,
|
|
144
153
|
update: (state: TActionState) => Promise<void>
|
|
145
154
|
) {
|
|
146
155
|
await this.lock(action.key, async () => {
|
|
147
|
-
const data = await this.loadInternal(action.key);
|
|
156
|
+
const data = await this.loadInternal<TActionState>(action.key);
|
|
148
157
|
const state = Object.assign(
|
|
149
158
|
action.stateConstructor(),
|
|
150
159
|
data[chatId]
|
|
151
|
-
)
|
|
160
|
+
);
|
|
152
161
|
|
|
153
162
|
await update(state);
|
|
154
163
|
|
package/services/telegramApi.ts
CHANGED
|
@@ -4,13 +4,8 @@ import { MessageContext } from '../entities/context/messageContext';
|
|
|
4
4
|
import { inverseRecord as inverseRecord } from '../helpers/inverseRecord';
|
|
5
5
|
import { IStorageClient } from '../types/storage';
|
|
6
6
|
import { Logger } from './logger';
|
|
7
|
-
import { Reaction } from '../entities/responses/reaction';
|
|
8
7
|
import { IncomingMessage } from '../entities/incomingMessage';
|
|
9
8
|
import { BotResponse, IReplyMessage } from '../types/response';
|
|
10
|
-
import { UnpinResponse } from '../entities/responses/unpin';
|
|
11
|
-
import { TextMessage } from '../entities/responses/textMessage';
|
|
12
|
-
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
13
|
-
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
14
9
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
15
10
|
import { setTimeout } from 'timers/promises';
|
|
16
11
|
import { Milliseconds } from '../types/timeValues';
|
|
@@ -28,7 +23,6 @@ export class TelegramApiService {
|
|
|
28
23
|
telegram: Telegram;
|
|
29
24
|
chats: Record<number, string>;
|
|
30
25
|
storage: IStorageClient;
|
|
31
|
-
interactions: IBotApiInteractions;
|
|
32
26
|
|
|
33
27
|
constructor(
|
|
34
28
|
botName: string,
|
|
@@ -40,12 +34,12 @@ export class TelegramApiService {
|
|
|
40
34
|
this.botName = botName;
|
|
41
35
|
this.chats = inverseRecord(chats);
|
|
42
36
|
this.storage = storage;
|
|
37
|
+
}
|
|
43
38
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
} as IBotApiInteractions;
|
|
39
|
+
enqueueBatchedResponses(responses: BotResponse[]) {
|
|
40
|
+
for (const response of responses) {
|
|
41
|
+
this.messageQueue.push(response);
|
|
42
|
+
}
|
|
49
43
|
}
|
|
50
44
|
|
|
51
45
|
async flushResponses() {
|
|
@@ -171,10 +165,6 @@ export class TelegramApiService {
|
|
|
171
165
|
}
|
|
172
166
|
}
|
|
173
167
|
|
|
174
|
-
private enqueue(response: BotResponse) {
|
|
175
|
-
this.messageQueue.push(response);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
168
|
initializeContextForMessage<TActionState extends IActionState>(
|
|
179
169
|
ctx: MessageContext<TActionState>,
|
|
180
170
|
incomingMessage: IncomingMessage,
|
|
@@ -183,7 +173,6 @@ export class TelegramApiService {
|
|
|
183
173
|
return ctx.initializeMessageContext(
|
|
184
174
|
this.botName,
|
|
185
175
|
command,
|
|
186
|
-
this.interactions,
|
|
187
176
|
incomingMessage,
|
|
188
177
|
this.storage
|
|
189
178
|
);
|
|
@@ -197,7 +186,6 @@ export class TelegramApiService {
|
|
|
197
186
|
return ctx.initializeChatContext(
|
|
198
187
|
this.botName,
|
|
199
188
|
scheduledAction,
|
|
200
|
-
this.interactions,
|
|
201
189
|
chatId,
|
|
202
190
|
this.chats[chatId],
|
|
203
191
|
`Scheduled:${scheduledAction.key}:${chatId}`,
|
|
@@ -205,9 +193,3 @@ export class TelegramApiService {
|
|
|
205
193
|
);
|
|
206
194
|
}
|
|
207
195
|
}
|
|
208
|
-
|
|
209
|
-
export interface IBotApiInteractions {
|
|
210
|
-
respond: (response: TextMessage | VideoMessage | ImageMessage) => void;
|
|
211
|
-
react: (reaction: Reaction) => void;
|
|
212
|
-
unpin: (unpinMessage: UnpinResponse) => void;
|
|
213
|
-
}
|
package/types/actionWithState.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { IActionState } from './actionState';
|
|
|
2
2
|
|
|
3
3
|
export type ActionKey = string & { __brand: 'actionKey' };
|
|
4
4
|
|
|
5
|
-
export interface IActionWithState {
|
|
5
|
+
export interface IActionWithState<TActionState extends IActionState> {
|
|
6
6
|
key: ActionKey;
|
|
7
|
-
stateConstructor: () =>
|
|
7
|
+
stateConstructor: () => TActionState;
|
|
8
8
|
}
|
package/types/response.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { DelayResponse } from '../entities/responses/delay';
|
|
1
2
|
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
2
3
|
import { Reaction } from '../entities/responses/reaction';
|
|
3
4
|
import { TextMessage } from '../entities/responses/textMessage';
|
|
4
5
|
import { UnpinResponse } from '../entities/responses/unpin';
|
|
5
6
|
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
7
|
+
import { IActionState } from './actionState';
|
|
6
8
|
import { IActionWithState } from './actionWithState';
|
|
7
9
|
|
|
8
10
|
export const BotResponseTypes = {
|
|
@@ -10,7 +12,8 @@ export const BotResponseTypes = {
|
|
|
10
12
|
text: 'text',
|
|
11
13
|
image: 'image',
|
|
12
14
|
video: 'video',
|
|
13
|
-
react: 'react'
|
|
15
|
+
react: 'react',
|
|
16
|
+
delay: 'delay'
|
|
14
17
|
} as const;
|
|
15
18
|
|
|
16
19
|
export type BotResponse =
|
|
@@ -18,6 +21,7 @@ export type BotResponse =
|
|
|
18
21
|
| Reaction
|
|
19
22
|
| TextMessage
|
|
20
23
|
| VideoMessage
|
|
24
|
+
| DelayResponse
|
|
21
25
|
| ImageMessage;
|
|
22
26
|
|
|
23
27
|
export interface IChatResponse {
|
|
@@ -25,7 +29,7 @@ export interface IChatResponse {
|
|
|
25
29
|
chatId: number;
|
|
26
30
|
traceId: number | string;
|
|
27
31
|
|
|
28
|
-
action: IActionWithState
|
|
32
|
+
action: IActionWithState<IActionState>;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
export interface IReplyMessage<TType> extends IChatResponse {
|
package/types/storage.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ActionKey, IActionWithState } from './actionWithState';
|
|
|
4
4
|
|
|
5
5
|
export interface IStorageClient {
|
|
6
6
|
updateStateFor<TActionState extends IActionState>(
|
|
7
|
-
action: IActionWithState
|
|
7
|
+
action: IActionWithState<TActionState>,
|
|
8
8
|
chatId: number,
|
|
9
9
|
update: (state: TActionState) => Promise<void>
|
|
10
10
|
): Promise<void>;
|
|
@@ -12,14 +12,17 @@ export interface IStorageClient {
|
|
|
12
12
|
load<TActionState extends IActionState>(
|
|
13
13
|
key: ActionKey
|
|
14
14
|
): Promise<Record<number, TActionState>>;
|
|
15
|
-
saveMetadata
|
|
15
|
+
saveMetadata<TActionState extends IActionState>(
|
|
16
|
+
actions: IActionWithState<TActionState>[],
|
|
17
|
+
botName: string
|
|
18
|
+
): Promise<void>;
|
|
16
19
|
getActionState<TActionState extends IActionState>(
|
|
17
|
-
action: IActionWithState
|
|
20
|
+
action: IActionWithState<TActionState>,
|
|
18
21
|
chatId: number
|
|
19
22
|
): Promise<TActionState>;
|
|
20
|
-
saveActionExecutionResult(
|
|
21
|
-
action: IActionWithState
|
|
23
|
+
saveActionExecutionResult<TActionState extends IActionState>(
|
|
24
|
+
action: IActionWithState<TActionState>,
|
|
22
25
|
chatId: number,
|
|
23
|
-
transactionResult: ActionExecutionResult
|
|
26
|
+
transactionResult: ActionExecutionResult<TActionState>
|
|
24
27
|
): Promise<void>;
|
|
25
28
|
}
|