chz-telegram-bot 0.3.14 → 0.3.16

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 (91) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/builtin/helpAction.d.ts +1 -1
  3. package/dist/builtin/helpAction.d.ts.map +1 -1
  4. package/dist/dtos/chatInfo.d.ts +10 -1
  5. package/dist/dtos/chatInfo.d.ts.map +1 -1
  6. package/dist/dtos/chatInfo.js +11 -5
  7. package/dist/dtos/commandTriggerCheckResult.d.ts +5 -5
  8. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
  9. package/dist/dtos/commandTriggerCheckResult.js +4 -4
  10. package/dist/dtos/cooldownInfo.d.ts +9 -3
  11. package/dist/dtos/cooldownInfo.d.ts.map +1 -1
  12. package/dist/dtos/cooldownInfo.js +5 -1
  13. package/dist/dtos/incomingMessage.d.ts +1 -1
  14. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  15. package/dist/dtos/incomingMessage.js +2 -2
  16. package/dist/dtos/incomingQuery.d.ts.map +1 -1
  17. package/dist/dtos/messageInfo.d.ts +21 -0
  18. package/dist/dtos/messageInfo.d.ts.map +1 -0
  19. package/dist/dtos/messageInfo.js +24 -0
  20. package/dist/dtos/userInfo.d.ts +12 -0
  21. package/dist/dtos/userInfo.d.ts.map +1 -0
  22. package/dist/dtos/userInfo.js +16 -0
  23. package/dist/entities/actions/commandAction.d.ts +2 -2
  24. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  25. package/dist/entities/actions/commandAction.js +7 -7
  26. package/dist/entities/actions/inlineQueryAction.d.ts +2 -2
  27. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  28. package/dist/entities/actions/replyCaptureAction.d.ts +4 -4
  29. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
  30. package/dist/entities/actions/replyCaptureAction.js +4 -4
  31. package/dist/entities/actions/scheduledAction.d.ts +2 -2
  32. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  33. package/dist/entities/context/baseContext.d.ts +2 -1
  34. package/dist/entities/context/baseContext.d.ts.map +1 -1
  35. package/dist/entities/context/baseContext.js +3 -3
  36. package/dist/entities/context/chatContext.d.ts +3 -2
  37. package/dist/entities/context/chatContext.d.ts.map +1 -1
  38. package/dist/entities/context/chatContext.js +3 -3
  39. package/dist/entities/context/inlineQueryContext.d.ts +3 -2
  40. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  41. package/dist/entities/context/inlineQueryContext.js +3 -3
  42. package/dist/entities/context/messageContext.d.ts +11 -15
  43. package/dist/entities/context/messageContext.d.ts.map +1 -1
  44. package/dist/entities/context/messageContext.js +15 -20
  45. package/dist/entities/context/replyContext.d.ts +9 -15
  46. package/dist/entities/context/replyContext.d.ts.map +1 -1
  47. package/dist/entities/context/replyContext.js +12 -20
  48. package/dist/helpers/mapUtils.d.ts +9 -2
  49. package/dist/helpers/mapUtils.d.ts.map +1 -1
  50. package/dist/helpers/mapUtils.js +1 -1
  51. package/dist/index.d.ts +13 -9
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +14 -11
  54. package/dist/services/actionProcessors/baseProcessor.d.ts +2 -2
  55. package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
  56. package/dist/services/actionProcessors/commandActionProcessor.d.ts +1 -0
  57. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  58. package/dist/services/actionProcessors/commandActionProcessor.js +18 -19
  59. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +1 -1
  60. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
  61. package/dist/services/actionProcessors/scheduledActionProcessor.js +2 -2
  62. package/dist/types/action.d.ts +2 -2
  63. package/dist/types/action.d.ts.map +1 -1
  64. package/dist/types/capture.d.ts +3 -3
  65. package/dist/types/capture.d.ts.map +1 -1
  66. package/dtos/chatInfo.ts +10 -9
  67. package/dtos/commandTriggerCheckResult.ts +5 -15
  68. package/dtos/cooldownInfo.ts +6 -1
  69. package/dtos/incomingMessage.ts +7 -2
  70. package/dtos/incomingQuery.ts +4 -12
  71. package/dtos/messageInfo.ts +17 -0
  72. package/dtos/userInfo.ts +8 -0
  73. package/entities/actions/commandAction.ts +11 -11
  74. package/entities/actions/inlineQueryAction.ts +2 -2
  75. package/entities/actions/replyCaptureAction.ts +9 -9
  76. package/entities/actions/scheduledAction.ts +2 -2
  77. package/entities/context/baseContext.ts +11 -1
  78. package/entities/context/chatContext.ts +16 -5
  79. package/entities/context/inlineQueryContext.ts +10 -2
  80. package/entities/context/messageContext.ts +24 -25
  81. package/entities/context/replyContext.ts +26 -25
  82. package/helpers/mapUtils.ts +15 -2
  83. package/index.ts +13 -9
  84. package/package.json +15 -1
  85. package/services/actionProcessors/baseProcessor.ts +3 -3
  86. package/services/actionProcessors/commandActionProcessor.ts +43 -21
  87. package/services/actionProcessors/inlineQueryActionProcessor.ts +3 -3
  88. package/services/actionProcessors/scheduledActionProcessor.ts +7 -4
  89. package/types/action.ts +2 -2
  90. package/types/capture.ts +5 -3
  91. package/chz-telegram-bot-0.3.14.tgz +0 -0
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,7 +145,7 @@ 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
  ) {
@@ -153,14 +153,14 @@ export class CommandAction<TActionState extends IActionState>
153
153
 
154
154
  if (!triggerCheckResult.shouldExecute) return triggerCheckResult;
155
155
 
156
- if (!ctx.fromUserId)
156
+ if (!ctx.userInfo.id)
157
157
  return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
158
158
  'UserIdMissing'
159
159
  );
