chz-telegram-bot 0.0.42 → 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.
Files changed (34) hide show
  1. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  2. package/dist/entities/actions/commandAction.js +2 -0
  3. package/dist/entities/actions/scheduledAction.d.ts +1 -1
  4. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  5. package/dist/entities/actions/scheduledAction.js +14 -3
  6. package/dist/entities/botInstance.d.ts.map +1 -1
  7. package/dist/entities/botInstance.js +6 -2
  8. package/dist/entities/context/chatContext.d.ts +3 -1
  9. package/dist/entities/context/chatContext.d.ts.map +1 -1
  10. package/dist/entities/context/chatContext.js +7 -1
  11. package/dist/entities/context/messageContext.d.ts +4 -3
  12. package/dist/entities/context/messageContext.d.ts.map +1 -1
  13. package/dist/entities/context/messageContext.js +7 -2
  14. package/dist/entities/incomingMessage.d.ts +2 -2
  15. package/dist/entities/incomingMessage.d.ts.map +1 -1
  16. package/dist/entities/incomingMessage.js +4 -2
  17. package/dist/services/telegramApi.d.ts +2 -2
  18. package/dist/services/telegramApi.d.ts.map +1 -1
  19. package/dist/services/telegramApi.js +4 -6
  20. package/dist/types/commandTrigger.d.ts +2 -2
  21. package/dist/types/commandTrigger.d.ts.map +1 -1
  22. package/dist/types/messageTypes.d.ts +2 -0
  23. package/dist/types/messageTypes.d.ts.map +1 -1
  24. package/dist/types/messageTypes.js +1 -0
  25. package/entities/actions/commandAction.ts +2 -0
  26. package/entities/actions/scheduledAction.ts +14 -3
  27. package/entities/botInstance.ts +8 -5
  28. package/entities/context/chatContext.ts +17 -8
  29. package/entities/context/messageContext.ts +22 -15
  30. package/entities/incomingMessage.ts +4 -3
  31. package/package.json +1 -1
  32. package/services/telegramApi.ts +6 -4
  33. package/types/commandTrigger.ts +2 -5
  34. package/types/messageTypes.ts +3 -0
