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.
- package/LICENSE.md +21 -0
- package/dist/builtin/helpAction.d.ts +1 -1
- package/dist/builtin/helpAction.d.ts.map +1 -1
- package/dist/dtos/chatInfo.d.ts +10 -1
- package/dist/dtos/chatInfo.d.ts.map +1 -1
- package/dist/dtos/chatInfo.js +11 -5
- package/dist/dtos/commandTriggerCheckResult.d.ts +5 -5
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/dtos/commandTriggerCheckResult.js +4 -4
- package/dist/dtos/cooldownInfo.d.ts +9 -3
- package/dist/dtos/cooldownInfo.d.ts.map +1 -1
- package/dist/dtos/cooldownInfo.js +5 -1
- package/dist/dtos/incomingMessage.d.ts +1 -1
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +2 -2
- package/dist/dtos/incomingQuery.d.ts.map +1 -1
- package/dist/dtos/messageInfo.d.ts +21 -0
- package/dist/dtos/messageInfo.d.ts.map +1 -0
- package/dist/dtos/messageInfo.js +24 -0
- package/dist/dtos/userInfo.d.ts +12 -0
- package/dist/dtos/userInfo.d.ts.map +1 -0
- package/dist/dtos/userInfo.js +16 -0
- package/dist/entities/actions/commandAction.d.ts +2 -2
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +7 -7
- package/dist/entities/actions/inlineQueryAction.d.ts +2 -2
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.d.ts +4 -4
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.js +4 -4
- package/dist/entities/actions/scheduledAction.d.ts +2 -2
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/context/baseContext.d.ts +2 -1
- package/dist/entities/context/baseContext.d.ts.map +1 -1
- package/dist/entities/context/baseContext.js +3 -3
- package/dist/entities/context/chatContext.d.ts +3 -2
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +3 -3
- package/dist/entities/context/inlineQueryContext.d.ts +3 -2
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/inlineQueryContext.js +3 -3
- package/dist/entities/context/messageContext.d.ts +11 -15
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +15 -20
- package/dist/entities/context/replyContext.d.ts +9 -15
- package/dist/entities/context/replyContext.d.ts.map +1 -1
- package/dist/entities/context/replyContext.js +12 -20
- package/dist/helpers/mapUtils.d.ts +9 -2
- package/dist/helpers/mapUtils.d.ts.map +1 -1
- package/dist/helpers/mapUtils.js +1 -1
- package/dist/index.d.ts +13 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -11
- package/dist/services/actionProcessors/baseProcessor.d.ts +2 -2
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +18 -19
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +1 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.js +2 -2
- package/dist/types/action.d.ts +2 -2
- package/dist/types/action.d.ts.map +1 -1
- package/dist/types/capture.d.ts +3 -3
- package/dist/types/capture.d.ts.map +1 -1
- package/dtos/chatInfo.ts +10 -9
- package/dtos/commandTriggerCheckResult.ts +5 -15
- package/dtos/cooldownInfo.ts +6 -1
- package/dtos/incomingMessage.ts +7 -2
- package/dtos/incomingQuery.ts +4 -12
- package/dtos/messageInfo.ts +17 -0
- package/dtos/userInfo.ts +8 -0
- package/entities/actions/commandAction.ts +11 -11
- package/entities/actions/inlineQueryAction.ts +2 -2
- package/entities/actions/replyCaptureAction.ts +9 -9
- package/entities/actions/scheduledAction.ts +2 -2
- package/entities/context/baseContext.ts +11 -1
- package/entities/context/chatContext.ts +16 -5
- package/entities/context/inlineQueryContext.ts +10 -2
- package/entities/context/messageContext.ts +24 -25
- package/entities/context/replyContext.ts +26 -25
- package/helpers/mapUtils.ts +15 -2
- package/index.ts +13 -9
- package/package.json +15 -1
- package/services/actionProcessors/baseProcessor.ts +3 -3
- package/services/actionProcessors/commandActionProcessor.ts +43 -21
- package/services/actionProcessors/inlineQueryActionProcessor.ts +3 -3
- package/services/actionProcessors/scheduledActionProcessor.ts +7 -4
- package/types/action.ts +2 -2
- package/types/capture.ts +5 -3
- package/chz-telegram-bot-0.3.14.tgz +0 -0
package/dtos/chatInfo.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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(
|
package/dtos/cooldownInfo.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Seconds } from '../types/timeValues';
|
|
2
2
|
|
|
3
3
|
export class CooldownInfo {
|
|
4
|
-
constructor(
|
|
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
|
}
|
package/dtos/incomingMessage.ts
CHANGED
|
@@ -40,7 +40,11 @@ export class IncomingMessage {
|
|
|
40
40
|
return MessageType.Unknown;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
constructor(
|
|
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;
|
package/dtos/incomingQuery.ts
CHANGED
|
@@ -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
|
+
}
|
package/dtos/userInfo.ts
ADDED
|
@@ -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 {
|
|
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:
|
|
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.
|
|
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:
|
|
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.
|
|
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.
|
|
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:
|
|
190
|
+
ctx: MessageContextInternal<TActionState>,
|
|
191
191
|
trigger: CommandTrigger
|
|
192
192
|
) {
|
|
193
|
-
if (trigger == MessageType.Any || trigger == ctx.
|
|
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.
|
|
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.
|
|
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.
|
|
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 {
|
|
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:
|
|
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 {
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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.
|
|
72
|
+
if (trigger == ctx.messageInfo.type)
|
|
73
73
|
return CommandTriggerCheckResult.Trigger();
|
|
74
74
|
|
|
75
75
|
if (typeof trigger == 'string')
|
|
76
|
-
if (ctx.
|
|
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.
|
|
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.
|
|
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 {
|
|
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:
|
|
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
|
|
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 {
|
|
18
|
-
import {
|
|
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
|
|
35
|
+
export class ChatContextInternal<
|
|
25
36
|
TActionState extends IActionState,
|
|
26
37
|
TAction extends IActionWithState<TActionState> = ScheduledAction<TActionState>
|
|
27
|
-
> extends
|
|
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:
|
|
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 {
|
|
5
|
+
import {
|
|
6
|
+
BaseContextInternal,
|
|
7
|
+
BaseContextPropertiesToOmit
|
|
8
|
+
} from './baseContext';
|
|
6
9
|
|
|
7
|
-
export
|
|
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 {
|
|
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
|
|
28
|
+
export class MessageContextInternal<
|
|
25
29
|
TActionState extends IActionState
|
|
26
|
-
> extends
|
|
27
|
-
/**
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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 {
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
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
|
-
/**
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
207
|
+
this.messageInfo.id,
|
|
207
208
|
emoji,
|
|
208
209
|
this.action
|
|
209
210
|
)
|
package/helpers/mapUtils.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
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>(
|
|
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
|
|