chz-telegram-bot 0.7.22 → 0.7.24

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.
@@ -1 +1 @@
1
- {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../src/dtos/incomingMessage.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAEzE,qBAAa,eAAe;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9C,QAAQ,CAAC,YAAY,EAAE,eAAe,CAAC;IAEvC,OAAO,CAAC,iBAAiB;gBAmBrB,UAAU,EAAE,eAAe,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,kBAAkB,EAAE;IAyBjC,OAAO,CAAC,cAAc;CAKzB"}
1
+ {"version":3,"file":"incomingMessage.d.ts","sourceRoot":"","sources":["../../src/dtos/incomingMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAEzE,qBAAa,eAAe;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9C,QAAQ,CAAC,YAAY,EAAE,eAAe,CAAC;IAEvC,OAAO,CAAC,iBAAiB;gBAmBrB,UAAU,EAAE,eAAe,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,kBAAkB,EAAE;IAyBjC,OAAO,CAAC,cAAc;CAKzB"}
@@ -1,4 +1,3 @@
1
- import { randomInt } from 'crypto';
2
1
  import { MessageType } from '../types/messageTypes';
3
2
  import { ChatInfo } from './chatInfo';
4
3
  import { createTrace } from '../helpers/traceFactory';
@@ -41,7 +40,7 @@ export class IncomingMessage {
41
40
  return MessageType.Unknown;
42
41
  }
43
42
  constructor(ctxMessage, botName, history) {
44
- this.traceId = createTrace(this, botName, randomInt(10000, 99999).toString());
43
+ this.traceId = createTrace(this, botName, Math.floor(Math.random() * 90000 + 10000).toString());
45
44
  this.messageId = ctxMessage.message_id;
46
45
  this.replyToMessageId =
47
46
  'reply_to_message' in ctxMessage
@@ -58,6 +57,6 @@ export class IncomingMessage {
58
57
  getMessageText(ctxMessage) {
59
58
  if ('text' in ctxMessage)
60
59
  return ctxMessage.text;
61
- return 'caption' in ctxMessage ? ctxMessage.caption ?? '' : '';
60
+ return 'caption' in ctxMessage ? (ctxMessage.caption ?? '') : '';
62
61
  }
63
62
  }
@@ -7,12 +7,16 @@ import { BaseActionProcessor } from './baseProcessor';
7
7
  import { BotInfo, TelegramBot } from '../../types/externalAliases';
8
8
  import { TraceId } from '../../types/trace';
9
9
  export declare class CommandActionProcessor extends BaseActionProcessor {
10
+ private static readonly fallbackFactory;
10
11
  private readonly replyCaptures;
11
12
  private readonly chatHistory;
12
13
  private botInfo;
13
14
  private commands;
14
15
  initialize(api: TelegramApiService, telegram: TelegramBot, commands: CommandAction<IActionState>[], botInfo: BotInfo): void;
15
16
  captureRegistrationCallback(capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId): void;
17
+ private updateChatHistory;
18
+ private processCommand;
19
+ private processReply;
16
20
  private startMessageProcessing;
17
21
  }
18
22
  //# sourceMappingURL=commandActionProcessor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/commandActionProcessor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAIrE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAM/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGtD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAEnE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C,qBAAa,sBAAuB,SAAQ,mBAAmB;IAC3D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0C;IACxE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA2C;IACvE,OAAO,CAAC,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pB,2BAA2B,CACvB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO;IA2CpB,OAAO,CAAC,iBAAiB;YAuBX,cAAc;YAuBd,YAAY;YAuBZ,sBAAsB;CAmCvC"}
@@ -5,11 +5,12 @@ import { ReplyContextInternal } from '../../entities/context/replyContext';
5
5
  import { INTERNAL_MESSAGE_TYPE_PREFIX, MessageType } from '../../types/messageTypes';
6
6
  import { typeSafeObjectFromEntries } from '../../helpers/objectFromEntries';
7
7
  import { BaseActionProcessor } from './baseProcessor';
8
- import { getOrSetIfNotExists } from '../../helpers/mapUtils';
8
+ import { getOrCreateIfNotExists } from '../../helpers/mapUtils';
9
9
  import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
10
10
  import { BotEventType } from '../../types/events';
11
11
  import { MESSAGE_HISTORY_LENGTH_LIMIT } from '../../helpers/constants';
12
12
  export class CommandActionProcessor extends BaseActionProcessor {
13
+ static fallbackFactory = () => [];
13
14
  replyCaptures = [];
14
15
  chatHistory = new Map();
15
16
  botInfo;
@@ -34,7 +35,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
34
35
  }
35
36
  if (commands.length > 0) {
36
37
  telegram.on('message', ({ message }) => {
37
- const internalMessage = new IncomingMessage(message, this.botName, getOrSetIfNotExists(this.chatHistory, message.chat.id, []));
38
+ const internalMessage = new IncomingMessage(message, this.botName, getOrCreateIfNotExists(this.chatHistory, message.chat.id, CommandActionProcessor.fallbackFactory));
38
39
  this.eventEmitter.emit(BotEventType.messageRecieved, {
39
40
  botInfo: this.botInfo,
40
41
  message: internalMessage,
@@ -65,51 +66,55 @@ export class CommandActionProcessor extends BaseActionProcessor {
65
66
  }
66
67
  }, { once: true });
67
68
  }
69
+ updateChatHistory(msg) {
70
+ const chatHistoryArray = getOrCreateIfNotExists(this.chatHistory, msg.chatInfo.id, CommandActionProcessor.fallbackFactory);
71
+ while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
72
+ chatHistoryArray.shift();
73
+ chatHistoryArray.push(new ChatHistoryMessage(msg.messageId, msg.from, msg.text, msg.type, msg.traceId, msg.replyToMessageId, msg.updateObject.date));
74
+ }
75
+ async processCommand(command, msg) {
76
+ const ctx = new MessageContextInternal(this.storage, this.scheduler, this.eventEmitter, command, msg, this.botName, this.botInfo);
77
+ const { proxy, revoke } = Proxy.revocable(ctx, {});
78
+ try {
79
+ await this.executeAction(command, proxy);
80
+ }
81
+ finally {
82
+ revoke();
83
+ }
84
+ }
85
+ async processReply(capture, msg) {
86
+ const ctx = new ReplyContextInternal(this.storage, this.scheduler, this.eventEmitter, capture, msg, this.botName, this.botInfo);
87
+ const { proxy, revoke } = Proxy.revocable(ctx, {});
88
+ try {
89
+ await this.executeAction(capture, proxy);
90
+ }
91
+ finally {
92
+ revoke();
93
+ }
94
+ }
68
95
  async startMessageProcessing(msg) {
69
96
  this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
70
97
  botInfo: this.botInfo,
71
98
  message: msg,
72
99
  traceId: msg.traceId
73
100
  });
74
- const chatHistoryArray = getOrSetIfNotExists(this.chatHistory, msg.chatInfo.id, []);
75
- while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
76
- chatHistoryArray.shift();
77
- chatHistoryArray.push(new ChatHistoryMessage(msg.messageId, msg.from, msg.text, msg.type, msg.traceId, msg.replyToMessageId, msg.updateObject.date));
78
- const commandsToCheck = new Set(this.commands[msg.type]);
79
- if (msg.type != MessageType.Text && msg.text != '') {
80
- for (const command of this.commands[MessageType.Text]) {
81
- commandsToCheck.add(command);
82
- }
101
+ this.updateChatHistory(msg);
102
+ const baseCommands = this.commands[msg.type];
103
+ const commandsToCheck = msg.type != MessageType.Text && msg.text != ''
104
+ ? new Set([...baseCommands, ...this.commands[MessageType.Text]])
105
+ : baseCommands;
106
+ const actionPromises = [];
107
+ for (const command of commandsToCheck) {
108
+ actionPromises.push(this.processCommand(command, msg));
83
109
  }
84
- const actionPromises = [...commandsToCheck].map(async (command) => {
85
- const ctx = new MessageContextInternal(this.storage, this.scheduler, this.eventEmitter, command, msg, this.botName, this.botInfo);
86
- const { proxy, revoke } = Proxy.revocable(ctx, {});
87
- try {
88
- await this.executeAction(command, proxy);
89
- }
90
- finally {
91
- revoke();
92
- this.api.flushResponses();
93
- }
94
- });
95
- if (this.replyCaptures.length != 0) {
96
- const replyPromises = this.replyCaptures.map(async (capture) => {
97
- const replyCtx = new ReplyContextInternal(this.storage, this.scheduler, this.eventEmitter, capture, msg, this.botName, this.botInfo);
98
- const { proxy, revoke } = Proxy.revocable(replyCtx, {});
99
- try {
100
- await this.executeAction(capture, proxy);
101
- }
102
- finally {
103
- revoke();
104
- this.api.flushResponses();
105
- }
106
- });
107
- actionPromises.push(...replyPromises);
110
+ for (const capture of this.replyCaptures) {
111
+ actionPromises.push(this.processReply(capture, msg));
108
112
  }
109
113
  try {
110
114
  await Promise.allSettled(actionPromises);
111
115
  }
112
116
  finally {
117
+ this.api.flushResponses();
113
118
  this.eventEmitter.emit(BotEventType.messageProcessingFinished, {
114
119
  botInfo: this.botInfo,
115
120
  message: msg,
@@ -28,7 +28,7 @@ export class ResponseProcessingQueue {
28
28
  await this.rateLimiter();
29
29
  if (Date.now() >= this.items[0].priority) {
30
30
  const item = this.items.shift();
31
- await item?.callback();
31
+ void item?.callback();
32
32
  }
33
33
  }
34
34
  }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "type": "git",
15
15
  "url": "https://github.com/AlexSolari/botFramework.git"
16
16
  },
17
- "version": "0.7.22",
17
+ "version": "0.7.24",
18
18
  "type": "module",
19
19
  "dependencies": {
20
20
  "async-sema": "^3.1.1",