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.
Files changed (72) hide show
  1. package/README.md +9 -10
  2. package/bun.lock +630 -307
  3. package/dist/dtos/chatHistoryMessage.d.ts +1 -1
  4. package/dist/dtos/chatHistoryMessage.d.ts.map +1 -1
  5. package/dist/dtos/incomingMessage.d.ts +4 -4
  6. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  7. package/dist/dtos/incomingMessage.js +1 -1
  8. package/dist/dtos/messageInfo.d.ts +4 -3
  9. package/dist/dtos/messageInfo.d.ts.map +1 -1
  10. package/dist/dtos/responses/imageMessage.d.ts +1 -1
  11. package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
  12. package/dist/dtos/responses/inlineQueryResponse.d.ts +1 -1
  13. package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -1
  14. package/dist/dtos/responses/reaction.d.ts +1 -1
  15. package/dist/dtos/responses/reaction.d.ts.map +1 -1
  16. package/dist/dtos/responses/videoMessage.d.ts +1 -1
  17. package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
  18. package/dist/entities/botInstance.d.ts +1 -1
  19. package/dist/entities/botInstance.d.ts.map +1 -1
  20. package/dist/entities/botInstance.js +2 -2
  21. package/dist/entities/context/inlineQueryContext.d.ts +1 -1
  22. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  23. package/dist/entities/context/messageContext.d.ts +2 -2
  24. package/dist/entities/context/messageContext.d.ts.map +1 -1
  25. package/dist/entities/context/replyContext.d.ts +2 -2
  26. package/dist/entities/context/replyContext.d.ts.map +1 -1
  27. package/dist/main.d.ts +1 -1
  28. package/dist/main.d.ts.map +1 -1
  29. package/dist/main.js +2 -2
  30. package/dist/services/actionProcessingService.d.ts +2 -2
  31. package/dist/services/actionProcessingService.d.ts.map +1 -1
  32. package/dist/services/actionProcessingService.js +13 -11
  33. package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
  34. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  35. package/dist/services/actionProcessors/commandActionProcessor.js +7 -7
  36. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +2 -2
  37. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
  38. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +4 -4
  39. package/dist/services/jsonLogger.d.ts +1 -1
  40. package/dist/services/jsonLogger.d.ts.map +1 -1
  41. package/dist/services/jsonLogger.js +33 -22
  42. package/dist/services/telegramApi.d.ts +2 -2
  43. package/dist/services/telegramApi.d.ts.map +1 -1
  44. package/dist/services/telegramApi.js +19 -18
  45. package/dist/types/inputFile.d.ts +5 -0
  46. package/dist/types/inputFile.d.ts.map +1 -0
  47. package/dist/types/inputFile.js +2 -0
  48. package/dist/types/logger.d.ts +2 -2
  49. package/dist/types/logger.d.ts.map +1 -1
  50. package/dist/types/messageTypes.d.ts +0 -2
  51. package/dist/types/messageTypes.d.ts.map +1 -1
  52. package/dtos/chatHistoryMessage.ts +1 -1
  53. package/dtos/incomingMessage.ts +7 -13
  54. package/dtos/messageInfo.ts +3 -5
  55. package/dtos/responses/imageMessage.ts +1 -1
  56. package/dtos/responses/inlineQueryResponse.ts +1 -1
  57. package/dtos/responses/reaction.ts +1 -1
  58. package/dtos/responses/videoMessage.ts +1 -1
  59. package/entities/botInstance.ts +2 -2
  60. package/entities/context/inlineQueryContext.ts +1 -1
  61. package/entities/context/messageContext.ts +2 -2
  62. package/entities/context/replyContext.ts +2 -2
  63. package/main.ts +2 -2
  64. package/package.json +3 -7
  65. package/services/actionProcessingService.ts +13 -13
  66. package/services/actionProcessors/commandActionProcessor.ts +15 -16
  67. package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -10
  68. package/services/jsonLogger.ts +42 -27
  69. package/services/telegramApi.ts +30 -31
  70. package/types/inputFile.ts +4 -0
  71. package/types/logger.ts +2 -6
  72. package/types/messageTypes.ts +0 -4
@@ -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: any) {
8
- const plainObject: Record<string, unknown> = {};
9
- Object.getOwnPropertyNames(error).forEach(function (key) {
10
- plainObject[key] = error[key];
11
- });
12
- return JSON.stringify(plainObject);
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 ancestors: unknown[] = [];
17
- return function <V>(this: V, _: unknown, value: V) {
18
- if (typeof value !== 'object' || value === null) {
19
- return value;
20
- }
21
- // `this` is the object that value is contained in,
22
- // i.e., its direct parent.
23
- while (ancestors.length > 0 && ancestors.at(-1) !== this) {
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: any) => {
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: any
61
+ data: unknown
59
62
  ) {
60
- data.botName = botName;
61
- data.traceId = traceId;
62
- data.chatName = chatName;
63
- console.log(data);
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(
@@ -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 { TelegramError } from 'telegraf';
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: 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: 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 TelegramError)) {
71
- const telegramResponse = error as TelegramError;
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, sentMessage: Message) {
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
- sentMessage.message_id,
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(sentMessage.message_id);
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
157
- } as any)
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
169
- } as any)
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
- type: 'emoji',
180
- emoji: response.emoji
181
- }
182
- ],
183
- true
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.chatInfo.id,
190
- response.messageId
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,
@@ -0,0 +1,4 @@
1
+ export interface InputFile {
2
+ source: string;
3
+ filename?: string;
4
+ }
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- data: any
22
+ data: unknown
27
23
  ): void;
28
24
 
29
25
  logWithTraceId(
@@ -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);