chz-telegram-bot 0.0.43 → 0.0.44
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/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +2 -0
- 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 +14 -3
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +6 -2
- package/dist/entities/context/chatContext.d.ts +3 -1
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +7 -1
- package/dist/entities/context/messageContext.d.ts +2 -1
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +7 -2
- package/dist/services/telegramApi.d.ts +2 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +4 -6
- package/entities/actions/commandAction.ts +2 -0
- package/entities/actions/scheduledAction.ts +14 -3
- package/entities/botInstance.ts +8 -5
- package/entities/context/chatContext.ts +17 -8
- package/entities/context/messageContext.ts +21 -14
- package/package.json +1 -1
- package/services/telegramApi.ts +6 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAG3D,OAAO,EAAE,cAAc,EAAE,M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AE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAE5D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB;IAE3B,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,EAAE,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,GAAG,EAAE,SAAS,CAAC;gBAGX,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,EACjB,cAAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY;IAelC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAkD5C,OAAO,CAAC,YAAY;CAwDvB"}
|
|
@@ -24,6 +24,8 @@ class CommandAction {
|
|
|
24
24
|
this.key = `command:${this.name.replace('.', '-')}`;
|
|
25
25
|
}
|
|
26
26
|
async exec(ctx) {
|
|
27
|
+
if (!ctx.isInitialized)
|
|
28
|
+
throw new Error('Context is not initialized');
|
|
27
29
|
if (!this.active || this.chatsBlacklist.includes(ctx.chatId))
|
|
28
30
|
return;
|
|
29
31
|
const isConditionMet = await this.condition(ctx);
|
|
@@ -6,7 +6,7 @@ import { IActionWithState, ActionKey } from '../../types/actionWithState';
|
|
|
6
6
|
import { CachedStateFactory } from '../cachedStateFactory';
|
|
7
7
|
import { ChatContext } from '../context/chatContext';
|
|
8
8
|
export declare class ScheduledAction<TActionState extends IActionState> implements IActionWithState {
|
|
9
|
-
static
|
|
9
|
+
static locks: Map<string, Semaphore>;
|
|
10
10
|
name: string;
|
|
11
11
|
timeinHours: HoursOfDay;
|
|
12
12
|
active: boolean;
|
|
@@ -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,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC1E,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,
|
|
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,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC1E,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,KAAK,yBAAgC;IAE5C,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,SAAS,CAAC;IAEf,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,CAAC,YAAY,CAAC;YAqC3B,cAAc;IA8C5B,OAAO,CAAC,aAAa;CAaxB"}
|
|
@@ -23,6 +23,8 @@ class ScheduledAction {
|
|
|
23
23
|
this.stateConstructor = stateConstructor;
|
|
24
24
|
}
|
|
25
25
|
async exec(ctx) {
|
|
26
|
+
if (!ctx.isInitialized)
|
|
27
|
+
throw new Error('Context is not initialized');
|
|
26
28
|
if (!this.active || !this.chatsWhitelist.includes(ctx.chatId))
|
|
27
29
|
return;
|
|
28
30
|
const state = await ctx.storage.getActionState(this, ctx.chatId);
|
|
@@ -39,7 +41,16 @@ class ScheduledAction {
|
|
|
39
41
|
if (!this.cachedStateFactories.has(key)) {
|
|
40
42
|
throw new Error(`No shared cache was set up for the key [${key}] in action '${this.name}'`);
|
|
41
43
|
}
|
|
42
|
-
|
|
44
|
+
const semaphoreKey = `${this.key}_cached:${key}`;
|
|
45
|
+
let semaphore;
|
|
46
|
+
if (ScheduledAction.locks.has(semaphoreKey)) {
|
|
47
|
+
semaphore = ScheduledAction.locks.get(semaphoreKey);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
semaphore = new async_sema_1.Sema(1);
|
|
51
|
+
ScheduledAction.locks.set(semaphoreKey, semaphore);
|
|
52
|
+
}
|
|
53
|
+
await semaphore.acquire();
|
|
43
54
|
try {
|
|
44
55
|
if (this.cachedState.has(key)) {
|
|
45
56
|
return this.cachedState.get(key);
|
|
@@ -51,7 +62,7 @@ class ScheduledAction {
|
|
|
51
62
|
return value;
|
|
52
63
|
}
|
|
53
64
|
finally {
|
|
54
|
-
|
|
65
|
+
semaphore.release();
|
|
55
66
|
}
|
|
56
67
|
}
|
|
57
68
|
shouldTrigger(state) {
|
|
@@ -67,4 +78,4 @@ class ScheduledAction {
|
|
|
67
78
|
}
|
|
68
79
|
}
|
|
69
80
|
exports.ScheduledAction = ScheduledAction;
|
|
70
|
-
ScheduledAction.
|
|
81
|
+
ScheduledAction.locks = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,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;
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,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;AAQ5D,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;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;KAC9C;IAqCD,OAAO,CAAC,6BAA6B;IA2CrC,OAAO,CAAC,2BAA2B;IAiC7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAwBZ,cAAc;CAqB/B"}
|
|
@@ -12,6 +12,8 @@ const logger_1 = require("../services/logger");
|
|
|
12
12
|
const taskScheduler_1 = require("../services/taskScheduler");
|
|
13
13
|
const incomingMessage_1 = require("./incomingMessage");
|
|
14
14
|
const moment_1 = __importDefault(require("moment"));
|
|
15
|
+
const chatContext_1 = require("./context/chatContext");
|
|
16
|
+
const messageContext_1 = require("./context/messageContext");
|
|
15
17
|
class BotInstance {
|
|
16
18
|
constructor(options) {
|
|
17
19
|
this.name = options.name;
|
|
@@ -74,9 +76,10 @@ class BotInstance {
|
|
|
74
76
|
this.telegraf.stop(code);
|
|
75
77
|
}
|
|
76
78
|
async runScheduled() {
|
|
79
|
+
const ctx = new chatContext_1.ChatContext();
|
|
77
80
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
78
81
|
for (const scheduledAction of this.scheduled) {
|
|
79
|
-
|
|
82
|
+
this.api.initializeContextForChat(ctx, chatId, scheduledAction);
|
|
80
83
|
try {
|
|
81
84
|
await scheduledAction.exec(ctx);
|
|
82
85
|
}
|
|
@@ -88,8 +91,9 @@ class BotInstance {
|
|
|
88
91
|
await this.api.flushResponses();
|
|
89
92
|
}
|
|
90
93
|
async processMessage(msg) {
|
|
94
|
+
const ctx = new messageContext_1.MessageContext();
|
|
91
95
|
for (const commandAction of this.commands) {
|
|
92
|
-
|
|
96
|
+
this.api.initializeContextForMessage(ctx, msg, commandAction);
|
|
93
97
|
try {
|
|
94
98
|
await commandAction.exec(ctx);
|
|
95
99
|
}
|
|
@@ -19,7 +19,9 @@ export declare class ChatContext<TActionState> {
|
|
|
19
19
|
chatName: string;
|
|
20
20
|
/** Storage client instance for this bot. */
|
|
21
21
|
storage: IStorageClient;
|
|
22
|
-
|
|
22
|
+
isInitialized: boolean;
|
|
23
|
+
constructor();
|
|
24
|
+
initializeChatContext(botName: string, action: IActionWithState, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient): this;
|
|
23
25
|
/**
|
|
24
26
|
* Manually update the state of an action.
|
|
25
27
|
* @param stateUpdateAction Function that will modify state.
|
|
@@ -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,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AK,IAAI;IAI5D;;;;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;CAKjC"}
|
|
@@ -10,8 +10,11 @@ const unpin_1 = require("../responses/unpin");
|
|
|
10
10
|
* Context of action executed in chat.
|
|
11
11
|
*/
|
|
12
12
|
class ChatContext {
|
|
13
|
-
constructor(
|
|
13
|
+
constructor() {
|
|
14
14
|
this.updateActions = [];
|
|
15
|
+
this.isInitialized = false;
|
|
16
|
+
}
|
|
17
|
+
initializeChatContext(botName, action, interactions, chatId, chatName, traceId, storage) {
|
|
15
18
|
this.botName = botName;
|
|
16
19
|
this.action = action;
|
|
17
20
|
this.interactions = interactions;
|
|
@@ -19,6 +22,9 @@ class ChatContext {
|
|
|
19
22
|
this.chatName = chatName;
|
|
20
23
|
this.traceId = traceId;
|
|
21
24
|
this.storage = storage;
|
|
25
|
+
this.updateActions = [];
|
|
26
|
+
this.isInitialized = true;
|
|
27
|
+
return this;
|
|
22
28
|
}
|
|
23
29
|
/**
|
|
24
30
|
* Manually update the state of an action.
|
|
@@ -25,7 +25,8 @@ export declare class MessageContext<TActionState extends IActionState> extends C
|
|
|
25
25
|
fromUserName: string;
|
|
26
26
|
/** Type of message being received */
|
|
27
27
|
messageType: MessageTypeValue;
|
|
28
|
-
constructor(
|
|
28
|
+
constructor();
|
|
29
|
+
initializeMessageContext(botName: string, action: IActionWithState, interactions: IBotApiInteractions, message: IncomingMessage, storage: IStorageClient): this;
|
|
29
30
|
/**
|
|
30
31
|
* Loads state of another action. Changes to the loaded state will no affect actual state of other action.
|
|
31
32
|
* @param commandName Name of an action to load state of.
|
|
@@ -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,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAa,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW,CAAC,YAAY,CAAC;IAC/B,kDAAkD;IAClD,SAAS,
|
|
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,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EACH,qBAAqB,EACrB,yBAAyB,EAC5B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAa,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW,CAAC,YAAY,CAAC;IAC/B,kDAAkD;IAClD,SAAS,EAAG,MAAM,CAAC;IACnB,oDAAoD;IACpD,WAAW,EAAG,MAAM,CAAC;IACrB,4HAA4H;IAC5H,YAAY,EAAE,gBAAgB,EAAE,CAAM;IACtC,mEAAmE;IACnE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,kGAAkG;IAClG,aAAa,EAAE,OAAO,CAAQ;IAC9B,qEAAqE;IACrE,YAAY,EAAG,MAAM,CAAC;IACtB,qCAAqC;IACrC,WAAW,EAAG,gBAAgB,CAAC;;IAM/B,wBAAwB,CACpB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,mBAAmB,EACjC,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,cAAc;IAwB3B;;;;OAIG;IACG,WAAW,CAAC,mBAAmB,SAAS,YAAY,EACtD,WAAW,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,CAAC;IAe/B;;;;OAIG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAa/D;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,aAAa;CAW7B"}
|
|
@@ -12,12 +12,14 @@ const chatContext_1 = require("./chatContext");
|
|
|
12
12
|
* Context of action executed in chat, in response to a message
|
|
13
13
|
*/
|
|
14
14
|
class MessageContext extends chatContext_1.ChatContext {
|
|
15
|
-
constructor(
|
|
16
|
-
super(
|
|
15
|
+
constructor() {
|
|
16
|
+
super();
|
|
17
17
|
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
18
18
|
this.matchResults = [];
|
|
19
19
|
/** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
|
|
20
20
|
this.startCooldown = true;
|
|
21
|
+
}
|
|
22
|
+
initializeMessageContext(botName, action, interactions, message, storage) {
|
|
21
23
|
this.messageId = message.message_id;
|
|
22
24
|
this.messageText = message.text ?? '';
|
|
23
25
|
this.messageType = message.type;
|
|
@@ -25,6 +27,9 @@ class MessageContext extends chatContext_1.ChatContext {
|
|
|
25
27
|
this.fromUserName =
|
|
26
28
|
(message.from?.first_name ?? 'Unknown user') +
|
|
27
29
|
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
30
|
+
this.matchResults = [];
|
|
31
|
+
this.startCooldown = true;
|
|
32
|
+
return this.initializeChatContext(botName, action, interactions, message.chat.id, message.chatName, message.traceId, storage);
|
|
28
33
|
}
|
|
29
34
|
/**
|
|
30
35
|
* Loads state of another action. Changes to the loaded state will no affect actual state of other action.
|
|
@@ -25,8 +25,8 @@ export declare class TelegramApiService {
|
|
|
25
25
|
private pinIfShould;
|
|
26
26
|
private processResponse;
|
|
27
27
|
private enqueue;
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
initializeContextForMessage<TActionState extends IActionState>(ctx: MessageContext<TActionState>, incomingMessage: IncomingMessage, command: CommandAction<TActionState>): MessageContext<TActionState>;
|
|
29
|
+
initializeContextForChat<TActionState extends IActionState>(ctx: ChatContext<TActionState>, chatId: number, scheduledAction: ScheduledAction<TActionState>): ChatContext<TActionState>;
|
|
30
30
|
}
|
|
31
31
|
export interface IBotApiInteractions {
|
|
32
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,WA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YAAY,EACzD,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,EACjC,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,aAAa,CAAC,YAAY,CAAC;IAWxC,wBAAwB,CAAC,YAAY,SAAS,YAAY,EACtD,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC,EAC9B,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"}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TelegramApiService = void 0;
|
|
4
|
-
const chatContext_1 = require("../entities/context/chatContext");
|
|
5
|
-
const messageContext_1 = require("../entities/context/messageContext");
|
|
6
4
|
const inverseRecord_1 = require("../helpers/inverseRecord");
|
|
7
5
|
const logger_1 = require("./logger");
|
|
8
6
|
const promises_1 = require("timers/promises");
|
|
@@ -92,11 +90,11 @@ class TelegramApiService {
|
|
|
92
90
|
enqueue(response) {
|
|
93
91
|
this.messageQueue.push(response);
|
|
94
92
|
}
|
|
95
|
-
|
|
96
|
-
return
|
|
93
|
+
initializeContextForMessage(ctx, incomingMessage, command) {
|
|
94
|
+
return ctx.initializeMessageContext(this.botName, command, this.interactions, incomingMessage, this.storage);
|
|
97
95
|
}
|
|
98
|
-
|
|
99
|
-
return
|
|
96
|
+
initializeContextForChat(ctx, chatId, scheduledAction) {
|
|
97
|
+
return ctx.initializeChatContext(this.botName, scheduledAction, this.interactions, chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
|
|
100
98
|
}
|
|
101
99
|
}
|
|
102
100
|
exports.TelegramApiService = TelegramApiService;
|
|
@@ -51,6 +51,8 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async exec(ctx: MessageContext<TActionState>) {
|
|
54
|
+
if (!ctx.isInitialized) throw new Error('Context is not initialized');
|
|
55
|
+
|
|
54
56
|
if (!this.active || this.chatsBlacklist.includes(ctx.chatId)) return;
|
|
55
57
|
|
|
56
58
|
const isConditionMet = await this.condition(ctx);
|
|
@@ -14,7 +14,7 @@ import { Scheduler } from '../../services/taskScheduler';
|
|
|
14
14
|
export class ScheduledAction<TActionState extends IActionState>
|
|
15
15
|
implements IActionWithState
|
|
16
16
|
{
|
|
17
|
-
static
|
|
17
|
+
static locks = new Map<string, Semaphore>();
|
|
18
18
|
|
|
19
19
|
name: string;
|
|
20
20
|
timeinHours: HoursOfDay;
|
|
@@ -47,6 +47,8 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
async exec(ctx: ChatContext<TActionState>) {
|
|
50
|
+
if (!ctx.isInitialized) throw new Error('Context is not initialized');
|
|
51
|
+
|
|
50
52
|
if (!this.active || !this.chatsWhitelist.includes(ctx.chatId)) return;
|
|
51
53
|
|
|
52
54
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
@@ -91,7 +93,16 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
91
93
|
);
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
96
|
+
const semaphoreKey = `${this.key}_cached:${key}`;
|
|
97
|
+
let semaphore: Semaphore;
|
|
98
|
+
if (ScheduledAction.locks.has(semaphoreKey)) {
|
|
99
|
+
semaphore = ScheduledAction.locks.get(semaphoreKey)!;
|
|
100
|
+
} else {
|
|
101
|
+
semaphore = new Semaphore(1);
|
|
102
|
+
ScheduledAction.locks.set(semaphoreKey, semaphore);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await semaphore.acquire();
|
|
95
106
|
|
|
96
107
|
try {
|
|
97
108
|
if (this.cachedState.has(key)) {
|
|
@@ -114,7 +125,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
114
125
|
|
|
115
126
|
return value as TResult;
|
|
116
127
|
} finally {
|
|
117
|
-
|
|
128
|
+
semaphore.release();
|
|
118
129
|
}
|
|
119
130
|
}
|
|
120
131
|
|
package/entities/botInstance.ts
CHANGED
|
@@ -14,6 +14,8 @@ import { Logger } from '../services/logger';
|
|
|
14
14
|
import { Scheduler } from '../services/taskScheduler';
|
|
15
15
|
import { IncomingMessage } from './incomingMessage';
|
|
16
16
|
import moment from 'moment';
|
|
17
|
+
import { ChatContext } from './context/chatContext';
|
|
18
|
+
import { MessageContext } from './context/messageContext';
|
|
17
19
|
|
|
18
20
|
export class BotInstance {
|
|
19
21
|
name: string;
|
|
@@ -160,12 +162,11 @@ export class BotInstance {
|
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
private async runScheduled() {
|
|
165
|
+
const ctx = new ChatContext<IActionState>();
|
|
166
|
+
|
|
163
167
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
164
168
|
for (const scheduledAction of this.scheduled) {
|
|
165
|
-
|
|
166
|
-
chatId,
|
|
167
|
-
scheduledAction
|
|
168
|
-
);
|
|
169
|
+
this.api.initializeContextForChat(ctx, chatId, scheduledAction);
|
|
169
170
|
|
|
170
171
|
try {
|
|
171
172
|
await scheduledAction.exec(ctx);
|
|
@@ -185,8 +186,10 @@ export class BotInstance {
|
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
private async processMessage(msg: IncomingMessage) {
|
|
189
|
+
const ctx = new MessageContext<IActionState>();
|
|
190
|
+
|
|
188
191
|
for (const commandAction of this.commands) {
|
|
189
|
-
|
|
192
|
+
this.api.initializeContextForMessage(ctx, msg, commandAction);
|
|
190
193
|
|
|
191
194
|
try {
|
|
192
195
|
await commandAction.exec(ctx);
|
|
@@ -15,21 +15,25 @@ import { IActionWithState } from '../../types/actionWithState';
|
|
|
15
15
|
* Context of action executed in chat.
|
|
16
16
|
*/
|
|
17
17
|
export class ChatContext<TActionState> {
|
|
18
|
-
protected action
|
|
19
|
-
protected interactions
|
|
18
|
+
protected action!: IActionWithState;
|
|
19
|
+
protected interactions!: IBotApiInteractions;
|
|
20
20
|
updateActions: Array<(state: TActionState) => void> = [];
|
|
21
21
|
/** Trace id of a action execution. */
|
|
22
|
-
traceId
|
|
22
|
+
traceId!: number | string;
|
|
23
23
|
/** Name of a bot that executes this action. */
|
|
24
|
-
botName
|
|
24
|
+
botName!: string;
|
|
25
25
|
/** Id of a chat that action is executed in. */
|
|
26
|
-
chatId
|
|
26
|
+
chatId!: number;
|
|
27
27
|
/** Name of a chat that action is executed in. */
|
|
28
|
-
chatName
|
|
28
|
+
chatName!: string;
|
|
29
29
|
/** Storage client instance for this bot. */
|
|
30
|
-
storage
|
|
30
|
+
storage!: IStorageClient;
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
isInitialized = false;
|
|
33
|
+
|
|
34
|
+
constructor() {}
|
|
35
|
+
|
|
36
|
+
initializeChatContext(
|
|
33
37
|
botName: string,
|
|
34
38
|
action: IActionWithState,
|
|
35
39
|
interactions: IBotApiInteractions,
|
|
@@ -45,6 +49,11 @@ export class ChatContext<TActionState> {
|
|
|
45
49
|
this.chatName = chatName;
|
|
46
50
|
this.traceId = traceId;
|
|
47
51
|
this.storage = storage;
|
|
52
|
+
|
|
53
|
+
this.updateActions = [];
|
|
54
|
+
this.isInitialized = true;
|
|
55
|
+
|
|
56
|
+
return this;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
59
|
/**
|
|
@@ -24,9 +24,9 @@ export class MessageContext<
|
|
|
24
24
|
TActionState extends IActionState
|
|
25
25
|
> extends ChatContext<TActionState> {
|
|
26
26
|
/** Id of a message that triggered this action. */
|
|
27
|
-
messageId
|
|
27
|
+
messageId!: number;
|
|
28
28
|
/** Text of a message that triggered this action. */
|
|
29
|
-
messageText
|
|
29
|
+
messageText!: string;
|
|
30
30
|
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
31
31
|
matchResults: RegExpMatchArray[] = [];
|
|
32
32
|
/** Id of a user that sent a message that triggered this action. */
|
|
@@ -34,18 +34,33 @@ export class MessageContext<
|
|
|
34
34
|
/** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
|
|
35
35
|
startCooldown: boolean = true;
|
|
36
36
|
/** Name of a user that sent a message that triggered this action. */
|
|
37
|
-
fromUserName
|
|
37
|
+
fromUserName!: string;
|
|
38
38
|
/** Type of message being received */
|
|
39
|
-
messageType
|
|
39
|
+
messageType!: MessageTypeValue;
|
|
40
40
|
|
|
41
|
-
constructor(
|
|
41
|
+
constructor() {
|
|
42
|
+
super();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
initializeMessageContext(
|
|
42
46
|
botName: string,
|
|
43
47
|
action: IActionWithState,
|
|
44
48
|
interactions: IBotApiInteractions,
|
|
45
49
|
message: IncomingMessage,
|
|
46
50
|
storage: IStorageClient
|
|
47
51
|
) {
|
|
48
|
-
|
|
52
|
+
this.messageId = message.message_id;
|
|
53
|
+
this.messageText = message.text ?? '';
|
|
54
|
+
this.messageType = message.type;
|
|
55
|
+
this.fromUserId = message.from?.id;
|
|
56
|
+
this.fromUserName =
|
|
57
|
+
(message.from?.first_name ?? 'Unknown user') +
|
|
58
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
59
|
+
|
|
60
|
+
this.matchResults = [];
|
|
61
|
+
this.startCooldown = true;
|
|
62
|
+
|
|
63
|
+
return this.initializeChatContext(
|
|
49
64
|
botName,
|
|
50
65
|
action,
|
|
51
66
|
interactions,
|
|
@@ -54,14 +69,6 @@ export class MessageContext<
|
|
|
54
69
|
message.traceId,
|
|
55
70
|
storage
|
|
56
71
|
);
|
|
57
|
-
|
|
58
|
-
this.messageId = message.message_id;
|
|
59
|
-
this.messageText = message.text ?? '';
|
|
60
|
-
this.messageType = message.type;
|
|
61
|
-
this.fromUserId = message.from?.id;
|
|
62
|
-
this.fromUserName =
|
|
63
|
-
(message.from?.first_name ?? 'Unknown user') +
|
|
64
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : '');
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
/**
|
package/package.json
CHANGED
package/services/telegramApi.ts
CHANGED
|
@@ -175,11 +175,12 @@ export class TelegramApiService {
|
|
|
175
175
|
this.messageQueue.push(response);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
initializeContextForMessage<TActionState extends IActionState>(
|
|
179
|
+
ctx: MessageContext<TActionState>,
|
|
179
180
|
incomingMessage: IncomingMessage,
|
|
180
181
|
command: CommandAction<TActionState>
|
|
181
182
|
) {
|
|
182
|
-
return
|
|
183
|
+
return ctx.initializeMessageContext(
|
|
183
184
|
this.botName,
|
|
184
185
|
command,
|
|
185
186
|
this.interactions,
|
|
@@ -188,11 +189,12 @@ export class TelegramApiService {
|
|
|
188
189
|
);
|
|
189
190
|
}
|
|
190
191
|
|
|
191
|
-
|
|
192
|
+
initializeContextForChat<TActionState extends IActionState>(
|
|
193
|
+
ctx: ChatContext<TActionState>,
|
|
192
194
|
chatId: number,
|
|
193
195
|
scheduledAction: ScheduledAction<TActionState>
|
|
194
196
|
) {
|
|
195
|
-
return
|
|
197
|
+
return ctx.initializeChatContext(
|
|
196
198
|
this.botName,
|
|
197
199
|
scheduledAction,
|
|
198
200
|
this.interactions,
|