chz-telegram-bot 0.2.1 → 0.3.1

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 (123) hide show
  1. package/dist/dtos/commandTriggerCheckResult.d.ts +1 -0
  2. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
  3. package/dist/dtos/commandTriggerCheckResult.js +3 -0
  4. package/dist/dtos/incomingMessage.d.ts +2 -1
  5. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  6. package/dist/dtos/incomingMessage.js +5 -1
  7. package/dist/dtos/incomingQuery.d.ts +1 -0
  8. package/dist/dtos/incomingQuery.d.ts.map +1 -1
  9. package/dist/dtos/incomingQuery.js +1 -0
  10. package/dist/dtos/replyInfo.d.ts.map +1 -0
  11. package/dist/dtos/responses/delay.d.ts +1 -1
  12. package/dist/dtos/responses/delay.d.ts.map +1 -1
  13. package/dist/dtos/responses/imageMessage.d.ts +8 -7
  14. package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
  15. package/dist/dtos/responses/imageMessage.js +1 -0
  16. package/dist/dtos/responses/reaction.d.ts +3 -4
  17. package/dist/dtos/responses/reaction.d.ts.map +1 -1
  18. package/dist/dtos/responses/textMessage.d.ts +8 -7
  19. package/dist/dtos/responses/textMessage.d.ts.map +1 -1
  20. package/dist/dtos/responses/textMessage.js +1 -0
  21. package/dist/dtos/responses/unpin.d.ts +1 -1
  22. package/dist/dtos/responses/unpin.d.ts.map +1 -1
  23. package/dist/dtos/responses/videoMessage.d.ts +8 -7
  24. package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
  25. package/dist/dtos/responses/videoMessage.js +1 -0
  26. package/dist/entities/actions/commandAction.d.ts +1 -1
  27. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  28. package/dist/entities/actions/commandAction.js +7 -12
  29. package/dist/entities/actions/inlineQueryAction.d.ts +2 -3
  30. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  31. package/dist/entities/actions/replyCaptureAction.d.ts +15 -0
  32. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -0
  33. package/dist/entities/actions/replyCaptureAction.js +56 -0
  34. package/dist/entities/actions/scheduledAction.d.ts +1 -1
  35. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  36. package/dist/entities/botInstance.d.ts +7 -36
  37. package/dist/entities/botInstance.d.ts.map +1 -1
  38. package/dist/entities/botInstance.js +5 -162
  39. package/dist/entities/context/chatContext.d.ts +7 -5
  40. package/dist/entities/context/chatContext.d.ts.map +1 -1
  41. package/dist/entities/context/chatContext.js +21 -3
  42. package/dist/entities/context/inlineQueryContext.d.ts +7 -3
  43. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  44. package/dist/entities/context/inlineQueryContext.js +0 -11
  45. package/dist/entities/context/messageContext.d.ts +6 -6
  46. package/dist/entities/context/messageContext.d.ts.map +1 -1
  47. package/dist/entities/context/messageContext.js +10 -4
  48. package/dist/entities/context/replyContext.d.ts +113 -0
  49. package/dist/entities/context/replyContext.d.ts.map +1 -0
  50. package/dist/entities/context/replyContext.js +108 -0
  51. package/dist/main.d.ts.map +1 -1
  52. package/dist/main.js +5 -9
  53. package/dist/services/actionProcessingService.d.ts +25 -0
  54. package/dist/services/actionProcessingService.d.ts.map +1 -0
  55. package/dist/services/actionProcessingService.js +40 -0
  56. package/dist/services/actionProcessors/commandActionProcessor.d.ts +28 -0
  57. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
  58. package/dist/services/actionProcessors/commandActionProcessor.js +116 -0
  59. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +20 -0
  60. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
  61. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +74 -0
  62. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +21 -0
  63. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
  64. package/dist/services/actionProcessors/scheduledActionProcessor.js +69 -0
  65. package/dist/services/jsonFileStorage.d.ts +1 -1
  66. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  67. package/dist/services/telegramApi.d.ts +5 -1
  68. package/dist/services/telegramApi.d.ts.map +1 -1
  69. package/dist/services/telegramApi.js +19 -23
  70. package/dist/types/{actionWithState.d.ts → action.d.ts} +5 -3
  71. package/dist/types/action.d.ts.map +1 -0
  72. package/dist/types/capture.d.ts +14 -0
  73. package/dist/types/capture.d.ts.map +1 -0
  74. package/dist/types/commandCondition.d.ts +1 -1
  75. package/dist/types/commandCondition.d.ts.map +1 -1
  76. package/dist/types/response.d.ts +9 -6
  77. package/dist/types/response.d.ts.map +1 -1
  78. package/dist/types/storage.d.ts +1 -1
  79. package/dist/types/storage.d.ts.map +1 -1
  80. package/dtos/commandTriggerCheckResult.ts +3 -0
  81. package/dtos/incomingMessage.ts +7 -2
  82. package/dtos/incomingQuery.ts +2 -0
  83. package/dtos/responses/delay.ts +1 -1
  84. package/dtos/responses/imageMessage.ts +11 -7
  85. package/dtos/responses/reaction.ts +3 -4
  86. package/dtos/responses/textMessage.ts +11 -7
  87. package/dtos/responses/unpin.ts +1 -1
  88. package/dtos/responses/videoMessage.ts +11 -7
  89. package/entities/actions/commandAction.ts +13 -20
  90. package/entities/actions/inlineQueryAction.ts +2 -3
  91. package/entities/actions/replyCaptureAction.ts +104 -0
  92. package/entities/actions/scheduledAction.ts +1 -1
  93. package/entities/botInstance.ts +19 -347
  94. package/entities/context/chatContext.ts +59 -29
  95. package/entities/context/inlineQueryContext.ts +7 -22
  96. package/entities/context/messageContext.ts +36 -29
  97. package/entities/context/replyContext.ts +225 -0
  98. package/main.ts +11 -10
  99. package/package.json +1 -1
  100. package/services/actionProcessingService.ts +123 -0
  101. package/services/actionProcessors/commandActionProcessor.ts +242 -0
  102. package/services/actionProcessors/inlineQueryActionProcessor.ts +176 -0
  103. package/services/actionProcessors/scheduledActionProcessor.ts +145 -0
  104. package/services/jsonFileStorage.ts +1 -1
  105. package/services/telegramApi.ts +45 -31
  106. package/types/{statefulAction.ts → action.ts} +6 -2
  107. package/types/capture.ts +21 -0
  108. package/types/commandCondition.ts +2 -1
  109. package/types/response.ts +10 -6
  110. package/types/storage.ts +1 -1
  111. package/dist/types/actionWithState.d.ts.map +0 -1
  112. package/dist/types/replyInfo.d.ts.map +0 -1
  113. package/dist/types/statefulAction.d.ts +0 -9
  114. package/dist/types/statefulAction.d.ts.map +0 -1
  115. package/dist/types/statelessAction.d.ts +0 -5
  116. package/dist/types/statelessAction.d.ts.map +0 -1
  117. package/dist/types/statelessAction.js +0 -2
  118. package/types/statelessAction.ts +0 -5
  119. /package/dist/{types → dtos}/replyInfo.d.ts +0 -0
  120. /package/dist/{types → dtos}/replyInfo.js +0 -0
  121. /package/dist/types/{actionWithState.js → action.js} +0 -0
  122. /package/dist/types/{statefulAction.js → capture.js} +0 -0
  123. /package/{types → dtos}/replyInfo.ts +0 -0
