@mtcute/dispatcher 0.16.7 → 0.16.13
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/{cjs/callback-data-builder.d.ts → callback-data-builder.d.ts} +2 -2
- package/callback-data-builder.test.d.ts +1 -0
- package/{esm/context → context}/base.d.ts +2 -2
- package/{esm/context → context}/business-message.d.ts +5 -6
- package/{esm/context → context}/callback-query.d.ts +6 -7
- package/{cjs/context → context}/chat-join-request.d.ts +3 -3
- package/{esm/context → context}/chosen-inline-result.d.ts +3 -3
- package/{esm/context → context}/inline-query.d.ts +4 -5
- package/{cjs/context → context}/message.d.ts +11 -6
- package/{cjs/context → context}/parse.d.ts +3 -3
- package/{esm/context → context}/pre-checkout-query.d.ts +3 -3
- package/{esm/context → context}/scene-transition.d.ts +1 -1
- package/{cjs/dispatcher.d.ts → dispatcher.d.ts} +9 -10
- package/{cjs/filters → filters}/bots.d.ts +4 -4
- package/filters/bots.test.d.ts +1 -0
- package/{cjs/filters → filters}/chat.d.ts +3 -3
- package/{esm/filters → filters}/group.d.ts +4 -4
- package/{cjs/filters → filters}/logic.d.ts +1 -1
- package/filters/logic.test.d.ts +1 -0
- package/{esm/filters → filters}/message.d.ts +9 -5
- package/{cjs/filters → filters}/state.d.ts +2 -2
- package/{esm/filters → filters}/text.d.ts +3 -3
- package/{cjs/filters → filters}/types.d.ts +2 -2
- package/{cjs/filters → filters}/updates.d.ts +2 -2
- package/{cjs/filters → filters}/user.d.ts +3 -4
- package/{cjs/handler.d.ts → handler.d.ts} +5 -5
- package/index.cjs +2534 -0
- package/index.d.ts +8 -1
- package/index.js +2529 -1
- package/package.json +28 -24
- package/{cjs/state → state}/key.d.ts +3 -3
- package/state/provider.d.ts +5 -0
- package/{cjs/state → state}/providers/memory.d.ts +3 -4
- package/{esm/state → state}/providers/sqlite.d.ts +3 -3
- package/{cjs/state → state}/repository.d.ts +1 -1
- package/{esm/state → state}/service.d.ts +2 -2
- package/{cjs/state → state}/update-state.d.ts +2 -2
- package/{esm/wizard.d.ts → wizard.d.ts} +5 -6
- package/cjs/callback-data-builder.js +0 -141
- package/cjs/callback-data-builder.js.map +0 -1
- package/cjs/context/base.d.ts +0 -9
- package/cjs/context/base.js +0 -3
- package/cjs/context/base.js.map +0 -1
- package/cjs/context/business-message.d.ts +0 -61
- package/cjs/context/business-message.js +0 -146
- package/cjs/context/business-message.js.map +0 -1
- package/cjs/context/callback-query.d.ts +0 -63
- package/cjs/context/callback-query.js +0 -109
- package/cjs/context/callback-query.js.map +0 -1
- package/cjs/context/chat-join-request.js +0 -35
- package/cjs/context/chat-join-request.js.map +0 -1
- package/cjs/context/chosen-inline-result.d.ts +0 -22
- package/cjs/context/chosen-inline-result.js +0 -36
- package/cjs/context/chosen-inline-result.js.map +0 -1
- package/cjs/context/index.js +0 -25
- package/cjs/context/index.js.map +0 -1
- package/cjs/context/inline-query.d.ts +0 -16
- package/cjs/context/inline-query.js +0 -23
- package/cjs/context/inline-query.js.map +0 -1
- package/cjs/context/message.js +0 -171
- package/cjs/context/message.js.map +0 -1
- package/cjs/context/parse.js +0 -42
- package/cjs/context/parse.js.map +0 -1
- package/cjs/context/pre-checkout-query.d.ts +0 -17
- package/cjs/context/pre-checkout-query.js +0 -27
- package/cjs/context/pre-checkout-query.js.map +0 -1
- package/cjs/context/scene-transition.d.ts +0 -24
- package/cjs/context/scene-transition.js +0 -52
- package/cjs/context/scene-transition.js.map +0 -1
- package/cjs/dispatcher.js +0 -908
- package/cjs/dispatcher.js.map +0 -1
- package/cjs/filters/bots.js +0 -135
- package/cjs/filters/bots.js.map +0 -1
- package/cjs/filters/bundle.js +0 -27
- package/cjs/filters/bundle.js.map +0 -1
- package/cjs/filters/chat.js +0 -56
- package/cjs/filters/chat.js.map +0 -1
- package/cjs/filters/group.d.ts +0 -26
- package/cjs/filters/group.js +0 -69
- package/cjs/filters/group.js.map +0 -1
- package/cjs/filters/index.js +0 -29
- package/cjs/filters/index.js.map +0 -1
- package/cjs/filters/logic.js +0 -112
- package/cjs/filters/logic.js.map +0 -1
- package/cjs/filters/message.d.ts +0 -215
- package/cjs/filters/message.js +0 -191
- package/cjs/filters/message.js.map +0 -1
- package/cjs/filters/state.js +0 -33
- package/cjs/filters/state.js.map +0 -1
- package/cjs/filters/text.d.ts +0 -64
- package/cjs/filters/text.js +0 -136
- package/cjs/filters/text.js.map +0 -1
- package/cjs/filters/types.js +0 -3
- package/cjs/filters/types.js.map +0 -1
- package/cjs/filters/updates.js +0 -40
- package/cjs/filters/updates.js.map +0 -1
- package/cjs/filters/user.js +0 -77
- package/cjs/filters/user.js.map +0 -1
- package/cjs/handler.js +0 -4
- package/cjs/handler.js.map +0 -1
- package/cjs/index.js +0 -31
- package/cjs/index.js.map +0 -1
- package/cjs/package.json +0 -3
- package/cjs/propagation.js +0 -27
- package/cjs/propagation.js.map +0 -1
- package/cjs/state/index.js +0 -22
- package/cjs/state/index.js.map +0 -1
- package/cjs/state/key.js +0 -43
- package/cjs/state/key.js.map +0 -1
- package/cjs/state/provider.d.ts +0 -5
- package/cjs/state/provider.js +0 -3
- package/cjs/state/provider.js.map +0 -1
- package/cjs/state/providers/index.js +0 -19
- package/cjs/state/providers/index.js.map +0 -1
- package/cjs/state/providers/memory.js +0 -81
- package/cjs/state/providers/memory.js.map +0 -1
- package/cjs/state/providers/sqlite.d.ts +0 -28
- package/cjs/state/providers/sqlite.js +0 -100
- package/cjs/state/providers/sqlite.js.map +0 -1
- package/cjs/state/repository.js +0 -3
- package/cjs/state/repository.js.map +0 -1
- package/cjs/state/service.d.ts +0 -20
- package/cjs/state/service.js +0 -70
- package/cjs/state/service.js.map +0 -1
- package/cjs/state/update-state.js +0 -219
- package/cjs/state/update-state.js.map +0 -1
- package/cjs/wizard.d.ts +0 -65
- package/cjs/wizard.js +0 -105
- package/cjs/wizard.js.map +0 -1
- package/esm/callback-data-builder.d.ts +0 -49
- package/esm/callback-data-builder.js +0 -137
- package/esm/callback-data-builder.js.map +0 -1
- package/esm/context/base.js +0 -2
- package/esm/context/base.js.map +0 -1
- package/esm/context/business-message.js +0 -142
- package/esm/context/business-message.js.map +0 -1
- package/esm/context/callback-query.js +0 -103
- package/esm/context/callback-query.js.map +0 -1
- package/esm/context/chat-join-request.d.ts +0 -17
- package/esm/context/chat-join-request.js +0 -31
- package/esm/context/chat-join-request.js.map +0 -1
- package/esm/context/chosen-inline-result.js +0 -32
- package/esm/context/chosen-inline-result.js.map +0 -1
- package/esm/context/index.d.ts +0 -9
- package/esm/context/index.js +0 -9
- package/esm/context/index.js.map +0 -1
- package/esm/context/inline-query.js +0 -19
- package/esm/context/inline-query.js.map +0 -1
- package/esm/context/message.d.ts +0 -77
- package/esm/context/message.js +0 -167
- package/esm/context/message.js.map +0 -1
- package/esm/context/parse.d.ts +0 -13
- package/esm/context/parse.js +0 -39
- package/esm/context/parse.js.map +0 -1
- package/esm/context/pre-checkout-query.js +0 -23
- package/esm/context/pre-checkout-query.js.map +0 -1
- package/esm/context/scene-transition.js +0 -48
- package/esm/context/scene-transition.js.map +0 -1
- package/esm/dispatcher.d.ts +0 -881
- package/esm/dispatcher.js +0 -904
- package/esm/dispatcher.js.map +0 -1
- package/esm/filters/bots.d.ts +0 -64
- package/esm/filters/bots.js +0 -131
- package/esm/filters/bots.js.map +0 -1
- package/esm/filters/bundle.d.ts +0 -10
- package/esm/filters/bundle.js +0 -11
- package/esm/filters/bundle.js.map +0 -1
- package/esm/filters/chat.d.ts +0 -27
- package/esm/filters/chat.js +0 -51
- package/esm/filters/chat.js.map +0 -1
- package/esm/filters/group.js +0 -65
- package/esm/filters/group.js.map +0 -1
- package/esm/filters/index.d.ts +0 -4
- package/esm/filters/index.js +0 -3
- package/esm/filters/index.js.map +0 -1
- package/esm/filters/logic.d.ts +0 -29
- package/esm/filters/logic.js +0 -105
- package/esm/filters/logic.js.map +0 -1
- package/esm/filters/message.js +0 -168
- package/esm/filters/message.js.map +0 -1
- package/esm/filters/state.d.ts +0 -15
- package/esm/filters/state.js +0 -28
- package/esm/filters/state.js.map +0 -1
- package/esm/filters/text.js +0 -129
- package/esm/filters/text.js.map +0 -1
- package/esm/filters/types.d.ts +0 -91
- package/esm/filters/types.js +0 -2
- package/esm/filters/types.js.map +0 -1
- package/esm/filters/updates.d.ts +0 -39
- package/esm/filters/updates.js +0 -34
- package/esm/filters/updates.js.map +0 -1
- package/esm/filters/user.d.ts +0 -25
- package/esm/filters/user.js +0 -71
- package/esm/filters/user.js.map +0 -1
- package/esm/handler.d.ts +0 -41
- package/esm/handler.js +0 -3
- package/esm/handler.js.map +0 -1
- package/esm/index.d.ts +0 -8
- package/esm/index.js +0 -9
- package/esm/index.js.map +0 -1
- package/esm/propagation.d.ts +0 -22
- package/esm/propagation.js +0 -24
- package/esm/propagation.js.map +0 -1
- package/esm/state/index.d.ts +0 -5
- package/esm/state/index.js +0 -6
- package/esm/state/index.js.map +0 -1
- package/esm/state/key.d.ts +0 -24
- package/esm/state/key.js +0 -39
- package/esm/state/key.js.map +0 -1
- package/esm/state/provider.d.ts +0 -5
- package/esm/state/provider.js +0 -2
- package/esm/state/provider.js.map +0 -1
- package/esm/state/providers/index.d.ts +0 -2
- package/esm/state/providers/index.js +0 -3
- package/esm/state/providers/index.js.map +0 -1
- package/esm/state/providers/memory.d.ts +0 -30
- package/esm/state/providers/memory.js +0 -77
- package/esm/state/providers/memory.js.map +0 -1
- package/esm/state/providers/sqlite.js +0 -96
- package/esm/state/providers/sqlite.js.map +0 -1
- package/esm/state/repository.d.ts +0 -62
- package/esm/state/repository.js +0 -2
- package/esm/state/repository.js.map +0 -1
- package/esm/state/service.js +0 -66
- package/esm/state/service.js.map +0 -1
- package/esm/state/update-state.d.ts +0 -151
- package/esm/state/update-state.js +0 -214
- package/esm/state/update-state.js.map +0 -1
- package/esm/wizard.js +0 -101
- package/esm/wizard.js.map +0 -1
- /package/{cjs/context → context}/index.d.ts +0 -0
- /package/{cjs/filters → filters}/bundle.d.ts +0 -0
- /package/{cjs/filters → filters}/index.d.ts +0 -0
- /package/{cjs/index.d.ts → index.d.cts} +0 -0
- /package/{cjs/propagation.d.ts → propagation.d.ts} +0 -0
- /package/{cjs/state → state}/index.d.ts +0 -0
- /package/{cjs/state → state}/providers/index.d.ts +0 -0
package/cjs/wizard.d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise } from '@mtcute/core';
|
|
2
|
-
import type { MessageContext } from './context/message.js';
|
|
3
|
-
import type { DispatcherParams } from './dispatcher.js';
|
|
4
|
-
import { Dispatcher } from './dispatcher.js';
|
|
5
|
-
import type { UpdateFilter } from './filters/index.js';
|
|
6
|
-
import type { UpdateState } from './state/update-state.js';
|
|
7
|
-
/**
|
|
8
|
-
* Action for the wizard scene.
|
|
9
|
-
*
|
|
10
|
-
* `Next`: Continue to the next registered step
|
|
11
|
-
* (or exit, if this is the last step)
|
|
12
|
-
*
|
|
13
|
-
* `Stay`: Stay on the same step
|
|
14
|
-
*
|
|
15
|
-
* `Exit`: Exit from the wizard scene
|
|
16
|
-
*
|
|
17
|
-
* You can also return a `number` to jump to that step
|
|
18
|
-
*/
|
|
19
|
-
export declare enum WizardSceneAction {
|
|
20
|
-
Next = "next",
|
|
21
|
-
Stay = "stay",
|
|
22
|
-
Exit = "exit"
|
|
23
|
-
}
|
|
24
|
-
interface WizardInternalState {
|
|
25
|
-
$step?: number;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Wizard is a special type of Dispatcher
|
|
29
|
-
* that can be used to simplify implementing
|
|
30
|
-
* step-by-step scenes.
|
|
31
|
-
*/
|
|
32
|
-
export declare class WizardScene<State extends object> extends Dispatcher<State & WizardInternalState> {
|
|
33
|
-
private _steps;
|
|
34
|
-
private _defaultState;
|
|
35
|
-
constructor(name: string, params?: Omit<DispatcherParams, 'sceneName'>);
|
|
36
|
-
static for: never;
|
|
37
|
-
static child: never;
|
|
38
|
-
static scene: never;
|
|
39
|
-
setDefaultState(defaultState: State): void;
|
|
40
|
-
/**
|
|
41
|
-
* Get the total number of registered steps
|
|
42
|
-
*/
|
|
43
|
-
get totalSteps(): number;
|
|
44
|
-
/**
|
|
45
|
-
* Go to the Nth step
|
|
46
|
-
*/
|
|
47
|
-
goToStep(state: UpdateState<WizardInternalState>, step: number): Promise<void>;
|
|
48
|
-
/**
|
|
49
|
-
* Skip N steps
|
|
50
|
-
*/
|
|
51
|
-
skip(state: UpdateState<WizardInternalState>, count?: number): Promise<void>;
|
|
52
|
-
/**
|
|
53
|
-
* Filter that will only pass if the current step is `step`
|
|
54
|
-
*/
|
|
55
|
-
static onNthStep(step: number): UpdateFilter<any, {}, WizardInternalState>;
|
|
56
|
-
/**
|
|
57
|
-
* Filter that will only pass if the current step is the one after last one added
|
|
58
|
-
*/
|
|
59
|
-
onCurrentStep(): UpdateFilter<any, {}, WizardInternalState>;
|
|
60
|
-
/**
|
|
61
|
-
* Add a step to the wizard
|
|
62
|
-
*/
|
|
63
|
-
addStep(handler: (msg: MessageContext, state: UpdateState<State & WizardInternalState>) => MaybePromise<WizardSceneAction | number>): void;
|
|
64
|
-
}
|
|
65
|
-
export {};
|
package/cjs/wizard.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WizardScene = exports.WizardSceneAction = void 0;
|
|
4
|
-
const dispatcher_js_1 = require("./dispatcher.js");
|
|
5
|
-
const index_js_1 = require("./filters/index.js");
|
|
6
|
-
/**
|
|
7
|
-
* Action for the wizard scene.
|
|
8
|
-
*
|
|
9
|
-
* `Next`: Continue to the next registered step
|
|
10
|
-
* (or exit, if this is the last step)
|
|
11
|
-
*
|
|
12
|
-
* `Stay`: Stay on the same step
|
|
13
|
-
*
|
|
14
|
-
* `Exit`: Exit from the wizard scene
|
|
15
|
-
*
|
|
16
|
-
* You can also return a `number` to jump to that step
|
|
17
|
-
*/
|
|
18
|
-
var WizardSceneAction;
|
|
19
|
-
(function (WizardSceneAction) {
|
|
20
|
-
WizardSceneAction["Next"] = "next";
|
|
21
|
-
WizardSceneAction["Stay"] = "stay";
|
|
22
|
-
WizardSceneAction["Exit"] = "exit";
|
|
23
|
-
})(WizardSceneAction || (exports.WizardSceneAction = WizardSceneAction = {}));
|
|
24
|
-
/**
|
|
25
|
-
* Wizard is a special type of Dispatcher
|
|
26
|
-
* that can be used to simplify implementing
|
|
27
|
-
* step-by-step scenes.
|
|
28
|
-
*/
|
|
29
|
-
class WizardScene extends dispatcher_js_1.Dispatcher {
|
|
30
|
-
_steps = 0;
|
|
31
|
-
_defaultState = {};
|
|
32
|
-
constructor(name, params) {
|
|
33
|
-
super(undefined, { sceneName: name, ...params });
|
|
34
|
-
}
|
|
35
|
-
setDefaultState(defaultState) {
|
|
36
|
-
this._defaultState = defaultState;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Get the total number of registered steps
|
|
40
|
-
*/
|
|
41
|
-
get totalSteps() {
|
|
42
|
-
return this._steps;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Go to the Nth step
|
|
46
|
-
*/
|
|
47
|
-
async goToStep(state, step) {
|
|
48
|
-
if (step >= this._steps) {
|
|
49
|
-
await state.exit();
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
await state.merge({ $step: step }, this._defaultState);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Skip N steps
|
|
57
|
-
*/
|
|
58
|
-
async skip(state, count = 1) {
|
|
59
|
-
const { $step } = (await state.get()) || {};
|
|
60
|
-
if ($step === undefined)
|
|
61
|
-
throw new Error('Wizard state is not initialized');
|
|
62
|
-
return this.goToStep(state, $step + count);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Filter that will only pass if the current step is `step`
|
|
66
|
-
*/
|
|
67
|
-
// eslint-disable-next-line ts/no-empty-object-type
|
|
68
|
-
static onNthStep(step) {
|
|
69
|
-
const filter = index_js_1.filters.state(it => it.$step === step);
|
|
70
|
-
if (step === 0)
|
|
71
|
-
return index_js_1.filters.or(index_js_1.filters.stateEmpty, filter);
|
|
72
|
-
return filter;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Filter that will only pass if the current step is the one after last one added
|
|
76
|
-
*/
|
|
77
|
-
// eslint-disable-next-line ts/no-empty-object-type
|
|
78
|
-
onCurrentStep() {
|
|
79
|
-
return WizardScene.onNthStep(this._steps);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Add a step to the wizard
|
|
83
|
-
*/
|
|
84
|
-
addStep(handler) {
|
|
85
|
-
const step = this._steps++;
|
|
86
|
-
this.onNewMessage(WizardScene.onNthStep(step), async (msg, state) => {
|
|
87
|
-
const result = await handler(msg, state);
|
|
88
|
-
if (typeof result === 'number') {
|
|
89
|
-
await this.goToStep(state, result);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
switch (result) {
|
|
93
|
-
case 'next': {
|
|
94
|
-
await this.goToStep(state, step + 1);
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
case 'exit':
|
|
98
|
-
await state.exit();
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
exports.WizardScene = WizardScene;
|
|
105
|
-
//# sourceMappingURL=wizard.js.map
|
package/cjs/wizard.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wizard.js","sourceRoot":"","sources":["../../src/wizard.ts"],"names":[],"mappings":";;;AAIA,mDAA4C;AAE5C,iDAA4C;AAG5C;;;;;;;;;;;GAWG;AACH,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IACzB,kCAAa,CAAA;IACb,kCAAa,CAAA;IACb,kCAAa,CAAA;AACjB,CAAC,EAJW,iBAAiB,iCAAjB,iBAAiB,QAI5B;AAMD;;;;GAIG;AACH,MAAa,WAAkC,SAAQ,0BAAuC;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AO,CAAC,EAAE,CAAC,kBAAO,CAAC,UAAU,EAAE,MAAM,CAAC,CAAA;QAE7D,OAAO,MAAM,CAAA;IACjB,CAAC;IAED;;OAEG;IACH,mDAAmD;IACnD,aAAa;QACT,OAAO,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAC7C,CAAC;IAED;;OAEG;IACH,OAAO,CACH,OAG6C;QAE7C,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;QAE1B,IAAI,CAAC,YAAY,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;YAChE,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;YAExC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;gBAC7B,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;gBAElC,OAAM;YACV,CAAC;YAED,QAAQ,MAAM,EAAE,CAAC;gBACb,KAAK,MAAM,CAAC,CAAC,CAAC;oBACV,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,GAAG,CAAC,CAAC,CAAA;oBACpC,MAAK;gBACT,CAAC;gBACD,KAAK,MAAM;oBACP,MAAM,KAAK,CAAC,IAAI,EAAE,CAAA;oBAClB,MAAK;YACb,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAjGD,kCAiGC","sourcesContent":["import type { MaybePromise } from '@mtcute/core'\n\nimport type { MessageContext } from './context/message.js'\nimport type { DispatcherParams } from './dispatcher.js'\nimport { Dispatcher } from './dispatcher.js'\nimport type { UpdateFilter } from './filters/index.js'\nimport { filters } from './filters/index.js'\nimport type { UpdateState } from './state/update-state.js'\n\n/**\n * Action for the wizard scene.\n *\n * `Next`: Continue to the next registered step\n * (or exit, if this is the last step)\n *\n * `Stay`: Stay on the same step\n *\n * `Exit`: Exit from the wizard scene\n *\n * You can also return a `number` to jump to that step\n */\nexport enum WizardSceneAction {\n Next = 'next',\n Stay = 'stay',\n Exit = 'exit',\n}\n\ninterface WizardInternalState {\n $step?: number\n}\n\n/**\n * Wizard is a special type of Dispatcher\n * that can be used to simplify implementing\n * step-by-step scenes.\n */\nexport class WizardScene<State extends object> extends Dispatcher<State & WizardInternalState> {\n private _steps = 0\n\n private _defaultState: State & WizardInternalState = {} as State & WizardInternalState\n\n constructor(name: string, params?: Omit<DispatcherParams, 'sceneName'>) {\n super(undefined, { sceneName: name, ...params })\n }\n\n // remove inherited statics from Dispatcher\n declare static for: never\n declare static child: never\n declare static scene: never\n\n setDefaultState(defaultState: State): void {\n this._defaultState = defaultState as State & WizardInternalState\n }\n\n /**\n * Get the total number of registered steps\n */\n get totalSteps(): number {\n return this._steps\n }\n\n /**\n * Go to the Nth step\n */\n async goToStep(state: UpdateState<WizardInternalState>, step: number): Promise<void> {\n if (step >= this._steps) {\n await state.exit()\n } else {\n await state.merge({ $step: step }, this._defaultState)\n }\n }\n\n /**\n * Skip N steps\n */\n async skip(state: UpdateState<WizardInternalState>, count = 1): Promise<void> {\n const { $step } = (await state.get()) || {}\n if ($step === undefined) throw new Error('Wizard state is not initialized')\n\n return this.goToStep(state, $step + count)\n }\n\n /**\n * Filter that will only pass if the current step is `step`\n */\n // eslint-disable-next-line ts/no-empty-object-type\n static onNthStep(step: number): UpdateFilter<any, {}, WizardInternalState> {\n const filter = filters.state<WizardInternalState>(it => it.$step === step)\n\n if (step === 0) return filters.or(filters.stateEmpty, filter)\n\n return filter\n }\n\n /**\n * Filter that will only pass if the current step is the one after last one added\n */\n // eslint-disable-next-line ts/no-empty-object-type\n onCurrentStep(): UpdateFilter<any, {}, WizardInternalState> {\n return WizardScene.onNthStep(this._steps)\n }\n\n /**\n * Add a step to the wizard\n */\n addStep(\n handler: (\n msg: MessageContext,\n state: UpdateState<State & WizardInternalState>,\n ) => MaybePromise<WizardSceneAction | number>,\n ): void {\n const step = this._steps++\n\n this.onNewMessage(WizardScene.onNthStep(step), async (msg, state) => {\n const result = await handler(msg, state)\n\n if (typeof result === 'number') {\n await this.goToStep(state, result)\n\n return\n }\n\n switch (result) {\n case 'next': {\n await this.goToStep(state, step + 1)\n break\n }\n case 'exit':\n await state.exit()\n break\n }\n })\n }\n}\n"]}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { BusinessCallbackQuery, CallbackQuery, InlineCallbackQuery, MaybeArray, MaybePromise } from '@mtcute/core';
|
|
2
|
-
import type { UpdateFilter } from './filters/types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Callback data builder, inspired by [aiogram](https://github.com/aiogram/aiogram).
|
|
5
|
-
*
|
|
6
|
-
* This can be used to simplify management of different callbacks.
|
|
7
|
-
*
|
|
8
|
-
* [Learn more in the docs](/guide/topics/keyboards.html#callback-data-builders)
|
|
9
|
-
*/
|
|
10
|
-
export declare class CallbackDataBuilder<T extends string> {
|
|
11
|
-
prefix: string;
|
|
12
|
-
private readonly _fields;
|
|
13
|
-
sep: string;
|
|
14
|
-
/**
|
|
15
|
-
* @param prefix Prefix for the data. Use something unique across your bot.
|
|
16
|
-
* @param fields Field names in the order they will be serialized.
|
|
17
|
-
*/
|
|
18
|
-
constructor(prefix: string, ...fields: T[]);
|
|
19
|
-
/**
|
|
20
|
-
* Build a callback data string
|
|
21
|
-
*
|
|
22
|
-
* @param obj Object containing the data
|
|
23
|
-
*/
|
|
24
|
-
build(obj: Record<T, string>): string;
|
|
25
|
-
/**
|
|
26
|
-
* Parse callback data to object
|
|
27
|
-
*
|
|
28
|
-
* @param data Callback data as string
|
|
29
|
-
* @param safe If `true`, will return `null` instead of throwing on invalid data
|
|
30
|
-
*/
|
|
31
|
-
parse(data: string, safe?: false): Record<T, string>;
|
|
32
|
-
parse(data: string, safe: true): Record<T, string> | null;
|
|
33
|
-
/**
|
|
34
|
-
* Create a filter for this callback data.
|
|
35
|
-
*
|
|
36
|
-
* You can either pass an object with field names as keys and values as strings or regexes,
|
|
37
|
-
* which will be compiled to a RegExp, or a function that will be called with the parsed data.
|
|
38
|
-
* Note that the strings will be passed to `RegExp` **directly**, so you may want to escape them.
|
|
39
|
-
*
|
|
40
|
-
* When using a function, you can either return a boolean, or an object with field names as keys
|
|
41
|
-
* and values as strings or regexes. In the latter case, the resulting object will be matched
|
|
42
|
-
* against the parsed data the same way as if you passed it directly.
|
|
43
|
-
*
|
|
44
|
-
* @param params
|
|
45
|
-
*/
|
|
46
|
-
filter<Update extends CallbackQuery | InlineCallbackQuery | BusinessCallbackQuery>(params?: ((upd: Update, parsed: Record<T, string>) => MaybePromise<Partial<Record<T, MaybeArray<string | RegExp>>> | boolean>) | Partial<Record<T, MaybeArray<string | RegExp>>>): UpdateFilter<Update, {
|
|
47
|
-
match: Record<T, string>;
|
|
48
|
-
}>;
|
|
49
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { MtArgumentError, } from '@mtcute/core';
|
|
2
|
-
/**
|
|
3
|
-
* Callback data builder, inspired by [aiogram](https://github.com/aiogram/aiogram).
|
|
4
|
-
*
|
|
5
|
-
* This can be used to simplify management of different callbacks.
|
|
6
|
-
*
|
|
7
|
-
* [Learn more in the docs](/guide/topics/keyboards.html#callback-data-builders)
|
|
8
|
-
*/
|
|
9
|
-
export class CallbackDataBuilder {
|
|
10
|
-
prefix;
|
|
11
|
-
_fields;
|
|
12
|
-
sep = ':';
|
|
13
|
-
/**
|
|
14
|
-
* @param prefix Prefix for the data. Use something unique across your bot.
|
|
15
|
-
* @param fields Field names in the order they will be serialized.
|
|
16
|
-
*/
|
|
17
|
-
constructor(prefix, ...fields) {
|
|
18
|
-
this.prefix = prefix;
|
|
19
|
-
this._fields = fields;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Build a callback data string
|
|
23
|
-
*
|
|
24
|
-
* @param obj Object containing the data
|
|
25
|
-
*/
|
|
26
|
-
build(obj) {
|
|
27
|
-
const ret = this.prefix
|
|
28
|
-
+ this.sep
|
|
29
|
-
+ this._fields
|
|
30
|
-
.map((f) => {
|
|
31
|
-
const val = obj[f];
|
|
32
|
-
if (val.includes(this.sep)) {
|
|
33
|
-
throw new MtArgumentError(`Value for ${f} ${val} contains separator ${this.sep} and cannot be used.`);
|
|
34
|
-
}
|
|
35
|
-
return val;
|
|
36
|
-
})
|
|
37
|
-
.join(this.sep);
|
|
38
|
-
if (ret.length > 64) {
|
|
39
|
-
throw new MtArgumentError('Resulting callback data is too long.');
|
|
40
|
-
}
|
|
41
|
-
return ret;
|
|
42
|
-
}
|
|
43
|
-
parse(data, safe = false) {
|
|
44
|
-
const parts = data.split(this.sep);
|
|
45
|
-
if (parts[0] !== this.prefix) {
|
|
46
|
-
if (safe)
|
|
47
|
-
return null;
|
|
48
|
-
throw new MtArgumentError(`Invalid data passed: "${data}" (bad prefix, expected ${this.prefix}, got ${parts[0]})`);
|
|
49
|
-
}
|
|
50
|
-
if (parts.length !== this._fields.length + 1) {
|
|
51
|
-
if (safe)
|
|
52
|
-
return null;
|
|
53
|
-
throw new MtArgumentError(`Invalid data passed: "${data}" (bad parts count, expected ${this._fields.length}, got ${parts.length - 1})`);
|
|
54
|
-
}
|
|
55
|
-
const ret = {};
|
|
56
|
-
parts.forEach((it, idx) => {
|
|
57
|
-
if (idx === 0)
|
|
58
|
-
return; // skip prefix
|
|
59
|
-
ret[this._fields[idx - 1]] = it;
|
|
60
|
-
});
|
|
61
|
-
return ret;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Create a filter for this callback data.
|
|
65
|
-
*
|
|
66
|
-
* You can either pass an object with field names as keys and values as strings or regexes,
|
|
67
|
-
* which will be compiled to a RegExp, or a function that will be called with the parsed data.
|
|
68
|
-
* Note that the strings will be passed to `RegExp` **directly**, so you may want to escape them.
|
|
69
|
-
*
|
|
70
|
-
* When using a function, you can either return a boolean, or an object with field names as keys
|
|
71
|
-
* and values as strings or regexes. In the latter case, the resulting object will be matched
|
|
72
|
-
* against the parsed data the same way as if you passed it directly.
|
|
73
|
-
*
|
|
74
|
-
* @param params
|
|
75
|
-
*/
|
|
76
|
-
filter(params = {}) {
|
|
77
|
-
if (typeof params === 'function') {
|
|
78
|
-
return async (query) => {
|
|
79
|
-
if (!query.dataStr)
|
|
80
|
-
return false;
|
|
81
|
-
const data = this.parse(query.dataStr, true);
|
|
82
|
-
if (!data)
|
|
83
|
-
return false;
|
|
84
|
-
const fnResult = await params(query, data);
|
|
85
|
-
if (typeof fnResult === 'boolean') {
|
|
86
|
-
query.match = data;
|
|
87
|
-
return fnResult;
|
|
88
|
-
}
|
|
89
|
-
// validate result
|
|
90
|
-
for (const key in fnResult) {
|
|
91
|
-
const value = data[key];
|
|
92
|
-
if (value === undefined)
|
|
93
|
-
return false;
|
|
94
|
-
let matchers = fnResult[key];
|
|
95
|
-
if (!Array.isArray(matchers))
|
|
96
|
-
matchers = [matchers];
|
|
97
|
-
for (const matcher of matchers) {
|
|
98
|
-
if (typeof matcher === 'string') {
|
|
99
|
-
if (value !== matcher)
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
else if (!matcher.test(value)) {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
query.match = data;
|
|
108
|
-
return true;
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
const parts = [];
|
|
112
|
-
this._fields.forEach((field) => {
|
|
113
|
-
if (!(field in params)) {
|
|
114
|
-
parts.push(`[^${this.sep}]*?`);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
const value = params[field];
|
|
118
|
-
if (Array.isArray(value)) {
|
|
119
|
-
parts.push(`(${value.map(i => (typeof i === 'string' ? i : i.source)).join('|')})`);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
// noinspection SuspiciousTypeOfGuard
|
|
123
|
-
parts.push(typeof value === 'string' ? value : value.source);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
const regex = new RegExp(`^${this.prefix}${this.sep}${parts.join(this.sep)}$`);
|
|
127
|
-
return (query) => {
|
|
128
|
-
const m = query.dataStr?.match(regex);
|
|
129
|
-
if (!m) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
query.match = this.parse(m[0]);
|
|
133
|
-
return true;
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
//# sourceMappingURL=callback-data-builder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"callback-data-builder.js","sourceRoot":"","sources":["../../src/callback-data-builder.ts"],"names":[],"mappings":"AAOA,OAAO,EACH,eAAe,GAClB,MAAM,cAAc,CAAA;AAIrB;;;;;;GAMG;AACH,MAAM,OAAO,mBAAmB;IAUjB;IATM,OAAO,CAAK;IAE7B,GAAG,GAAG,GAAG,CAAA;IAET;;;OAGG;IACH,YACW,MAAc,EACrB,GAAG,MAAW;QADP,WAAM,GAAN,MAAM,CAAQ;QAGrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACzB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,GAAsB;QACxB,MAAM,GAAG,GACH,IAAI,CAAC,MAAM;cACX,IAAI,CAAC,GAAG;cACR,IAAI,CAAC,OAAO;iBACT,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;gBACP,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;gBAElB,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;oBACzB,MAAM,IAAI,eAAe,CACrB,aAAa,CAAC,IAAI,GAAG,uBAAuB,IAAI,CAAC,GAAG,sBAAsB,CAC7E,CAAA;gBACL,CAAC;gBAED,OAAO,GAAG,CAAA;YACd,CAAC,CAAC;iBACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAEvB,IAAI,GAAG,CAAC,MAAM,GAAG,EAAE,E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ourcesContent":["import type {\n BusinessCallbackQuery,\n CallbackQuery,\n InlineCallbackQuery,\n MaybeArray,\n MaybePromise,\n} from '@mtcute/core'\nimport {\n MtArgumentError,\n} from '@mtcute/core'\n\nimport type { UpdateFilter } from './filters/types.js'\n\n/**\n * Callback data builder, inspired by [aiogram](https://github.com/aiogram/aiogram).\n *\n * This can be used to simplify management of different callbacks.\n *\n * [Learn more in the docs](/guide/topics/keyboards.html#callback-data-builders)\n */\nexport class CallbackDataBuilder<T extends string> {\n private readonly _fields: T[]\n\n sep = ':'\n\n /**\n * @param prefix Prefix for the data. Use something unique across your bot.\n * @param fields Field names in the order they will be serialized.\n */\n constructor(\n public prefix: string,\n ...fields: T[]\n ) {\n this._fields = fields\n }\n\n /**\n * Build a callback data string\n *\n * @param obj Object containing the data\n */\n build(obj: Record<T, string>): string {\n const ret\n = this.prefix\n + this.sep\n + this._fields\n .map((f) => {\n const val = obj[f]\n\n if (val.includes(this.sep)) {\n throw new MtArgumentError(\n `Value for ${f} ${val} contains separator ${this.sep} and cannot be used.`,\n )\n }\n\n return val\n })\n .join(this.sep)\n\n if (ret.length > 64) {\n throw new MtArgumentError('Resulting callback data is too long.')\n }\n\n return ret\n }\n\n /**\n * Parse callback data to object\n *\n * @param data Callback data as string\n * @param safe If `true`, will return `null` instead of throwing on invalid data\n */\n parse(data: string, safe?: false): Record<T, string>\n parse(data: string, safe: true): Record<T, string> | null\n parse(data: string, safe = false): Record<T, string> | null {\n const parts = data.split(this.sep)\n\n if (parts[0] !== this.prefix) {\n if (safe) return null\n throw new MtArgumentError(\n `Invalid data passed: \"${data}\" (bad prefix, expected ${this.prefix}, got ${parts[0]})`,\n )\n }\n\n if (parts.length !== this._fields.length + 1) {\n if (safe) return null\n throw new MtArgumentError(\n `Invalid data passed: \"${data}\" (bad parts count, expected ${this._fields.length}, got ${\n parts.length - 1\n })`,\n )\n }\n\n const ret = {} as Record<T, string>\n parts.forEach((it, idx) => {\n if (idx === 0) return // skip prefix\n\n ret[this._fields[idx - 1]] = it\n })\n\n return ret\n }\n\n /**\n * Create a filter for this callback data.\n *\n * You can either pass an object with field names as keys and values as strings or regexes,\n * which will be compiled to a RegExp, or a function that will be called with the parsed data.\n * Note that the strings will be passed to `RegExp` **directly**, so you may want to escape them.\n *\n * When using a function, you can either return a boolean, or an object with field names as keys\n * and values as strings or regexes. In the latter case, the resulting object will be matched\n * against the parsed data the same way as if you passed it directly.\n *\n * @param params\n */\n filter<Update extends CallbackQuery | InlineCallbackQuery | BusinessCallbackQuery>(\n params:\n | ((\n upd: Update,\n parsed: Record<T, string>,\n ) => MaybePromise<Partial<Record<T, MaybeArray<string | RegExp>>> | boolean>)\n | Partial<Record<T, MaybeArray<string | RegExp>>> = {},\n ): UpdateFilter<\n Update,\n {\n match: Record<T, string>\n }\n > {\n if (typeof params === 'function') {\n return async (query) => {\n if (!query.dataStr) return false\n\n const data = this.parse(query.dataStr, true)\n if (!data) return false\n\n const fnResult = await params(query, data)\n\n if (typeof fnResult === 'boolean') {\n (\n query as Update & {\n match: Record<T, string>\n }\n ).match = data\n\n return fnResult\n }\n\n // validate result\n for (const key in fnResult) {\n const value = data[key]\n if (value === undefined) return false\n\n let matchers = fnResult[key] as MaybeArray<string | RegExp>\n if (!Array.isArray(matchers)) matchers = [matchers]\n\n for (const matcher of matchers) {\n if (typeof matcher === 'string') {\n if (value !== matcher) return false\n } else if (!matcher.test(value)) {\n return false\n }\n }\n }\n\n (\n query as Update & {\n match: Record<T, string>\n }\n ).match = data\n\n return true\n }\n }\n\n const parts: string[] = []\n\n this._fields.forEach((field) => {\n if (!(field in params)) {\n parts.push(`[^${this.sep}]*?`)\n\n return\n }\n\n const value = params[field]\n\n if (Array.isArray(value)) {\n parts.push(`(${value.map(i => (typeof i === 'string' ? i : i.source)).join('|')})`)\n } else {\n // noinspection SuspiciousTypeOfGuard\n parts.push(typeof value === 'string' ? value : (value as RegExp).source)\n }\n })\n\n const regex = new RegExp(`^${this.prefix}${this.sep}${parts.join(this.sep)}$`)\n\n return (query) => {\n const m = query.dataStr?.match(regex)\n if (!m) {\n return false\n }(\n query as Update & {\n match: Record<T, string>\n }\n ).match = this.parse(m[0])\n\n return true\n }\n }\n}\n"]}
|
package/esm/context/base.js
DELETED
package/esm/context/base.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../src/context/base.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParsedUpdate } from '@mtcute/core'\nimport type { TelegramClient } from '@mtcute/core/client.js'\n\nexport type UpdateContext<T> = T & {\n client: TelegramClient\n _name: Extract<ParsedUpdate, { data: T }>['name']\n}\n\nexport type UpdateContextDistributed<T> = T extends never ? never : UpdateContext<T>\n"]}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { BusinessMessage } from '@mtcute/core';
|
|
2
|
-
/**
|
|
3
|
-
* Context of a business message related update.
|
|
4
|
-
*
|
|
5
|
-
* This is a subclass of {@link BusinessMessage}, so all fields
|
|
6
|
-
* of the message are available.
|
|
7
|
-
*
|
|
8
|
-
* For message groups, own fields are related to the last message
|
|
9
|
-
* in the group. To access all messages, use {@link BusinessMessageContext#messages}.
|
|
10
|
-
*/
|
|
11
|
-
export class BusinessMessageContext extends BusinessMessage {
|
|
12
|
-
client;
|
|
13
|
-
// this is primarily for proper types in filters, so don't bother much with actual value
|
|
14
|
-
_name = 'new_business_message';
|
|
15
|
-
/**
|
|
16
|
-
* List of messages in the message group.
|
|
17
|
-
*
|
|
18
|
-
* For other updates, this is a list with a single element (`this`).
|
|
19
|
-
*/
|
|
20
|
-
messages;
|
|
21
|
-
/** Whether this update is about a message group */
|
|
22
|
-
isMessageGroup;
|
|
23
|
-
constructor(client, message) {
|
|
24
|
-
const msg = Array.isArray(message) ? message[message.length - 1] : message;
|
|
25
|
-
super(msg.update, msg._peers);
|
|
26
|
-
this.client = client;
|
|
27
|
-
this.messages = Array.isArray(message) ? message.map(it => new BusinessMessageContext(client, it)) : [this];
|
|
28
|
-
this.isMessageGroup = Array.isArray(message);
|
|
29
|
-
}
|
|
30
|
-
/** Get all custom emojis contained in this message (message group), if any */
|
|
31
|
-
getCustomEmojis() {
|
|
32
|
-
return this.client.getCustomEmojisFromMessages(this.messages);
|
|
33
|
-
}
|
|
34
|
-
/** Send a text message to the same chat (and topic, if applicable) as a given message */
|
|
35
|
-
answerText(...params) {
|
|
36
|
-
const [send, params_ = {}] = params;
|
|
37
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
38
|
-
return this.client.answerText(this, send, params_);
|
|
39
|
-
}
|
|
40
|
-
/** Send a media to the same chat (and topic, if applicable) as a given message */
|
|
41
|
-
answerMedia(...params) {
|
|
42
|
-
const [send, params_ = {}] = params;
|
|
43
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
44
|
-
return this.client.answerMedia(this, send, params_);
|
|
45
|
-
}
|
|
46
|
-
/** Send a media group to the same chat (and topic, if applicable) as a given message */
|
|
47
|
-
answerMediaGroup(...params) {
|
|
48
|
-
const [send, params_ = {}] = params;
|
|
49
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
50
|
-
return this.client.answerMediaGroup(this, send, params_);
|
|
51
|
-
}
|
|
52
|
-
/** Send a text message in reply to this message */
|
|
53
|
-
replyText(...params) {
|
|
54
|
-
const [send, params_ = {}] = params;
|
|
55
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
56
|
-
return this.client.replyText(this, send, params_);
|
|
57
|
-
}
|
|
58
|
-
/** Send a media in reply to this message */
|
|
59
|
-
replyMedia(...params) {
|
|
60
|
-
const [send, params_ = {}] = params;
|
|
61
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
62
|
-
return this.client.replyMedia(this, send, params_);
|
|
63
|
-
}
|
|
64
|
-
/** Send a media group in reply to this message */
|
|
65
|
-
replyMediaGroup(...params) {
|
|
66
|
-
const [send, params_ = {}] = params;
|
|
67
|
-
params_.businessConnectionId = this.update.connectionId;
|
|
68
|
-
return this.client.replyMediaGroup(this, send, params_);
|
|
69
|
-
}
|
|
70
|
-
/** Send a text message in reply to this message */
|
|
71
|
-
quoteWithText(params) {
|
|
72
|
-
params.businessConnectionId = this.update.connectionId;
|
|
73
|
-
return this.client.quoteWithText(this, params);
|
|
74
|
-
}
|
|
75
|
-
/** Send a media in reply to this message */
|
|
76
|
-
quoteWithMedia(params) {
|
|
77
|
-
params.businessConnectionId = this.update.connectionId;
|
|
78
|
-
return this.client.quoteWithMedia(this, params);
|
|
79
|
-
}
|
|
80
|
-
/** Send a media group in reply to this message */
|
|
81
|
-
quoteWithMediaGroup(params) {
|
|
82
|
-
params.businessConnectionId = this.update.connectionId;
|
|
83
|
-
return this.client.quoteWithMediaGroup(this, params);
|
|
84
|
-
}
|
|
85
|
-
/** Delete this message (message group) */
|
|
86
|
-
delete(params) {
|
|
87
|
-
return this.client.deleteMessagesById(this.chat.inputPeer, this.messages.map(it => it.id), params);
|
|
88
|
-
}
|
|
89
|
-
/** Pin this message */
|
|
90
|
-
pin(params) {
|
|
91
|
-
return this.client.pinMessage({
|
|
92
|
-
chatId: this.chat.inputPeer,
|
|
93
|
-
message: this.id,
|
|
94
|
-
...params,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/** Unpin this message */
|
|
98
|
-
unpin() {
|
|
99
|
-
return this.client.unpinMessage({
|
|
100
|
-
chatId: this.chat.inputPeer,
|
|
101
|
-
message: this.id,
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
/** Edit this message */
|
|
105
|
-
edit(params) {
|
|
106
|
-
return this.client.editMessage({
|
|
107
|
-
chatId: this.chat.inputPeer,
|
|
108
|
-
message: this.id,
|
|
109
|
-
...params,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
/** Forward this message (message group) */
|
|
113
|
-
forwardTo(params) {
|
|
114
|
-
return this.client.forwardMessagesById({
|
|
115
|
-
fromChatId: this.chat.inputPeer,
|
|
116
|
-
messages: this.messages.map(it => it.id),
|
|
117
|
-
...params,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
/** Send a copy of this message (message group) */
|
|
121
|
-
copy(params) {
|
|
122
|
-
if (this.isMessageGroup) {
|
|
123
|
-
return this.client.sendCopyGroup({
|
|
124
|
-
messages: this.messages,
|
|
125
|
-
...params,
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
return this.client.sendCopy({
|
|
129
|
-
message: this,
|
|
130
|
-
...params,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
/** React to this message */
|
|
134
|
-
react(params) {
|
|
135
|
-
return this.client.sendReaction({
|
|
136
|
-
chatId: this.chat.inputPeer,
|
|
137
|
-
message: this.id,
|
|
138
|
-
...params,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
//# sourceMappingURL=business-message.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"business-message.js","sourceRoot":"","sources":["../../../src/context/business-message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAW9C;;;;;;;;GAQG;AACH,MAAM,OAAO,sBAAuB,SAAQ,eAAe;IAe1C;IAdb,wFAAwF;IAC/E,KAAK,GAAG,sBAAsB,CAAA;IAEvC;;;;OAIG;IACM,QAAQ,CAA0B;IAE3C,mDAAmD;IAC1C,cAAc,CAAS;IAEhC,YACa,MAAsB,EAC/B,OAA4C;QAE5C,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAA;QAC1E,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;QAJpB,WAAM,GAAN,MAAM,CAAgB;QAM/B,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,O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AC,SAAS;YAC/B,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;YACxC,GAAG,MAAM;SACZ,CAAC,CAAA;IACN,CAAC;IAED,kDAAkD;IAClD,IAAI,CAAC,MAA4C;QAC7C,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC;gBAC7B,QAAQ,EAAE,IAAI,CAAC,QAAQ;gBACvB,GAAG,MAAM;aACZ,CAAC,CAAA;QACN,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;YACxB,OAAO,EAAE,IAAI;YACb,GAAG,MAAM;SACZ,CAAC,CAAA;IACN,CAAC;IAED,4BAA4B;IAC5B,KAAK,CAAC,MAAyE;QAC3E,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;YAC5B,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,SAAS;YAC3B,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,GAAG,MAAM;SACZ,CAAC,CAAA;IACN,CAAC;CACJ","sourcesContent":["import type { Message, OmitInputMessageId, ParametersSkip1, Sticker } from '@mtcute/core'\nimport { BusinessMessage } from '@mtcute/core'\nimport type { TelegramClient } from '@mtcute/core/client.js'\nimport type {\n DeleteMessagesParams,\n ForwardMessageOptions,\n SendCopyGroupParams,\n SendCopyParams,\n} from '@mtcute/core/methods.js'\n\nimport type { UpdateContext } from './base.js'\n\n/**\n * Context of a business message related update.\n *\n * This is a subclass of {@link BusinessMessage}, so all fields\n * of the message are available.\n *\n * For message groups, own fields are related to the last message\n * in the group. To access all messages, use {@link BusinessMessageContext#messages}.\n */\nexport class BusinessMessageContext extends BusinessMessage implements UpdateContext<BusinessMessage> {\n // this is primarily for proper types in filters, so don't bother much with actual value\n readonly _name = 'new_business_message'\n\n /**\n * List of messages in the message group.\n *\n * For other updates, this is a list with a single element (`this`).\n */\n readonly messages: BusinessMessageContext[]\n\n /** Whether this update is about a message group */\n readonly isMessageGroup: boolean\n\n constructor(\n readonly client: TelegramClient,\n message: BusinessMessage | BusinessMessage[],\n ) {\n const msg = Array.isArray(message) ? message[message.length - 1] : message\n super(msg.update, msg._peers)\n\n this.messages = Array.isArray(message) ? message.map(it => new BusinessMessageContext(client, it)) : [this]\n this.isMessageGroup = Array.isArray(message)\n }\n\n /** Get all custom emojis contained in this message (message group), if any */\n getCustomEmojis(): Promise<Sticker[]> {\n return this.client.getCustomEmojisFromMessages(this.messages)\n }\n\n /** Send a text message to the same chat (and topic, if applicable) as a given message */\n answerText(...params: ParametersSkip1<TelegramClient['answerText']>): Promise<Message> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.answerText(this, send, params_)\n }\n\n /** Send a media to the same chat (and topic, if applicable) as a given message */\n answerMedia(...params: ParametersSkip1<TelegramClient['answerMedia']>): Promise<Message> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.answerMedia(this, send, params_)\n }\n\n /** Send a media group to the same chat (and topic, if applicable) as a given message */\n answerMediaGroup(...params: ParametersSkip1<TelegramClient['answerMediaGroup']>): Promise<Message[]> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.answerMediaGroup(this, send, params_)\n }\n\n /** Send a text message in reply to this message */\n replyText(...params: ParametersSkip1<TelegramClient['replyText']>): Promise<Message> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.replyText(this, send, params_)\n }\n\n /** Send a media in reply to this message */\n replyMedia(...params: ParametersSkip1<TelegramClient['replyMedia']>): Promise<Message> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.replyMedia(this, send, params_)\n }\n\n /** Send a media group in reply to this message */\n replyMediaGroup(...params: ParametersSkip1<TelegramClient['replyMediaGroup']>): Promise<Message[]> {\n const [send, params_ = {}] = params\n params_.businessConnectionId = this.update.connectionId\n\n return this.client.replyMediaGroup(this, send, params_)\n }\n\n /** Send a text message in reply to this message */\n quoteWithText(params: Parameters<TelegramClient['quoteWithText']>[1]): Promise<Message> {\n params.businessConnectionId = this.update.connectionId\n\n return this.client.quoteWithText(this, params)\n }\n\n /** Send a media in reply to this message */\n quoteWithMedia(params: Parameters<TelegramClient['quoteWithMedia']>[1]): Promise<Message> {\n params.businessConnectionId = this.update.connectionId\n\n return this.client.quoteWithMedia(this, params)\n }\n\n /** Send a media group in reply to this message */\n quoteWithMediaGroup(params: Parameters<TelegramClient['quoteWithMediaGroup']>[1]): Promise<Message[]> {\n params.businessConnectionId = this.update.connectionId\n\n return this.client.quoteWithMediaGroup(this, params)\n }\n\n /** Delete this message (message group) */\n delete(params?: DeleteMessagesParams): Promise<void> {\n return this.client.deleteMessagesById(\n this.chat.inputPeer,\n this.messages.map(it => it.id),\n params,\n )\n }\n\n /** Pin this message */\n pin(params?: OmitInputMessageId<Parameters<TelegramClient['pinMessage']>[0]>): Promise<Message | null> {\n return this.client.pinMessage({\n chatId: this.chat.inputPeer,\n message: this.id,\n ...params,\n })\n }\n\n /** Unpin this message */\n unpin(): Promise<void> {\n return this.client.unpinMessage({\n chatId: this.chat.inputPeer,\n message: this.id,\n })\n }\n\n /** Edit this message */\n edit(params: OmitInputMessageId<Parameters<TelegramClient['editMessage']>[0]>): Promise<Message> {\n return this.client.editMessage({\n chatId: this.chat.inputPeer,\n message: this.id,\n ...params,\n })\n }\n\n /** Forward this message (message group) */\n forwardTo(params: ForwardMessageOptions): Promise<Message[]> {\n return this.client.forwardMessagesById({\n fromChatId: this.chat.inputPeer,\n messages: this.messages.map(it => it.id),\n ...params,\n })\n }\n\n /** Send a copy of this message (message group) */\n copy(params: SendCopyParams & SendCopyGroupParams): Promise<Message | Message[]> {\n if (this.isMessageGroup) {\n return this.client.sendCopyGroup({\n messages: this.messages,\n ...params,\n })\n }\n\n return this.client.sendCopy({\n message: this,\n ...params,\n })\n }\n\n /** React to this message */\n react(params: OmitInputMessageId<Parameters<TelegramClient['sendReaction']>[0]>): Promise<Message | null> {\n return this.client.sendReaction({\n chatId: this.chat.inputPeer,\n message: this.id,\n ...params,\n })\n }\n}\n"]}
|