chz-telegram-bot 0.3.13 → 0.3.15

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 (84) hide show
  1. package/README.md +92 -73
  2. package/dist/dtos/chatInfo.d.ts +10 -1
  3. package/dist/dtos/chatInfo.d.ts.map +1 -1
  4. package/dist/dtos/chatInfo.js +11 -5
  5. package/dist/dtos/commandTriggerCheckResult.d.ts +5 -5
  6. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
  7. package/dist/dtos/commandTriggerCheckResult.js +4 -4
  8. package/dist/dtos/cooldownInfo.d.ts +9 -3
  9. package/dist/dtos/cooldownInfo.d.ts.map +1 -1
  10. package/dist/dtos/cooldownInfo.js +5 -1
  11. package/dist/dtos/incomingMessage.d.ts +1 -1
  12. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  13. package/dist/dtos/incomingMessage.js +2 -2
  14. package/dist/dtos/incomingQuery.d.ts.map +1 -1
  15. package/dist/dtos/messageInfo.d.ts +21 -0
  16. package/dist/dtos/messageInfo.d.ts.map +1 -0
  17. package/dist/dtos/messageInfo.js +24 -0
  18. package/dist/dtos/userInfo.d.ts +12 -0
  19. package/dist/dtos/userInfo.d.ts.map +1 -0
  20. package/dist/dtos/userInfo.js +16 -0
  21. package/dist/entities/actions/commandAction.d.ts +2 -2
  22. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  23. package/dist/entities/actions/commandAction.js +11 -8
  24. package/dist/entities/actions/inlineQueryAction.d.ts +2 -2
  25. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  26. package/dist/entities/actions/replyCaptureAction.d.ts +4 -4
  27. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
  28. package/dist/entities/actions/replyCaptureAction.js +4 -4
  29. package/dist/entities/actions/scheduledAction.d.ts +2 -2
  30. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  31. package/dist/entities/context/baseContext.d.ts +2 -1
  32. package/dist/entities/context/baseContext.d.ts.map +1 -1
  33. package/dist/entities/context/baseContext.js +3 -3
  34. package/dist/entities/context/chatContext.d.ts +3 -2
  35. package/dist/entities/context/chatContext.d.ts.map +1 -1
  36. package/dist/entities/context/chatContext.js +3 -3
  37. package/dist/entities/context/inlineQueryContext.d.ts +3 -2
  38. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  39. package/dist/entities/context/inlineQueryContext.js +3 -3
  40. package/dist/entities/context/messageContext.d.ts +11 -15
  41. package/dist/entities/context/messageContext.d.ts.map +1 -1
  42. package/dist/entities/context/messageContext.js +15 -20
  43. package/dist/entities/context/replyContext.d.ts +9 -15
  44. package/dist/entities/context/replyContext.d.ts.map +1 -1
  45. package/dist/entities/context/replyContext.js +12 -20
  46. package/dist/helpers/mapUtils.d.ts +9 -2
  47. package/dist/helpers/mapUtils.d.ts.map +1 -1
  48. package/dist/helpers/mapUtils.js +1 -1
  49. package/dist/services/actionProcessors/baseProcessor.d.ts +2 -2
  50. package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
  51. package/dist/services/actionProcessors/commandActionProcessor.d.ts +1 -0
  52. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  53. package/dist/services/actionProcessors/commandActionProcessor.js +18 -19
  54. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +1 -1
  55. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
  56. package/dist/services/actionProcessors/scheduledActionProcessor.js +2 -2
  57. package/dist/types/action.d.ts +2 -2
  58. package/dist/types/action.d.ts.map +1 -1
  59. package/dist/types/capture.d.ts +3 -3
  60. package/dist/types/capture.d.ts.map +1 -1
  61. package/dtos/chatInfo.ts +10 -9
  62. package/dtos/commandTriggerCheckResult.ts +5 -15
  63. package/dtos/cooldownInfo.ts +6 -1
  64. package/dtos/incomingMessage.ts +7 -2
  65. package/dtos/incomingQuery.ts +4 -12
  66. package/dtos/messageInfo.ts +17 -0
  67. package/dtos/userInfo.ts +8 -0
  68. package/entities/actions/commandAction.ts +16 -12
  69. package/entities/actions/inlineQueryAction.ts +2 -2
  70. package/entities/actions/replyCaptureAction.ts +9 -9
  71. package/entities/actions/scheduledAction.ts +2 -2
  72. package/entities/context/baseContext.ts +11 -1
  73. package/entities/context/chatContext.ts +16 -5
  74. package/entities/context/inlineQueryContext.ts +10 -2
  75. package/entities/context/messageContext.ts +24 -25
  76. package/entities/context/replyContext.ts +26 -25
  77. package/helpers/mapUtils.ts +15 -2
  78. package/package.json +1 -1
  79. package/services/actionProcessors/baseProcessor.ts +3 -3
  80. package/services/actionProcessors/commandActionProcessor.ts +43 -21
  81. package/services/actionProcessors/inlineQueryActionProcessor.ts +3 -3
  82. package/services/actionProcessors/scheduledActionProcessor.ts +7 -4
  83. package/types/action.ts +2 -2
  84. package/types/capture.ts +5 -3
