chz-telegram-bot 0.0.24 → 0.0.25
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/entities/actions/scheduledAction.d.ts +1 -1
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +1 -0
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +6 -6
- package/dist/entities/context/chatContext.d.ts +7 -1
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +8 -0
- package/dist/entities/context/messageContext.d.ts +1 -7
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +0 -8
- package/dist/services/telegramApi.d.ts +5 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +4 -4
- package/dist/types/handlers.d.ts +1 -1
- package/dist/types/handlers.d.ts.map +1 -1
- package/entities/actions/scheduledAction.ts +2 -1
- package/entities/botInstance.ts +9 -6
- package/entities/context/chatContext.ts +12 -1
- package/entities/context/messageContext.ts +1 -12
- package/package.json +1 -1
- package/services/telegramApi.ts +14 -8
- package/types/handlers.ts +1 -1
|
@@ -17,7 +17,7 @@ export declare class ScheduledAction<TActionState extends IActionState> implemen
|
|
|
17
17
|
cachedStateFactories: Map<string, CachedStateFactory>;
|
|
18
18
|
handler: ScheduledHandler<TActionState>;
|
|
19
19
|
constructor(name: string, handler: ScheduledHandler<TActionState>, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>, stateConstructor: () => TActionState);
|
|
20
|
-
exec(ctx: ChatContext): Promise<void>;
|
|
20
|
+
exec(ctx: ChatContext<TActionState>): Promise<void>;
|
|
21
21
|
private getCachedValue;
|
|
22
22
|
private shouldTrigger;
|
|
23
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAKrD,qBAAa,eAAe,CAAC,YAAY,SAAS,YAAY,CAC1D,YAAW,gBAAgB;IAE3B,MAAM,CAAC,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,uBAA8B;IACzC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAGpC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAYlC,IAAI,CAAC,GAAG,EAAE,WAAW;
|
|
1
|
+
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IA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executing [${this.name}] in ${ctx.chatId}`);
|
|
32
32
|
await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName), state);
|
|
33
33
|
state.lastExecutedDate = (0, moment_1.default)().valueOf();
|
|
34
|
+
ctx.updateActions.forEach((action) => action(state));
|
|
34
35
|
await ctx.storage.saveActionExecutionResult(this, ctx.chatId, new actionExecutionResult_1.ActionExecutionResult(state, isAllowedToTrigger));
|
|
35
36
|
}
|
|
36
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAM5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IA6CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAM5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IA6CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAyBZ,cAAc;CAmB/B"}
|
|
@@ -71,10 +71,10 @@ class BotInstance {
|
|
|
71
71
|
}
|
|
72
72
|
async runScheduled() {
|
|
73
73
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
74
|
-
for (const
|
|
75
|
-
const ctx = this.api.createContextForChat(chatId,
|
|
74
|
+
for (const scheduledAction of this.scheduled) {
|
|
75
|
+
const ctx = this.api.createContextForChat(chatId, scheduledAction);
|
|
76
76
|
try {
|
|
77
|
-
await
|
|
77
|
+
await scheduledAction.exec(ctx);
|
|
78
78
|
}
|
|
79
79
|
catch (error) {
|
|
80
80
|
logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
|
|
@@ -84,10 +84,10 @@ class BotInstance {
|
|
|
84
84
|
await this.api.flushResponses();
|
|
85
85
|
}
|
|
86
86
|
async processMessage(msg) {
|
|
87
|
-
for (const
|
|
88
|
-
const ctx = this.api.createContextForMessage(msg,
|
|
87
|
+
for (const commandAction of this.commands) {
|
|
88
|
+
const ctx = this.api.createContextForMessage(msg, commandAction);
|
|
89
89
|
try {
|
|
90
|
-
await
|
|
90
|
+
await commandAction.exec(ctx);
|
|
91
91
|
}
|
|
92
92
|
catch (error) {
|
|
93
93
|
logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, error, ctx);
|
|
@@ -4,9 +4,10 @@ import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/me
|
|
|
4
4
|
/**
|
|
5
5
|
* Context of action executed in chat.
|
|
6
6
|
*/
|
|
7
|
-
export declare class ChatContext {
|
|
7
|
+
export declare class ChatContext<TActionState> {
|
|
8
8
|
protected actionKey: string;
|
|
9
9
|
protected interactions: IBotApiInteractions;
|
|
10
|
+
updateActions: Array<(state: TActionState) => void>;
|
|
10
11
|
/** Trace id of a action execution. */
|
|
11
12
|
traceId: number | string;
|
|
12
13
|
/** Name of a bot that executes this action. */
|
|
@@ -18,6 +19,11 @@ export declare class ChatContext {
|
|
|
18
19
|
/** Storage client instance for this bot. */
|
|
19
20
|
storage: IStorageClient;
|
|
20
21
|
constructor(botName: string, actionKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient);
|
|
22
|
+
/**
|
|
23
|
+
* Manually update the state of an action.
|
|
24
|
+
* @param stateUpdateAction Function that will modify state.
|
|
25
|
+
*/
|
|
26
|
+
updateState(stateUpdateAction: (state: TActionState) => void): void;
|
|
21
27
|
/**
|
|
22
28
|
* Sends text message to chat.
|
|
23
29
|
* @param text Message contents.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,WAAW;
|
|
1
|
+
{"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,WAAW,CAAC,YAAY;IACjC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC;IAC5B,SAAS,CAAC,YAAY,EAAE,mBAAmB,CAAC;IAC5C,aAAa,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,CAAM;IACzD,sCAAsC;IACtC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,+CAA+C;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,OAAO,EAAE,cAAc,CAAC;gBAGpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,mBAAmB,EACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,OAAO,EAAE,cAAc;IAW3B;;;OAGG;IACH,WAAW,CAAC,iBAAiB,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI;IAM5D;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAahE;;;;OAIG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;;OAIG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc7D;;;OAGG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM;CAUjC"}
|
|
@@ -11,6 +11,7 @@ const unpin_1 = require("../responses/unpin");
|
|
|
11
11
|
*/
|
|
12
12
|
class ChatContext {
|
|
13
13
|
constructor(botName, actionKey, interactions, chatId, chatName, traceId, storage) {
|
|
14
|
+
this.updateActions = [];
|
|
14
15
|
this.botName = botName;
|
|
15
16
|
this.actionKey = actionKey;
|
|
16
17
|
this.interactions = interactions;
|
|
@@ -19,6 +20,13 @@ class ChatContext {
|
|
|
19
20
|
this.traceId = traceId;
|
|
20
21
|
this.storage = storage;
|
|
21
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Manually update the state of an action.
|
|
25
|
+
* @param stateUpdateAction Function that will modify state.
|
|
26
|
+
*/
|
|
27
|
+
updateState(stateUpdateAction) {
|
|
28
|
+
this.updateActions.push(stateUpdateAction);
|
|
29
|
+
}
|
|
22
30
|
/**
|
|
23
31
|
* Sends text message to chat.
|
|
24
32
|
* @param text Message contents.
|
|
@@ -7,8 +7,7 @@ import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/me
|
|
|
7
7
|
/**
|
|
8
8
|
* Context of action executed in chat, in response to a message
|
|
9
9
|
*/
|
|
10
|
-
export declare class MessageContext<TActionState extends IActionState> extends ChatContext {
|
|
11
|
-
updateActions: Array<(state: TActionState) => void>;
|
|
10
|
+
export declare class MessageContext<TActionState extends IActionState> extends ChatContext<TActionState> {
|
|
12
11
|
/** Id of a message that triggered this action. */
|
|
13
12
|
messageId: number;
|
|
14
13
|
/** Text of a message that triggered this action. */
|
|
@@ -28,11 +27,6 @@ export declare class MessageContext<TActionState extends IActionState> extends C
|
|
|
28
27
|
* @template TAnotherActionState - Type of a state that is used by another action.
|
|
29
28
|
*/
|
|
30
29
|
loadStateOf<TAnotherActionState extends IActionState>(commandName: string): Promise<TAnotherActionState>;
|
|
31
|
-
/**
|
|
32
|
-
* Manually update the state of an action.
|
|
33
|
-
* @param stateUpdateAction Function that will modify state.
|
|
34
|
-
*/
|
|
35
|
-
updateState(stateUpdateAction: (state: TActionState) => void): void;
|
|
36
30
|
/**
|
|
37
31
|
* Reply with text message to message that triggered this action.
|
|
38
32
|
* @param text Message contents.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW
|
|
1
|
+
{"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM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AAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,aAAa;CAW7B"}
|
|
@@ -14,7 +14,6 @@ const chatContext_1 = require("./chatContext");
|
|
|
14
14
|
class MessageContext extends chatContext_1.ChatContext {
|
|
15
15
|
constructor(botName, scheduledKey, interactions, chatId, chatName, messageId, messageText, fromUserId, traceId, fromUserName, storage) {
|
|
16
16
|
super(botName, scheduledKey, interactions, chatId, chatName, traceId, storage);
|
|
17
|
-
this.updateActions = [];
|
|
18
17
|
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
19
18
|
this.matchResults = [];
|
|
20
19
|
/** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
|
|
@@ -32,13 +31,6 @@ class MessageContext extends chatContext_1.ChatContext {
|
|
|
32
31
|
async loadStateOf(commandName) {
|
|
33
32
|
return ((await this.storage.load(`command:${commandName.replace('.', '-')}`))[this.chatId] ?? new actionStateBase_1.ActionStateBase());
|
|
34
33
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Manually update the state of an action.
|
|
37
|
-
* @param stateUpdateAction Function that will modify state.
|
|
38
|
-
*/
|
|
39
|
-
updateState(stateUpdateAction) {
|
|
40
|
-
this.updateActions.push(stateUpdateAction);
|
|
41
|
-
}
|
|
42
34
|
/**
|
|
43
35
|
* Reply with text message to message that triggered this action.
|
|
44
36
|
* @param text Message contents.
|
|
@@ -9,6 +9,9 @@ import { TextMessage } from '../entities/responses/textMessage';
|
|
|
9
9
|
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
10
10
|
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
11
11
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
12
|
+
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
13
|
+
import { IActionState } from '../types/actionState';
|
|
14
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
12
15
|
export declare class TelegramApiService {
|
|
13
16
|
isFlushing: boolean;
|
|
14
17
|
messageQueue: Array<BotResponse>;
|
|
@@ -22,8 +25,8 @@ export declare class TelegramApiService {
|
|
|
22
25
|
private processResponse;
|
|
23
26
|
private enqueue;
|
|
24
27
|
private getInteractions;
|
|
25
|
-
createContextForMessage(incomingMessage: IncomingMessage,
|
|
26
|
-
createContextForChat(chatId: number,
|
|
28
|
+
createContextForMessage<TActionState extends IActionState>(incomingMessage: IncomingMessage, command: CommandAction<TActionState>): MessageContext<TActionState>;
|
|
29
|
+
createContextForChat<TActionState extends IActionState>(chatId: number, scheduledAction: ScheduledAction<TActionState>): ChatContext<TActionState>;
|
|
27
30
|
}
|
|
28
31
|
export interface IBotApiInteractions {
|
|
29
32
|
respond: (response: TextMessage | VideoMessage | ImageMessage) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"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YAAY,EACrD,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,aAAa,CAAC,YAAY,CAAC;IAsBxC,oBAAoB,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,MAAM,EACd,eAAe,EAAE,eAAe,CAAC,YAAY,CAAC;CAYrD;AAED,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,KAAK,IAAI,CAAC;IACvE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IACpC,KAAK,EAAE,CAAC,YAAY,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD"}
|
|
@@ -93,15 +93,15 @@ class TelegramApiService {
|
|
|
93
93
|
unpin: (unpinMessage) => this.enqueue(unpinMessage)
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
|
-
createContextForMessage(incomingMessage,
|
|
96
|
+
createContextForMessage(incomingMessage, command) {
|
|
97
97
|
const firstName = incomingMessage.from?.first_name ?? 'Unknown user';
|
|
98
98
|
const lastName = incomingMessage.from?.last_name
|
|
99
99
|
? ` ${incomingMessage.from?.last_name}`
|
|
100
100
|
: '';
|
|
101
|
-
return new messageContext_1.MessageContext(this.botName,
|
|
101
|
+
return new messageContext_1.MessageContext(this.botName, command.key, this.getInteractions(), incomingMessage.chat.id, incomingMessage.chatName, incomingMessage.message_id, incomingMessage.text, incomingMessage.from?.id, incomingMessage.traceId, firstName + lastName, this.storage);
|
|
102
102
|
}
|
|
103
|
-
createContextForChat(chatId,
|
|
104
|
-
return new chatContext_1.ChatContext(this.botName,
|
|
103
|
+
createContextForChat(chatId, scheduledAction) {
|
|
104
|
+
return new chatContext_1.ChatContext(this.botName, scheduledAction.key, this.getInteractions(), chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
exports.TelegramApiService = TelegramApiService;
|
package/dist/types/handlers.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ ctx: MessageContext<TActionState>,
|
|
|
9
9
|
state: TActionState) => Promise<void>;
|
|
10
10
|
export type ScheduledHandler<TActionState extends IActionState> = (
|
|
11
11
|
/** Context of action executed in chat. */
|
|
12
|
-
ctx: ChatContext
|
|
12
|
+
ctx: ChatContext<TActionState>,
|
|
13
13
|
/** Function that will attempt to get value from cache. If there is no value found, corresponding cached state factory will be called. */
|
|
14
14
|
getCached: CachedValueAccessor,
|
|
15
15
|
/** State of an action being executed. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI;AAC5D,oEAAoE;AACpE,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;AACjC,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI;AAC9D,0CAA0C;AAC1C,GAAG,EAAE,WAAW;
|
|
1
|
+
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI;AAC5D,oEAAoE;AACpE,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;AACjC,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI;AAC9D,0CAA0C;AAC1C,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC;AAC9B,yIAAyI;AACzI,SAAS,EAAE,mBAAmB;AAC9B,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -46,7 +46,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
46
46
|
this.stateConstructor = stateConstructor;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
async exec(ctx: ChatContext) {
|
|
49
|
+
async exec(ctx: ChatContext<TActionState>) {
|
|
50
50
|
if (!this.active || !this.chatsWhitelist.includes(ctx.chatId)) return;
|
|
51
51
|
|
|
52
52
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
@@ -72,6 +72,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
72
72
|
|
|
73
73
|
state.lastExecutedDate = moment().valueOf();
|
|
74
74
|
|
|
75
|
+
ctx.updateActions.forEach((action) => action(state));
|
|
75
76
|
await ctx.storage.saveActionExecutionResult(
|
|
76
77
|
this,
|
|
77
78
|
ctx.chatId,
|
package/entities/botInstance.ts
CHANGED
|
@@ -144,11 +144,14 @@ export class BotInstance {
|
|
|
144
144
|
|
|
145
145
|
private async runScheduled() {
|
|
146
146
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
147
|
-
for (const
|
|
148
|
-
const ctx = this.api.createContextForChat(
|
|
147
|
+
for (const scheduledAction of this.scheduled) {
|
|
148
|
+
const ctx = this.api.createContextForChat(
|
|
149
|
+
chatId,
|
|
150
|
+
scheduledAction
|
|
151
|
+
);
|
|
149
152
|
|
|
150
153
|
try {
|
|
151
|
-
await
|
|
154
|
+
await scheduledAction.exec(ctx);
|
|
152
155
|
} catch (error) {
|
|
153
156
|
Logger.errorWithTraceId(
|
|
154
157
|
ctx.botName,
|
|
@@ -165,11 +168,11 @@ export class BotInstance {
|
|
|
165
168
|
}
|
|
166
169
|
|
|
167
170
|
private async processMessage(msg: IncomingMessage) {
|
|
168
|
-
for (const
|
|
169
|
-
const ctx = this.api.createContextForMessage(msg,
|
|
171
|
+
for (const commandAction of this.commands) {
|
|
172
|
+
const ctx = this.api.createContextForMessage(msg, commandAction);
|
|
170
173
|
|
|
171
174
|
try {
|
|
172
|
-
await
|
|
175
|
+
await commandAction.exec(ctx);
|
|
173
176
|
} catch (error) {
|
|
174
177
|
Logger.errorWithTraceId(
|
|
175
178
|
ctx.botName,
|
|
@@ -13,9 +13,10 @@ import {
|
|
|
13
13
|
/**
|
|
14
14
|
* Context of action executed in chat.
|
|
15
15
|
*/
|
|
16
|
-
export class ChatContext {
|
|
16
|
+
export class ChatContext<TActionState> {
|
|
17
17
|
protected actionKey: string;
|
|
18
18
|
protected interactions: IBotApiInteractions;
|
|
19
|
+
updateActions: Array<(state: TActionState) => void> = [];
|
|
19
20
|
/** Trace id of a action execution. */
|
|
20
21
|
traceId: number | string;
|
|
21
22
|
/** Name of a bot that executes this action. */
|
|
@@ -45,6 +46,16 @@ export class ChatContext {
|
|
|
45
46
|
this.storage = storage;
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Manually update the state of an action.
|
|
51
|
+
* @param stateUpdateAction Function that will modify state.
|
|
52
|
+
*/
|
|
53
|
+
updateState(stateUpdateAction: (state: TActionState) => void) {
|
|
54
|
+
this.updateActions.push(
|
|
55
|
+
stateUpdateAction as (state: TActionState) => void
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
48
59
|
/**
|
|
49
60
|
* Sends text message to chat.
|
|
50
61
|
* @param text Message contents.
|
|
@@ -19,8 +19,7 @@ import {
|
|
|
19
19
|
*/
|
|
20
20
|
export class MessageContext<
|
|
21
21
|
TActionState extends IActionState
|
|
22
|
-
> extends ChatContext {
|
|
23
|
-
updateActions: Array<(state: TActionState) => void> = [];
|
|
22
|
+
> extends ChatContext<TActionState> {
|
|
24
23
|
/** Id of a message that triggered this action. */
|
|
25
24
|
messageId: number;
|
|
26
25
|
/** Text of a message that triggered this action. */
|
|
@@ -80,16 +79,6 @@ export class MessageContext<
|
|
|
80
79
|
);
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
/**
|
|
84
|
-
* Manually update the state of an action.
|
|
85
|
-
* @param stateUpdateAction Function that will modify state.
|
|
86
|
-
*/
|
|
87
|
-
updateState(stateUpdateAction: (state: TActionState) => void) {
|
|
88
|
-
this.updateActions.push(
|
|
89
|
-
stateUpdateAction as (state: TActionState) => void
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
82
|
/**
|
|
94
83
|
* Reply with text message to message that triggered this action.
|
|
95
84
|
* @param text Message contents.
|
package/package.json
CHANGED
package/services/telegramApi.ts
CHANGED
|
@@ -14,6 +14,9 @@ import { ImageMessage } from '../entities/responses/imageMessage';
|
|
|
14
14
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
15
15
|
import { setTimeout } from 'timers/promises';
|
|
16
16
|
import { Milliseconds } from '../types/timeValues';
|
|
17
|
+
import { ScheduledAction } from '../entities/actions/scheduledAction';
|
|
18
|
+
import { IActionState } from '../types/actionState';
|
|
19
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
17
20
|
|
|
18
21
|
export class TelegramApiService {
|
|
19
22
|
isFlushing = false;
|
|
@@ -171,18 +174,18 @@ export class TelegramApiService {
|
|
|
171
174
|
} as IBotApiInteractions;
|
|
172
175
|
}
|
|
173
176
|
|
|
174
|
-
createContextForMessage(
|
|
177
|
+
createContextForMessage<TActionState extends IActionState>(
|
|
175
178
|
incomingMessage: IncomingMessage,
|
|
176
|
-
|
|
179
|
+
command: CommandAction<TActionState>
|
|
177
180
|
) {
|
|
178
181
|
const firstName = incomingMessage.from?.first_name ?? 'Unknown user';
|
|
179
182
|
const lastName = incomingMessage.from?.last_name
|
|
180
183
|
? ` ${incomingMessage.from?.last_name}`
|
|
181
184
|
: '';
|
|
182
185
|
|
|
183
|
-
return new MessageContext(
|
|
186
|
+
return new MessageContext<TActionState>(
|
|
184
187
|
this.botName,
|
|
185
|
-
|
|
188
|
+
command.key,
|
|
186
189
|
this.getInteractions(),
|
|
187
190
|
incomingMessage.chat.id,
|
|
188
191
|
incomingMessage.chatName,
|
|
@@ -195,14 +198,17 @@ export class TelegramApiService {
|
|
|
195
198
|
);
|
|
196
199
|
}
|
|
197
200
|
|
|
198
|
-
createContextForChat
|
|
199
|
-
|
|
201
|
+
createContextForChat<TActionState extends IActionState>(
|
|
202
|
+
chatId: number,
|
|
203
|
+
scheduledAction: ScheduledAction<TActionState>
|
|
204
|
+
) {
|
|
205
|
+
return new ChatContext<TActionState>(
|
|
200
206
|
this.botName,
|
|
201
|
-
|
|
207
|
+
scheduledAction.key,
|
|
202
208
|
this.getInteractions(),
|
|
203
209
|
chatId,
|
|
204
210
|
this.chats[chatId],
|
|
205
|
-
`Scheduled:${
|
|
211
|
+
`Scheduled:${scheduledAction.key}:${chatId}`,
|
|
206
212
|
this.storage
|
|
207
213
|
);
|
|
208
214
|
}
|
package/types/handlers.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type CommandHandler<TActionState extends IActionState> = (
|
|
|
12
12
|
|
|
13
13
|
export type ScheduledHandler<TActionState extends IActionState> = (
|
|
14
14
|
/** Context of action executed in chat. */
|
|
15
|
-
ctx: ChatContext
|
|
15
|
+
ctx: ChatContext<TActionState>,
|
|
16
16
|
/** Function that will attempt to get value from cache. If there is no value found, corresponding cached state factory will be called. */
|
|
17
17
|
getCached: CachedValueAccessor,
|
|
18
18
|
/** State of an action being executed. */
|