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.
- package/dist/dtos/commandTriggerCheckResult.d.ts +1 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/dtos/commandTriggerCheckResult.js +3 -0
- package/dist/dtos/incomingMessage.d.ts +2 -1
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +5 -1
- package/dist/dtos/incomingQuery.d.ts +1 -0
- package/dist/dtos/incomingQuery.d.ts.map +1 -1
- package/dist/dtos/incomingQuery.js +1 -0
- package/dist/dtos/replyInfo.d.ts.map +1 -0
- package/dist/dtos/responses/delay.d.ts +1 -1
- package/dist/dtos/responses/delay.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.d.ts +8 -7
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.js +1 -0
- package/dist/dtos/responses/reaction.d.ts +3 -4
- package/dist/dtos/responses/reaction.d.ts.map +1 -1
- package/dist/dtos/responses/textMessage.d.ts +8 -7
- package/dist/dtos/responses/textMessage.d.ts.map +1 -1
- package/dist/dtos/responses/textMessage.js +1 -0
- package/dist/dtos/responses/unpin.d.ts +1 -1
- package/dist/dtos/responses/unpin.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.d.ts +8 -7
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.js +1 -0
- package/dist/entities/actions/commandAction.d.ts +1 -1
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +7 -12
- package/dist/entities/actions/inlineQueryAction.d.ts +2 -3
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.d.ts +15 -0
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -0
- package/dist/entities/actions/replyCaptureAction.js +56 -0
- package/dist/entities/actions/scheduledAction.d.ts +1 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/botInstance.d.ts +7 -36
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +5 -162
- package/dist/entities/context/chatContext.d.ts +7 -5
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +21 -3
- package/dist/entities/context/inlineQueryContext.d.ts +7 -3
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/inlineQueryContext.js +0 -11
- package/dist/entities/context/messageContext.d.ts +6 -6
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +10 -4
- package/dist/entities/context/replyContext.d.ts +113 -0
- package/dist/entities/context/replyContext.d.ts.map +1 -0
- package/dist/entities/context/replyContext.js +108 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +5 -9
- package/dist/services/actionProcessingService.d.ts +25 -0
- package/dist/services/actionProcessingService.d.ts.map +1 -0
- package/dist/services/actionProcessingService.js +40 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +28 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.js +116 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +20 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +74 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +21 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.js +69 -0
- package/dist/services/jsonFileStorage.d.ts +1 -1
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/telegramApi.d.ts +5 -1
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +19 -23
- package/dist/types/{actionWithState.d.ts → action.d.ts} +5 -3
- package/dist/types/action.d.ts.map +1 -0
- package/dist/types/capture.d.ts +14 -0
- package/dist/types/capture.d.ts.map +1 -0
- package/dist/types/commandCondition.d.ts +1 -1
- package/dist/types/commandCondition.d.ts.map +1 -1
- package/dist/types/response.d.ts +9 -6
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/storage.d.ts +1 -1
- package/dist/types/storage.d.ts.map +1 -1
- package/dtos/commandTriggerCheckResult.ts +3 -0
- package/dtos/incomingMessage.ts +7 -2
- package/dtos/incomingQuery.ts +2 -0
- package/dtos/responses/delay.ts +1 -1
- package/dtos/responses/imageMessage.ts +11 -7
- package/dtos/responses/reaction.ts +3 -4
- package/dtos/responses/textMessage.ts +11 -7
- package/dtos/responses/unpin.ts +1 -1
- package/dtos/responses/videoMessage.ts +11 -7
- package/entities/actions/commandAction.ts +13 -20
- package/entities/actions/inlineQueryAction.ts +2 -3
- package/entities/actions/replyCaptureAction.ts +104 -0
- package/entities/actions/scheduledAction.ts +1 -1
- package/entities/botInstance.ts +19 -347
- package/entities/context/chatContext.ts +59 -29
- package/entities/context/inlineQueryContext.ts +7 -22
- package/entities/context/messageContext.ts +36 -29
- package/entities/context/replyContext.ts +225 -0
- package/main.ts +11 -10
- package/package.json +1 -1
- package/services/actionProcessingService.ts +123 -0
- package/services/actionProcessors/commandActionProcessor.ts +242 -0
- package/services/actionProcessors/inlineQueryActionProcessor.ts +176 -0
- package/services/actionProcessors/scheduledActionProcessor.ts +145 -0
- package/services/jsonFileStorage.ts +1 -1
- package/services/telegramApi.ts +45 -31
- package/types/{statefulAction.ts → action.ts} +6 -2
- package/types/capture.ts +21 -0
- package/types/commandCondition.ts +2 -1
- package/types/response.ts +10 -6
- package/types/storage.ts +1 -1
- package/dist/types/actionWithState.d.ts.map +0 -1
- package/dist/types/replyInfo.d.ts.map +0 -1
- package/dist/types/statefulAction.d.ts +0 -9
- package/dist/types/statefulAction.d.ts.map +0 -1
- package/dist/types/statelessAction.d.ts +0 -5
- package/dist/types/statelessAction.d.ts.map +0 -1
- package/dist/types/statelessAction.js +0 -2
- package/types/statelessAction.ts +0 -5
- /package/dist/{types → dtos}/replyInfo.d.ts +0 -0
- /package/dist/{types → dtos}/replyInfo.js +0 -0
- /package/dist/types/{actionWithState.js → action.js} +0 -0
- /package/dist/types/{statefulAction.js → capture.js} +0 -0
- /package/{types → dtos}/replyInfo.ts +0 -0
package/services/telegramApi.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
+
}
|
package/types/capture.ts
ADDED
|
@@ -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 {
|
|
10
|
-
import { ReplyInfo } from '
|
|
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:
|
|
39
|
+
readonly action: IAction;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
export interface IReplyResponse
|
|
43
|
-
readonly
|
|
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 +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 +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"}
|
package/types/statelessAction.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|