160
160
 
161
161
  const isUserAllowed =
162
162
  this.allowedUsers.length == 0 ||
163
- this.allowedUsers.includes(ctx.fromUserId);
163
+ this.allowedUsers.includes(ctx.userInfo.id);
164
164
 
165
165
  if (!isUserAllowed)
166
166
  return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
@@ -187,14 +187,14 @@ export class CommandAction<TActionState extends IActionState>
187
187
  }
188
188
 
189
189
  private checkTrigger(
190
- ctx: MessageContext<TActionState>,
190
+ ctx: MessageContextInternal<TActionState>,
191
191
  trigger: CommandTrigger
192
192
  ) {
193
- if (trigger == MessageType.Any || trigger == ctx.messageType)
193
+ if (trigger == MessageType.Any || trigger == ctx.messageInfo.type)
194
194
  return CommandTriggerCheckResult.Trigger();
195
195
 
196
196
  if (typeof trigger == 'string')
197
- return ctx.messageText.toLowerCase() == trigger.toLowerCase()
197
+ return ctx.messageInfo.text.toLowerCase() == trigger.toLowerCase()
198
198
  ? CommandTriggerCheckResult.Trigger()
199
199
  : CommandTriggerCheckResult.DoNotTrigger('TriggerNotSatisfied');
200
200
 
@@ -202,14 +202,14 @@ export class CommandAction<TActionState extends IActionState>
202
202
 
203
203
  trigger.lastIndex = 0;
204
204
 
205
- const execResult = trigger.exec(ctx.messageText);
205
+ const execResult = trigger.exec(ctx.messageInfo.text);
206
206
  if (execResult != null) {
207
207
  let regexMatchLimit = 100;
208
208
  matchResults.push(execResult);
209
209
 
210
210
  if (trigger.global) {
211
211
  while (regexMatchLimit > 0) {
212
- const nextResult = trigger.exec(ctx.messageText);
212
+ const nextResult = trigger.exec(ctx.messageInfo.text);
213
213
 
214
214
  if (nextResult == null) break;
215
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.
@@ -5,51 +5,46 @@ import { ImageMessage } from '../../dtos/responses/imageMessage';
5
5
  import { Reaction } from '../../dtos/responses/reaction';
6
6
  import { TextMessage } from '../../dtos/responses/textMessage';
7
7
  import { VideoMessage } from '../../dtos/responses/videoMessage';
8
- import { ChatContext } from './chatContext';
8
+ import { ChatContextInternal } from './chatContext';
9
9
  import {
10
10
  MessageSendingOptions,
11
11
  TextMessageSendingOptions
12
12
  } from '../../types/messageSendingOptions';
13
- import {
14
- MessageTypeValue,
15
- TelegrafContextMessage
16
- } from '../../types/messageTypes';
17
13
  import { ReplyInfo } from '../../dtos/replyInfo';
18
14
  import { CommandAction } from '../actions/commandAction';
19
15
  import { Seconds } from '../../types/timeValues';
16
+ import { BaseContextPropertiesToOmit } from './baseContext';
17
+ import { MessageInfo } from '../../dtos/messageInfo';
18
+ import { UserInfo } from '../../dtos/userInfo';
19
+
20
+ export type MessageContext<TActionState extends IActionState> = Omit<
21
+ MessageContextInternal<TActionState>,
22
+ BaseContextPropertiesToOmit | 'startCooldown' | 'customCooldown'
23
+ >;
20
24
 
21
25
  /**
22
26
  * Context of action executed in chat, in response to a message
23
27
  */
24
- export class MessageContext<
28
+ export class MessageContextInternal<
25
29
  TActionState extends IActionState
26
- > extends ChatContext<TActionState, CommandAction<TActionState>> {
27
- /** Id of a message that triggered this action. */
28
- messageId!: number;
29
- /** Text of a message that triggered this action. */
30
- messageText!: string;
30
+ > extends ChatContextInternal<TActionState, CommandAction<TActionState>> {
31
+ /** Information about the user that triggered this action */
32
+ userInfo!: UserInfo;
33
+ /** Information about the message that triggered this action */
34
+ messageInfo!: MessageInfo;
31
35
  /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
32
36
  matchResults: RegExpMatchArray[] = [];
33
- /** Id of a user that sent a message that triggered this action. */
34
- fromUserId: number | undefined;
35
37
  /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
36
38
  startCooldown: boolean = true;
37
- /** Name of a user that sent a message that triggered this action. */
38
- fromUserName!: string;
39
- /** Type of message being received */
40
- messageType!: MessageTypeValue;
41
- /** Message object recieved from Telegram */
42
- messageUpdateObject!: TelegrafContextMessage;
43
39
  /** Bot info from Telegram */
44
40
  botInfo!: UserFromGetMe;
45
-
46
41
  customCooldown: Seconds | undefined;
47
42
 
48
43
  private getQuotePart(quote: boolean | string) {
49
44
  return typeof quote == 'boolean'
50
45
  ? this.matchResults.length != 0
51
46
  ? this.matchResults[0][1]
52
- : this.messageText
47
+ : this.messageInfo.text
53
48
  : quote;
54
49
  }
55
50
 
@@ -65,7 +60,7 @@ export class MessageContext<
65
60
  this.chatInfo,
66
61
  this.traceId,
67
62
  this.action,
68
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
63
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
69
64
  options
70
65
  );
71
66
 
@@ -86,7 +81,7 @@ export class MessageContext<
86
81
  this.chatInfo,
87
82
  this.traceId,
88
83
  this.action,
89
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
84
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
90
85
  options
91
86
  );
92
87
 
@@ -107,7 +102,7 @@ export class MessageContext<
107
102
  this.chatInfo,
108
103
  this.traceId,
109
104
  this.action,
110
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
105
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
111
106
  options
112
107
  );
113
108
 
@@ -116,6 +111,10 @@ export class MessageContext<
116
111
  return this.createCaptureController(response);
117
112
  }
118
113
 
114
+ skipCooldown() {
115
+ this.startCooldown = false;
116
+ }
117
+
119
118
  startCustomCooldown(customCooldown: Seconds) {
120
119
  this.startCooldown = true;
121
120
  this.customCooldown = customCooldown;
@@ -202,7 +201,7 @@ export class MessageContext<
202
201
  new Reaction(
203
202
  this.traceId,
204
203
  this.chatInfo,
205
- this.messageId,
204
+ this.messageInfo.id,
206
205
  emoji,
207
206
  this.action
208
207
  )
@@ -9,33 +9,34 @@ import {
9
9
  TextMessageSendingOptions,
10
10
  MessageSendingOptions
11
11
  } from '../../types/messageSendingOptions';
12
- import {
13
- MessageTypeValue,
14
- TelegrafContextMessage
15
- } from '../../types/messageTypes';
16
12
  import { ReplyCaptureAction } from '../actions/replyCaptureAction';
17
13
  import { resolve } from 'path';
18
- import { BaseContext } from './baseContext';
19
-
20
- export class ReplyContext<
14
+ import {
15
+ BaseContextInternal,
16
+ BaseContextPropertiesToOmit
17
+ } from './baseContext';
18
+ import { UserInfo } from '../../dtos/userInfo';
19
+ import { MessageInfo } from '../../dtos/messageInfo';
20
+
21
+ export type ReplyContext<TActionState extends IActionState> = Omit<
22
+ ReplyContextInternal<TActionState>,
23
+ | BaseContextPropertiesToOmit
24
+ | 'messageId'
25
+ | 'startCooldown'
26
+ | 'customCooldown'
27
+ >;
28
+
29
+ export class ReplyContextInternal<
21
30
  TParentActionState extends IActionState
22
- > extends BaseContext<ReplyCaptureAction<TParentActionState>> {
31
+ > extends BaseContextInternal<ReplyCaptureAction<TParentActionState>> {
23
32
  /** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
24
33
  matchResults!: RegExpExecArray[];
25
34
  /** Id of a message that triggered this action. */
26
35
  replyMessageId!: number | undefined;
27
- /** Id of a message that triggered this action. */
28
- messageId!: number;
29
- /** Type of message being received */
30
- messageType!: MessageTypeValue;
31
- /** Text of a message that triggered this action. */
32
- messageText!: string;
33
- /** Id of a user that sent a message that triggered this action. */
34
- fromUserId: number | undefined;
35
- /** Name of a user that sent a message that triggered this action. */
36
- fromUserName!: string;
37
- /** Message object recieved from Telegram */
38
- messageUpdateObject!: TelegrafContextMessage;
36
+ /** Information about the user that triggered this action */
37
+ userInfo!: UserInfo;
38
+ /** Information about the message that triggered this action */
39
+ messageInfo!: MessageInfo;
39
40
  /** Bot info from Telegram */
40
41
  botInfo!: UserFromGetMe;
41
42
 
@@ -45,7 +46,7 @@ export class ReplyContext<
45
46
  return typeof quote == 'boolean'
46
47
  ? this.matchResults.length != 0
47
48
  ? this.matchResults[0][1]
48
- : this.messageText
49
+ : this.messageInfo.text
49
50
  : quote;
50
51
  }
51
52
 
@@ -61,7 +62,7 @@ export class ReplyContext<
61
62
  this.chatInfo,
62
63
  this.traceId,
63
64
  this.action,
64
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
65
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
65
66
  options
66
67
  );
67
68
 
@@ -80,7 +81,7 @@ export class ReplyContext<
80
81
  this.chatInfo,
81
82
  this.traceId,
82
83
  this.action,
83
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
84
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
84
85
  options
85
86
  );
86
87
 
@@ -99,7 +100,7 @@ export class ReplyContext<
99
100
  this.chatInfo,
100
101
  this.traceId,
101
102
  this.action,
102
- new ReplyInfo(this.messageId, quote ? quotedPart : undefined),
103
+ new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
103
104
  options
104
105
  );
105
106
 
@@ -203,7 +204,7 @@ export class ReplyContext<
203
204
  new Reaction(
204
205
  this.traceId,
205
206
  this.chatInfo,
206
- this.messageId,
207
+ this.messageInfo.id,
207
208
  emoji,
208
209
  this.action
209
210
  )
@@ -1,4 +1,13 @@
1
- export function getOrSetIfNotExists<K, V>(map: Map<K, V>, key: K, fallback: V) {
1
+ type KeyedReadonlyCollection<K, V> = { get: (key: K) => V | undefined };
2
+ type KeyedWriteableCollection<K, V> = KeyedReadonlyCollection<K, V> & {
3
+ set: (key: K, value: V) => KeyedWriteableCollection<K, V>;
4
+ };
5
+
6
+ export function getOrSetIfNotExists<K, V>(
7
+ map: KeyedWriteableCollection<K, V>,
8
+ key: K,
9
+ fallback: V
10
+ ) {
2
11
  const existingValue = map.get(key);
3
12
  if (existingValue) return existingValue;
4
13
 
@@ -7,7 +16,11 @@ export function getOrSetIfNotExists<K, V>(map: Map<K, V>, key: K, fallback: V) {
7
16
  return fallback;
8
17
  }
9
18
 
10
- export function getOrThrow<K, V>(map: Map<K, V>, key: K, error: string) {
19
+ export function getOrThrow<K, V>(
20
+ map: KeyedReadonlyCollection<K, V>,
21
+ key: K,
22
+ error: string = 'Key not found in collection'
23
+ ) {
11
24
  const existingValue = map.get(key);
12
25
  if (existingValue) return existingValue;
13
26