chz-telegram-bot 0.0.50 → 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.
Files changed (87) hide show
  1. package/dist/dtos/actionExecutionResult.d.ts +7 -0
  2. package/dist/dtos/actionExecutionResult.d.ts.map +1 -0
  3. package/dist/dtos/actionExecutionResult.js +10 -0
  4. package/dist/dtos/chatInfo.d.ts +8 -0
  5. package/dist/dtos/chatInfo.d.ts.map +1 -0
  6. package/dist/dtos/chatInfo.js +10 -0
  7. package/dist/dtos/commandTriggerCheckResult.d.ts +10 -0
  8. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
  9. package/dist/dtos/commandTriggerCheckResult.js +23 -0
  10. package/dist/dtos/incomingMessage.d.ts +14 -0
  11. package/dist/dtos/incomingMessage.d.ts.map +1 -0
  12. package/dist/dtos/incomingMessage.js +44 -0
  13. package/dist/dtos/responses/delay.d.ts +14 -0
  14. package/dist/dtos/responses/delay.d.ts.map +1 -0
  15. package/dist/dtos/responses/delay.js +14 -0
  16. package/dist/dtos/responses/imageMessage.d.ts +18 -0
  17. package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
  18. package/dist/dtos/responses/imageMessage.js +17 -0
  19. package/dist/dtos/responses/reaction.d.ts +15 -0
  20. package/dist/dtos/responses/reaction.d.ts.map +1 -0
  21. package/dist/dtos/responses/reaction.js +15 -0
  22. package/dist/dtos/responses/textMessage.d.ts +17 -0
  23. package/dist/dtos/responses/textMessage.d.ts.map +1 -0
  24. package/dist/dtos/responses/textMessage.js +17 -0
  25. package/dist/dtos/responses/unpin.d.ts +13 -0
  26. package/dist/dtos/responses/unpin.d.ts.map +1 -0
  27. package/dist/dtos/responses/unpin.js +14 -0
  28. package/dist/dtos/responses/videoMessage.d.ts +18 -0
  29. package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
  30. package/dist/dtos/responses/videoMessage.js +17 -0
  31. package/dist/entities/actions/commandAction.d.ts +10 -10
  32. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  33. package/dist/entities/actions/commandAction.js +6 -6
  34. package/dist/entities/actions/scheduledAction.d.ts +10 -10
  35. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  36. package/dist/entities/actions/scheduledAction.js +5 -5
  37. package/dist/entities/botInstance.d.ts +7 -7
  38. package/dist/entities/botInstance.d.ts.map +1 -1
  39. package/dist/entities/botInstance.js +8 -7
  40. package/dist/entities/cachedStateFactory.d.ts +2 -2
  41. package/dist/entities/cachedStateFactory.d.ts.map +1 -1
  42. package/dist/entities/context/chatContext.d.ts +4 -5
  43. package/dist/entities/context/chatContext.d.ts.map +1 -1
  44. package/dist/entities/context/chatContext.js +12 -13
  45. package/dist/entities/context/messageContext.d.ts +1 -1
  46. package/dist/entities/context/messageContext.d.ts.map +1 -1
  47. package/dist/entities/context/messageContext.js +10 -10
  48. package/dist/entities/taskRecord.d.ts +3 -3
  49. package/dist/entities/taskRecord.d.ts.map +1 -1
  50. package/dist/services/jsonFileStorage.d.ts +5 -5
  51. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  52. package/dist/services/taskScheduler.d.ts +1 -1
  53. package/dist/services/taskScheduler.d.ts.map +1 -1
  54. package/dist/services/telegramApi.d.ts +5 -14
  55. package/dist/services/telegramApi.d.ts.map +1 -1
  56. package/dist/services/telegramApi.js +10 -18
  57. package/dist/types/actionWithState.d.ts +2 -2
  58. package/dist/types/actionWithState.d.ts.map +1 -1
  59. package/dist/types/response.d.ts +15 -14
  60. package/dist/types/response.d.ts.map +1 -1
  61. package/dist/types/storage.d.ts +1 -1
  62. package/dist/types/storage.d.ts.map +1 -1
  63. package/{entities → dtos}/actionExecutionResult.ts +2 -2
  64. package/dtos/chatInfo.ts +11 -0
  65. package/{entities → dtos}/incomingMessage.ts +8 -7
  66. package/{entities → dtos}/responses/delay.ts +8 -7
  67. package/{entities → dtos}/responses/imageMessage.ts +11 -10
  68. package/{entities → dtos}/responses/reaction.ts +9 -8
  69. package/{entities → dtos}/responses/textMessage.ts +11 -10
  70. package/{entities → dtos}/responses/unpin.ts +8 -7
  71. package/{entities → dtos}/responses/videoMessage.ts +11 -10
  72. package/entities/actions/commandAction.ts +18 -17
  73. package/entities/actions/scheduledAction.ts +16 -16
  74. package/entities/botInstance.ts +26 -15
  75. package/entities/cachedStateFactory.ts +2 -2
  76. package/entities/context/chatContext.ts +20 -18
  77. package/entities/context/messageContext.ts +11 -12
  78. package/entities/taskRecord.ts +3 -3
  79. package/package.json +1 -1
  80. package/services/jsonFileStorage.ts +5 -5
  81. package/services/taskScheduler.ts +1 -1
  82. package/services/telegramApi.ts +16 -58
  83. package/types/actionWithState.ts +2 -2
  84. package/types/response.ts +15 -14
  85. package/types/storage.ts +1 -1
  86. package/helpers/inverseRecord.ts +0 -7
  87. /package/{entities → dtos}/commandTriggerCheckResult.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.50",
