chz-telegram-bot 0.3.9 → 0.3.10

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.
@@ -65,13 +65,13 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
65
65
  }
66
66
  for (const commandAction of commandsToCheck) {
67
67
  this.initializeMessageContext(ctx, commandAction, msg);
68
- this.executeAction(commandAction, ctx);
68
+ await this.executeAction(commandAction, ctx);
69
69
  }
70
70
  if (this.replyCaptures.length != 0) {
71
71
  const replyCtx = new replyContext_1.ReplyContext(this.storage, this.scheduler);
72
72
  for (const replyAction of this.replyCaptures) {
73
73
  this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
74
- this.executeAction(replyAction, replyCtx);
74
+ await this.executeAction(replyAction, replyCtx);
75
75
  }
76
76
  }
77
77
  this.api.flushResponses();
@@ -36,7 +36,7 @@ class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
36
36
  queriesInProcessing.set(inlineQuery.userId, inlineQuery);
37
37
  for (const inlineQueryAction of this.inlineQueries) {
38
38
  this.initializeInlineQueryContext(ctx, inlineQuery.query, inlineQuery.queryId, inlineQueryAction, inlineQuery.abortController.signal, inlineQuery.traceId);
39
- this.executeAction(inlineQueryAction, ctx, (error, ctx) => {
39
+ await this.executeAction(inlineQueryAction, ctx, (error, ctx) => {
40
40
  if (error.name == 'AbortError') {
41
41
  ctx.logger.logWithTraceId(`Aborting query ${inlineQuery.queryId} (${inlineQuery.query}) successful.`);
42
42
  }
@@ -43,7 +43,7 @@ class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
43
43
  for (const [chatName, chatId] of Object.entries(this.chats)) {
44
44
  for (const scheduledAction of this.scheduled) {
45
45
  this.initializeChatContext(ctx, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), (0, traceFactory_1.createTrace)(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
46
- this.executeAction(scheduledAction, ctx);
46
+ await this.executeAction(scheduledAction, ctx);
47
47
  }
48
48
  }
49
49
  this.api.flushResponses();
@@ -3,6 +3,7 @@ export type QueueItem = {
3
3
  callback: () => Promise<void>;
4
4
  };
5
5
  export declare class ResponseProcessingQueue {
6
+ rateLimiter: () => Promise<void>;
6
7
  items: QueueItem[];
7
8
  isFlushing: boolean;
8
9
  enqueue(item: QueueItem): void;
@@ -1 +1 @@
1
- {"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAIF,qBAAa,uBAAuB;IAChC,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAiBxB"}
1
+ {"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAIF,qBAAa,uBAAuB;IAChC,WAAW,sBAAwD;IACnE,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAiBxB"}
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResponseProcessingQueue = void 0;
4
- const promises_1 = require("timers/promises");
4
+ const async_sema_1 = require("async-sema");
5
5
  const TELEGRAM_RATELIMIT_DELAY = 35;
6
6
  class ResponseProcessingQueue {
7
7
  constructor() {
8
+ this.rateLimiter = (0, async_sema_1.RateLimit)(1, { timeUnit: TELEGRAM_RATELIMIT_DELAY });
8
9
  this.items = [];
9
10
  this.isFlushing = false;
10
11
  }
@@ -27,12 +28,10 @@ class ResponseProcessingQueue {
27
28
  this.isFlushing = true;
28
29
  while (this.items.length) {
29
30
  if (Date.now() >= this.items[0].priority) {
31
+ await this.rateLimiter();
30
32
  const item = this.items.shift();
31
33
  await item.callback();
32
34
  }
33
- else {
34
- await (0, promises_1.setTimeout)(TELEGRAM_RATELIMIT_DELAY);
35
- }
36
35
  }
37
36
  this.isFlushing = false;
38
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -149,7 +149,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
149
149
 
150
150
  for (const commandAction of commandsToCheck) {
151
151
  this.initializeMessageContext(ctx, commandAction, msg);
152
- this.executeAction(commandAction, ctx);
152
+ await this.executeAction(commandAction, ctx);
153
153
  }
154
154
 
155
155
  if (this.replyCaptures.length != 0) {
@@ -160,7 +160,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
160
160
 
161
161
  for (const replyAction of this.replyCaptures) {
162
162
  this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
163
- this.executeAction(replyAction, replyCtx);
163
+ await this.executeAction(replyAction, replyCtx);
164
164
  }
165
165
  }
166
166
 
@@ -101,7 +101,7 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
101
101
  inlineQuery.traceId
102
102
  );
103
103
 
104
- this.executeAction(
104
+ await this.executeAction(
105
105
  inlineQueryAction,
106
106
  ctx,
107
107
  (error, ctx) => {
@@ -96,7 +96,7 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
96
96
  )
97
97
  );
98
98
 
99
- this.executeAction(scheduledAction, ctx);
99
+ await this.executeAction(scheduledAction, ctx);
100
100
  }
101
101
  }
102
102
 
@@ -1,5 +1,5 @@
1
- import { setTimeout } from 'timers/promises';
2
1
  import { Milliseconds } from '../types/timeValues';
2
+ import { RateLimit } from 'async-sema';
3
3
 
4
4
  export type QueueItem = {
5
5
  priority: number;
@@ -9,6 +9,7 @@ export type QueueItem = {
9
9
  const TELEGRAM_RATELIMIT_DELAY = 35 as Milliseconds;
10
10
 
11
11
  export class ResponseProcessingQueue {
12
+ rateLimiter = RateLimit(1, { timeUnit: TELEGRAM_RATELIMIT_DELAY });
12
13
  items: QueueItem[] = [];
13
14
  isFlushing = false;
14
15
 
@@ -38,11 +39,11 @@ export class ResponseProcessingQueue {
38
39
 
39
40
  while (this.items.length) {
40
41
  if (Date.now() >= this.items[0].priority) {
42
+ await this.rateLimiter();
43
+
41
44
  const item = this.items.shift()!;
42
45
 
43
46
  await item.callback();
44
- } else {
45
- await setTimeout(TELEGRAM_RATELIMIT_DELAY);
46
47
  }
47
48
  }
48
49