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
|
@@ -12,14 +12,15 @@ import {
|
|
|
12
12
|
MessageSendingOptions,
|
|
13
13
|
TextMessageSendingOptions
|
|
14
14
|
} from '../../types/messageSendingOptions';
|
|
15
|
-
import { ActionKey } from '../../types/
|
|
15
|
+
import { ActionKey } from '../../types/action';
|
|
16
16
|
import {
|
|
17
17
|
MessageTypeValue,
|
|
18
18
|
TelegrafContextMessage
|
|
19
19
|
} from '../../types/messageTypes';
|
|
20
20
|
import { ILogger } from '../../types/logger';
|
|
21
21
|
import { IScheduler } from '../../types/scheduler';
|
|
22
|
-
import { ReplyInfo } from '../../
|
|
22
|
+
import { ReplyInfo } from '../../dtos/replyInfo';
|
|
23
|
+
|
|
23
24
|
/**
|
|
24
25
|
* Context of action executed in chat, in response to a message
|
|
25
26
|
*/
|
|
@@ -61,16 +62,18 @@ export class MessageContext<
|
|
|
61
62
|
? this.matchResults[0][1]
|
|
62
63
|
: this.messageText;
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
options
|
|
72
|
-
)
|
|
65
|
+
const response = new TextMessage(
|
|
66
|
+
text,
|
|
67
|
+
this.chatInfo,
|
|
68
|
+
this.traceId,
|
|
69
|
+
this.action,
|
|
70
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
71
|
+
options
|
|
73
72
|
);
|
|
73
|
+
|
|
74
|
+
this.responses.push(response);
|
|
75
|
+
|
|
76
|
+
return this.createCaptureController_TEMP(response);
|
|
74
77
|
}
|
|
75
78
|
|
|
76
79
|
private replyWithImage(
|
|
@@ -83,16 +86,18 @@ export class MessageContext<
|
|
|
83
86
|
? this.matchResults[0][1]
|
|
84
87
|
: this.messageText;
|
|
85
88
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
options
|
|
94
|
-
)
|
|
89
|
+
const response = new ImageMessage(
|
|
90
|
+
{ source: resolve(`./content/${name}.png`) },
|
|
91
|
+
this.chatInfo,
|
|
92
|
+
this.traceId,
|
|
93
|
+
this.action,
|
|
94
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
95
|
+
options
|
|
95
96
|
);
|
|
97
|
+
|
|
98
|
+
this.responses.push(response);
|
|
99
|
+
|
|
100
|
+
return this.createCaptureController_TEMP(response);
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
private replyWithVideo(
|
|
@@ -105,16 +110,18 @@ export class MessageContext<
|
|
|
105
110
|
? this.matchResults[0][1]
|
|
106
111
|
: this.messageText;
|
|
107
112
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
options
|
|
116
|
-
)
|
|
113
|
+
const response = new VideoMessage(
|
|
114
|
+
{ source: resolve(`./content/${name}.mp4`) },
|
|
115
|
+
this.chatInfo,
|
|
116
|
+
this.traceId,
|
|
117
|
+
this.action,
|
|
118
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
119
|
+
options
|
|
117
120
|
);
|
|
121
|
+
|
|
122
|
+
this.responses.push(response);
|
|
123
|
+
|
|
124
|
+
return this.createCaptureController_TEMP(response);
|
|
118
125
|
}
|
|
119
126
|
|
|
120
127
|
/**
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { TelegramEmoji } from 'telegraf/types';
|
|
2
|
+
import { ChatInfo } from '../../dtos/chatInfo';
|
|
3
|
+
import { ReplyInfo } from '../../dtos/replyInfo';
|
|
4
|
+
import { ImageMessage } from '../../dtos/responses/imageMessage';
|
|
5
|
+
import { Reaction } from '../../dtos/responses/reaction';
|
|
6
|
+
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
7
|
+
import { VideoMessage } from '../../dtos/responses/videoMessage';
|
|
8
|
+
import { IActionState } from '../../types/actionState';
|
|
9
|
+
import { ILogger } from '../../types/logger';
|
|
10
|
+
import {
|
|
11
|
+
TextMessageSendingOptions,
|
|
12
|
+
MessageSendingOptions
|
|
13
|
+
} from '../../types/messageSendingOptions';
|
|
14
|
+
import {
|
|
15
|
+
MessageTypeValue,
|
|
16
|
+
TelegrafContextMessage
|
|
17
|
+
} from '../../types/messageTypes';
|
|
18
|
+
import { BotResponse } from '../../types/response';
|
|
19
|
+
import { IScheduler } from '../../types/scheduler';
|
|
20
|
+
import { IStorageClient } from '../../types/storage';
|
|
21
|
+
import { TraceId } from '../../types/trace';
|
|
22
|
+
import { ReplyCaptureAction } from '../actions/replyCaptureAction';
|
|
23
|
+
import { resolve } from 'path';
|
|
24
|
+
|
|
25
|
+
export class ReplyContext<TParentActionState extends IActionState> {
|
|
26
|
+
action!: ReplyCaptureAction<TParentActionState>;
|
|
27
|
+
|
|
28
|
+
/** Storage client instance for the bot executing this action. */
|
|
29
|
+
readonly storage: IStorageClient;
|
|
30
|
+
/** Logger instance for the bot executing this action */
|
|
31
|
+
readonly logger: ILogger;
|
|
32
|
+
/** Scheduler instance for the bot executing this action */
|
|
33
|
+
readonly scheduler: IScheduler;
|
|
34
|
+
|
|
35
|
+
/** Trace id of a action execution. */
|
|
36
|
+
traceId!: TraceId;
|
|
37
|
+
/** Name of a bot that executes this action. */
|
|
38
|
+
botName!: string;
|
|
39
|
+
|
|
40
|
+
/** Ordered collection of responses to be processed */
|
|
41
|
+
responses: BotResponse[] = [];
|
|
42
|
+
/** Chat information. */
|
|
43
|
+
chatInfo!: ChatInfo;
|
|
44
|
+
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
45
|
+
matchResults!: RegExpExecArray[];
|
|
46
|
+
/** Id of a message that triggered this action. */
|
|
47
|
+
replyMessageId!: number | undefined;
|
|
48
|
+
/** Id of a message that triggered this action. */
|
|
49
|
+
messageId!: number;
|
|
50
|
+
/** Type of message being received */
|
|
51
|
+
messageType!: MessageTypeValue;
|
|
52
|
+
/** Text of a message that triggered this action. */
|
|
53
|
+
messageText!: string;
|
|
54
|
+
/** Id of a user that sent a message that triggered this action. */
|
|
55
|
+
fromUserId: number | undefined;
|
|
56
|
+
/** Name of a user that sent a message that triggered this action. */
|
|
57
|
+
fromUserName!: string;
|
|
58
|
+
/** Message object recieved from Telegram */
|
|
59
|
+
messageUpdateObject!: TelegrafContextMessage;
|
|
60
|
+
|
|
61
|
+
isInitialized = false;
|
|
62
|
+
|
|
63
|
+
constructor(
|
|
64
|
+
storage: IStorageClient,
|
|
65
|
+
logger: ILogger,
|
|
66
|
+
scheduler: IScheduler
|
|
67
|
+
) {
|
|
68
|
+
this.storage = storage;
|
|
69
|
+
this.logger = logger;
|
|
70
|
+
this.scheduler = scheduler;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private replyWithText(
|
|
74
|
+
text: string,
|
|
75
|
+
quote: boolean,
|
|
76
|
+
options?: TextMessageSendingOptions
|
|
77
|
+
) {
|
|
78
|
+
const quotedPart =
|
|
79
|
+
this.matchResults.length != 0
|
|
80
|
+
? this.matchResults[0][1]
|
|
81
|
+
: this.messageText;
|
|
82
|
+
|
|
83
|
+
const response = new TextMessage(
|
|
84
|
+
text,
|
|
85
|
+
this.chatInfo,
|
|
86
|
+
this.traceId,
|
|
87
|
+
this.action,
|
|
88
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
89
|
+
options
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
this.responses.push(response);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private replyWithImage(
|
|
96
|
+
name: string,
|
|
97
|
+
quote: boolean,
|
|
98
|
+
options?: MessageSendingOptions
|
|
99
|
+
) {
|
|
100
|
+
const quotedPart =
|
|
101
|
+
this.matchResults.length != 0
|
|
102
|
+
? this.matchResults[0][1]
|
|
103
|
+
: this.messageText;
|
|
104
|
+
|
|
105
|
+
const response = new ImageMessage(
|
|
106
|
+
{ source: resolve(`./content/${name}.png`) },
|
|
107
|
+
this.chatInfo,
|
|
108
|
+
this.traceId,
|
|
109
|
+
this.action,
|
|
110
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
111
|
+
options
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
this.responses.push(response);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private replyWithVideo(
|
|
118
|
+
name: string,
|
|
119
|
+
quote: boolean,
|
|
120
|
+
options?: MessageSendingOptions
|
|
121
|
+
) {
|
|
122
|
+
const quotedPart =
|
|
123
|
+
this.matchResults.length != 0
|
|
124
|
+
? this.matchResults[0][1]
|
|
125
|
+
: this.messageText;
|
|
126
|
+
|
|
127
|
+
const response = new VideoMessage(
|
|
128
|
+
{ source: resolve(`./content/${name}.mp4`) },
|
|
129
|
+
this.chatInfo,
|
|
130
|
+
this.traceId,
|
|
131
|
+
this.action,
|
|
132
|
+
new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
|
|
133
|
+
options
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
this.responses.push(response);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Stops capturing replies and removes this action
|
|
141
|
+
*/
|
|
142
|
+
stopCapture() {
|
|
143
|
+
this.action.abortController.abort();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Collection of actions that can be done as a reply to a message that triggered this action
|
|
148
|
+
*/
|
|
149
|
+
reply = {
|
|
150
|
+
/**
|
|
151
|
+
* Collection of actions that can be done as a reply to a message, quoting the part that triggered this action
|
|
152
|
+
* If regex is matched, first match will be quoted
|
|
153
|
+
*/
|
|
154
|
+
andQuote: {
|
|
155
|
+
/**
|
|
156
|
+
* Reply with text message to message that triggered this action after action execution is finished.
|
|
157
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
158
|
+
* @param text Message contents.
|
|
159
|
+
* @param options Message sending option.
|
|
160
|
+
*/
|
|
161
|
+
withText: (text: string, options?: TextMessageSendingOptions) =>
|
|
162
|
+
this.replyWithText(text, true, options),
|
|
163
|
+
/**
|
|
164
|
+
* Reply with image message to message that triggered this action after action execution is finished.
|
|
165
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
166
|
+
* @param text Message contents.
|
|
167
|
+
* @param options Message sending option.
|
|
168
|
+
*/
|
|
169
|
+
withImage: (name: string, options?: MessageSendingOptions) =>
|
|
170
|
+
this.replyWithImage(name, true, options),
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
174
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
175
|
+
* @param text Message contents.
|
|
176
|
+
* @param options Message sending option.
|
|
177
|
+
*/
|
|
178
|
+
withVideo: (name: string, options?: MessageSendingOptions) =>
|
|
179
|
+
this.replyWithVideo(name, true, options)
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Reply with text message to message that triggered this action after action execution is finished.
|
|
184
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
185
|
+
* @param text Message contents.
|
|
186
|
+
* @param options Message sending option.
|
|
187
|
+
*/
|
|
188
|
+
withText: (text: string, options?: TextMessageSendingOptions) =>
|
|
189
|
+
this.replyWithText(text, false, options),
|
|
190
|
+
/**
|
|
191
|
+
* Reply with image message to message that triggered this action after action execution is finished.
|
|
192
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
193
|
+
* @param text Message contents.
|
|
194
|
+
* @param options Message sending option.
|
|
195
|
+
*/
|
|
196
|
+
withImage: (name: string, options?: MessageSendingOptions) =>
|
|
197
|
+
this.replyWithImage(name, false, options),
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
201
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
202
|
+
* @param text Message contents.
|
|
203
|
+
* @param options Message sending option.
|
|
204
|
+
*/
|
|
205
|
+
withVideo: (name: string, options?: MessageSendingOptions) =>
|
|
206
|
+
this.replyWithVideo(name, false, options),
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* React to the message that triggered this action after action execution is finished.
|
|
210
|
+
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
211
|
+
* @param emoji Telegram emoji to react with.
|
|
212
|
+
*/
|
|
213
|
+
withReaction: (emoji: TelegramEmoji) => {
|
|
214
|
+
this.responses.push(
|
|
215
|
+
new Reaction(
|
|
216
|
+
this.traceId,
|
|
217
|
+
this.chatInfo,
|
|
218
|
+
this.messageId,
|
|
219
|
+
emoji,
|
|
220
|
+
this.action
|
|
221
|
+
)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
package/main.ts
CHANGED
|
@@ -46,24 +46,25 @@ class BotOrchestrator {
|
|
|
46
46
|
};
|
|
47
47
|
}) {
|
|
48
48
|
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
49
|
-
|
|
49
|
+
|
|
50
|
+
const bot = new BotInstance({
|
|
50
51
|
name: options.name,
|
|
51
|
-
token,
|
|
52
52
|
actions: options.actions,
|
|
53
53
|
chats: options.chats,
|
|
54
|
-
storagePath: options.storagePath,
|
|
55
|
-
scheduledPeriod: options.scheduledPeriod,
|
|
56
|
-
verboseLoggingForIncomingMessage:
|
|
57
|
-
options.verboseLoggingForIncomingMessage,
|
|
58
54
|
services: {
|
|
59
55
|
storageClient: options.services?.storageClient,
|
|
60
56
|
logger: options.services?.logger,
|
|
61
57
|
scheduler: options.services?.scheduler
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
},
|
|
59
|
+
storagePath: options.storagePath
|
|
60
|
+
});
|
|
65
61
|
|
|
66
|
-
await bot.start(
|
|
62
|
+
await bot.start(
|
|
63
|
+
token,
|
|
64
|
+
options.actions,
|
|
65
|
+
options.scheduledPeriod,
|
|
66
|
+
options.verboseLoggingForIncomingMessage
|
|
67
|
+
);
|
|
67
68
|
this.bots.push(bot);
|
|
68
69
|
|
|
69
70
|
return bot;
|
package/package.json
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { hoursToSeconds } from '../helpers/timeConvertions';
|
|
2
|
+
import { Seconds, Milliseconds, Hours } from '../types/timeValues';
|
|
3
|
+
import { Telegraf } from 'telegraf';
|
|
4
|
+
import { ILogger } from '../types/logger';
|
|
5
|
+
import { IScheduler } from '../types/scheduler';
|
|
6
|
+
import { IStorageClient } from '../types/storage';
|
|
7
|
+
import { TelegramApiService } from './telegramApi';
|
|
8
|
+
import { InlineQueryAction } from '../entities/actions/inlineQueryAction';
|
|
9
|
+
import { IActionState } from '../types/actionState';
|
|
10
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
11
|
+
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
12
|
+
import { buildHelpCommand } from '../builtin/helpAction';
|
|
13
|
+
import { CommandActionProcessor } from './actionProcessors/commandActionProcessor';
|
|
14
|
+
import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryActionProcessor';
|
|
15
|
+
import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
|
|
16
|
+
|
|
17
|
+
export class ActionProcessingService {
|
|
18
|
+
private readonly storage: IStorageClient;
|
|
19
|
+
private readonly logger: ILogger;
|
|
20
|
+
|
|
21
|
+
private readonly commandProcessor: CommandActionProcessor;
|
|
22
|
+
private readonly scheduledProcessor: ScheduledActionProcessor;
|
|
23
|
+
private readonly inlineQueryProcessor: InlineQueryActionProcessor;
|
|
24
|
+
|
|
25
|
+
private readonly botName: string;
|
|
26
|
+
|
|
27
|
+
private telegraf!: Telegraf;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
botName: string,
|
|
31
|
+
chats: Record<string, number>,
|
|
32
|
+
storage: IStorageClient,
|
|
33
|
+
scheduler: IScheduler,
|
|
34
|
+
logger: ILogger
|
|
35
|
+
) {
|
|
36
|
+
this.storage = storage;
|
|
37
|
+
this.logger = logger;
|
|
38
|
+
|
|
39
|
+
this.commandProcessor = new CommandActionProcessor(
|
|
40
|
+
botName,
|
|
41
|
+
storage,
|
|
42
|
+
scheduler,
|
|
43
|
+
logger
|
|
44
|
+
);
|
|
45
|
+
this.scheduledProcessor = new ScheduledActionProcessor(
|
|
46
|
+
botName,
|
|
47
|
+
chats,
|
|
48
|
+
storage,
|
|
49
|
+
scheduler,
|
|
50
|
+
logger
|
|
51
|
+
);
|
|
52
|
+
this.inlineQueryProcessor = new InlineQueryActionProcessor(
|
|
53
|
+
botName,
|
|
54
|
+
storage,
|
|
55
|
+
scheduler,
|
|
56
|
+
logger
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
this.botName = botName;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async initialize(
|
|
63
|
+
token: string,
|
|
64
|
+
actions: {
|
|
65
|
+
commands: CommandAction<IActionState>[];
|
|
66
|
+
scheduled: ScheduledAction<IActionState>[];
|
|
67
|
+
inlineQueries: InlineQueryAction[];
|
|
68
|
+
},
|
|
69
|
+
scheduledPeriod?: Seconds,
|
|
70
|
+
verboseLoggingForIncomingMessage?: boolean
|
|
71
|
+
) {
|
|
72
|
+
this.telegraf = new Telegraf(token);
|
|
73
|
+
const api = new TelegramApiService(
|
|
74
|
+
this.botName,
|
|
75
|
+
this.telegraf.telegram,
|
|
76
|
+
this.storage,
|
|
77
|
+
this.logger,
|
|
78
|
+
(capture, id, chatInfo, traceId) =>
|
|
79
|
+
this.commandProcessor.captureRegistrationCallback(
|
|
80
|
+
capture,
|
|
81
|
+
id,
|
|
82
|
+
chatInfo,
|
|
83
|
+
traceId
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const botInfo = await this.telegraf.telegram.getMe();
|
|
88
|
+
|
|
89
|
+
this.commandProcessor.initialize(
|
|
90
|
+
api,
|
|
91
|
+
this.telegraf,
|
|
92
|
+
actions.commands.length > 0
|
|
93
|
+
? [
|
|
94
|
+
buildHelpCommand(
|
|
95
|
+
actions.commands
|
|
96
|
+
.map((x) => x.readmeFactory(botInfo.username))
|
|
97
|
+
.filter((x) => !!x),
|
|
98
|
+
botInfo.username
|
|
99
|
+
),
|
|
100
|
+
...actions.commands
|
|
101
|
+
]
|
|
102
|
+
: [],
|
|
103
|
+
verboseLoggingForIncomingMessage ?? false
|
|
104
|
+
);
|
|
105
|
+
this.inlineQueryProcessor.initialize(
|
|
106
|
+
api,
|
|
107
|
+
this.telegraf,
|
|
108
|
+
actions.inlineQueries,
|
|
109
|
+
300 as Milliseconds
|
|
110
|
+
);
|
|
111
|
+
this.scheduledProcessor.initialize(
|
|
112
|
+
api,
|
|
113
|
+
actions.scheduled,
|
|
114
|
+
scheduledPeriod ?? hoursToSeconds(1 as Hours)
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
this.telegraf.launch();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
stop(code: string) {
|
|
121
|
+
this.telegraf.stop(code);
|
|
122
|
+
}
|
|
123
|
+
}
|