3
+ "version": "0.0.51",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -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 '../entities/actionExecutionResult';
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,
@@ -3,7 +3,7 @@ import { Milliseconds } from '../types/timeValues';
3
3
  import { Logger } from './logger';
4
4
 
5
5
  class TaskScheduler {
6
- activeTasks: TaskRecord[] = [];
6
+ readonly activeTasks: TaskRecord[] = [];
7
7
 
8
8
  stopAll() {
9
9
  this.activeTasks.forEach((task) => {
@@ -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
- chats: Record<number, string>;
25
- storage: IStorageClient;
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
 
@@ -59,7 +45,7 @@ export class TelegramApiService {
59
45
  Logger.errorWithTraceId(
60
46
  this.botName,
61
47
  message.traceId,
62
- this.chats[message.chatId],
48
+ message.chatInfo.name,
63
49
  error,
64
50
  message
65
51
  );
@@ -75,14 +61,14 @@ export class TelegramApiService {
75
61
  ) {
76
62
  if (response.shouldPin) {
77
63
  await this.telegram.pinChatMessage(
78
- response.chatId,
64
+ response.chatInfo.id,
79
65
  sentMessage.message_id,
80
66
  { disable_notification: true }
81
67
  );
82
68
 
83
69
  await this.storage.updateStateFor(
84
70
  response.action,
85
- response.chatId,
71
+ response.chatInfo.id,
86
72
  async (state) => {
87
73
  state.pinnedMessages.push(sentMessage.message_id);
88
74
  }
@@ -96,7 +82,7 @@ export class TelegramApiService {
96
82
  switch (response.kind) {
97
83
  case 'text':
98
84
  sentMessage = await this.telegram.sendMessage(
99
- response.chatId,
85
+ response.chatInfo.id,
100
86
  response.content,
101
87
  {
102
88
  reply_to_message_id: response.replyId,
@@ -110,7 +96,7 @@ export class TelegramApiService {
110
96
  break;
111
97
  case 'image':
112
98
  sentMessage = await this.telegram.sendPhoto(
113
- response.chatId,
99
+ response.chatInfo.id,
114
100
  response.content,
115
101
  response.replyId
116
102
  ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -122,7 +108,7 @@ export class TelegramApiService {
122
108
  break;
123
109
  case 'video':
124
110
  sentMessage = await this.telegram.sendVideo(
125
- response.chatId,
111
+ response.chatInfo.id,
126
112
  response.content,
127
113
  response.replyId
128
114
  ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -134,7 +120,7 @@ export class TelegramApiService {
134
120
  break;
135
121
  case 'react':
136
122
  await this.telegram.setMessageReaction(
137
- response.chatId,
123
+ response.chatInfo.id,
138
124
  response.messageId,
139
125
  [
140
126
  {
@@ -148,13 +134,13 @@ export class TelegramApiService {
148
134
  return;
149
135
  case 'unpin':
150
136
  await this.telegram.unpinChatMessage(
151
- response.chatId,
137
+ response.chatInfo.id,
152
138
  response.messageId
153
139
  );
154
140
 
155
141
  await this.storage.updateStateFor(
156
142
  response.action,
157
- response.chatId,
143
+ response.chatInfo.id,
158
144
  async (state) => {
159
145
  state.pinnedMessages = state.pinnedMessages.filter(
160
146
  (x) => x != response.messageId
@@ -168,32 +154,4 @@ export class TelegramApiService {
168
154
  }
169
155
  }
170
156
  }
171
-
172
- initializeContextForMessage<TActionState extends IActionState>(
173
- ctx: MessageContext<TActionState>,
174
- incomingMessage: IncomingMessage,
175
- command: CommandAction<TActionState>
176
- ) {
177
- return ctx.initializeMessageContext(
178
- this.botName,
179
- command,
180
- incomingMessage,
181
- this.storage
182
- );
183
- }
184
-
185
- initializeContextForChat<TActionState extends IActionState>(
186
- ctx: ChatContext<TActionState>,
187
- chatId: number,
188
- scheduledAction: ScheduledAction<TActionState>
189
- ) {
190
- return ctx.initializeChatContext(
191
- this.botName,
192
- scheduledAction,
193
- chatId,
194
- this.chats[chatId],
195
- `Scheduled:${scheduledAction.key}:${chatId}`,
196
- this.storage
197
- );
198
- }
199
157
  }
@@ -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 { DelayResponse } from '../entities/responses/delay';
2
- import { ImageMessage } from '../entities/responses/imageMessage';
3
- import { Reaction } from '../entities/responses/reaction';
4
- import { TextMessage } from '../entities/responses/textMessage';
5
- import { UnpinResponse } from '../entities/responses/unpin';
6
- import { VideoMessage } from '../entities/responses/videoMessage';
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
- chatId: number;
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
@@ -1,4 +1,4 @@
1
- import { ActionExecutionResult } from '../entities/actionExecutionResult';
1
+ import { ActionExecutionResult } from '../dtos/actionExecutionResult';
2
2
  import { IActionState } from './actionState';
3
3
  import { ActionKey, IActionWithState } from './actionWithState';
4
4
 
@@ -1,7 +0,0 @@
1
- export function inverseRecord<T extends PropertyKey, U extends PropertyKey>(
2
- input: Record<T, U>
3
- ) {
4
- return Object.fromEntries(
5
- Object.entries(input).map(([key, value]) => [value, key])
6
- ) as Record<U, T>;
7
- }
File without changes