chz-telegram-bot 0.2.1 → 0.3.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.
- 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 +6 -11
- 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/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 +11 -18
- 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/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/entities/botInstance.ts
CHANGED
|
@@ -1,54 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
hoursToSeconds,
|
|
4
|
-
secondsToMilliseconds
|
|
5
|
-
} from '../helpers/timeConvertions';
|
|
6
|
-
import { Hours, Milliseconds, Seconds } from '../types/timeValues';
|
|
1
|
+
import { Seconds } from '../types/timeValues';
|
|
7
2
|
import { IStorageClient } from '../types/storage';
|
|
8
3
|
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
9
|
-
import { TelegramApiService } from '../services/telegramApi';
|
|
10
4
|
import { IActionState } from '../types/actionState';
|
|
11
5
|
import { CommandAction } from './actions/commandAction';
|
|
12
6
|
import { ScheduledAction } from './actions/scheduledAction';
|
|
13
7
|
import { JsonLogger } from '../services/jsonLogger';
|
|
14
|
-
import { IncomingMessage } from '../dtos/incomingMessage';
|
|
15
|
-
import moment from 'moment';
|
|
16
|
-
import { ChatContext } from './context/chatContext';
|
|
17
|
-
import { MessageContext } from './context/messageContext';
|
|
18
|
-
import { ChatInfo } from '../dtos/chatInfo';
|
|
19
8
|
import { ILogger } from '../types/logger';
|
|
20
9
|
import { IScheduler } from '../types/scheduler';
|
|
21
10
|
import { NodeTimeoutScheduler } from '../services/nodeTimeoutScheduler';
|
|
22
11
|
import { createTrace } from '../helpers/traceFactory';
|
|
23
12
|
import { InlineQueryAction } from './actions/inlineQueryAction';
|
|
24
|
-
import {
|
|
25
|
-
import { InlineQueryContext } from './context/inlineQueryContext';
|
|
26
|
-
import { buildHelpCommand } from '../builtin/helpAction';
|
|
27
|
-
import { UserFromGetMe } from 'telegraf/types';
|
|
28
|
-
import {
|
|
29
|
-
INTERNAL_MESSAGE_TYPE_PREFIX,
|
|
30
|
-
MessageType
|
|
31
|
-
} from '../types/messageTypes';
|
|
32
|
-
import { IActionWithState } from '../types/statefulAction';
|
|
33
|
-
import { TraceId } from '../types/trace';
|
|
13
|
+
import { ActionProcessingService } from '../services/actionProcessingService';
|
|
34
14
|
|
|
35
15
|
export class BotInstance {
|
|
36
|
-
private readonly api: TelegramApiService;
|
|
37
16
|
private readonly storage: IStorageClient;
|
|
38
17
|
private readonly scheduler: IScheduler;
|
|
39
18
|
private readonly logger: ILogger;
|
|
40
|
-
private readonly
|
|
19
|
+
private readonly actionProcessingService: ActionProcessingService;
|
|
41
20
|
|
|
42
|
-
name
|
|
43
|
-
private botInfo!: UserFromGetMe;
|
|
44
|
-
private commands!: CommandAction<IActionState>[];
|
|
45
|
-
private scheduled!: ScheduledAction<IActionState>[];
|
|
46
|
-
private inlineQueries!: InlineQueryAction[];
|
|
47
|
-
private chats!: Record<string, number>;
|
|
21
|
+
readonly name: string;
|
|
48
22
|
|
|
49
23
|
constructor(options: {
|
|
50
24
|
name: string;
|
|
51
|
-
token: string;
|
|
52
25
|
actions: {
|
|
53
26
|
commands: CommandAction<IActionState>[];
|
|
54
27
|
scheduled: ScheduledAction<IActionState>[];
|
|
@@ -56,8 +29,6 @@ export class BotInstance {
|
|
|
56
29
|
};
|
|
57
30
|
chats: Record<string, number>;
|
|
58
31
|
storagePath?: string;
|
|
59
|
-
scheduledPeriod?: Seconds;
|
|
60
|
-
verboseLoggingForIncomingMessage?: boolean;
|
|
61
32
|
services?: {
|
|
62
33
|
storageClient?: IStorageClient;
|
|
63
34
|
logger?: ILogger;
|
|
@@ -70,9 +41,7 @@ export class BotInstance {
|
|
|
70
41
|
];
|
|
71
42
|
|
|
72
43
|
this.name = options.name;
|
|
73
|
-
this.chats = options.chats;
|
|
74
44
|
|
|
75
|
-
this.telegraf = new Telegraf(options.token);
|
|
76
45
|
this.logger = options.services?.logger ?? new JsonLogger();
|
|
77
46
|
this.scheduler =
|
|
78
47
|
options.services?.scheduler ??
|
|
@@ -80,63 +49,34 @@ export class BotInstance {
|
|
|
80
49
|
this.storage =
|
|
81
50
|
options.services?.storageClient ??
|
|
82
51
|
new JsonFileStorage(options.name, actions, options.storagePath);
|
|
83
|
-
this.
|
|
52
|
+
this.actionProcessingService = new ActionProcessingService(
|
|
84
53
|
this.name,
|
|
85
|
-
|
|
54
|
+
options.chats,
|
|
86
55
|
this.storage,
|
|
56
|
+
this.scheduler,
|
|
87
57
|
this.logger
|
|
88
58
|
);
|
|
89
59
|
|
|
90
60
|
this.storage.saveMetadata(actions, this.name);
|
|
91
61
|
}
|
|
92
62
|
|
|
93
|
-
async start(
|
|
94
|
-
|
|
95
|
-
token: string;
|
|
63
|
+
async start(
|
|
64
|
+
token: string,
|
|
96
65
|
actions: {
|
|
97
66
|
commands: CommandAction<IActionState>[];
|
|
98
67
|
scheduled: ScheduledAction<IActionState>[];
|
|
99
68
|
inlineQueries: InlineQueryAction[];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
};
|
|
110
|
-
}) {
|
|
111
|
-
this.botInfo = await this.telegraf.telegram.getMe();
|
|
112
|
-
|
|
113
|
-
this.commands =
|
|
114
|
-
options.actions.commands.length > 0
|
|
115
|
-
? [
|
|
116
|
-
buildHelpCommand(
|
|
117
|
-
options.actions.commands
|
|
118
|
-
.map((x) =>
|
|
119
|
-
x.readmeFactory(this.botInfo.username)
|
|
120
|
-
)
|
|
121
|
-
.filter((x) => !!x),
|
|
122
|
-
this.botInfo.username
|
|
123
|
-
),
|
|
124
|
-
...options.actions.commands
|
|
125
|
-
]
|
|
126
|
-
: [];
|
|
127
|
-
this.scheduled = options.actions.scheduled;
|
|
128
|
-
this.inlineQueries = options.actions.inlineQueries;
|
|
129
|
-
|
|
130
|
-
this.initializeMessageProcessing(
|
|
131
|
-
options.verboseLoggingForIncomingMessage ?? false
|
|
132
|
-
);
|
|
133
|
-
this.initializeInlineQueryProcessing(1000 as Milliseconds);
|
|
134
|
-
this.initializeScheduledProcessing(
|
|
135
|
-
options.scheduledPeriod ?? hoursToSeconds(1 as Hours)
|
|
69
|
+
},
|
|
70
|
+
scheduledPeriod?: Seconds,
|
|
71
|
+
verboseLoggingForIncomingMessage?: boolean
|
|
72
|
+
) {
|
|
73
|
+
this.actionProcessingService.initialize(
|
|
74
|
+
token,
|
|
75
|
+
actions,
|
|
76
|
+
scheduledPeriod,
|
|
77
|
+
verboseLoggingForIncomingMessage
|
|
136
78
|
);
|
|
137
79
|
|
|
138
|
-
this.telegraf.launch();
|
|
139
|
-
|
|
140
80
|
this.logger.logWithTraceId(
|
|
141
81
|
this.name,
|
|
142
82
|
createTrace(this, this.name, 'Start'),
|
|
@@ -145,168 +85,6 @@ export class BotInstance {
|
|
|
145
85
|
);
|
|
146
86
|
}
|
|
147
87
|
|
|
148
|
-
private initializeScheduledProcessing(period: Seconds) {
|
|
149
|
-
if (this.scheduled.length > 0) {
|
|
150
|
-
const now = moment();
|
|
151
|
-
|
|
152
|
-
if (now.minute() == 0 && now.second() == 0) {
|
|
153
|
-
this.scheduler.createTask(
|
|
154
|
-
'ScheduledProcessing',
|
|
155
|
-
async () => {
|
|
156
|
-
await this.runScheduled();
|
|
157
|
-
},
|
|
158
|
-
secondsToMilliseconds(period),
|
|
159
|
-
true,
|
|
160
|
-
this.name
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let nextExecutionTime = now.clone().startOf('hour');
|
|
167
|
-
if (now.minute() > 0 || now.second() > 0) {
|
|
168
|
-
nextExecutionTime = nextExecutionTime.add(1, 'hour');
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const delay = nextExecutionTime.diff(now);
|
|
172
|
-
|
|
173
|
-
this.scheduler.createOnetimeTask(
|
|
174
|
-
'ScheduledProcessing_OneTime',
|
|
175
|
-
async () => {
|
|
176
|
-
this.scheduler.createTask(
|
|
177
|
-
'ScheduledProcessing',
|
|
178
|
-
async () => {
|
|
179
|
-
await this.runScheduled();
|
|
180
|
-
},
|
|
181
|
-
secondsToMilliseconds(period),
|
|
182
|
-
true,
|
|
183
|
-
this.name
|
|
184
|
-
);
|
|
185
|
-
},
|
|
186
|
-
delay as Milliseconds,
|
|
187
|
-
this.name
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
private initializeInlineQueryProcessing(period: Milliseconds) {
|
|
193
|
-
let pendingInlineQueries: IncomingInlineQuery[] = [];
|
|
194
|
-
|
|
195
|
-
if (this.inlineQueries.length > 0) {
|
|
196
|
-
this.telegraf.on('inline_query', async (ctx) => {
|
|
197
|
-
const query = new IncomingInlineQuery(
|
|
198
|
-
ctx.inlineQuery.id,
|
|
199
|
-
ctx.inlineQuery.query,
|
|
200
|
-
ctx.inlineQuery.from.id,
|
|
201
|
-
createTrace('InlineQuery', this.name, ctx.inlineQuery.id)
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
this.logger.logWithTraceId(
|
|
205
|
-
this.name,
|
|
206
|
-
query.traceId,
|
|
207
|
-
'Query',
|
|
208
|
-
`${ctx.inlineQuery.from.username} (${ctx.inlineQuery.from.id}): Query for ${ctx.inlineQuery.query}`
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
pendingInlineQueries = pendingInlineQueries.filter(
|
|
212
|
-
(q) => q.userId != query.userId
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
pendingInlineQueries.push(query);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
this.scheduler.createTask(
|
|
219
|
-
'InlineQueryProcessing',
|
|
220
|
-
async () => {
|
|
221
|
-
const ctx = new InlineQueryContext(
|
|
222
|
-
this.storage,
|
|
223
|
-
this.logger,
|
|
224
|
-
this.scheduler
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
const queriesToProcess = [...pendingInlineQueries];
|
|
228
|
-
pendingInlineQueries = [];
|
|
229
|
-
|
|
230
|
-
for (const inlineQuery of queriesToProcess) {
|
|
231
|
-
for (const inlineQueryAction of this.inlineQueries) {
|
|
232
|
-
ctx.initializeContext(
|
|
233
|
-
inlineQuery.query,
|
|
234
|
-
inlineQuery.queryId,
|
|
235
|
-
this.name,
|
|
236
|
-
inlineQueryAction,
|
|
237
|
-
inlineQuery.traceId
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
try {
|
|
241
|
-
const responses = await inlineQueryAction.exec(
|
|
242
|
-
ctx
|
|
243
|
-
);
|
|
244
|
-
this.api.enqueueBatchedResponses(responses);
|
|
245
|
-
ctx.isInitialized = false;
|
|
246
|
-
} catch (error) {
|
|
247
|
-
this.logger.errorWithTraceId(
|
|
248
|
-
ctx.botName,
|
|
249
|
-
ctx.traceId,
|
|
250
|
-
'Unknown',
|
|
251
|
-
error,
|
|
252
|
-
ctx
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
this.api.flushResponses();
|
|
259
|
-
},
|
|
260
|
-
period,
|
|
261
|
-
false,
|
|
262
|
-
this.name
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private initializeMessageProcessing(
|
|
268
|
-
verboseLoggingForIncomingMessage: boolean
|
|
269
|
-
) {
|
|
270
|
-
if (this.commands.length > 0) {
|
|
271
|
-
const triggersToBeProcessed = new Set(
|
|
272
|
-
this.commands
|
|
273
|
-
.flatMap((x) => x.triggers)
|
|
274
|
-
.map((x) =>
|
|
275
|
-
typeof x == 'string'
|
|
276
|
-
? x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)
|
|
277
|
-
? x
|
|
278
|
-
: MessageType.Text
|
|
279
|
-
: MessageType.Text
|
|
280
|
-
)
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
this.telegraf.on('message', async (ctx) => {
|
|
284
|
-
const msg = new IncomingMessage(ctx.update.message, this.name);
|
|
285
|
-
|
|
286
|
-
if (verboseLoggingForIncomingMessage) {
|
|
287
|
-
this.logger.logObjectWithTraceId(
|
|
288
|
-
this.name,
|
|
289
|
-
msg.traceId,
|
|
290
|
-
msg.chatInfo.name,
|
|
291
|
-
ctx.update.message
|
|
292
|
-
);
|
|
293
|
-
} else {
|
|
294
|
-
this.logger.logWithTraceId(
|
|
295
|
-
this.name,
|
|
296
|
-
msg.traceId,
|
|
297
|
-
msg.chatInfo.name,
|
|
298
|
-
`${msg.from?.first_name ?? 'Unknown'} (${
|
|
299
|
-
msg.from?.id ?? 'Unknown'
|
|
300
|
-
}): ${msg.text || `<non-text message: ${msg.type}>`}`
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (triggersToBeProcessed.has(msg.type))
|
|
305
|
-
await this.processMessage(msg);
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
88
|
async stop(code: string) {
|
|
311
89
|
this.logger.logWithTraceId(
|
|
312
90
|
this.name,
|
|
@@ -317,112 +95,6 @@ export class BotInstance {
|
|
|
317
95
|
|
|
318
96
|
this.scheduler.stopAll();
|
|
319
97
|
await this.storage.close();
|
|
320
|
-
this.
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
private initializeMessageContext<TActionState extends IActionState>(
|
|
324
|
-
ctx: MessageContext<IActionState>,
|
|
325
|
-
action: IActionWithState<TActionState>,
|
|
326
|
-
message: IncomingMessage
|
|
327
|
-
) {
|
|
328
|
-
ctx.messageId = message.message_id;
|
|
329
|
-
ctx.messageText = message.text ?? '';
|
|
330
|
-
ctx.messageType = message.type;
|
|
331
|
-
ctx.fromUserId = message.from?.id;
|
|
332
|
-
ctx.fromUserName =
|
|
333
|
-
(message.from?.first_name ?? 'Unknown user') +
|
|
334
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
335
|
-
ctx.messageUpdateObject = message.updateObject;
|
|
336
|
-
|
|
337
|
-
ctx.matchResults = [];
|
|
338
|
-
ctx.startCooldown = true;
|
|
339
|
-
|
|
340
|
-
this.initializeChatContext(
|
|
341
|
-
ctx,
|
|
342
|
-
action,
|
|
343
|
-
message.chatInfo,
|
|
344
|
-
message.traceId
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
private initializeChatContext<TActionState extends IActionState>(
|
|
349
|
-
ctx: ChatContext<IActionState>,
|
|
350
|
-
action: IActionWithState<TActionState>,
|
|
351
|
-
chatInfo: ChatInfo,
|
|
352
|
-
traceId: TraceId
|
|
353
|
-
) {
|
|
354
|
-
ctx.responses = [];
|
|
355
|
-
ctx.isInitialized = true;
|
|
356
|
-
ctx.botName = this.name;
|
|
357
|
-
ctx.action = action;
|
|
358
|
-
ctx.chatInfo = chatInfo;
|
|
359
|
-
ctx.traceId = traceId;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
private async runScheduled() {
|
|
363
|
-
const ctx = new ChatContext<IActionState>(
|
|
364
|
-
this.storage,
|
|
365
|
-
this.logger,
|
|
366
|
-
this.scheduler
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
370
|
-
for (const scheduledAction of this.scheduled) {
|
|
371
|
-
this.initializeChatContext(
|
|
372
|
-
ctx,
|
|
373
|
-
scheduledAction,
|
|
374
|
-
new ChatInfo(chatId, chatName),
|
|
375
|
-
createTrace(
|
|
376
|
-
scheduledAction,
|
|
377
|
-
this.name,
|
|
378
|
-
`${scheduledAction.key}-${chatId}`
|
|
379
|
-
)
|
|
380
|
-
);
|
|
381
|
-
|
|
382
|
-
try {
|
|
383
|
-
const responses = await scheduledAction.exec(ctx);
|
|
384
|
-
this.api.enqueueBatchedResponses(responses);
|
|
385
|
-
ctx.isInitialized = false;
|
|
386
|
-
} catch (error) {
|
|
387
|
-
this.logger.errorWithTraceId(
|
|
388
|
-
ctx.botName,
|
|
389
|
-
ctx.traceId,
|
|
390
|
-
chatName,
|
|
391
|
-
error,
|
|
392
|
-
ctx
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
this.api.flushResponses();
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
private async processMessage(msg: IncomingMessage) {
|
|
402
|
-
const ctx = new MessageContext<IActionState>(
|
|
403
|
-
this.storage,
|
|
404
|
-
this.logger,
|
|
405
|
-
this.scheduler
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
for (const commandAction of this.commands) {
|
|
409
|
-
this.initializeMessageContext(ctx, commandAction, msg);
|
|
410
|
-
|
|
411
|
-
try {
|
|
412
|
-
const responses = await commandAction.exec(ctx);
|
|
413
|
-
this.api.enqueueBatchedResponses(responses);
|
|
414
|
-
ctx.isInitialized = false;
|
|
415
|
-
} catch (error) {
|
|
416
|
-
this.logger.errorWithTraceId(
|
|
417
|
-
ctx.botName,
|
|
418
|
-
ctx.traceId,
|
|
419
|
-
ctx.chatInfo.name,
|
|
420
|
-
error,
|
|
421
|
-
ctx
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
this.api.flushResponses();
|
|
98
|
+
this.actionProcessingService.stop(code);
|
|
427
99
|
}
|
|
428
100
|
}
|
|
@@ -8,15 +8,18 @@ import {
|
|
|
8
8
|
MessageSendingOptions,
|
|
9
9
|
TextMessageSendingOptions
|
|
10
10
|
} from '../../types/messageSendingOptions';
|
|
11
|
-
import { IActionWithState } from '../../types/
|
|
11
|
+
import { IActionWithState } from '../../types/action';
|
|
12
12
|
import { IActionState } from '../../types/actionState';
|
|
13
|
-
import { BotResponse } from '../../types/response';
|
|
13
|
+
import { BotResponse, IReplyResponse } from '../../types/response';
|
|
14
14
|
import { Milliseconds } from '../../types/timeValues';
|
|
15
15
|
import { DelayResponse } from '../../dtos/responses/delay';
|
|
16
16
|
import { ChatInfo } from '../../dtos/chatInfo';
|
|
17
17
|
import { ILogger } from '../../types/logger';
|
|
18
18
|
import { IScheduler } from '../../types/scheduler';
|
|
19
19
|
import { TraceId } from '../../types/trace';
|
|
20
|
+
import { ICaptureController } from '../../types/capture';
|
|
21
|
+
import { CommandTrigger } from '../../types/commandTrigger';
|
|
22
|
+
import { ReplyContext } from './replyContext';
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* Context of action executed in chat.
|
|
@@ -52,6 +55,27 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
52
55
|
this.scheduler = scheduler;
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
protected createCaptureController_TEMP(
|
|
59
|
+
response: IReplyResponse
|
|
60
|
+
): ICaptureController {
|
|
61
|
+
return {
|
|
62
|
+
captureReplies: (
|
|
63
|
+
trigger: CommandTrigger[],
|
|
64
|
+
handler: (
|
|
65
|
+
replyContext: ReplyContext<TActionState>
|
|
66
|
+
) => Promise<void>,
|
|
67
|
+
abortController: AbortController
|
|
68
|
+
) => {
|
|
69
|
+
response.captures.push({
|
|
70
|
+
trigger,
|
|
71
|
+
handler,
|
|
72
|
+
abortController,
|
|
73
|
+
action: this.action
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
55
79
|
/**
|
|
56
80
|
* Collection of actions that send something to chat as a standalone message.
|
|
57
81
|
*/
|
|
@@ -63,16 +87,18 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
63
87
|
* @param options Message sending option.
|
|
64
88
|
*/
|
|
65
89
|
text: (text: string, options?: TextMessageSendingOptions) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
options
|
|
74
|
-
)
|
|
90
|
+
const response = new TextMessage(
|
|
91
|
+
text,
|
|
92
|
+
this.chatInfo,
|
|
93
|
+
this.traceId,
|
|
94
|
+
this.action,
|
|
95
|
+
undefined,
|
|
96
|
+
options
|
|
75
97
|
);
|
|
98
|
+
|
|
99
|
+
this.responses.push(response);
|
|
100
|
+
|
|
101
|
+
return this.createCaptureController_TEMP(response);
|
|
76
102
|
},
|
|
77
103
|
|
|
78
104
|
/**
|
|
@@ -82,16 +108,18 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
82
108
|
* @param options Message sending option.
|
|
83
109
|
*/
|
|
84
110
|
image: (name: string, options?: MessageSendingOptions) => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
options
|
|
93
|
-
)
|
|
111
|
+
const response = new ImageMessage(
|
|
112
|
+
{ source: resolve(`./content/${name}.png`) },
|
|
113
|
+
this.chatInfo,
|
|
114
|
+
this.traceId,
|
|
115
|
+
this.action,
|
|
116
|
+
undefined,
|
|
117
|
+
options
|
|
94
118
|
);
|
|
119
|
+
|
|
120
|
+
this.responses.push(response);
|
|
121
|
+
|
|
122
|
+
return this.createCaptureController_TEMP(response);
|
|
95
123
|
},
|
|
96
124
|
|
|
97
125
|
/**
|
|
@@ -101,16 +129,18 @@ export class ChatContext<TActionState extends IActionState> {
|
|
|
101
129
|
* @param options Message sending option.
|
|
102
130
|
*/
|
|
103
131
|
video: (name: string, options?: MessageSendingOptions) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
options
|
|
112
|
-
)
|
|
132
|
+
const response = new VideoMessage(
|
|
133
|
+
{ source: resolve(`./content/${name}.mp4`) },
|
|
134
|
+
this.chatInfo,
|
|
135
|
+
this.traceId,
|
|
136
|
+
this.action,
|
|
137
|
+
undefined,
|
|
138
|
+
options
|
|
113
139
|
);
|
|
140
|
+
|
|
141
|
+
this.responses.push(response);
|
|
142
|
+
|
|
143
|
+
return this.createCaptureController_TEMP(response);
|
|
114
144
|
}
|
|
115
145
|
};
|
|
116
146
|
|
|
@@ -8,8 +8,8 @@ import { InlineQueryAction } from '../actions/inlineQueryAction';
|
|
|
8
8
|
import { InlineQueryResponse } from '../../dtos/responses/inlineQueryResponse';
|
|
9
9
|
|
|
10
10
|
export class InlineQueryContext {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
action!: InlineQueryAction;
|
|
12
|
+
queryResults: InlineQueryResult[] = [];
|
|
13
13
|
|
|
14
14
|
/** Storage client instance for the bot executing this action. */
|
|
15
15
|
readonly storage: IStorageClient;
|
|
@@ -22,6 +22,11 @@ export class InlineQueryContext {
|
|
|
22
22
|
traceId!: TraceId;
|
|
23
23
|
/** Name of a bot that executes this action. */
|
|
24
24
|
botName!: string;
|
|
25
|
+
/**
|
|
26
|
+
* Abort signal to be utilized in query handler.
|
|
27
|
+
* Signal will be aborted if new query comes from the same user.
|
|
28
|
+
*/
|
|
29
|
+
abortSignal!: AbortSignal;
|
|
25
30
|
|
|
26
31
|
/** Ordered collection of responses to be processed */
|
|
27
32
|
get responses(): BotResponse[] {
|
|
@@ -53,26 +58,6 @@ export class InlineQueryContext {
|
|
|
53
58
|
this.scheduler = scheduler;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
initializeContext(
|
|
57
|
-
queryText: string,
|
|
58
|
-
queryId: string,
|
|
59
|
-
botName: string,
|
|
60
|
-
action: InlineQueryAction,
|
|
61
|
-
traceId: TraceId
|
|
62
|
-
) {
|
|
63
|
-
this.queryText = queryText;
|
|
64
|
-
this.queryId = queryId;
|
|
65
|
-
this.botName = botName;
|
|
66
|
-
this.action = action;
|
|
67
|
-
this.traceId = traceId;
|
|
68
|
-
|
|
69
|
-
this.isInitialized = true;
|
|
70
|
-
this.queryResults = [];
|
|
71
|
-
this.matchResults = [];
|
|
72
|
-
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
61
|
/**
|
|
77
62
|
* This result will be shown to user as a response to inline query.
|
|
78
63
|
* @param queryResult Inline query result to be shown to user.
|