chz-telegram-bot 0.7.23 → 0.7.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.
@@ -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;YAwBd,YAAY;YAwBZ,sBAAsB;CAkCvC"}
@@ -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,46 +66,51 @@ 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
+ this.api.flushResponses();
83
+ revoke();
84
+ }
85
+ }
86
+ async processReply(capture, msg) {
87
+ const ctx = new ReplyContextInternal(this.storage, this.scheduler, this.eventEmitter, capture, msg, this.botName, this.botInfo);
88
+ const { proxy, revoke } = Proxy.revocable(ctx, {});
89
+ try {
90
+ await this.executeAction(capture, proxy);
91
+ }
92
+ finally {
93
+ this.api.flushResponses();
94
+ revoke();
95
+ }
96
+ }
68
97
  async startMessageProcessing(msg) {
69
98
  this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
70
99
  botInfo: this.botInfo,
71
100
  message: msg,
72
101
  traceId: msg.traceId
73
102
  });
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
- }
103
+ this.updateChatHistory(msg);
104
+ const baseCommands = this.commands[msg.type];
105
+ const commandsToCheck = msg.type != MessageType.Text && msg.text != ''
106
+ ? new Set([...baseCommands, ...this.commands[MessageType.Text]])
107
+ : baseCommands;
108
+ const actionPromises = [];
109
+ for (const command of commandsToCheck) {
110
+ actionPromises.push(this.processCommand(command, msg));
83
111
  }
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);
112
+ for (const capture of this.replyCaptures) {
113
+ actionPromises.push(this.processReply(capture, msg));
108
114
  }
109
115
  try {
110
116
  await Promise.allSettled(actionPromises);
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.23",
17
+ "version": "0.7.25",
18
18
  "type": "module",
19
19
  "dependencies": {
20
20
  "async-sema": "^3.1.1",