@@ -1 +1 @@
1
- {"version":3,"file":"commandAction.d.ts","sourceRoot":"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AC,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 semaphore: Semaphore;
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,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC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M,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
- await ScheduledAction.semaphore.acquire();
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
- ScheduledAction.semaphore.release();
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.semaphore = new async_sema_1.Sema(1);
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;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;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO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wBZ,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
- const ctx = this.api.createContextForChat(chatId, scheduledAction);
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
- const ctx = this.api.createContextForMessage(msg, commandAction);
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
- constructor(botName: string, action: IActionWithState, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient);
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"],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,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(botName, action, interactions, chatId, chatName, traceId, storage) {
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.
@@ -6,7 +6,7 @@ import { ChatContext } from './chatContext';
6
6
  import { IncomingMessage } from '../incomingMessage';
7
7
  import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
8
8
  import { IActionWithState } from '../../types/actionWithState';
9
- import { MessageType } from '../../types/messageTypes';
9
+ import { MessageTypeValue } from '../../types/messageTypes';
10
10
  /**
11
11
  * Context of action executed in chat, in response to a message
12
12
  */
@@ -24,8 +24,9 @@ export declare class MessageContext<TActionState extends IActionState> extends C
24
24
  /** Name of a user that sent a message that triggered this action. */
25
25
  fromUserName: string;
26
26
  /** Type of message being received */
27
- messageType: (typeof MessageType)[keyof typeof MessageType];
28
- constructor(botName: string, action: IActionWithState, interactions: IBotApiInteractions, message: IncomingMessage, storage: IStorageClient);
27
+ messageType: MessageTypeValue;
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,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD;;GAEG;AACH,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW,CAAC,YAAY,CAAC;IAC/B,kDAAkD;IAClD,SAAS,EAAE,MAAM,CAAC;IAClB,oDAAoD;IACpD,WAAW,EAAE,MAAM,CAAC;IACpB,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,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,WAAW,EAAE,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;gBAGxD,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,mBAAmB,EACjC,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,cA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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(botName, action, interactions, message, storage) {
16
- super(botName, action, interactions, message.chat.id, message.chatName, message.traceId, storage);
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.
@@ -1,12 +1,12 @@
1
1
  import { Chat, Message, Update, User } from 'telegraf/types';
2
- import { MessageType } from '../types/messageTypes';
2
+ import { MessageTypeValue } from '../types/messageTypes';
3
3
  export declare class IncomingMessage {
4
4
  message_id: number;
5
5
  chat: Chat;
6
6
  from: User | undefined;
7
7
  text: string;
8
8
  chatName: string;
9
- type: (typeof MessageType)[keyof typeof MessageType];
9
+ type: MessageTypeValue;
10
10
  traceId: number;
11
11
  private detectMessageType;
12
12
  constructor(ctxMessage: Update.New & (Update.NonChannel & Message));
@@ -1 +1 @@
1
- {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../entities/incomingMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,qBAAa,eAAe;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;IACrD,OAAO,SAA2B;IAElC,OAAO,CAAC,iBAAiB;gBAgBb,UAAU,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC;CAWrE"}
1
+ {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../entities/incomingMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE7D,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEtE,qBAAa,eAAe;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,SAA2B;IAElC,OAAO,CAAC,iBAAiB;gBAiBb,UAAU,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC;CAWrE"}
@@ -5,16 +5,18 @@ const crypto_1 = require("crypto");
5
5
  const messageTypes_1 = require("../types/messageTypes");
6
6
  class IncomingMessage {
7
7
  detectMessageType(message) {
8
+ if ('photo' in message)
9
+ return messageTypes_1.MessageType.Photo;
8
10
  if ('sticker' in message)
9
11
  return messageTypes_1.MessageType.Sticker;
10
12
  if ('animation' in message)
11
13
  return messageTypes_1.MessageType.Animation;
12
- if ('document' in message)
13
- return messageTypes_1.MessageType.Document;
14
14
  if ('voice' in message)
15
15
  return messageTypes_1.MessageType.Voice;
16
16
  if ('audio' in message)
17
17
  return messageTypes_1.MessageType.Audio;
18
+ if ('document' in message)
19
+ return messageTypes_1.MessageType.Document;
18
20
  if ('left_chat_member' in message)
19
21
  return messageTypes_1.MessageType.LeftChatMember;
20
22
  if ('new_chat_member' in message)
@@ -25,8 +25,8 @@ export declare class TelegramApiService {
25
25
  private pinIfShould;
26
26
  private processResponse;
27
27
  private enqueue;
28
- createContextForMessage<TActionState extends IActionState>(incomingMessage: IncomingMessage, command: CommandAction<TActionState>): MessageContext<TActionState>;
29
- createContextForChat<TActionState extends IActionState>(chatId: number, scheduledAction: ScheduledAction<TActionState>): ChatContext<TActionState>;
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,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;AAGrD,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAIlE,qBAAa,kBAAkB;IAC3B,UAAU,UAAS;IACnB,YAAY,EAAE,WAAW,EAAE,CAAM;IAEj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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
- createContextForMessage(incomingMessage, command) {
96
- return new messageContext_1.MessageContext(this.botName, command, this.interactions, incomingMessage, this.storage);
93
+ initializeContextForMessage(ctx, incomingMessage, command) {
94
+ return ctx.initializeMessageContext(this.botName, command, this.interactions, incomingMessage, this.storage);
97
95
  }
98
- createContextForChat(chatId, scheduledAction) {
99
- return new chatContext_1.ChatContext(this.botName, scheduledAction, this.interactions, chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
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;
@@ -1,3 +1,3 @@
1
- import { MessageType } from './messageTypes';
2
- export type CommandTrigger = (typeof MessageType)[keyof typeof MessageType] | string | RegExp;
1
+ import { MessageTypeValue } from './messageTypes';
2
+ export type CommandTrigger = MessageTypeValue | string | RegExp;
3
3
  //# sourceMappingURL=commandTrigger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandTrigger.d.ts","sourceRoot":"","sources":["../../types/commandTrigger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,MAAM,MAAM,cAAc,GACpB,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,GAC9C,MAAM,GACN,MAAM,CAAC"}
1
+ {"version":3,"file":"commandTrigger.d.ts","sourceRoot":"","sources":["../../types/commandTrigger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,MAAM,MAAM,cAAc,GAAG,gBAAgB,GAAG,MAAM,GAAG,MAAM,CAAC"}
@@ -9,6 +9,8 @@ export declare const MessageType: {
9
9
  readonly NewChatMember: "__msg:NewChatMember";
10
10
  readonly Poll: "__msg:Poll";
11
11
  readonly Location: "__msg:Location";
12
+ readonly Photo: "__msg:Photo";
12
13
  readonly Unknown: "__msg:Unknown";
13
14
  };
15
+ export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
14
16
  //# sourceMappingURL=messageTypes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"messageTypes.d.ts","sourceRoot":"","sources":["../../types/messageTypes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW;;;;;;;;;;;;CAYd,CAAC"}
1
+ {"version":3,"file":"messageTypes.d.ts","sourceRoot":"","sources":["../../types/messageTypes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW;;;;;;;;;;;;;CAad,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC"}
@@ -12,5 +12,6 @@ exports.MessageType = {
12
12
  NewChatMember: '__msg:NewChatMember',
13
13
  Poll: '__msg:Poll',
14
14
  Location: '__msg:Location',
15
+ Photo: '__msg:Photo',
15
16
  Unknown: '__msg:Unknown'
16
17
  };
@@ -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 semaphore = new Semaphore(1);
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
- await ScheduledAction.semaphore.acquire();
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
- ScheduledAction.semaphore.release();
128
+ semaphore.release();
118
129
  }
119
130
  }
120
131
 
@@ -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
- const ctx = this.api.createContextForChat(
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
- const ctx = this.api.createContextForMessage(msg, commandAction);
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: IActionWithState;
19
- protected interactions: IBotApiInteractions;
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: number | string;
22
+ traceId!: number | string;
23
23
  /** Name of a bot that executes this action. */
24
- botName: string;
24
+ botName!: string;
25
25
  /** Id of a chat that action is executed in. */
26
- chatId: number;
26
+ chatId!: number;
27
27
  /** Name of a chat that action is executed in. */
28
- chatName: string;
28
+ chatName!: string;
29
29
  /** Storage client instance for this bot. */
30
- storage: IStorageClient;
30
+ storage!: IStorageClient;
31
31
 
32
- constructor(
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
  /**
@@ -15,7 +15,7 @@ import {
15
15
  TextMessageSendingOptions
16
16
  } from '../../types/messageSendingOptions';
17
17
  import { IActionWithState, ActionKey } from '../../types/actionWithState';
18
- import { MessageType } from '../../types/messageTypes';
18
+ import { MessageTypeValue } from '../../types/messageTypes';
19
19
 
20
20
  /**
21
21
  * Context of action executed in chat, in response to a message
@@ -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: number;
27
+ messageId!: number;
28
28
  /** Text of a message that triggered this action. */
29
- messageText: string;
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: string;
37
+ fromUserName!: string;
38
38
  /** Type of message being received */
39
- messageType: (typeof MessageType)[keyof typeof 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
- super(
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
  /**
@@ -1,6 +1,6 @@
1
1
  import { Chat, Message, Update, User } from 'telegraf/types';
2
2
  import { randomInt } from 'crypto';
3
- import { MessageType } from '../types/messageTypes';
3
+ import { MessageType, MessageTypeValue } from '../types/messageTypes';
4
4
 
5
5
  export class IncomingMessage {
6
6
  message_id: number;
@@ -8,17 +8,18 @@ export class IncomingMessage {
8
8
  from: User | undefined;
9
9
  text: string;
10
10
  chatName: string;
11
- type: (typeof MessageType)[keyof typeof MessageType];
11
+ type: MessageTypeValue;
12
12
  traceId = randomInt(10000, 99999);
13
13
 
14
14
  private detectMessageType(
15
15
  message: Update.New & (Update.NonChannel & Message)
16
16
  ) {
17
+ if ('photo' in message) return MessageType.Photo;
17
18
  if ('sticker' in message) return MessageType.Sticker;
18
19
  if ('animation' in message) return MessageType.Animation;
19
- if ('document' in message) return MessageType.Document;
20
20
  if ('voice' in message) return MessageType.Voice;
21
21
  if ('audio' in message) return MessageType.Audio;
22
+ if ('document' in message) return MessageType.Document;
22
23
  if ('left_chat_member' in message) return MessageType.LeftChatMember;
23
24
  if ('new_chat_member' in message) return MessageType.NewChatMember;
24
25
  if ('poll' in message) return MessageType.Poll;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -175,11 +175,12 @@ export class TelegramApiService {
175
175
  this.messageQueue.push(response);
176
176
  }
177
177
 
178
- createContextForMessage<TActionState extends IActionState>(
178
+ initializeContextForMessage<TActionState extends IActionState>(
179
+ ctx: MessageContext<TActionState>,
179
180
  incomingMessage: IncomingMessage,
180
181
  command: CommandAction<TActionState>
181
182
  ) {
182
- return new MessageContext<TActionState>(
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
- createContextForChat<TActionState extends IActionState>(
192
+ initializeContextForChat<TActionState extends IActionState>(
193
+ ctx: ChatContext<TActionState>,
192
194
  chatId: number,
193
195
  scheduledAction: ScheduledAction<TActionState>
194
196
  ) {
195
- return new ChatContext<TActionState>(
197
+ return ctx.initializeChatContext(
196
198
  this.botName,
197
199
  scheduledAction,
198
200
  this.interactions,
@@ -1,6 +1,3 @@
1
- import { MessageType } from './messageTypes';
1
+ import { MessageTypeValue } from './messageTypes';
2
2
 
3
- export type CommandTrigger =
4
- | (typeof MessageType)[keyof typeof MessageType]
5
- | string
6
- | RegExp;
3
+ export type CommandTrigger = MessageTypeValue | string | RegExp;
@@ -9,5 +9,8 @@ export const MessageType = {
9
9
  NewChatMember: '__msg:NewChatMember',
10
10
  Poll: '__msg:Poll',
11
11
  Location: '__msg:Location',
12
+ Photo: '__msg:Photo',
12
13
  Unknown: '__msg:Unknown'
13
14
  } as const;
15
+
16
+ export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];