chz-telegram-bot 0.7.13 → 0.7.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.
- package/dist/dtos/propertyProviderSets.d.ts +0 -1
- package/dist/dtos/propertyProviderSets.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.d.ts +1 -1
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +2 -2
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +2 -3
- package/package.json +4 -1
- package/eslint.config.ts +0 -62
- package/src/builtin/helpAction.ts +0 -17
- package/src/dtos/chatHistoryMessage.ts +0 -22
- package/src/dtos/chatInfo.ts +0 -12
- package/src/dtos/commandTriggerCheckResult.ts +0 -40
- package/src/dtos/cooldownInfo.ts +0 -10
- package/src/dtos/incomingMessage.ts +0 -71
- package/src/dtos/incomingQuery.ts +0 -14
- package/src/dtos/messageInfo.ts +0 -15
- package/src/dtos/propertyProviderSets.ts +0 -21
- package/src/dtos/replyInfo.ts +0 -9
- package/src/dtos/responses/delay.ts +0 -28
- package/src/dtos/responses/imageMessage.ts +0 -41
- package/src/dtos/responses/inlineQueryResponse.ts +0 -26
- package/src/dtos/responses/reaction.ts +0 -30
- package/src/dtos/responses/textMessage.ts +0 -44
- package/src/dtos/responses/unpin.ts +0 -27
- package/src/dtos/responses/videoMessage.ts +0 -41
- package/src/dtos/userInfo.ts +0 -8
- package/src/entities/actions/commandAction.ts +0 -275
- package/src/entities/actions/inlineQueryAction.ts +0 -83
- package/src/entities/actions/replyCaptureAction.ts +0 -110
- package/src/entities/actions/scheduledAction.ts +0 -182
- package/src/entities/botInstance.ts +0 -92
- package/src/entities/cachedStateFactory.ts +0 -14
- package/src/entities/context/baseContext.ts +0 -111
- package/src/entities/context/chatContext.ts +0 -135
- package/src/entities/context/inlineQueryContext.ts +0 -63
- package/src/entities/context/messageContext.ts +0 -250
- package/src/entities/context/replyContext.ts +0 -260
- package/src/entities/states/actionStateBase.ts +0 -6
- package/src/entities/taskRecord.ts +0 -11
- package/src/helpers/builders/commandActionBuilder.ts +0 -214
- package/src/helpers/builders/inlineQueryActionBuilder.ts +0 -71
- package/src/helpers/builders/scheduledActionBuilder.ts +0 -143
- package/src/helpers/mapUtils.ts +0 -28
- package/src/helpers/noop.ts +0 -20
- package/src/helpers/objectFromEntries.ts +0 -7
- package/src/helpers/timeConvertions.ts +0 -13
- package/src/helpers/toArray.ts +0 -3
- package/src/helpers/traceFactory.ts +0 -11
- package/src/index.ts +0 -33
- package/src/main.ts +0 -76
- package/src/services/actionProcessingService.ts +0 -125
- package/src/services/actionProcessors/baseProcessor.ts +0 -67
- package/src/services/actionProcessors/commandActionProcessor.ts +0 -231
- package/src/services/actionProcessors/inlineQueryActionProcessor.ts +0 -165
- package/src/services/actionProcessors/scheduledActionProcessor.ts +0 -136
- package/src/services/jsonFileStorage.ts +0 -181
- package/src/services/nodeTimeoutScheduler.ts +0 -79
- package/src/services/responseProcessingQueue.ts +0 -57
- package/src/services/telegramApi.ts +0 -278
- package/src/types/action.ts +0 -15
- package/src/types/actionState.ts +0 -4
- package/src/types/cachedValueAccessor.ts +0 -1
- package/src/types/capture.ts +0 -33
- package/src/types/commandCondition.ts +0 -9
- package/src/types/commandTrigger.ts +0 -1
- package/src/types/events.ts +0 -286
- package/src/types/externalAliases.ts +0 -18
- package/src/types/handlers.ts +0 -26
- package/src/types/inputFile.ts +0 -4
- package/src/types/messageSendingOptions.ts +0 -10
- package/src/types/messageTypes.ts +0 -21
- package/src/types/propertyProvider.ts +0 -14
- package/src/types/response.ts +0 -51
- package/src/types/scheduler.ts +0 -20
- package/src/types/storage.ts +0 -23
- package/src/types/timeValues.ts +0 -33
- package/src/types/trace.ts +0 -5
- package/tests/dtos/commandTriggerCheckResult.test.ts +0 -301
- package/tests/entities/actions/inlineQueryAction.test.ts +0 -359
- package/tests/entities/actions/replyCaptureAction.test.ts +0 -501
- package/tests/entities/cachedStateFactory.test.ts +0 -98
- package/tests/entities/context/chatContext.test.ts +0 -606
- package/tests/entities/context/messageContext.test.ts +0 -370
- package/tests/entities/states/actionStateBase.test.ts +0 -138
- package/tests/entities/taskRecord.test.ts +0 -195
- package/tests/helpers/mapUtils.test.ts +0 -163
- package/tests/helpers/timeConvertions.test.ts +0 -129
- package/tests/services/actionProcessors/baseActionProcessor.test.ts +0 -359
- package/tests/services/actionProcessors/commandActionProcessor.test.ts +0 -268
- package/tests/services/actionProcessors/inlineQueryActionProcessor.test.ts +0 -616
- package/tests/services/actionProcessors/processorTestHelpers.ts +0 -147
- package/tests/services/actionProcessors/scheduledActionProcessor.test.ts +0 -153
- package/tests/services/jsonFileStorage.test.ts +0 -927
- package/tests/services/nodeTimeoutScheduler.test.ts +0 -421
- package/tests/services/responseProcessingQueue.test.ts +0 -388
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -118
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { Seconds } from '../types/timeValues';
|
|
2
|
-
import { IStorageClient } from '../types/storage';
|
|
3
|
-
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
4
|
-
import { IActionState } from '../types/actionState';
|
|
5
|
-
import { CommandAction } from './actions/commandAction';
|
|
6
|
-
import { ScheduledAction } from './actions/scheduledAction';
|
|
7
|
-
import { IScheduler } from '../types/scheduler';
|
|
8
|
-
import { NodeTimeoutScheduler } from '../services/nodeTimeoutScheduler';
|
|
9
|
-
import { InlineQueryAction } from './actions/inlineQueryAction';
|
|
10
|
-
import { ActionProcessingService } from '../services/actionProcessingService';
|
|
11
|
-
import { BotEventType, TypedEventEmitter } from '../types/events';
|
|
12
|
-
import { createTrace } from '../helpers/traceFactory';
|
|
13
|
-
|
|
14
|
-
export class BotInstance {
|
|
15
|
-
private readonly storage: IStorageClient;
|
|
16
|
-
private readonly scheduler: IScheduler;
|
|
17
|
-
private readonly actionProcessingService: ActionProcessingService;
|
|
18
|
-
|
|
19
|
-
readonly name: string;
|
|
20
|
-
readonly eventEmitter: TypedEventEmitter<Record<string, unknown>>;
|
|
21
|
-
|
|
22
|
-
constructor(options: {
|
|
23
|
-
name: string;
|
|
24
|
-
actions: {
|
|
25
|
-
commands: CommandAction<IActionState>[];
|
|
26
|
-
scheduled: ScheduledAction<IActionState>[];
|
|
27
|
-
inlineQueries: InlineQueryAction[];
|
|
28
|
-
};
|
|
29
|
-
chats: Record<string, number>;
|
|
30
|
-
storagePath?: string;
|
|
31
|
-
services?: {
|
|
32
|
-
storageClient?: IStorageClient;
|
|
33
|
-
scheduler?: IScheduler;
|
|
34
|
-
eventEmitter?: TypedEventEmitter<Record<string, unknown>>;
|
|
35
|
-
};
|
|
36
|
-
}) {
|
|
37
|
-
const actions = [
|
|
38
|
-
...options.actions.commands,
|
|
39
|
-
...options.actions.scheduled
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
this.name = options.name;
|
|
43
|
-
this.eventEmitter =
|
|
44
|
-
options.services?.eventEmitter ?? new TypedEventEmitter();
|
|
45
|
-
|
|
46
|
-
this.scheduler =
|
|
47
|
-
options.services?.scheduler ??
|
|
48
|
-
new NodeTimeoutScheduler(this.eventEmitter, this.name);
|
|
49
|
-
this.storage =
|
|
50
|
-
options.services?.storageClient ??
|
|
51
|
-
new JsonFileStorage(options.name, actions, options.storagePath);
|
|
52
|
-
this.actionProcessingService = new ActionProcessingService(
|
|
53
|
-
this.name,
|
|
54
|
-
options.chats,
|
|
55
|
-
this.storage,
|
|
56
|
-
this.scheduler,
|
|
57
|
-
this.eventEmitter
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async start(
|
|
62
|
-
token: string,
|
|
63
|
-
actions: {
|
|
64
|
-
commands: CommandAction<IActionState>[];
|
|
65
|
-
scheduled: ScheduledAction<IActionState>[];
|
|
66
|
-
inlineQueries: InlineQueryAction[];
|
|
67
|
-
},
|
|
68
|
-
scheduledPeriod?: Seconds
|
|
69
|
-
) {
|
|
70
|
-
this.eventEmitter.emit(BotEventType.botStarting, {
|
|
71
|
-
botName: this.name,
|
|
72
|
-
traceId: createTrace(this, this.name, 'startup')
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
await this.actionProcessingService.initialize(
|
|
76
|
-
token,
|
|
77
|
-
actions,
|
|
78
|
-
scheduledPeriod
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async stop() {
|
|
83
|
-
this.eventEmitter.emit(BotEventType.botStopping, {
|
|
84
|
-
botName: this.name,
|
|
85
|
-
traceId: createTrace(this, this.name, 'stop')
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
this.scheduler.stopAll();
|
|
89
|
-
await this.storage.close();
|
|
90
|
-
this.actionProcessingService.stop();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Hours } from '../types/timeValues';
|
|
2
|
-
|
|
3
|
-
export class CachedStateFactory {
|
|
4
|
-
readonly getValue: () => Promise<unknown>;
|
|
5
|
-
readonly invalidationTimeoutInHours: Hours;
|
|
6
|
-
|
|
7
|
-
constructor(
|
|
8
|
-
itemFactory: () => Promise<unknown>,
|
|
9
|
-
invalidationTimeout: Hours
|
|
10
|
-
) {
|
|
11
|
-
this.getValue = itemFactory;
|
|
12
|
-
this.invalidationTimeoutInHours = invalidationTimeout;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { ChatInfo } from '../../dtos/chatInfo';
|
|
2
|
-
import { IAction, IActionWithState } from '../../types/action';
|
|
3
|
-
import { IActionState } from '../../types/actionState';
|
|
4
|
-
import { ICaptureController } from '../../types/capture';
|
|
5
|
-
import { CommandTrigger } from '../../types/commandTrigger';
|
|
6
|
-
import { TypedEventEmitter } from '../../types/events';
|
|
7
|
-
import { BotResponse, IReplyResponse } from '../../types/response';
|
|
8
|
-
import { IScheduler } from '../../types/scheduler';
|
|
9
|
-
import { IStorageClient } from '../../types/storage';
|
|
10
|
-
import { TraceId } from '../../types/trace';
|
|
11
|
-
import { ReplyContext } from './replyContext';
|
|
12
|
-
|
|
13
|
-
export type BaseContextPropertiesToOmit =
|
|
14
|
-
| 'action'
|
|
15
|
-
| 'isInitialized'
|
|
16
|
-
| 'storage'
|
|
17
|
-
| 'scheduler'
|
|
18
|
-
| 'responses';
|
|
19
|
-
|
|
20
|
-
export abstract class BaseContextInternal<TAction extends IAction> {
|
|
21
|
-
readonly responses: BotResponse[] = [];
|
|
22
|
-
readonly action: TAction;
|
|
23
|
-
/** Storage client instance for the bot executing this action. */
|
|
24
|
-
readonly storage: IStorageClient;
|
|
25
|
-
/** Scheduler instance for the bot executing this action */
|
|
26
|
-
readonly scheduler: IScheduler;
|
|
27
|
-
/** Name of a bot that executes this action. */
|
|
28
|
-
readonly botName: string;
|
|
29
|
-
/** Chat information. */
|
|
30
|
-
readonly chatInfo: ChatInfo;
|
|
31
|
-
|
|
32
|
-
readonly observability: {
|
|
33
|
-
/** Event emitter for emitting events related to action execution. */
|
|
34
|
-
eventEmitter: TypedEventEmitter;
|
|
35
|
-
/** Trace id of a action execution. */
|
|
36
|
-
traceId: TraceId;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
get actionKey() {
|
|
40
|
-
return this.action.key;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
constructor(
|
|
44
|
-
storage: IStorageClient,
|
|
45
|
-
scheduler: IScheduler,
|
|
46
|
-
eventEmitter: TypedEventEmitter,
|
|
47
|
-
action: TAction,
|
|
48
|
-
chatInfo: ChatInfo,
|
|
49
|
-
traceId: TraceId,
|
|
50
|
-
botName: string
|
|
51
|
-
) {
|
|
52
|
-
this.storage = storage;
|
|
53
|
-
this.scheduler = scheduler;
|
|
54
|
-
this.botName = botName;
|
|
55
|
-
this.action = action;
|
|
56
|
-
this.chatInfo = chatInfo;
|
|
57
|
-
this.observability = {
|
|
58
|
-
eventEmitter,
|
|
59
|
-
traceId
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
protected createCaptureController(
|
|
64
|
-
response: IReplyResponse
|
|
65
|
-
): ICaptureController {
|
|
66
|
-
return {
|
|
67
|
-
captureReplies: (
|
|
68
|
-
trigger: CommandTrigger[],
|
|
69
|
-
handler: (
|
|
70
|
-
replyContext: ReplyContext<IActionState>
|
|
71
|
-
) => Promise<void>,
|
|
72
|
-
abortController?: AbortController
|
|
73
|
-
) => {
|
|
74
|
-
response.captures.push({
|
|
75
|
-
trigger,
|
|
76
|
-
handler,
|
|
77
|
-
abortController: abortController ?? new AbortController(),
|
|
78
|
-
action: this.action
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Loads state of another action for current chat.
|
|
86
|
-
* @param action Action to load state of.
|
|
87
|
-
* @template TAnotherActionState - Type of a state that is used by another action.
|
|
88
|
-
*/
|
|
89
|
-
loadStateOf<TAnotherActionState extends IActionState>(
|
|
90
|
-
action: IActionWithState<TAnotherActionState>
|
|
91
|
-
) {
|
|
92
|
-
const allStates = this.storage.load(action);
|
|
93
|
-
const stateForChat =
|
|
94
|
-
allStates[this.chatInfo.id] ?? action.stateConstructor();
|
|
95
|
-
|
|
96
|
-
return Object.freeze(structuredClone(stateForChat));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Mutates state of another action for current chat.
|
|
101
|
-
* @param action Action to load state of.
|
|
102
|
-
* @param mutation Fuction that mutates the state.
|
|
103
|
-
* @template TAnotherActionState - Type of a state that is used by another action.
|
|
104
|
-
*/
|
|
105
|
-
async updateStateOf<TAnotherActionState extends IActionState>(
|
|
106
|
-
action: IActionWithState<TAnotherActionState>,
|
|
107
|
-
mutation: (state: TAnotherActionState) => Promise<void>
|
|
108
|
-
) {
|
|
109
|
-
await this.storage.updateStateFor(action, this.chatInfo.id, mutation);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { resolve } from 'path';
|
|
2
|
-
import { ImageMessage } from '../../dtos/responses/imageMessage';
|
|
3
|
-
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
4
|
-
import { VideoMessage } from '../../dtos/responses/videoMessage';
|
|
5
|
-
import { UnpinResponse } from '../../dtos/responses/unpin';
|
|
6
|
-
import {
|
|
7
|
-
MessageSendingOptions,
|
|
8
|
-
TextMessageSendingOptions
|
|
9
|
-
} from '../../types/messageSendingOptions';
|
|
10
|
-
import { IActionWithState } from '../../types/action';
|
|
11
|
-
import { IActionState } from '../../types/actionState';
|
|
12
|
-
import { Milliseconds } from '../../types/timeValues';
|
|
13
|
-
import { DelayResponse } from '../../dtos/responses/delay';
|
|
14
|
-
import {
|
|
15
|
-
BaseContextInternal,
|
|
16
|
-
BaseContextPropertiesToOmit
|
|
17
|
-
} from './baseContext';
|
|
18
|
-
import { ScheduledAction } from '../actions/scheduledAction';
|
|
19
|
-
|
|
20
|
-
export type ChatContext<
|
|
21
|
-
TActionState extends IActionState,
|
|
22
|
-
TAction extends IActionWithState<TActionState> =
|
|
23
|
-
ScheduledAction<TActionState>
|
|
24
|
-
> = Omit<
|
|
25
|
-
ChatContextInternal<TActionState, TAction>,
|
|
26
|
-
BaseContextPropertiesToOmit
|
|
27
|
-
>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Context of action executed in chat.
|
|
31
|
-
*/
|
|
32
|
-
export class ChatContextInternal<
|
|
33
|
-
TActionState extends IActionState,
|
|
34
|
-
TAction extends IActionWithState<TActionState> =
|
|
35
|
-
ScheduledAction<TActionState>
|
|
36
|
-
> extends BaseContextInternal<TAction> {
|
|
37
|
-
/**
|
|
38
|
-
* Collection of actions that send something to chat as a standalone message.
|
|
39
|
-
*/
|
|
40
|
-
send = {
|
|
41
|
-
/**
|
|
42
|
-
* Sends text message to chat after action execution is finished.
|
|
43
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
44
|
-
* @param text Message contents.
|
|
45
|
-
* @param options Message sending option.
|
|
46
|
-
*/
|
|
47
|
-
text: (text: string, options?: TextMessageSendingOptions) => {
|
|
48
|
-
const response = new TextMessage(
|
|
49
|
-
text,
|
|
50
|
-
this.chatInfo,
|
|
51
|
-
this.observability.traceId,
|
|
52
|
-
this.action,
|
|
53
|
-
undefined,
|
|
54
|
-
options
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
this.responses.push(response);
|
|
58
|
-
|
|
59
|
-
return this.createCaptureController(response);
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Sends image message to chat after action execution is finished.
|
|
64
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
65
|
-
* @param name Message contents.
|
|
66
|
-
* @param options Message sending option.
|
|
67
|
-
*/
|
|
68
|
-
image: (name: string, options?: MessageSendingOptions) => {
|
|
69
|
-
const response = new ImageMessage(
|
|
70
|
-
{ source: resolve(`./content/${name}.png`) },
|
|
71
|
-
this.chatInfo,
|
|
72
|
-
this.observability.traceId,
|
|
73
|
-
this.action,
|
|
74
|
-
undefined,
|
|
75
|
-
options
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
this.responses.push(response);
|
|
79
|
-
|
|
80
|
-
return this.createCaptureController(response);
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Sends video/gif message to chat after action execution is finished.
|
|
85
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
86
|
-
* @param name Message contents.
|
|
87
|
-
* @param options Message sending option.
|
|
88
|
-
*/
|
|
89
|
-
video: (name: string, options?: MessageSendingOptions) => {
|
|
90
|
-
const response = new VideoMessage(
|
|
91
|
-
{ source: resolve(`./content/${name}.mp4`) },
|
|
92
|
-
this.chatInfo,
|
|
93
|
-
this.observability.traceId,
|
|
94
|
-
this.action,
|
|
95
|
-
undefined,
|
|
96
|
-
options
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
this.responses.push(response);
|
|
100
|
-
|
|
101
|
-
return this.createCaptureController(response);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Unpins message after action execution is finished.
|
|
107
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
108
|
-
* @param messageId Message id.
|
|
109
|
-
*/
|
|
110
|
-
unpinMessage(messageId: number) {
|
|
111
|
-
this.responses.push(
|
|
112
|
-
new UnpinResponse(
|
|
113
|
-
messageId,
|
|
114
|
-
this.chatInfo,
|
|
115
|
-
this.observability.traceId,
|
|
116
|
-
this.action
|
|
117
|
-
)
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Delays next responses by specified amount of time.
|
|
123
|
-
* @param delay Delay in milliseconds.
|
|
124
|
-
*/
|
|
125
|
-
wait(delay: Milliseconds) {
|
|
126
|
-
this.responses.push(
|
|
127
|
-
new DelayResponse(
|
|
128
|
-
delay,
|
|
129
|
-
this.chatInfo,
|
|
130
|
-
this.observability.traceId,
|
|
131
|
-
this.action
|
|
132
|
-
)
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { InlineQueryAction } from '../actions/inlineQueryAction';
|
|
2
|
-
import {
|
|
3
|
-
BaseContextInternal,
|
|
4
|
-
BaseContextPropertiesToOmit
|
|
5
|
-
} from './baseContext';
|
|
6
|
-
import { TelegramInlineQueryResult } from '../../types/externalAliases';
|
|
7
|
-
import { TypedEventEmitter } from '../../types/events';
|
|
8
|
-
import { IScheduler } from '../../types/scheduler';
|
|
9
|
-
import { IStorageClient } from '../../types/storage';
|
|
10
|
-
import { ChatInfo } from '../../dtos/chatInfo';
|
|
11
|
-
import { IncomingInlineQuery } from '../../dtos/incomingQuery';
|
|
12
|
-
|
|
13
|
-
export type InlineQueryContext = Omit<
|
|
14
|
-
InlineQueryContextInternal,
|
|
15
|
-
BaseContextPropertiesToOmit | 'queryResults' | 'queryId'
|
|
16
|
-
>;
|
|
17
|
-
|
|
18
|
-
export class InlineQueryContextInternal extends BaseContextInternal<InlineQueryAction> {
|
|
19
|
-
readonly queryResults: TelegramInlineQueryResult[] = [];
|
|
20
|
-
/**
|
|
21
|
-
* Abort signal to be utilized in query handler.
|
|
22
|
-
* Signal will be aborted if new query comes from the same user.
|
|
23
|
-
*/
|
|
24
|
-
readonly abortSignal: AbortSignal;
|
|
25
|
-
/** Inline query text */
|
|
26
|
-
readonly queryText: string;
|
|
27
|
-
/** Internal query id */
|
|
28
|
-
readonly queryId: string;
|
|
29
|
-
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
30
|
-
matchResults: RegExpMatchArray[] = [];
|
|
31
|
-
|
|
32
|
-
constructor(
|
|
33
|
-
storage: IStorageClient,
|
|
34
|
-
scheduler: IScheduler,
|
|
35
|
-
eventEmitter: TypedEventEmitter,
|
|
36
|
-
action: InlineQueryAction,
|
|
37
|
-
query: IncomingInlineQuery,
|
|
38
|
-
chatInfo: ChatInfo,
|
|
39
|
-
botName: string
|
|
40
|
-
) {
|
|
41
|
-
super(
|
|
42
|
-
storage,
|
|
43
|
-
scheduler,
|
|
44
|
-
eventEmitter,
|
|
45
|
-
action,
|
|
46
|
-
chatInfo,
|
|
47
|
-
query.traceId,
|
|
48
|
-
botName
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
this.queryText = query.query;
|
|
52
|
-
this.queryId = query.queryId;
|
|
53
|
-
this.abortSignal = query.abortController.signal;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* This result will be shown to user as a response to inline query.
|
|
58
|
-
* @param queryResult Inline query result to be shown to user.
|
|
59
|
-
*/
|
|
60
|
-
showInlineQueryResult(queryResult: TelegramInlineQueryResult) {
|
|
61
|
-
this.queryResults.push(queryResult);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import { resolve } from 'path';
|
|
2
|
-
import { IActionState } from '../../types/actionState';
|
|
3
|
-
import { ImageMessage } from '../../dtos/responses/imageMessage';
|
|
4
|
-
import { Reaction } from '../../dtos/responses/reaction';
|
|
5
|
-
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
6
|
-
import { VideoMessage } from '../../dtos/responses/videoMessage';
|
|
7
|
-
import { ChatContextInternal } from './chatContext';
|
|
8
|
-
import {
|
|
9
|
-
MessageSendingOptions,
|
|
10
|
-
TextMessageSendingOptions
|
|
11
|
-
} from '../../types/messageSendingOptions';
|
|
12
|
-
import { ReplyInfo } from '../../dtos/replyInfo';
|
|
13
|
-
import { CommandAction } from '../actions/commandAction';
|
|
14
|
-
import { Seconds } from '../../types/timeValues';
|
|
15
|
-
import { BaseContextPropertiesToOmit } from './baseContext';
|
|
16
|
-
import { MessageInfo } from '../../dtos/messageInfo';
|
|
17
|
-
import { UserInfo } from '../../dtos/userInfo';
|
|
18
|
-
import { BotInfo, TelegramEmoji } from '../../types/externalAliases';
|
|
19
|
-
import { TypedEventEmitter } from '../../types/events';
|
|
20
|
-
import { IScheduler } from '../../types/scheduler';
|
|
21
|
-
import { IStorageClient } from '../../types/storage';
|
|
22
|
-
import { IncomingMessage } from '../../dtos/incomingMessage';
|
|
23
|
-
|
|
24
|
-
export type MessageContext<TActionState extends IActionState> = Omit<
|
|
25
|
-
MessageContextInternal<TActionState>,
|
|
26
|
-
BaseContextPropertiesToOmit | 'startCooldown' | 'customCooldown'
|
|
27
|
-
>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Context of action executed in chat, in response to a message
|
|
31
|
-
*/
|
|
32
|
-
export class MessageContextInternal<
|
|
33
|
-
TActionState extends IActionState
|
|
34
|
-
> extends ChatContextInternal<TActionState, CommandAction<TActionState>> {
|
|
35
|
-
/** Information about the user that triggered this action */
|
|
36
|
-
readonly userInfo!: UserInfo;
|
|
37
|
-
/** Information about the message that triggered this action */
|
|
38
|
-
readonly messageInfo!: MessageInfo;
|
|
39
|
-
/** Bot info from Telegram */
|
|
40
|
-
readonly botInfo!: BotInfo;
|
|
41
|
-
/** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
|
|
42
|
-
startCooldown: boolean = true;
|
|
43
|
-
/** Custom cooldown duration in seconds, if specified. */
|
|
44
|
-
customCooldown: Seconds | undefined;
|
|
45
|
-
/** Collection of Regexp match results on a message that triggered this action. Will be empty if trigger is not a Regexp. */
|
|
46
|
-
matchResults: RegExpMatchArray[] = [];
|
|
47
|
-
|
|
48
|
-
constructor(
|
|
49
|
-
storage: IStorageClient,
|
|
50
|
-
scheduler: IScheduler,
|
|
51
|
-
eventEmitter: TypedEventEmitter,
|
|
52
|
-
action: CommandAction<TActionState>,
|
|
53
|
-
message: IncomingMessage,
|
|
54
|
-
botName: string,
|
|
55
|
-
botInfo: BotInfo
|
|
56
|
-
) {
|
|
57
|
-
super(
|
|
58
|
-
storage,
|
|
59
|
-
scheduler,
|
|
60
|
-
eventEmitter,
|
|
61
|
-
action,
|
|
62
|
-
message.chatInfo,
|
|
63
|
-
message.traceId,
|
|
64
|
-
botName
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
this.messageInfo = new MessageInfo(
|
|
68
|
-
message.messageId,
|
|
69
|
-
message.text,
|
|
70
|
-
message.type,
|
|
71
|
-
message.updateObject
|
|
72
|
-
);
|
|
73
|
-
this.userInfo = new UserInfo(
|
|
74
|
-
message.from?.id ?? -1,
|
|
75
|
-
(message.from?.first_name ?? 'Unknown user') +
|
|
76
|
-
(message.from?.last_name ? ` ${message.from.last_name}` : '')
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
this.botInfo = botInfo;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
private getQuotePart(quote: boolean | string) {
|
|
83
|
-
if (typeof quote != 'boolean') return quote;
|
|
84
|
-
|
|
85
|
-
return this.matchResults.length == 0
|
|
86
|
-
? this.messageInfo.text
|
|
87
|
-
: this.matchResults[0][1];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private replyWithText(
|
|
91
|
-
text: string,
|
|
92
|
-
quote: boolean | string,
|
|
93
|
-
options?: TextMessageSendingOptions
|
|
94
|
-
) {
|
|
95
|
-
const quotedPart = this.getQuotePart(quote);
|
|
96
|
-
|
|
97
|
-
const response = new TextMessage(
|
|
98
|
-
text,
|
|
99
|
-
this.chatInfo,
|
|
100
|
-
this.observability.traceId,
|
|
101
|
-
this.action,
|
|
102
|
-
new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
|
|
103
|
-
options
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
this.responses.push(response);
|
|
107
|
-
|
|
108
|
-
return this.createCaptureController(response);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private replyWithImage(
|
|
112
|
-
name: string,
|
|
113
|
-
quote: boolean | string,
|
|
114
|
-
options?: MessageSendingOptions
|
|
115
|
-
) {
|
|
116
|
-
const quotedPart = this.getQuotePart(quote);
|
|
117
|
-
|
|
118
|
-
const response = new ImageMessage(
|
|
119
|
-
{ source: resolve(`./content/${name}.png`) },
|
|
120
|
-
this.chatInfo,
|
|
121
|
-
this.observability.traceId,
|
|
122
|
-
this.action,
|
|
123
|
-
new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
|
|
124
|
-
options
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
this.responses.push(response);
|
|
128
|
-
|
|
129
|
-
return this.createCaptureController(response);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
private replyWithVideo(
|
|
133
|
-
name: string,
|
|
134
|
-
quote: boolean | string,
|
|
135
|
-
options?: MessageSendingOptions
|
|
136
|
-
) {
|
|
137
|
-
const quotedPart = this.getQuotePart(quote);
|
|
138
|
-
|
|
139
|
-
const response = new VideoMessage(
|
|
140
|
-
{ source: resolve(`./content/${name}.mp4`) },
|
|
141
|
-
this.chatInfo,
|
|
142
|
-
this.observability.traceId,
|
|
143
|
-
this.action,
|
|
144
|
-
new ReplyInfo(this.messageInfo.id, quote ? quotedPart : undefined),
|
|
145
|
-
options
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
this.responses.push(response);
|
|
149
|
-
|
|
150
|
-
return this.createCaptureController(response);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
skipCooldown() {
|
|
154
|
-
this.startCooldown = false;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
startCustomCooldown(customCooldown: Seconds) {
|
|
158
|
-
this.startCooldown = true;
|
|
159
|
-
this.customCooldown = customCooldown;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Collection of actions that can be done as a reply to a message that triggered this action
|
|
164
|
-
*/
|
|
165
|
-
reply = {
|
|
166
|
-
/**
|
|
167
|
-
* Collection of actions that can be done as a reply to a message, quoting the part that triggered this action
|
|
168
|
-
* If regex is matched, first match will be quoted
|
|
169
|
-
*/
|
|
170
|
-
andQuote: {
|
|
171
|
-
/**
|
|
172
|
-
* Reply with text message to message that triggered this action after action execution is finished.
|
|
173
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
174
|
-
* @param text Message contents.
|
|
175
|
-
* @param options Message sending option.
|
|
176
|
-
*/
|
|
177
|
-
withText: (
|
|
178
|
-
text: string,
|
|
179
|
-
quote?: string,
|
|
180
|
-
options?: TextMessageSendingOptions
|
|
181
|
-
) => this.replyWithText(text, quote ?? true, options),
|
|
182
|
-
/**
|
|
183
|
-
* Reply with image message to message that triggered this action after action execution is finished.
|
|
184
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
185
|
-
* @param text Message contents.
|
|
186
|
-
* @param options Message sending option.
|
|
187
|
-
*/
|
|
188
|
-
withImage: (
|
|
189
|
-
name: string,
|
|
190
|
-
quote?: string,
|
|
191
|
-
options?: MessageSendingOptions
|
|
192
|
-
) => this.replyWithImage(name, quote ?? true, options),
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
196
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
197
|
-
* @param text Message contents.
|
|
198
|
-
* @param options Message sending option.
|
|
199
|
-
*/
|
|
200
|
-
withVideo: (
|
|
201
|
-
name: string,
|
|
202
|
-
quote?: string,
|
|
203
|
-
options?: MessageSendingOptions
|
|
204
|
-
) => this.replyWithVideo(name, quote ?? true, options)
|
|
205
|
-
},
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Reply with text message to message that triggered this action after action execution is finished.
|
|
209
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
210
|
-
* @param text Message contents.
|
|
211
|
-
* @param options Message sending option.
|
|
212
|
-
*/
|
|
213
|
-
withText: (text: string, options?: TextMessageSendingOptions) =>
|
|
214
|
-
this.replyWithText(text, false, options),
|
|
215
|
-
/**
|
|
216
|
-
* Reply with image message to message that triggered this action after action execution is finished.
|
|
217
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
218
|
-
* @param text Message contents.
|
|
219
|
-
* @param options Message sending option.
|
|
220
|
-
*/
|
|
221
|
-
withImage: (name: string, options?: MessageSendingOptions) =>
|
|
222
|
-
this.replyWithImage(name, false, options),
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Reply with video/gif message to message that triggered this action after action execution is finished.
|
|
226
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
227
|
-
* @param text Message contents.
|
|
228
|
-
* @param options Message sending option.
|
|
229
|
-
*/
|
|
230
|
-
withVideo: (name: string, options?: MessageSendingOptions) =>
|
|
231
|
-
this.replyWithVideo(name, false, options),
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* React to the message that triggered this action after action execution is finished.
|
|
235
|
-
* If multiple responses are sent, they will be sent in the order they were added, with delay of at least 35ms as per Telegram rate-limit.
|
|
236
|
-
* @param emoji Telegram emoji to react with.
|
|
237
|
-
*/
|
|
238
|
-
withReaction: (emoji: TelegramEmoji) => {
|
|
239
|
-
this.responses.push(
|
|
240
|
-
new Reaction(
|
|
241
|
-
this.observability.traceId,
|
|
242
|
-
this.chatInfo,
|
|
243
|
-
this.messageInfo.id,
|
|
244
|
-
emoji,
|
|
245
|
-
this.action
|
|
246
|
-
)
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|