@gramio/scenes
Version:
Scenes plugin for GramIO
279 lines (272 loc) • 12.8 kB
TypeScript
import * as gramio from 'gramio';
import { MaybePromise, ContextType, Bot, ErrorDefinitions, DeriveDefinitions, Composer, AnyPlugin, UpdateName, MaybeArray, Handler, Context, Plugin } from 'gramio';
import { Storage } from '@gramio/storage';
import { NextMiddleware } from 'middleware-io';
/** The Standard Schema interface. */
interface StandardSchemaV1<Input = unknown, Output = Input> {
/** The Standard Schema properties. */
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
}
declare namespace StandardSchemaV1 {
/** The Standard Schema properties interface. */
export interface Props<Input = unknown, Output = Input> {
/** The version number of the standard. */
readonly version: 1;
/** The vendor name of the schema library. */
readonly vendor: string;
/** Validates unknown input values. */
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
/** Inferred types associated with the schema. */
readonly types?: Types<Input, Output> | undefined;
}
/** The result interface of the validate function. */
export type Result<Output> = SuccessResult<Output> | FailureResult;
/** The result interface if validation succeeds. */
export interface SuccessResult<Output> {
/** The typed output value. */
readonly value: Output;
/** The non-existent issues. */
readonly issues?: undefined;
}
/** The result interface if validation fails. */
export interface FailureResult {
/** The issues of failed validation. */
readonly issues: ReadonlyArray<Issue>;
}
/** The issue interface of the failure output. */
export interface Issue {
/** The error message of the issue. */
readonly message: string;
/** The path of the issue, if any. */
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
}
/** The path segment interface of the issue. */
export interface PathSegment {
/** The key representing a path segment. */
readonly key: PropertyKey;
}
/** The Standard Schema types interface. */
export interface Types<Input = unknown, Output = Input> {
/** The input type of the schema. */
readonly input: Input;
/** The output type of the schema. */
readonly output: Output;
}
/** Infers the input type of a Standard Schema. */
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
/** Infers the output type of a Standard Schema. */
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
export { };
}
type Modify<Base, Mod> = Omit<Base, keyof Mod> & Mod;
type StateTypesDefault = Record<string | number, any>;
type UpdateData<T extends StateTypesDefault> = {};
interface ScenesOptions {
storage?: Storage;
}
interface ScenesStorageData<Params = any, State = any> {
name: string;
params: Params;
state: State;
stepId: number;
previousStepId: number;
firstTime: boolean;
}
interface SceneUpdateState {
/**
* @default sceneData.stepId + 1
*/
step?: number;
firstTime?: boolean;
}
type SceneEnterHandler<Scene extends AnyScene = AnyScene> = (scene: Scene, ...args: Scene["~"]["params"] extends never ? [] : [params: Scene["~"]["params"]]) => Promise<void>;
interface EnterExit {
enter: SceneEnterHandler;
exit: () => MaybePromise<boolean>;
}
type SceneStepReturn = {
id: number;
previousId: number;
firstTime: boolean;
go: (stepId: number, firstTime?: boolean) => Promise<void>;
next: () => Promise<void>;
previous: () => Promise<void>;
};
interface InActiveSceneHandlerReturn<Params, State extends StateTypesDefault> extends EnterExit {
state: State;
params: Params;
update: <T extends StateTypesDefault>(state: T, options?: SceneUpdateState) => Promise<UpdateData<T>>;
step: SceneStepReturn;
reenter: () => Promise<void>;
}
interface InUnknownScene<Params, State extends StateTypesDefault, GlobalScene extends AnyScene | null = null> extends InActiveSceneHandlerReturn<Params, State> {
is<Scene extends AnyScene>(scene: Scene): this is InUnknownScene<Scene["~"]["params"], Scene["~"]["state"], Scene>;
}
interface PossibleInUnknownScene<Params, State extends StateTypesDefault, Scene extends AnyScene | null = null> extends EnterExit {
current: Scene extends AnyScene ? InActiveSceneHandlerReturn<Scene["~"]["params"], Partial<Scene["~"]["state"]>> : InUnknownScene<Params, State, Scene> | undefined;
}
type ContextWithFrom = Pick<ContextType<Bot, "message" | "callback_query">, "from">;
declare function getInActiveSceneHandler<Params, State extends StateTypesDefault>(context: ContextWithFrom, storage: Storage, sceneData: ScenesStorageData<Params, State>, scene: AnyScene, key: string, allowedScenes: string[]): InActiveSceneHandlerReturn<Params, State>;
type AnyScene = Scene<any, any, any, any>;
type StepHandler<T, Return = any> = (context: T, next: NextMiddleware) => any;
type SceneDerivesDefinitions<Params, State extends StateTypesDefault> = DeriveDefinitions & {
global: {
scene: ReturnType<typeof getInActiveSceneHandler<Params, State>>;
};
};
declare class Scene<Params = never, Errors extends ErrorDefinitions = {}, State extends StateTypesDefault = Record<string, never>, Derives extends SceneDerivesDefinitions<Params, State> = SceneDerivesDefinitions<Params, State>> {
/** @internal */
"~": {
params: Params;
state: State;
composer: Composer;
};
name: string;
stepsCount: number;
constructor(name: string);
params<SceneParams>(): Scene<SceneParams, Errors, State, Modify<Derives, {
global: {
scene: Modify<Derives["global"]["scene"], {
params: SceneParams;
}>;
};
}>>;
state<StateParams extends StateTypesDefault>(): Scene<Params, Errors, StateParams, Derives & {
global: {
scene: Modify<Derives["global"]["scene"], {
state: StateParams;
}>;
};
}>;
/**
* ! ** At the moment, it can only pick up types** */
extend<NewPlugin extends AnyPlugin>(plugin: MaybePromise<NewPlugin>): Scene<Params, Errors & NewPlugin["_"]["Errors"], State, Derives & NewPlugin["_"]["Derives"]>;
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<Bot, T> & Derives["global"] & Derives[T]>): this;
use(handler: Handler<Context<Bot> & Derives["global"]>): this;
step<T extends UpdateName, Handler extends StepHandler<ContextType<Bot, T> & Derives["global"] & Derives[T], any>, Return = Awaited<ReturnType<Handler>>>(updateName: MaybeArray<T>, handler: Handler): Scene<Params, Errors, Extract<Return, UpdateData<any>> extends UpdateData<infer Type> ? Record<string, never> extends State ? Type : State & Type : State, Modify<Derives, {
global: Modify<Derives["global"], {
scene: Modify<Derives["global"]["scene"], {
state: Extract<Return, UpdateData<any>> extends UpdateData<infer Type> ? Record<string, never> extends State ? Type : State & Type : State;
}>;
}>;
}>>;
step(handler: Handler<Context<Bot> & Derives["global"]>): this;
ask<Key extends string, Schema extends StandardSchemaV1, Return extends StateTypesDefault = {
[key in Key]: StandardSchemaV1.InferOutput<Schema>;
}>(key: Key, validator: Schema, firstTimeMessage: string): Scene<Params, Errors, Record<string, never> extends State ? Return : State & Return, Modify<Derives, {
global: Modify<Derives["global"], {
scene: Modify<Derives["global"]["scene"], {
state: Record<string, never> extends State ? Return : State & Return;
}>;
}>;
}>>;
compose(context: Context<Bot> & {
[key: string]: unknown;
}, onNext?: () => unknown): Promise<void>;
run(context: Context<Bot> & {
[key: string]: unknown;
}, storage: Storage, key: string, data: ScenesStorageData<unknown, unknown>): Promise<void>;
}
interface ScenesDerivesOptions<WithCurrentScene extends boolean = false> extends ScenesOptions {
withCurrentScene?: WithCurrentScene;
scenes?: AnyScene[];
/**
* You should use the same storage for scenes and scenesDerives
*/
storage: Storage;
}
declare function scenesDerives<WithCurrentScene extends boolean = false>(scenesOrOptions: AnyScene[] | ScenesDerivesOptions<WithCurrentScene>, optionsRaw?: ScenesDerivesOptions<WithCurrentScene>): Plugin<{}, gramio.DeriveDefinitions & {
message: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
channel_post: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
inline_query: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
chosen_inline_result: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
callback_query: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
shipping_query: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
pre_checkout_query: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
chat_join_request: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
new_chat_members: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
new_chat_title: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
new_chat_photo: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
delete_chat_photo: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
group_chat_created: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
message_auto_delete_timer_changed: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
migrate_to_chat_id: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
migrate_from_chat_id: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
pinned_message: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
invoice: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
successful_payment: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
chat_shared: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
proximity_alert_triggered: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
video_chat_scheduled: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
video_chat_started: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
video_chat_ended: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
video_chat_participants_invited: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
web_app_data: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
location: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
passport_data: {
scene: WithCurrentScene extends true ? PossibleInUnknownScene<any, any> : EnterExit;
};
}>;
declare function scenes(scenes: AnyScene[], options?: ScenesOptions): Plugin<{}, gramio.DeriveDefinitions & {
message: {
scene: Omit<EnterExit, "exit">;
};
callback_query: {
scene: Omit<EnterExit, "exit">;
};
}>;
export { type AnyScene, type EnterExit, type InActiveSceneHandlerReturn, type InUnknownScene, type Modify, type PossibleInUnknownScene, Scene, type SceneDerivesDefinitions, type SceneEnterHandler, type SceneStepReturn, type SceneUpdateState, type ScenesOptions, type ScenesStorageData, type StateTypesDefault, type StepHandler, type UpdateData, scenes, scenesDerives };