chz-telegram-bot 0.3.3 → 0.3.4

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.
@@ -17,7 +17,6 @@ export declare class CommandActionProcessor {
17
17
  private api;
18
18
  private telegraf;
19
19
  private commands;
20
- private permanentTriggersToBeProcessed;
21
20
  constructor(botName: string, storage: IStorageClient, scheduler: IScheduler, logger: ILogger);
22
21
  initialize(api: TelegramApiService, telegraf: Telegraf, commands: CommandAction<IActionState>[], verboseLoggingForIncomingMessage: boolean): void;
23
22
  captureRegistrationCallback(capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId): void;
@@ -1 +1 @@
1
- {"version":3,"file":"commandActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/commandActionProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAKrE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAK7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,qBAAa,sBAAsB;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,OAAO,CAAC,QAAQ,CAAC,OAAO,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AAa,sBAAsB;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IAEjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqC;IAEnE,OAAO,CAAC,GAAG,CAAsB;IACjC,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,QAAQ,CAgBV;gBAGF,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,OAAO;IAUnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,EACvC,gCAAgC,EAAE,OAAO;IA0D7C,2BAA2B,CACvB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO;YA+BN,cAAc;IAsC5B,OAAO,CAAC,6BAA6B;IA6BrC,OAAO,CAAC,wBAAwB;CA8BnC"}
@@ -8,6 +8,22 @@ const replyContext_1 = require("../../entities/context/replyContext");
8
8
  const messageTypes_1 = require("../../types/messageTypes");
9
9
  class CommandActionProcessor {
10
10
  constructor(botName, storage, scheduler, logger) {
11
+ this.commands = {
12
+ '__msg:Any': [],
13
+ '__msg:Text': [],
14
+ '__msg:Sticker': [],
15
+ '__msg:Animation': [],
16
+ '__msg:Document': [],
17
+ '__msg:Voice': [],
18
+ '__msg:Audio': [],
19
+ '__msg:LeftChatMember': [],
20
+ '__msg:NewChatMember': [],
21
+ '__msg:Poll': [],
22
+ '__msg:Location': [],
23
+ '__msg:Photo': [],
24
+ '__msg:Forward': [],
25
+ '__msg:Unknown': []
26
+ };
11
27
  this.storage = storage;
12
28
  this.scheduler = scheduler;
13
29
  this.logger = logger;
@@ -17,15 +33,20 @@ class CommandActionProcessor {
17
33
  initialize(api, telegraf, commands, verboseLoggingForIncomingMessage) {
18
34
  this.api = api;
19
35
  this.telegraf = telegraf;
20
- this.commands = commands;
21
- if (this.commands.length > 0) {
22
- this.permanentTriggersToBeProcessed = new Set(this.commands
23
- .flatMap((x) => x.triggers)
24
- .map((x) => typeof x == 'string'
25
- ? x.startsWith(messageTypes_1.INTERNAL_MESSAGE_TYPE_PREFIX)
26
- ? x
27
- : messageTypes_1.MessageType.Text
28
- : messageTypes_1.MessageType.Text));
36
+ for (const msgType of Object.values(messageTypes_1.MessageType)) {
37
+ if (msgType == messageTypes_1.MessageType.Text) {
38
+ this.commands[msgType] = commands.filter((cmd) => cmd.triggers.find((x) => typeof x != 'string') !=
39
+ undefined ||
40
+ cmd.triggers.find((x) => typeof x == 'string' &&
41
+ !x.startsWith(messageTypes_1.INTERNAL_MESSAGE_TYPE_PREFIX)) ||
42
+ cmd.triggers.includes(messageTypes_1.MessageType.Text) ||
43
+ cmd.triggers.includes(messageTypes_1.MessageType.Any));
44
+ continue;
45
+ }
46
+ this.commands[msgType] = commands.filter((cmd) => cmd.triggers.includes(msgType) ||
47
+ cmd.triggers.includes(messageTypes_1.MessageType.Any));
48
+ }
49
+ if (commands.length > 0) {
29
50
  this.telegraf.on('message', async (ctx) => {
30
51
  const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.botName);
31
52
  const logger = this.logger.createScope(this.botName, msg.traceId, msg.chatInfo.name);
@@ -35,9 +56,7 @@ class CommandActionProcessor {
35
56
  else {
36
57
  logger.logWithTraceId(`${msg.from?.first_name ?? 'Unknown'} (${msg.from?.id ?? 'Unknown'}): ${msg.text || `<non-text message: ${msg.type}>`}`);
37
58
  }
38
- if (this.permanentTriggersToBeProcessed.has(msg.type) ||
39
- this.replyCaptures.find((x) => x.parentMessageId == msg.replyToMessageId))
40
- await this.processMessage(msg);
59
+ await this.processMessage(msg);
41
60
  });
42
61
  }
43
62
  }
@@ -54,7 +73,7 @@ class CommandActionProcessor {
54
73
  }
55
74
  async processMessage(msg) {
56
75
  const ctx = new messageContext_1.MessageContext(this.storage, this.scheduler);
57
- for (const commandAction of this.commands) {
76
+ for (const commandAction of this.commands[msg.type]) {
58
77
  this.initializeMessageContext(ctx, commandAction, msg);
59
78
  try {
60
79
  const responses = await commandAction.exec(ctx);
@@ -1,7 +1,7 @@
1
1
  import { Message, Update } from 'telegraf/types';
2
2
  export declare const INTERNAL_MESSAGE_TYPE_PREFIX = "__msg:";
3
3
  export declare const MessageType: {
4
- readonly Any: "__msg:ANY";
4
+ readonly Any: "__msg:Any";
5
5
  readonly Text: "__msg:Text";
6
6
  readonly Sticker: "__msg:Sticker";
7
7
  readonly Animation: "__msg:Animation";
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MessageType = exports.INTERNAL_MESSAGE_TYPE_PREFIX = void 0;
4
4
  exports.INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
5
5
  exports.MessageType = {
6
- Any: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}ANY`,
6
+ Any: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Any`,
7
7
  Text: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Text`,
8
8
  Sticker: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Sticker`,
9
9
  Animation: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Animation`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -7,16 +7,17 @@ import { ReplyContext } from '../../entities/context/replyContext';
7
7
  import { IActionWithState } from '../../types/action';
8
8
  import { IActionState } from '../../types/actionState';
9
9
  import { ILogger } from '../../types/logger';
10
- import {
11
- INTERNAL_MESSAGE_TYPE_PREFIX,
12
- MessageType
13
- } from '../../types/messageTypes';
14
10
  import { IScheduler } from '../../types/scheduler';
15
11
  import { IStorageClient } from '../../types/storage';
16
12
  import { TelegramApiService } from '../telegramApi';
17
13
  import { IReplyCapture } from '../../types/capture';
18
14
  import { TraceId } from '../../types/trace';
19
15
  import { ChatInfo } from '../../dtos/chatInfo';
16
+ import {
17
+ INTERNAL_MESSAGE_TYPE_PREFIX,
18
+ MessageType,
19
+ MessageTypeValue
20
+ } from '../../types/messageTypes';
20
21
 
21
22
  export class CommandActionProcessor {
22
23
  private readonly storage: IStorageClient;
@@ -28,9 +29,23 @@ export class CommandActionProcessor {
28
29
 
29
30
  private api!: TelegramApiService;
30
31
  private telegraf!: Telegraf;
31
- private commands!: CommandAction<IActionState>[];
32
-
33
- private permanentTriggersToBeProcessed!: Set<string>;
32
+ private commands: Record<MessageTypeValue, CommandAction<IActionState>[]> =
33
+ {
34
+ '__msg:Any': [],
35
+ '__msg:Text': [],
36
+ '__msg:Sticker': [],
37
+ '__msg:Animation': [],
38
+ '__msg:Document': [],
39
+ '__msg:Voice': [],
40
+ '__msg:Audio': [],
41
+ '__msg:LeftChatMember': [],
42
+ '__msg:NewChatMember': [],
43
+ '__msg:Poll': [],
44
+ '__msg:Location': [],
45
+ '__msg:Photo': [],
46
+ '__msg:Forward': [],
47
+ '__msg:Unknown': []
48
+ };
34
49
 
35
50
  constructor(
36
51
  botName: string,
@@ -54,21 +69,33 @@ export class CommandActionProcessor {
54
69
  ) {
55
70
  this.api = api;
56
71
  this.telegraf = telegraf;
57
- this.commands = commands;
58
-
59
- if (this.commands.length > 0) {
60
- this.permanentTriggersToBeProcessed = new Set(
61
- this.commands
62
- .flatMap((x) => x.triggers)
63
- .map((x) =>
64
- typeof x == 'string'
65
- ? x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)
66
- ? x
67
- : MessageType.Text
68
- : MessageType.Text
69
- )
72
+
73
+ for (const msgType of Object.values(MessageType)) {
74
+ if (msgType == MessageType.Text) {
75
+ this.commands[msgType] = commands.filter(
76
+ (cmd) =>
77
+ cmd.triggers.find((x) => typeof x != 'string') !=
78
+ undefined ||
79
+ cmd.triggers.find(
80
+ (x) =>
81
+ typeof x == 'string' &&
82
+ !x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)
83
+ ) ||
84
+ cmd.triggers.includes(MessageType.Text) ||
85
+ cmd.triggers.includes(MessageType.Any)
86
+ );
87
+
88
+ continue;
89
+ }
90
+
91
+ this.commands[msgType] = commands.filter(
92
+ (cmd) =>
93
+ cmd.triggers.includes(msgType) ||
94
+ cmd.triggers.includes(MessageType.Any)
70
95
  );
96
+ }
71
97
 
98
+ if (commands.length > 0) {
72
99
  this.telegraf.on('message', async (ctx) => {
73
100
  const msg = new IncomingMessage(
74
101
  ctx.update.message,
@@ -91,13 +118,7 @@ export class CommandActionProcessor {
91
118
  );
92
119
  }
93
120
 
94
- if (
95
- this.permanentTriggersToBeProcessed.has(msg.type) ||
96
- this.replyCaptures.find(
97
- (x) => x.parentMessageId == msg.replyToMessageId
98
- )
99
- )
100
- await this.processMessage(msg);
121
+ await this.processMessage(msg);
101
122
  });
102
123
  }
103
124
  }
@@ -137,14 +158,13 @@ export class CommandActionProcessor {
137
158
  );
138
159
  });
139
160
  }
140
-
141
161
  private async processMessage(msg: IncomingMessage) {
142
162
  const ctx = new MessageContext<IActionState>(
143
163
  this.storage,
144
164
  this.scheduler
145
165
  );
146
166
 
147
- for (const commandAction of this.commands) {
167
+ for (const commandAction of this.commands[msg.type]) {
148
168
  this.initializeMessageContext(ctx, commandAction, msg);
149
169
 
150
170
  try {
@@ -3,7 +3,7 @@ import { Message, Update } from 'telegraf/types';
3
3
  export const INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
4
4
 
5
5
  export const MessageType = {
6
- Any: `${INTERNAL_MESSAGE_TYPE_PREFIX}ANY`,
6
+ Any: `${INTERNAL_MESSAGE_TYPE_PREFIX}Any`,
7
7
  Text: `${INTERNAL_MESSAGE_TYPE_PREFIX}Text`,
8
8
  Sticker: `${INTERNAL_MESSAGE_TYPE_PREFIX}Sticker`,
9
9
  Animation: `${INTERNAL_MESSAGE_TYPE_PREFIX}Animation`,