@@ -4,12 +4,23 @@ import { BotResponse, IReplyResponse } from '../types/response';
4
4
  import { Telegram } from 'telegraf/typings/telegram';
5
5
  import { ILogger } from '../types/logger';
6
6
  import { QueueItem, ResponseProcessingQueue } from './responseProcessingQueue';
7
+ import { IReplyCapture } from '../types/capture';
8
+ import { IActionWithState } from '../types/action';
9
+ import { IActionState } from '../types/actionState';
10
+ import { TraceId } from '../types/trace';
11
+ import { ChatInfo } from '../dtos/chatInfo';
7
12
 
8
13
  export class TelegramApiService {
9
14
  private readonly queue = new ResponseProcessingQueue();
10
15
  private readonly telegram: Telegram;
11
16
  private readonly storage: IStorageClient;
12
17
  private readonly logger: ILogger;
18
+ private readonly captureRegistrationCallback: (
19
+ capture: IReplyCapture,
20
+ parentMessageId: number,
21
+ chatInfo: ChatInfo,
22
+ traceId: TraceId
23
+ ) => void;
13
24
 
14
25
  private readonly botName: string;
15
26
 
@@ -17,12 +28,19 @@ export class TelegramApiService {
17
28
  botName: string,
18
29
  telegram: Telegram,
19
30
  storage: IStorageClient,
20
- logger: ILogger
31
+ logger: ILogger,
32
+ captureRegistrationCallback: (
33
+ capture: IReplyCapture,
34
+ parentMessageId: number,
35
+ chatInfo: ChatInfo,
36
+ traceId: TraceId
37
+ ) => void
21
38
  ) {
22
39
  this.telegram = telegram;
23
40
  this.botName = botName;
24
41
  this.storage = storage;
25
42
  this.logger = logger;
43
+ this.captureRegistrationCallback = captureRegistrationCallback;
26
44
  }
27
45
 
28
46
  enqueueBatchedResponses(responses: BotResponse[]) {
@@ -59,10 +77,7 @@ export class TelegramApiService {
59
77
  this.queue.flushReadyItems();
60
78
  }
61
79
 
62
- private async pinIfShould<T>(
63
- response: IReplyResponse<T>,
64
- sentMessage: Message
65
- ) {
80
+ private async pinIfShould(response: IReplyResponse, sentMessage: Message) {
66
81
  if (response.shouldPin) {
67
82
  await this.telegram.pinChatMessage(
68
83
  response.chatInfo.id,
@@ -71,7 +86,7 @@ export class TelegramApiService {
71
86
  );
72
87
 
73
88
  await this.storage.updateStateFor(
74
- response.action,
89
+ response.action as IActionWithState<IActionState>,
75
90
  response.chatInfo.id,
76
91
  async (state) => {
77
92
  state.pinnedMessages.push(sentMessage.message_id);
@@ -81,7 +96,7 @@ export class TelegramApiService {
81
96
  }
82
97
 
83
98
  private async processResponse(response: BotResponse) {
84
- let sentMessage: Message;
99
+ let sentMessage: Message | null = null;
85
100
 
86
101
  switch (response.kind) {
87
102
  case 'text':
@@ -101,42 +116,28 @@ export class TelegramApiService {
101
116
  }
102
117
  }
103
118
  );
104
-
105
- await this.pinIfShould(response, sentMessage);
106
119
  break;
107
120
  case 'image':
108
121
  sentMessage = await this.telegram.sendPhoto(
109
122
  response.chatInfo.id,
110
123
  response.content,
111
- {
112
- reply_parameters: response.replyInfo
113
- ? {
114
- message_id: response.replyInfo.id,
115
- quote: response.replyInfo.quote
116
- }
117
- : undefined,
118
- parse_mode: 'MarkdownV2'
119
- }
124
+ response.replyInfo?.id
125
+ ? ({
126
+ reply_to_message_id: response.replyInfo?.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ } as any)
128
+ : undefined
120
129
  );
121
-
122
- await this.pinIfShould(response, sentMessage);
123
130
  break;
124
131
  case 'video':
125
132
  sentMessage = await this.telegram.sendVideo(
126
133
  response.chatInfo.id,
127
134
  response.content,
128
- {
129
- reply_parameters: response.replyInfo
130
- ? {
131
- message_id: response.replyInfo.id,
132
- quote: response.replyInfo.quote
133
- }
134
- : undefined,
135
- parse_mode: 'MarkdownV2'
136
- }
135
+ response.replyInfo?.id
136
+ ? ({
137
+ reply_to_message_id: response.replyInfo?.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ } as any)
139
+ : undefined
137
140
  );
138
-
139
- await this.pinIfShould(response, sentMessage);
140
141
  break;
141
142
  case 'react':
142
143
  await this.telegram.setMessageReaction(
@@ -178,5 +179,18 @@ export class TelegramApiService {
178
179
  case 'delay':
179
180
  break;
180
181
  }
182
+
183
+ if ('content' in response && sentMessage) {
184
+ await this.pinIfShould(response, sentMessage);
185
+
186
+ for (const capture of response.captures) {
187
+ this.captureRegistrationCallback(
188
+ capture,
189
+ sentMessage.message_id,
190
+ response.chatInfo,
191
+ response.traceId
192
+ );
193
+ }
194
+ }
181
195
  }
182
196
  }
@@ -2,7 +2,11 @@ import { IActionState } from './actionState';
2
2
 
3
3
  export type ActionKey = string & { __brand: 'actionKey' };
4
4
 
5
- export interface IActionWithState<TActionState extends IActionState> {
6
- readonly key: ActionKey;
5
+ export interface IActionWithState<TActionState extends IActionState>
6
+ extends IAction {
7
7
  readonly stateConstructor: () => TActionState;
8
8
  }
9
+
10
+ export interface IAction {
11
+ readonly key: ActionKey;
12
+ }
@@ -0,0 +1,21 @@
1
+ import { ReplyContext } from '../entities/context/replyContext';
2
+ import { IActionState } from './actionState';
3
+ import { CommandTrigger } from './commandTrigger';
4
+ import { IActionWithState } from './action';
5
+
6
+ export interface ICaptureController {
7
+ captureReplies: <TParentActionState extends IActionState>(
8
+ trigger: CommandTrigger[],
9
+ handler: (
10
+ replyContext: ReplyContext<TParentActionState>
11
+ ) => Promise<void>,
12
+ abortController: AbortController
13
+ ) => void;
14
+ }
15
+
16
+ export interface IReplyCapture {
17
+ trigger: CommandTrigger[];
18
+ handler: (replyContext: ReplyContext<IActionState>) => Promise<void>;
19
+ abortController: AbortController;
20
+ action: IActionWithState<IActionState>;
21
+ }
@@ -2,5 +2,6 @@ import { MessageContext } from '../entities/context/messageContext';
2
2
  import { IActionState } from './actionState';
3
3
 
4
4
  export type CommandCondition<TActionState extends IActionState> = (
5
- ctx: MessageContext<TActionState>
5
+ ctx: MessageContext<TActionState>,
6
+ state: TActionState
6
7
  ) => boolean;
package/types/response.ts CHANGED
@@ -6,10 +6,10 @@ import { Reaction } from '../dtos/responses/reaction';
6
6
  import { TextMessage } from '../dtos/responses/textMessage';
7
7
  import { UnpinResponse } from '../dtos/responses/unpin';
8
8
  import { VideoMessage } from '../dtos/responses/videoMessage';
9
- import { IActionState } from './actionState';
10
- import { ReplyInfo } from './replyInfo';
11
- import { IActionWithState } from './statefulAction';
9
+ import { IReplyCapture } from './capture';
10
+ import { ReplyInfo } from '../dtos/replyInfo';
12
11
  import { TraceId } from './trace';
12
+ import { IAction } from './action';
13
13
 
14
14
  export const BotResponseTypes = {
15
15
  unpin: 'unpin',
@@ -36,12 +36,16 @@ export interface IChatResponse {
36
36
  readonly traceId: TraceId;
37
37
  readonly createdAt: number;
38
38
 
39
- readonly action: IActionWithState<IActionState>;
39
+ readonly action: IAction;
40
40
  }
41
41
 
42
- export interface IReplyResponse<TType> extends IChatResponse {
43
- readonly content: TType;
42
+ export interface IReplyResponse extends IChatResponse {
43
+ readonly captures: IReplyCapture[];
44
44
  readonly replyInfo: ReplyInfo | undefined;
45
45
  readonly disableWebPreview: boolean;
46
46
  readonly shouldPin: boolean;
47
47
  }
48
+
49
+ export interface IReplyResponseWithContent<TType> extends IReplyResponse {
50
+ readonly content: TType;
51
+ }
package/types/storage.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IActionState } from './actionState';
2
- import { ActionKey, IActionWithState } from './statefulAction';
2
+ import { ActionKey, IActionWithState } from './action';
3
3
 
4
4
  export interface IStorageClient {
5
5
  updateStateFor<TActionState extends IActionState>(
@@ -1 +0,0 @@
1
- {"version":3,"file":"actionWithState.d.ts","sourceRoot":"","sources":["../../types/actionWithState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1D,MAAM,WAAW,gBAAgB,CAAC,YAAY,SAAS,YAAY;IAC/D,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACjD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"replyInfo.d.ts","sourceRoot":"","sources":["../../types/replyInfo.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAS;IAClB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEvB,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;CAIzC"}
@@ -1,9 +0,0 @@
1
- import { IActionState } from './actionState';
2
- export type ActionKey = string & {
3
- __brand: 'actionKey';
4
- };
5
- export interface IActionWithState<TActionState extends IActionState> {
6
- readonly key: ActionKey;
7
- readonly stateConstructor: () => TActionState;
8
- }
9
- //# sourceMappingURL=statefulAction.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"statefulAction.d.ts","sourceRoot":"","sources":["../../types/statefulAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1D,MAAM,WAAW,gBAAgB,CAAC,YAAY,SAAS,YAAY;IAC/D,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACjD"}
@@ -1,5 +0,0 @@
1
- import { ActionKey } from './statefulAction';
2
- export interface IActionWithoutState {
3
- readonly key: ActionKey;
4
- }
5
- //# sourceMappingURL=statelessAction.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"statelessAction.d.ts","sourceRoot":"","sources":["../../types/statelessAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,MAAM,WAAW,mBAAmB;IAChC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;CAC3B"}
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +0,0 @@
1
- import { ActionKey } from './statefulAction';
2
-
3
- export interface IActionWithoutState {
4
- readonly key: ActionKey;
5
- }
File without changes
File without changes
File without changes
File without changes
File without changes