chz-telegram-bot 0.3.10 → 0.3.12
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/builtin/helpAction.ts +1 -1
- package/dist/builtin/helpAction.js +1 -1
- package/dist/dtos/chatInfo.js +4 -0
- package/dist/dtos/commandTriggerCheckResult.js +3 -0
- package/dist/dtos/incomingMessage.js +9 -1
- package/dist/dtos/incomingQuery.js +5 -0
- package/dist/dtos/replyInfo.js +2 -0
- package/dist/dtos/responses/delay.js +6 -2
- package/dist/dtos/responses/imageMessage.js +10 -4
- package/dist/dtos/responses/inlineQueryResponse.js +6 -2
- package/dist/dtos/responses/reaction.js +7 -2
- package/dist/dtos/responses/textMessage.js +10 -3
- package/dist/dtos/responses/unpin.js +6 -2
- package/dist/dtos/responses/videoMessage.js +10 -4
- package/dist/entities/actions/commandAction.d.ts +4 -1
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +46 -17
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/inlineQueryAction.js +8 -1
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.js +8 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +13 -14
- package/dist/entities/botInstance.js +6 -1
- package/dist/entities/cachedStateFactory.js +2 -0
- package/dist/entities/context/baseContext.js +14 -2
- package/dist/entities/context/chatContext.d.ts +0 -3
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +38 -41
- package/dist/entities/context/inlineQueryContext.d.ts +0 -3
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/inlineQueryContext.js +12 -6
- package/dist/entities/context/messageContext.d.ts +0 -3
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +77 -68
- package/dist/entities/context/replyContext.d.ts +0 -3
- package/dist/entities/context/replyContext.d.ts.map +1 -1
- package/dist/entities/context/replyContext.js +90 -65
- package/dist/entities/states/actionStateBase.js +2 -4
- package/dist/entities/taskRecord.js +3 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts +3 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +18 -9
- package/dist/helpers/builders/inlineQueryActionBuilder.js +7 -6
- package/dist/helpers/builders/scheduledActionBuilder.js +7 -5
- package/dist/helpers/mapUtils.d.ts +3 -0
- package/dist/helpers/mapUtils.d.ts.map +1 -0
- package/dist/helpers/mapUtils.js +17 -0
- package/dist/helpers/noop.d.ts +3 -4
- package/dist/helpers/noop.d.ts.map +1 -1
- package/dist/helpers/noop.js +2 -1
- package/dist/helpers/traceFactory.d.ts +1 -1
- package/dist/helpers/traceFactory.d.ts.map +1 -1
- package/dist/main.js +1 -3
- package/dist/services/actionProcessingService.d.ts.map +1 -1
- package/dist/services/actionProcessingService.js +15 -3
- package/dist/services/actionProcessors/baseProcessor.d.ts +1 -3
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/baseProcessor.js +6 -2
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +0 -4
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +10 -13
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +0 -4
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +4 -6
- package/dist/services/actionProcessors/scheduledActionProcessor.js +6 -4
- package/dist/services/jsonFileStorage.d.ts +4 -3
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +22 -14
- package/dist/services/jsonLogger.d.ts +1 -1
- package/dist/services/jsonLogger.d.ts.map +1 -1
- package/dist/services/jsonLogger.js +9 -3
- package/dist/services/nodeTimeoutScheduler.d.ts +2 -2
- package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -1
- package/dist/services/nodeTimeoutScheduler.js +6 -5
- package/dist/services/responseProcessingQueue.d.ts.map +1 -1
- package/dist/services/responseProcessingQueue.js +9 -7
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +17 -8
- package/dist/types/commandTrigger.d.ts +1 -2
- package/dist/types/commandTrigger.d.ts.map +1 -1
- package/dist/types/handlers.d.ts +1 -1
- package/dist/types/handlers.d.ts.map +1 -1
- package/dist/types/logger.d.ts +2 -2
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/scheduler.d.ts +2 -2
- package/dist/types/scheduler.d.ts.map +1 -1
- package/dist/types/storage.d.ts +2 -2
- package/dist/types/storage.d.ts.map +1 -1
- package/dist/types/timeValues.d.ts +3 -3
- package/dist/types/timeValues.d.ts.map +1 -1
- package/dist/types/trace.d.ts +1 -1
- package/dist/types/trace.d.ts.map +1 -1
- package/dtos/incomingMessage.ts +1 -1
- package/entities/actions/commandAction.ts +55 -28
- package/entities/actions/inlineQueryAction.ts +5 -1
- package/entities/actions/replyCaptureAction.ts +5 -1
- package/entities/actions/scheduledAction.ts +11 -13
- package/entities/botInstance.ts +7 -7
- package/entities/context/chatContext.ts +0 -6
- package/entities/context/inlineQueryContext.ts +0 -6
- package/entities/context/messageContext.ts +0 -6
- package/entities/context/replyContext.ts +18 -15
- package/eslint.config.js +26 -1
- package/helpers/builders/commandActionBuilder.ts +10 -0
- package/helpers/mapUtils.ts +15 -0
- package/helpers/noop.ts +4 -4
- package/helpers/traceFactory.ts +2 -2
- package/package.json +1 -1
- package/services/actionProcessingService.ts +8 -4
- package/services/actionProcessors/baseProcessor.ts +5 -8
- package/services/actionProcessors/commandActionProcessor.ts +5 -17
- package/services/actionProcessors/inlineQueryActionProcessor.ts +5 -15
- package/services/actionProcessors/scheduledActionProcessor.ts +4 -4
- package/services/jsonFileStorage.ts +39 -21
- package/services/jsonLogger.ts +12 -8
- package/services/nodeTimeoutScheduler.ts +6 -6
- package/services/responseProcessingQueue.ts +9 -4
- package/services/telegramApi.ts +7 -5
- package/tsconfig.json +1 -1
- package/types/commandTrigger.ts +1 -3
- package/types/handlers.ts +1 -1
- package/types/logger.ts +3 -6
- package/types/scheduler.ts +2 -2
- package/types/storage.ts +2 -2
- package/types/timeValues.ts +3 -3
- package/types/trace.ts +3 -1
|
@@ -6,6 +6,10 @@ export type QueueItem = {
|
|
|
6
6
|
callback: () => Promise<void>;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
function notEmpty<T>(arr: T[]): arr is [T, ...T[]] {
|
|
10
|
+
return arr.length > 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
const TELEGRAM_RATELIMIT_DELAY = 35 as Milliseconds;
|
|
10
14
|
|
|
11
15
|
export class ResponseProcessingQueue {
|
|
@@ -16,7 +20,7 @@ export class ResponseProcessingQueue {
|
|
|
16
20
|
enqueue(item: QueueItem) {
|
|
17
21
|
if (
|
|
18
22
|
this.items.length === 0 ||
|
|
19
|
-
item.priority >= this.items[this.items.length - 1]
|
|
23
|
+
item.priority >= this.items[this.items.length - 1].priority
|
|
20
24
|
) {
|
|
21
25
|
this.items.push(item);
|
|
22
26
|
return;
|
|
@@ -25,7 +29,7 @@ export class ResponseProcessingQueue {
|
|
|
25
29
|
let insertIndex = this.items.length;
|
|
26
30
|
while (
|
|
27
31
|
insertIndex > 0 &&
|
|
28
|
-
this.items[insertIndex - 1]
|
|
32
|
+
this.items[insertIndex - 1].priority > item.priority
|
|
29
33
|
) {
|
|
30
34
|
insertIndex--;
|
|
31
35
|
}
|
|
@@ -37,11 +41,12 @@ export class ResponseProcessingQueue {
|
|
|
37
41
|
|
|
38
42
|
this.isFlushing = true;
|
|
39
43
|
|
|
40
|
-
while (this.items
|
|
44
|
+
while (notEmpty(this.items)) {
|
|
41
45
|
if (Date.now() >= this.items[0].priority) {
|
|
42
46
|
await this.rateLimiter();
|
|
43
47
|
|
|
44
|
-
const item = this.items
|
|
48
|
+
const [item] = this.items;
|
|
49
|
+
this.items.shift();
|
|
45
50
|
|
|
46
51
|
await item.callback();
|
|
47
52
|
}
|
package/services/telegramApi.ts
CHANGED
|
@@ -101,7 +101,7 @@ export class TelegramApiService {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
flushResponses() {
|
|
104
|
-
this.queue.flushReadyItems();
|
|
104
|
+
void this.queue.flushReadyItems();
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
private async pinIfShould(response: IReplyResponse, sentMessage: Message) {
|
|
@@ -115,7 +115,7 @@ export class TelegramApiService {
|
|
|
115
115
|
await this.storage.updateStateFor(
|
|
116
116
|
response.action as IActionWithState<IActionState>,
|
|
117
117
|
response.chatInfo.id,
|
|
118
|
-
|
|
118
|
+
(state) => {
|
|
119
119
|
state.pinnedMessages.push(sentMessage.message_id);
|
|
120
120
|
}
|
|
121
121
|
);
|
|
@@ -150,9 +150,10 @@ export class TelegramApiService {
|
|
|
150
150
|
sentMessage = await this.telegram.sendPhoto(
|
|
151
151
|
response.chatInfo.id,
|
|
152
152
|
response.content,
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
153
154
|
response.replyInfo?.id
|
|
154
155
|
? ({
|
|
155
|
-
reply_to_message_id: response.replyInfo
|
|
156
|
+
reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
156
157
|
} as any)
|
|
157
158
|
: undefined
|
|
158
159
|
);
|
|
@@ -161,9 +162,10 @@ export class TelegramApiService {
|
|
|
161
162
|
sentMessage = await this.telegram.sendVideo(
|
|
162
163
|
response.chatInfo.id,
|
|
163
164
|
response.content,
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
164
166
|
response.replyInfo?.id
|
|
165
167
|
? ({
|
|
166
|
-
reply_to_message_id: response.replyInfo
|
|
168
|
+
reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
167
169
|
} as any)
|
|
168
170
|
: undefined
|
|
169
171
|
);
|
|
@@ -191,7 +193,7 @@ export class TelegramApiService {
|
|
|
191
193
|
await this.storage.updateStateFor(
|
|
192
194
|
response.action,
|
|
193
195
|
response.chatInfo.id,
|
|
194
|
-
|
|
196
|
+
(state) => {
|
|
195
197
|
state.pinnedMessages = state.pinnedMessages.filter(
|
|
196
198
|
(x) => x != response.messageId
|
|
197
199
|
);
|
package/tsconfig.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
12
|
|
|
13
13
|
/* Language and Environment */
|
|
14
|
-
"target": "
|
|
14
|
+
"target": "es2024" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
15
15
|
"lib": [
|
|
16
16
|
"es2021",
|
|
17
17
|
"DOM",
|
package/types/commandTrigger.ts
CHANGED
package/types/handlers.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type CommandHandler<TActionState extends IActionState> = (
|
|
|
14
14
|
ctx: MessageContext<TActionState>,
|
|
15
15
|
/** State of an action being executed. */
|
|
16
16
|
state: TActionState
|
|
17
|
-
) => Promise<void
|
|
17
|
+
) => Promise<void> | void;
|
|
18
18
|
|
|
19
19
|
export type ScheduledHandler<TActionState extends IActionState> = (
|
|
20
20
|
/** Context of action executed in chat. */
|
package/types/logger.ts
CHANGED
|
@@ -8,10 +8,7 @@ export interface IScopedLogger {
|
|
|
8
8
|
|
|
9
9
|
logWithTraceId(text: string): void;
|
|
10
10
|
|
|
11
|
-
errorWithTraceId
|
|
12
|
-
errorObj: unknown,
|
|
13
|
-
extraData?: TData | undefined
|
|
14
|
-
): void;
|
|
11
|
+
errorWithTraceId(errorObj: unknown, extraData?: unknown): void;
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
export interface ILogger {
|
|
@@ -36,11 +33,11 @@ export interface ILogger {
|
|
|
36
33
|
text: string
|
|
37
34
|
): void;
|
|
38
35
|
|
|
39
|
-
errorWithTraceId
|
|
36
|
+
errorWithTraceId(
|
|
40
37
|
botName: string,
|
|
41
38
|
traceId: TraceId,
|
|
42
39
|
chatName: string,
|
|
43
40
|
errorObj: unknown,
|
|
44
|
-
extraData?:
|
|
41
|
+
extraData?: unknown
|
|
45
42
|
): void;
|
|
46
43
|
}
|
package/types/scheduler.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface IScheduler {
|
|
|
5
5
|
|
|
6
6
|
createTask(
|
|
7
7
|
name: string,
|
|
8
|
-
action: () =>
|
|
8
|
+
action: () => unknown,
|
|
9
9
|
interval: Milliseconds,
|
|
10
10
|
executeRightAway: boolean,
|
|
11
11
|
ownerName: string
|
|
@@ -13,7 +13,7 @@ export interface IScheduler {
|
|
|
13
13
|
|
|
14
14
|
createOnetimeTask(
|
|
15
15
|
name: string,
|
|
16
|
-
action: () =>
|
|
16
|
+
action: () => unknown,
|
|
17
17
|
delay: Milliseconds,
|
|
18
18
|
ownerName: string
|
|
19
19
|
): void;
|
package/types/storage.ts
CHANGED
|
@@ -5,12 +5,12 @@ export interface IStorageClient {
|
|
|
5
5
|
updateStateFor<TActionState extends IActionState>(
|
|
6
6
|
action: IActionWithState<TActionState>,
|
|
7
7
|
chatId: number,
|
|
8
|
-
update: (state: TActionState) => Promise<void>
|
|
8
|
+
update: (state: TActionState) => Promise<void> | void
|
|
9
9
|
): Promise<void>;
|
|
10
10
|
close(): Promise<void>;
|
|
11
11
|
load<TActionState extends IActionState>(
|
|
12
12
|
key: ActionKey
|
|
13
|
-
): Promise<Record<number, TActionState>>;
|
|
13
|
+
): Promise<Record<number, TActionState | undefined>>;
|
|
14
14
|
saveMetadata<TActionState extends IActionState>(
|
|
15
15
|
actions: IActionWithState<TActionState>[]
|
|
16
16
|
): Promise<void>;
|
package/types/timeValues.ts
CHANGED
|
@@ -2,9 +2,9 @@ declare const millisecondsSymbol: unique symbol;
|
|
|
2
2
|
declare const secondsSymbol: unique symbol;
|
|
3
3
|
declare const hoursSymbol: unique symbol;
|
|
4
4
|
|
|
5
|
-
export type Milliseconds = number & { [millisecondsSymbol]:
|
|
6
|
-
export type Seconds = number & { [secondsSymbol]:
|
|
7
|
-
export type Hours = number & { [hoursSymbol]:
|
|
5
|
+
export type Milliseconds = number & { [millisecondsSymbol]: never };
|
|
6
|
+
export type Seconds = number & { [secondsSymbol]: never };
|
|
7
|
+
export type Hours = number & { [hoursSymbol]: never };
|
|
8
8
|
|
|
9
9
|
export type HoursOfDay =
|
|
10
10
|
| 0
|
package/types/trace.ts
CHANGED