chz-telegram-bot 0.3.30 → 0.4.0
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/README.md +9 -10
- package/bun.lock +630 -307
- package/dist/dtos/chatHistoryMessage.d.ts +1 -1
- package/dist/dtos/chatHistoryMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.d.ts +4 -4
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +1 -1
- package/dist/dtos/messageInfo.d.ts +4 -3
- package/dist/dtos/messageInfo.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.d.ts +1 -1
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
- package/dist/dtos/responses/inlineQueryResponse.d.ts +1 -1
- package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -1
- package/dist/dtos/responses/reaction.d.ts +1 -1
- package/dist/dtos/responses/reaction.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.d.ts +1 -1
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
- package/dist/entities/botInstance.d.ts +1 -1
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +2 -2
- package/dist/entities/context/inlineQueryContext.d.ts +1 -1
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.d.ts +2 -2
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/replyContext.d.ts +2 -2
- package/dist/entities/context/replyContext.d.ts.map +1 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +2 -2
- package/dist/services/actionProcessingService.d.ts +2 -2
- package/dist/services/actionProcessingService.d.ts.map +1 -1
- package/dist/services/actionProcessingService.js +13 -11
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +7 -7
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +2 -2
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +4 -4
- package/dist/services/jsonLogger.d.ts +1 -1
- package/dist/services/jsonLogger.d.ts.map +1 -1
- package/dist/services/jsonLogger.js +33 -22
- package/dist/services/telegramApi.d.ts +2 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +19 -18
- package/dist/types/inputFile.d.ts +5 -0
- package/dist/types/inputFile.d.ts.map +1 -0
- package/dist/types/inputFile.js +2 -0
- package/dist/types/logger.d.ts +2 -2
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/messageTypes.d.ts +0 -2
- package/dist/types/messageTypes.d.ts.map +1 -1
- package/dtos/chatHistoryMessage.ts +1 -1
- package/dtos/incomingMessage.ts +7 -13
- package/dtos/messageInfo.ts +3 -5
- package/dtos/responses/imageMessage.ts +1 -1
- package/dtos/responses/inlineQueryResponse.ts +1 -1
- package/dtos/responses/reaction.ts +1 -1
- package/dtos/responses/videoMessage.ts +1 -1
- package/entities/botInstance.ts +2 -2
- package/entities/context/inlineQueryContext.ts +1 -1
- package/entities/context/messageContext.ts +2 -2
- package/entities/context/replyContext.ts +2 -2
- package/main.ts +2 -2
- package/package.json +3 -7
- package/services/actionProcessingService.ts +13 -13
- package/services/actionProcessors/commandActionProcessor.ts +15 -16
- package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -10
- package/services/jsonLogger.ts +42 -27
- package/services/telegramApi.ts +30 -31
- package/types/inputFile.ts +4 -0
- package/types/logger.ts +2 -6
- package/types/messageTypes.ts +0 -4
package/services/jsonLogger.ts
CHANGED
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
1
|
import { ILogger, IScopedLogger } from '../types/logger';
|
|
4
2
|
import { TraceId } from '../types/trace';
|
|
5
3
|
|
|
6
4
|
export class JsonLogger implements ILogger {
|
|
7
|
-
private serializeError(error:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
private serializeError(error: unknown): string {
|
|
6
|
+
if (error instanceof Error) {
|
|
7
|
+
const plainObject: Record<string, unknown> = {
|
|
8
|
+
name: error.name,
|
|
9
|
+
message: error.message,
|
|
10
|
+
stack: error.stack
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
for (const [key, value] of Object.entries(error)) {
|
|
14
|
+
plainObject[key] = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return JSON.stringify(plainObject);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return JSON.stringify({ error });
|
|
13
21
|
}
|
|
14
22
|
|
|
15
23
|
private getCircularReplacer() {
|
|
16
|
-
const
|
|
17
|
-
return
|
|
18
|
-
if (typeof value
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
ancestors.pop();
|
|
25
|
-
}
|
|
26
|
-
if (ancestors.includes(value)) {
|
|
27
|
-
return '[Circular]';
|
|
24
|
+
const cache = new Set();
|
|
25
|
+
return <T>(_: string, value: T) => {
|
|
26
|
+
if (typeof value === 'object' && value !== null) {
|
|
27
|
+
if (cache.has(value)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
cache.add(value);
|
|
28
32
|
}
|
|
29
|
-
ancestors.push(value);
|
|
30
33
|
return value;
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
createScope(botName: string, traceId: TraceId, chatName: string) {
|
|
35
38
|
return {
|
|
36
|
-
logObjectWithTraceId: (data:
|
|
39
|
+
logObjectWithTraceId: (data: unknown) => {
|
|
37
40
|
this.logObjectWithTraceId(botName, traceId, chatName, data);
|
|
38
41
|
},
|
|
39
42
|
logWithTraceId: (text: string) => {
|
|
@@ -55,12 +58,24 @@ export class JsonLogger implements ILogger {
|
|
|
55
58
|
botName: string,
|
|
56
59
|
traceId: TraceId,
|
|
57
60
|
chatName: string,
|
|
58
|
-
data:
|
|
61
|
+
data: unknown
|
|
59
62
|
) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
const enrichedData =
|
|
64
|
+
typeof data == 'object'
|
|
65
|
+
? {
|
|
66
|
+
...data,
|
|
67
|
+
botName,
|
|
68
|
+
traceId,
|
|
69
|
+
chatName
|
|
70
|
+
}
|
|
71
|
+
: {
|
|
72
|
+
botName,
|
|
73
|
+
traceId,
|
|
74
|
+
chatName,
|
|
75
|
+
data
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
console.log(enrichedData);
|
|
64
79
|
}
|
|
65
80
|
|
|
66
81
|
logWithTraceId(
|
package/services/telegramApi.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { Message } from 'telegraf/types';
|
|
2
1
|
import { IStorageClient } from '../types/storage';
|
|
3
2
|
import { BotResponse, IReplyResponse } from '../types/response';
|
|
4
|
-
import { Telegram } from 'telegraf/typings/telegram';
|
|
5
3
|
import { ILogger } from '../types/logger';
|
|
6
4
|
import { QueueItem, ResponseProcessingQueue } from './responseProcessingQueue';
|
|
7
5
|
import { IReplyCapture } from '../types/capture';
|
|
@@ -9,13 +7,14 @@ import { IActionWithState } from '../types/action';
|
|
|
9
7
|
import { IActionState } from '../types/actionState';
|
|
10
8
|
import { TraceId } from '../types/trace';
|
|
11
9
|
import { ChatInfo } from '../dtos/chatInfo';
|
|
12
|
-
import {
|
|
10
|
+
import TelegramBot, { Message } from 'node-telegram-bot-api';
|
|
11
|
+
import { createReadStream } from 'fs';
|
|
13
12
|
|
|
14
13
|
export const TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
|
|
15
14
|
|
|
16
15
|
export class TelegramApiService {
|
|
17
16
|
private readonly queue = new ResponseProcessingQueue();
|
|
18
|
-
private readonly telegram:
|
|
17
|
+
private readonly telegram: TelegramBot;
|
|
19
18
|
private readonly storage: IStorageClient;
|
|
20
19
|
private readonly logger: ILogger;
|
|
21
20
|
private readonly captureRegistrationCallback: (
|
|
@@ -29,7 +28,7 @@ export class TelegramApiService {
|
|
|
29
28
|
|
|
30
29
|
constructor(
|
|
31
30
|
botName: string,
|
|
32
|
-
telegram:
|
|
31
|
+
telegram: TelegramBot,
|
|
33
32
|
storage: IStorageClient,
|
|
34
33
|
logger: ILogger,
|
|
35
34
|
captureRegistrationCallback: (
|
|
@@ -67,8 +66,10 @@ export class TelegramApiService {
|
|
|
67
66
|
try {
|
|
68
67
|
await this.processResponse(response);
|
|
69
68
|
} catch (error) {
|
|
70
|
-
if ('message' in (error as
|
|
71
|
-
const telegramResponse = error as
|
|
69
|
+
if ('message' in (error as { message?: string })) {
|
|
70
|
+
const telegramResponse = error as {
|
|
71
|
+
message: string;
|
|
72
|
+
};
|
|
72
73
|
|
|
73
74
|
if (
|
|
74
75
|
telegramResponse.message.includes(
|
|
@@ -104,11 +105,11 @@ export class TelegramApiService {
|
|
|
104
105
|
void this.queue.flushReadyItems();
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
private async pinIfShould(response: IReplyResponse,
|
|
108
|
+
private async pinIfShould(response: IReplyResponse, message: Message) {
|
|
108
109
|
if (response.shouldPin) {
|
|
109
110
|
await this.telegram.pinChatMessage(
|
|
110
111
|
response.chatInfo.id,
|
|
111
|
-
|
|
112
|
+
message.message_id,
|
|
112
113
|
{ disable_notification: true }
|
|
113
114
|
);
|
|
114
115
|
|
|
@@ -116,7 +117,7 @@ export class TelegramApiService {
|
|
|
116
117
|
response.action as IActionWithState<IActionState>,
|
|
117
118
|
response.chatInfo.id,
|
|
118
119
|
(state) => {
|
|
119
|
-
state.pinnedMessages.push(
|
|
120
|
+
state.pinnedMessages.push(message.message_id);
|
|
120
121
|
}
|
|
121
122
|
);
|
|
122
123
|
}
|
|
@@ -149,24 +150,22 @@ export class TelegramApiService {
|
|
|
149
150
|
case 'image':
|
|
150
151
|
sentMessage = await this.telegram.sendPhoto(
|
|
151
152
|
response.chatInfo.id,
|
|
152
|
-
response.content,
|
|
153
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
153
|
+
createReadStream(response.content.source),
|
|
154
154
|
response.replyInfo?.id
|
|
155
|
-
?
|
|
156
|
-
reply_to_message_id: response.replyInfo.id
|
|
157
|
-
}
|
|
155
|
+
? {
|
|
156
|
+
reply_to_message_id: response.replyInfo.id
|
|
157
|
+
}
|
|
158
158
|
: undefined
|
|
159
159
|
);
|
|
160
160
|
break;
|
|
161
161
|
case 'video':
|
|
162
162
|
sentMessage = await this.telegram.sendVideo(
|
|
163
163
|
response.chatInfo.id,
|
|
164
|
-
response.content,
|
|
165
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
164
|
+
createReadStream(response.content.source),
|
|
166
165
|
response.replyInfo?.id
|
|
167
|
-
?
|
|
168
|
-
reply_to_message_id: response.replyInfo.id
|
|
169
|
-
}
|
|
166
|
+
? {
|
|
167
|
+
reply_to_message_id: response.replyInfo.id
|
|
168
|
+
}
|
|
170
169
|
: undefined
|
|
171
170
|
);
|
|
172
171
|
break;
|
|
@@ -174,21 +173,21 @@ export class TelegramApiService {
|
|
|
174
173
|
await this.telegram.setMessageReaction(
|
|
175
174
|
response.chatInfo.id,
|
|
176
175
|
response.messageId,
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
176
|
+
{
|
|
177
|
+
reaction: [
|
|
178
|
+
{
|
|
179
|
+
type: 'emoji',
|
|
180
|
+
emoji: response.emoji
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
}
|
|
184
184
|
);
|
|
185
185
|
|
|
186
186
|
return;
|
|
187
187
|
case 'unpin':
|
|
188
|
-
await this.telegram.unpinChatMessage(
|
|
189
|
-
response.
|
|
190
|
-
|
|
191
|
-
);
|
|
188
|
+
await this.telegram.unpinChatMessage(response.chatInfo.id, {
|
|
189
|
+
message_id: response.messageId
|
|
190
|
+
});
|
|
192
191
|
|
|
193
192
|
await this.storage.updateStateFor(
|
|
194
193
|
response.action,
|
package/types/logger.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { TraceId } from './trace';
|
|
2
2
|
|
|
3
3
|
export interface IScopedLogger {
|
|
4
|
-
logObjectWithTraceId(
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
-
data: any
|
|
7
|
-
): void;
|
|
4
|
+
logObjectWithTraceId(data: unknown): void;
|
|
8
5
|
|
|
9
6
|
logWithTraceId(text: string): void;
|
|
10
7
|
|
|
@@ -22,8 +19,7 @@ export interface ILogger {
|
|
|
22
19
|
botName: string,
|
|
23
20
|
traceId: TraceId,
|
|
24
21
|
chatName: string,
|
|
25
|
-
|
|
26
|
-
data: any
|
|
22
|
+
data: unknown
|
|
27
23
|
): void;
|
|
28
24
|
|
|
29
25
|
logWithTraceId(
|
package/types/messageTypes.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Message, Update } from 'telegraf/types';
|
|
2
|
-
|
|
3
1
|
export const INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
|
|
4
2
|
|
|
5
3
|
export const MessageType = {
|
|
@@ -21,5 +19,3 @@ export const MessageType = {
|
|
|
21
19
|
} as const;
|
|
22
20
|
|
|
23
21
|
export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
|
|
24
|
-
|
|
25
|
-
export type TelegrafContextMessage = Update.New & (Update.NonChannel & Message);
|