@@ -8,9 +8,14 @@ const replyContext_1 = require("../../entities/context/replyContext");
8
8
  const messageTypes_1 = require("../../types/messageTypes");
9
9
  const objectFromEntries_1 = require("../../helpers/objectFromEntries");
10
10
  const baseProcessor_1 = require("./baseProcessor");
11
+ const mapUtils_1 = require("../../helpers/mapUtils");
12
+ const messageInfo_1 = require("../../dtos/messageInfo");
13
+ const userInfo_1 = require("../../dtos/userInfo");
14
+ const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
11
15
  class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
12
16
  replyCaptures = [];
13
17
  botInfo;
18
+ lastMessages = new Map();
14
19
  commands = (0, objectFromEntries_1.typeSafeObjectFromEntries)(Object.values(messageTypes_1.MessageType).map((x) => [
15
20
  x,
16
21
  []
@@ -33,7 +38,7 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
33
38
  }
34
39
  if (commands.length > 0) {
35
40
  telegraf.on('message', (ctx) => {
36
- const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.botName);
41
+ const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.botName, (0, mapUtils_1.getOrSetIfNotExists)(this.lastMessages, ctx.chat.id, []));
37
42
  const logger = this.logger.createScope(this.botName, msg.traceId, msg.chatInfo.name);
38
43
  if (verboseLoggingForIncomingMessage) {
39
44
  logger.logObjectWithTraceId(ctx.update.message);
@@ -57,7 +62,11 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
57
62
  });
58
63
  }
59
64
  async processMessage(msg) {
60
- const ctx = new messageContext_1.MessageContext(this.storage, this.scheduler);
65
+ const messageHistoryArray = (0, mapUtils_1.getOrSetIfNotExists)(this.lastMessages, msg.chatInfo.id, []);
66
+ if (messageHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
67
+ messageHistoryArray.shift();
68
+ messageHistoryArray.push(msg);
69
+ const ctx = new messageContext_1.MessageContextInternal(this.storage, this.scheduler);
61
70
  const commandsToCheck = new Set(this.commands[msg.type]);
62
71
  if (msg.type != messageTypes_1.MessageType.Text && msg.text != '') {
63
72
  this.commands[messageTypes_1.MessageType.Text].forEach((x) => commandsToCheck.add(x));
@@ -67,7 +76,7 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
67
76
  await this.executeAction(commandAction, ctx);
68
77
  }
69
78
  if (this.replyCaptures.length != 0) {
70
- const replyCtx = new replyContext_1.ReplyContext(this.storage, this.scheduler);
79
+ const replyCtx = new replyContext_1.ReplyContextInternal(this.storage, this.scheduler);
71
80
  for (const replyAction of this.replyCaptures) {
72
81
  this.initializeReplyCaptureContext(replyCtx, replyAction, msg);
73
82
  await this.executeAction(replyAction, replyCtx);
@@ -77,14 +86,9 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
77
86
  }
78
87
  initializeReplyCaptureContext(ctx, action, message) {
79
88
  ctx.replyMessageId = message.replyToMessageId;
80
- ctx.messageId = message.messageId;
81
- ctx.messageText = message.text;
82
- ctx.messageType = message.type;
83
- ctx.fromUserId = message.from?.id;
84
- ctx.fromUserName =
85
- (message.from?.first_name ?? 'Unknown user') +
86
- (message.from?.last_name ? ` ${message.from.last_name}` : '');
87
- ctx.messageUpdateObject = message.updateObject;
89
+ ctx.messageInfo = new messageInfo_1.MessageInfo(message.messageId, message.text, message.type, message.updateObject);
90
+ ctx.userInfo = new userInfo_1.UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
91
+ (message.from?.last_name ? ` ${message.from.last_name}` : ''));
88
92
  ctx.botName = this.botName;
89
93
  ctx.action = action;
90
94
  ctx.chatInfo = message.chatInfo;
@@ -95,14 +99,9 @@ class CommandActionProcessor extends baseProcessor_1.BaseActionProcessor {
95
99
  ctx.logger = this.logger.createScope(this.botName, message.traceId, message.chatInfo.name);
96
100
  }
97
101
  initializeMessageContext(ctx, action, message) {
98
- ctx.messageId = message.messageId;
99
- ctx.messageText = message.text;
100
- ctx.messageType = message.type;
101
- ctx.fromUserId = message.from?.id;
102
- ctx.fromUserName =
103
- (message.from?.first_name ?? 'Unknown user') +
104
- (message.from?.last_name ? ` ${message.from.last_name}` : '');
105
- ctx.messageUpdateObject = message.updateObject;
102
+ ctx.messageInfo = new messageInfo_1.MessageInfo(message.messageId, message.text, message.type, message.updateObject);
103
+ ctx.userInfo = new userInfo_1.UserInfo(message.from?.id ?? -1, (message.from?.first_name ?? 'Unknown user') +
104
+ (message.from?.last_name ? ` ${message.from.last_name}` : ''));
106
105
  ctx.matchResults = [];
107
106
  ctx.startCooldown = true;
108
107
  ctx.responses = [];
@@ -27,7 +27,7 @@ class InlineQueryActionProcessor extends baseProcessor_1.BaseActionProcessor {
27
27
  pendingInlineQueries.push(query);
28
28
  });
29
29
  this.scheduler.createTask('InlineQueryProcessing', async () => {
30
- const ctx = new inlineQueryContext_1.InlineQueryContext(this.storage, this.scheduler);
30
+ const ctx = new inlineQueryContext_1.InlineQueryContextInternal(this.storage, this.scheduler);
31
31
  const queriesToProcess = [...pendingInlineQueries];
32
32
  pendingInlineQueries = [];
33
33
  for (const inlineQuery of queriesToProcess) {
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/scheduledActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAgB,MAAM,wBAAwB,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,qBAAa,wBAAyB,SAAQ,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAE/C,OAAO,CAAC,SAAS,CAAmC;gBAGhD,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,OAAO;IAMnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YAgDL,YAAY;IAuB1B,OAAO,CAAC,qBAAqB;CAmBhC"}
1
+ {"version":3,"file":"scheduledActionProcessor.d.ts","sourceRoot":"","sources":["../../../services/actionProcessors/scheduledActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAgB,MAAM,wBAAwB,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,qBAAa,wBAAyB,SAAQ,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAE/C,OAAO,CAAC,SAAS,CAAmC;gBAGhD,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,OAAO;IAMnB,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YAgDL,YAAY;IA0B1B,OAAO,CAAC,qBAAqB;CAmBhC"}
@@ -41,10 +41,10 @@ class ScheduledActionProcessor extends baseProcessor_1.BaseActionProcessor {
41
41
  }
42
42
  }
43
43
  async runScheduled() {
44
- const ctx = new chatContext_1.ChatContext(this.storage, this.scheduler);
44
+ const ctx = new chatContext_1.ChatContextInternal(this.storage, this.scheduler);
45
45
  for (const [chatName, chatId] of Object.entries(this.chats)) {
46
46
  for (const scheduledAction of this.scheduled) {
47
- this.initializeChatContext(ctx, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName), (0, traceFactory_1.createTrace)(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
47
+ this.initializeChatContext(ctx, scheduledAction, new chatInfo_1.ChatInfo(chatId, chatName, []), (0, traceFactory_1.createTrace)(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`));
48
48
  await this.executeAction(scheduledAction, ctx);
49
49
  }
50
50
  }
@@ -1,4 +1,4 @@
1
- import { BaseContext } from '../entities/context/baseContext';
1
+ import { BaseContextInternal } from '../entities/context/baseContext';
2
2
  import { IActionState } from './actionState';
3
3
  import { BotResponse } from './response';
4
4
  export type ActionKey = string & {
@@ -9,6 +9,6 @@ export interface IActionWithState<TActionState extends IActionState> extends IAc
9
9
  }
10
10
  export interface IAction {
11
11
  readonly key: ActionKey;
12
- exec(ctx: BaseContext<IAction>): Promise<BotResponse[]>;
12
+ exec(ctx: BaseContextInternal<IAction>): Promise<BotResponse[]>;
13
13
  }
14
14
  //# sourceMappingURL=action.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"action.d.ts","sourceRoot":"","sources":["../../types/action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1D,MAAM,WAAW,gBAAgB,CAAC,YAAY,SAAS,YAAY,CAC/D,SAAQ,OAAO;IACf,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACjD;AAED,MAAM,WAAW,OAAO;IACpB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAC3D"}
1
+ {"version":3,"file":"action.d.ts","sourceRoot":"","sources":["../../types/action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1D,MAAM,WAAW,gBAAgB,CAAC,YAAY,SAAS,YAAY,CAC/D,SAAQ,OAAO;IACf,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACjD;AAED,MAAM,WAAW,OAAO;IACpB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,IAAI,CAAC,GAAG,EAAE,mBAAmB,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CACnE"}
@@ -1,4 +1,4 @@
1
- import { ReplyContext } from '../entities/context/replyContext';
1
+ import { ReplyContextInternal } from '../entities/context/replyContext';
2
2
  import { IActionState } from './actionState';
3
3
  import { CommandTrigger } from './commandTrigger';
4
4
  import { IActionWithState } from './action';
@@ -11,13 +11,13 @@ export interface ICaptureController {
11
11
  */
12
12
  trigger: CommandTrigger[],
13
13
  /** Callback that will be called on trigger */
14
- handler: (replyContext: ReplyContext<TParentActionState>) => Promise<void>,
14
+ handler: (replyContext: ReplyContextInternal<TParentActionState>) => Promise<void>,
15
15
  /** Abort controller to abort capturing manually */
16
16
  abortController: AbortController) => void;
17
17
  }
18
18
  export interface IReplyCapture {
19
19
  trigger: CommandTrigger[];
20
- handler: (replyContext: ReplyContext<IActionState>) => Promise<void>;
20
+ handler: (replyContext: ReplyContextInternal<IActionState>) => Promise<void>;
21
21
  abortController: AbortController;
22
22
  action: IActionWithState<IActionState>;
23
23
  }
@@ -1 +1 @@
1
- {"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../types/capture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,WAAW,kBAAkB;IAC/B,cAAc,EAAE,CAAC,kBAAkB,SAAS,YAAY;IACpD;;;;OAIG;IACH,OAAO,EAAE,cAAc,EAAE;IACzB,8CAA8C;IAC9C,OAAO,EAAE,CACL,YAAY,EAAE,YAAY,CAAC,kBAAkB,CAAC,KAC7C,OAAO,CAAC,IAAI,CAAC;IAClB,mDAAmD;IACnD,eAAe,EAAE,eAAe,KAC/B,IAAI,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC,YAAY,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE,eAAe,EAAE,eAAe,CAAC;IACjC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;CAC1C"}
1
+ {"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../types/capture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,WAAW,kBAAkB;IAC/B,cAAc,EAAE,CAAC,kBAAkB,SAAS,YAAY;IACpD;;;;OAIG;IACH,OAAO,EAAE,cAAc,EAAE;IACzB,8CAA8C;IAC9C,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC;IAClB,mDAAmD;IACnD,eAAe,EAAE,eAAe,KAC/B,IAAI,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,YAAY,CAAC,KAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,eAAe,EAAE,eAAe,CAAC;IACjC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;CAC1C"}
package/dtos/chatInfo.ts CHANGED
@@ -1,11 +1,12 @@
1
- export class ChatInfo {
2
- /** Id of a chat that action is executed in. */
3
- readonly id: number;
4
- /** Name of a chat that action is executed in. */
5
- readonly name: string;
1
+ import { IncomingMessage } from './incomingMessage';
6
2
 
7
- constructor(chatId: number, chatName: string) {
8
- this.id = chatId;
9
- this.name = chatName;
10
- }
3
+ export class ChatInfo {
4
+ constructor(
5
+ /** Id of a chat that action is executed in. */
6
+ readonly id: number,
7
+ /** Name of a chat that action is executed in. */
8
+ readonly name: string,
9
+ /** Last 100 messages in chat where action is executed */
10
+ readonly messageHistory: IncomingMessage[]
11
+ ) {}
11
12
  }
@@ -20,22 +20,12 @@ export class CommandTriggerCheckResult {
20
20
  return new CommandTriggerCheckResult(true, [], false);
21
21
  }
22
22
 
23
- readonly shouldExecute: boolean;
24
- readonly matchResults: RegExpExecArray[];
25
- readonly skipCooldown: boolean;
26
- readonly reason: SkipTriggerReasons | undefined;
27
-
28
23
  constructor(
29
- shouldExecute: boolean,
30
- matchResults: RegExpExecArray[],
31
- skipCooldown: boolean,
32
- reason?: SkipTriggerReasons
33
- ) {
34
- this.shouldExecute = shouldExecute;
35
- this.matchResults = matchResults;
36
- this.skipCooldown = skipCooldown;
37
- this.reason = reason;
38
- }
24
+ readonly shouldExecute: boolean,
25
+ readonly matchResults: RegExpExecArray[],
26
+ readonly skipCooldown: boolean,
27
+ readonly reason?: SkipTriggerReasons
28
+ ) {}
39
29
 
40
30
  mergeWith(other: CommandTriggerCheckResult) {
41
31
  return new CommandTriggerCheckResult(
@@ -1,5 +1,10 @@
1
1
  import { Seconds } from '../types/timeValues';
2
2
 
3
3
  export class CooldownInfo {
4
- constructor(public seconds: Seconds, public message: string | undefined) {}
4
+ constructor(
5
+ /** New one-time cooldown in seconds */
6
+ readonly seconds: Seconds,
7
+ /** Cooldown message to be shown */
8
+ readonly message: string | undefined
9
+ ) {}
5
10
  }
@@ -40,7 +40,11 @@ export class IncomingMessage {
40
40
  return MessageType.Unknown;
41
41
  }
42
42
 
43
- constructor(ctxMessage: TelegrafContextMessage, botName: string) {
43
+ constructor(
44
+ ctxMessage: TelegrafContextMessage,
45
+ botName: string,
46
+ history: IncomingMessage[]
47
+ ) {
44
48
  this.traceId = createTrace(
45
49
  this,
46
50
  botName,
@@ -62,7 +66,8 @@ export class IncomingMessage {
62
66
  ctxMessage.chat.id,
63
67
  'title' in ctxMessage.chat
64
68
  ? `${ctxMessage.chat.title} ${ctxMessage.chat.id}`
65
- : 'DM'
69
+ : 'DM',
70
+ history
66
71
  );
67
72
  this.type = this.detectMessageType(ctxMessage);
68
73
  this.updateObject = ctxMessage;
@@ -1,22 +1,14 @@
1
1
  import { TraceId } from '../types/trace';
2
2
 
3
3
  export class IncomingInlineQuery {
4
- readonly queryId: string;
5
- readonly query: string;
6
- readonly userId: number;
7
- readonly traceId: TraceId;
8
4
  readonly abortController: AbortController;
9
5
 
10
6
  constructor(
11
- queryId: string,
12
- query: string,
13
- userId: number,
14
- traceId: TraceId
7
+ readonly queryId: string,
8
+ readonly query: string,
9
+ readonly userId: number,
10
+ readonly traceId: TraceId
15
11
  ) {
16
- this.queryId = queryId;
17
- this.query = query;
18
- this.userId = userId;
19
- this.traceId = traceId;
20
12
  this.abortController = new AbortController();
21
13
  }
22
14
  }
@@ -0,0 +1,17 @@
1
+ import {
2
+ MessageTypeValue,
3
+ TelegrafContextMessage
4
+ } from '../types/messageTypes';
5
+
6
+ export class MessageInfo {
7
+ constructor(
8
+ /** Id of a message that triggered this action. */
9
+ readonly id: number,
10
+ /** Text of a message that triggered this action. */
11
+ readonly text: string,
12
+ /** Type of message being received */
13
+ readonly type: MessageTypeValue,
14
+ /** Message object recieved from Telegram */
15
+ readonly telegramUpdateObject: TelegrafContextMessage
16
+ ) {}
17
+ }
@@ -0,0 +1,8 @@
1
+ export class UserInfo {
2
+ constructor(
3
+ /** Id of a user that sent a message that triggered this action. */
4
+ readonly id: number,
5
+ /** Name of a user that sent a message that triggered this action. */
6
+ readonly name: string
7
+ ) {}
8
+ }
@@ -6,7 +6,7 @@ import { toArray } from '../../helpers/toArray';
6
6
  import { IActionState } from '../../types/actionState';
7
7
  import { IActionWithState, ActionKey } from '../../types/action';
8
8
  import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
9
- import { MessageContext } from '../context/messageContext';
9
+ import { MessageContextInternal } from '../context/messageContext';
10
10
  import { CommandTrigger } from '../../types/commandTrigger';
11
11
  import { Noop } from '../../helpers/noop';
12
12
  import { MessageType } from '../../types/messageTypes';
@@ -66,7 +66,7 @@ export class CommandAction<TActionState extends IActionState>
66
66
  this.key = `command:${this.name.replace('.', '-')}` as ActionKey;
67
67
  }
68
68
 
69
- async exec(ctx: MessageContext<TActionState>) {
69
+ async exec(ctx: MessageContextInternal<TActionState>) {
70
70
  if (!ctx.isInitialized)
71
71
  throw new Error(
72
72
  `Context for ${this.key} is not initialized or already consumed`
@@ -108,7 +108,7 @@ export class CommandAction<TActionState extends IActionState>
108
108
  ctx.chatInfo,
109
109
  ctx.traceId,
110
110
  this,
111
- new ReplyInfo(ctx.messageId)
111
+ new ReplyInfo(ctx.messageInfo.id)
112
112
  )
113
113
  ];
114
114
 
@@ -145,18 +145,22 @@ export class CommandAction<TActionState extends IActionState>
145
145
  }
146
146
 
147
147
  private checkIfShouldBeExecuted(
148
- ctx: MessageContext<TActionState>,
148
+ ctx: MessageContextInternal<TActionState>,
149
149
  trigger: CommandTrigger,
150
150
  state: TActionState
151
151
  ) {
152
- if (!ctx.fromUserId)
152
+ const triggerCheckResult = this.checkTrigger(ctx, trigger);
153
+
154
+ if (!triggerCheckResult.shouldExecute) return triggerCheckResult;
155
+
156
+ if (!ctx.userInfo.id)
153
157
  return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
154
158
  'UserIdMissing'
155
159
  );
156
160
 
157
161
  const isUserAllowed =
158
162
  this.allowedUsers.length == 0 ||
159
- this.allowedUsers.includes(ctx.fromUserId);
163
+ this.allowedUsers.includes(ctx.userInfo.id);
160
164
 
161
165
  if (!isUserAllowed)
162
166
  return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
@@ -179,18 +183,18 @@ export class CommandAction<TActionState extends IActionState>
179
183
  'CustomConditionNotMet'
180
184
  );
181
185
 
182
- return this.checkTrigger(ctx, trigger);
186
+ return triggerCheckResult;
183
187
  }
184
188
 
185
189
  private checkTrigger(
186
- ctx: MessageContext<TActionState>,
190
+ ctx: MessageContextInternal<TActionState>,
187
191
  trigger: CommandTrigger
188
192
  ) {
189
- if (trigger == MessageType.Any || trigger == ctx.messageType)
193
+ if (trigger == MessageType.Any || trigger == ctx.messageInfo.type)
190
194
  return CommandTriggerCheckResult.Trigger();
191
195
 
192
196
  if (typeof trigger == 'string')
193
- return ctx.messageText.toLowerCase() == trigger.toLowerCase()
197
+ return ctx.messageInfo.text.toLowerCase() == trigger.toLowerCase()
194
198
  ? CommandTriggerCheckResult.Trigger()
195
199
  : CommandTriggerCheckResult.DoNotTrigger('TriggerNotSatisfied');
196
200
 
@@ -198,14 +202,14 @@ export class CommandAction<TActionState extends IActionState>
198
202
 
199
203
  trigger.lastIndex = 0;
200
204
 
201
- const execResult = trigger.exec(ctx.messageText);
205
+ const execResult = trigger.exec(ctx.messageInfo.text);
202
206
  if (execResult != null) {
203
207
  let regexMatchLimit = 100;
204
208
  matchResults.push(execResult);
205
209
 
206
210
  if (trigger.global) {
207
211
  while (regexMatchLimit > 0) {
208
- const nextResult = trigger.exec(ctx.messageText);
212
+ const nextResult = trigger.exec(ctx.messageInfo.text);
209
213
 
210
214
  if (nextResult == null) break;
211
215
 
@@ -1,6 +1,6 @@
1
1
  import { Noop } from '../../helpers/noop';
2
2
  import { ActionKey, IAction } from '../../types/action';
3
- import { InlineQueryContext } from '../context/inlineQueryContext';
3
+ import { InlineQueryContextInternal } from '../context/inlineQueryContext';
4
4
  import { InlineQueryHandler } from '../../types/handlers';
5
5
 
6
6
  export class InlineQueryAction implements IAction {
@@ -24,7 +24,7 @@ export class InlineQueryAction implements IAction {
24
24
  this.key = `inline:${this.name.replace('.', '-')}` as ActionKey;
25
25
  }
26
26
 
27
- async exec(ctx: InlineQueryContext) {
27
+ async exec(ctx: InlineQueryContextInternal) {
28
28
  if (!ctx.isInitialized)
29
29
  throw new Error(
30
30
  `Context for ${this.key} is not initialized or already consumed`
@@ -3,7 +3,7 @@ import { Noop } from '../../helpers/noop';
3
3
  import { IActionState } from '../../types/actionState';
4
4
  import { CommandTrigger } from '../../types/commandTrigger';
5
5
  import { ActionKey, IAction, IActionWithState } from '../../types/action';
6
- import { ReplyContext } from '../context/replyContext';
6
+ import { ReplyContextInternal } from '../context/replyContext';
7
7
 
8
8
  export class ReplyCaptureAction<TParentActionState extends IActionState>
9
9
  implements IAction
@@ -11,7 +11,7 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
11
11
  readonly parentMessageId: number;
12
12
  readonly key: ActionKey;
13
13
  readonly handler: (
14
- replyContext: ReplyContext<TParentActionState>
14
+ replyContext: ReplyContextInternal<TParentActionState>
15
15
  ) => Promise<void>;
16
16
  readonly triggers: CommandTrigger[];
17
17
  readonly abortController: AbortController;
@@ -20,7 +20,7 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
20
20
  parentMessageId: number,
21
21
  parentAction: IActionWithState<TParentActionState>,
22
22
  handler: (
23
- replyContext: ReplyContext<TParentActionState>
23
+ replyContext: ReplyContextInternal<TParentActionState>
24
24
  ) => Promise<void>,
25
25
  triggers: CommandTrigger[],
26
26
  abortController: AbortController
@@ -35,7 +35,7 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
35
35
  .replace('.', '')}` as ActionKey;
36
36
  }
37
37
 
38
- async exec(ctx: ReplyContext<TParentActionState>) {
38
+ async exec(ctx: ReplyContextInternal<TParentActionState>) {
39
39
  if (!ctx.isInitialized)
40
40
  throw new Error(
41
41
  `Context for ${this.key} is not initialized or already consumed`
@@ -61,7 +61,7 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
61
61
  }
62
62
 
63
63
  private checkIfShouldBeExecuted(
64
- ctx: ReplyContext<TParentActionState>,
64
+ ctx: ReplyContextInternal<TParentActionState>,
65
65
  trigger: CommandTrigger
66
66
  ) {
67
67
  if (ctx.replyMessageId != this.parentMessageId)
@@ -69,11 +69,11 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
69
69
  'TriggerNotSatisfied'
70
70
  );
71
71
 
72
- if (trigger == ctx.messageType)
72
+ if (trigger == ctx.messageInfo.type)
73
73
  return CommandTriggerCheckResult.Trigger();
74
74
 
75
75
  if (typeof trigger == 'string')
76
- if (ctx.messageText.toLowerCase() == trigger.toLowerCase())
76
+ if (ctx.messageInfo.text.toLowerCase() == trigger.toLowerCase())
77
77
  return CommandTriggerCheckResult.Trigger();
78
78
  else
79
79
  return CommandTriggerCheckResult.DoNotTrigger(
@@ -84,14 +84,14 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
84
84
 
85
85
  trigger.lastIndex = 0;
86
86
 
87
- const execResult = trigger.exec(ctx.messageText);
87
+ const execResult = trigger.exec(ctx.messageInfo.text);
88
88
  if (execResult != null) {
89
89
  let regexMatchLimit = 100;
90
90
  matchResults.push(execResult);
91
91
 
92
92
  if (trigger.global) {
93
93
  while (regexMatchLimit > 0) {
94
- const nextResult = trigger.exec(ctx.messageText);
94
+ const nextResult = trigger.exec(ctx.messageInfo.text);
95
95
 
96
96
  if (nextResult == null) break;
97
97
 
@@ -6,7 +6,7 @@ import { HoursOfDay } from '../../types/timeValues';
6
6
  import { IActionState } from '../../types/actionState';
7
7
  import { IActionWithState, ActionKey } from '../../types/action';
8
8
  import { CachedStateFactory } from '../cachedStateFactory';
9
- import { ChatContext } from '../context/chatContext';
9
+ import { ChatContextInternal } from '../context/chatContext';
10
10
  import { Noop } from '../../helpers/noop';
11
11
  import { IScheduler } from '../../types/scheduler';
12
12
  import { getOrSetIfNotExists, getOrThrow } from '../../helpers/mapUtils';
@@ -46,7 +46,7 @@ export class ScheduledAction<TActionState extends IActionState>
46
46
  this.stateConstructor = stateConstructor;
47
47
  }
48
48
 
49
- async exec(ctx: ChatContext<TActionState>) {
49
+ async exec(ctx: ChatContextInternal<TActionState>) {
50
50
  if (!ctx.isInitialized)
51
51
  throw new Error(
52
52
  `Context for ${this.key} is not initialized or already consumed`
@@ -7,7 +7,17 @@ import { IScheduler } from '../../types/scheduler';
7
7
  import { IStorageClient } from '../../types/storage';
8
8
  import { TraceId } from '../../types/trace';
9
9
 
10
- export abstract class BaseContext<TAction extends IAction> {
10
+ export type BaseContextPropertiesToOmit =
11
+ | 'action'
12
+ | 'isInitialized'
13
+ | 'storage'
14
+ | 'scheduler'
15
+ | 'logger'
16
+ | 'responses'
17
+ | 'traceId'
18
+ | 'botName';
19
+
20
+ export abstract class BaseContextInternal<TAction extends IAction> {
11
21
  isInitialized = false;
12
22
  private _responses: BotResponse[] = [];
13
23
 
@@ -14,17 +14,28 @@ import { Milliseconds } from '../../types/timeValues';
14
14
  import { DelayResponse } from '../../dtos/responses/delay';
15
15
  import { ICaptureController } from '../../types/capture';
16
16
  import { CommandTrigger } from '../../types/commandTrigger';
17
- import { ReplyContext } from './replyContext';
18
- import { BaseContext } from './baseContext';
17
+ import { ReplyContextInternal } from './replyContext';
18
+ import {
19
+ BaseContextInternal,
20
+ BaseContextPropertiesToOmit
21
+ } from './baseContext';
19
22
  import { ScheduledAction } from '../actions/scheduledAction';
20
23
 
24
+ export type ChatContext<
25
+ TActionState extends IActionState,
26
+ TAction extends IActionWithState<TActionState> = ScheduledAction<TActionState>
27
+ > = Omit<
28
+ ChatContextInternal<TActionState, TAction>,
29
+ BaseContextPropertiesToOmit
30
+ >;
31
+
21
32
  /**
22
33
  * Context of action executed in chat.
23
34
  */
24
- export class ChatContext<
35
+ export class ChatContextInternal<
25
36
  TActionState extends IActionState,
26
37
  TAction extends IActionWithState<TActionState> = ScheduledAction<TActionState>
27
- > extends BaseContext<TAction> {
38
+ > extends BaseContextInternal<TAction> {
28
39
  protected createCaptureController(
29
40
  response: IReplyResponse
30
41
  ): ICaptureController {
@@ -32,7 +43,7 @@ export class ChatContext<
32
43
  captureReplies: (
33
44
  trigger: CommandTrigger[],
34
45
  handler: (
35
- replyContext: ReplyContext<TActionState>
46
+ replyContext: ReplyContextInternal<TActionState>
36
47
  ) => Promise<void>,
37
48
  abortController: AbortController
38
49
  ) => {
@@ -2,9 +2,17 @@ import { InlineQueryResult } from 'telegraf/types';
2
2
  import { BotResponse } from '../../types/response';
3
3
  import { InlineQueryAction } from '../actions/inlineQueryAction';
4
4
  import { InlineQueryResponse } from '../../dtos/responses/inlineQueryResponse';
5
- import { BaseContext } from './baseContext';
5
+ import {
6
+ BaseContextInternal,
7
+ BaseContextPropertiesToOmit
8
+ } from './baseContext';
6
9
 
7
- export class InlineQueryContext extends BaseContext<InlineQueryAction> {
10
+ export type InlineQueryContext = Omit<
11
+ InlineQueryContextInternal,
12
+ BaseContextPropertiesToOmit | 'queryResults' | 'queryId'
13
+ >;
14
+
15
+ export class InlineQueryContextInternal extends BaseContextInternal<InlineQueryAction> {
8
16
  queryResults: InlineQueryResult[] = [];
9
17
  /**
10
18
  * Abort signal to be utilized in query handler.