UNPKG

11.7 kBTypeScriptView Raw
1/** @format */
2import * as tg from './core/types/typegram';
3import * as tt from './telegram-types';
4import { Middleware, MiddlewareFn, MiddlewareObj } from './middleware';
5import Context from './context';
6export declare type MaybeArray<T> = T | T[];
7export declare type MaybePromise<T> = T | Promise<T>;
8export declare type NonemptyReadonlyArray<T> = readonly [T, ...T[]];
9export declare type Triggers<C> = MaybeArray<string | RegExp | ((value: string, ctx: C) => RegExpExecArray | null)>;
10export declare type Predicate<T> = (t: T) => boolean;
11export declare type AsyncPredicate<T> = (t: T) => Promise<boolean>;
12export declare type MatchedMiddleware<C extends Context, T extends tt.UpdateType | tt.MessageSubType = 'message' | 'channel_post'> = NonemptyReadonlyArray<Middleware<MatchedContext<C & {
13 match: RegExpExecArray;
14}, T>>>;
15/** Takes: a context type and an update type (or message subtype).
16 Produces: a context that has some properties required, and some undefined.
17 The required ones are those that are always present when the given update (or message) arrives.
18 The undefined ones are those that are always absent when the given update (or message) arrives. */
19/** @deprecated */
20declare type MatchedContext<C extends Context, T extends tt.UpdateType | tt.MessageSubType> = NarrowedContext<C, tt.MountMap[T]>;
21/**
22 * Narrows down `C['update']` (and derived getters)
23 * to specific update type `U`.
24 *
25 * Used by [[`Composer`]],
26 * possibly useful for splitting a bot into multiple files.
27 */
28export declare type NarrowedContext<C extends Context, U extends tg.Update> = Context<U> & Omit<C, keyof Context>;
29export interface GameQueryUpdate extends tg.Update.CallbackQueryUpdate {
30 callback_query: tg.CallbackQuery.GameShortGameCallbackQuery;
31}
32export declare class Composer<C extends Context> implements MiddlewareObj<C> {
33 private handler;
34 constructor(...fns: ReadonlyArray<Middleware<C>>);
35 /**
36 * Registers a middleware.
37 */
38 use(...fns: ReadonlyArray<Middleware<C>>): this;
39 /**
40 * Registers middleware for handling updates
41 * matching given type guard function.
42 */
43 guard<U extends tg.Update>(guardFn: (update: tg.Update) => update is U, ...fns: NonemptyReadonlyArray<Middleware<NarrowedContext<C, U>>>): this;
44 /**
45 * Registers middleware for handling provided update types.
46 */
47 on<T extends tt.UpdateType | tt.MessageSubType>(updateType: MaybeArray<T>, ...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, T>>>): this;
48 /**
49 * Registers middleware for handling matching text messages.
50 */
51 hears(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'text'>): this;
52 /**
53 * Registers middleware for handling specified commands.
54 */
55 command(command: MaybeArray<string>, ...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, 'text'>>>): this;
56 /**
57 * Registers middleware for handling matching callback queries.
58 */
59 action(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'callback_query'>): this;
60 /**
61 * Registers middleware for handling matching inline queries.
62 */
63 inlineQuery(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'inline_query'>): this;
64 /**
65 * Registers middleware for handling game queries
66 */
67 gameQuery(...fns: NonemptyReadonlyArray<Middleware<NarrowedContext<C, GameQueryUpdate>>>): this;
68 /**
69 * Registers middleware for dropping matching updates.
70 */
71 drop(predicate: Predicate<C>): this;
72 filter(predicate: Predicate<C>): this;
73 private entity;
74 email(email: Triggers<C>, ...fns: MatchedMiddleware<C>): this;
75 url(url: Triggers<C>, ...fns: MatchedMiddleware<C>): this;
76 textLink(link: Triggers<C>, ...fns: MatchedMiddleware<C>): this;
77 textMention(mention: Triggers<C>, ...fns: MatchedMiddleware<C>): this;
78 mention(mention: MaybeArray<string>, ...fns: MatchedMiddleware<C>): this;
79 phone(number: Triggers<C>, ...fns: MatchedMiddleware<C>): this;
80 hashtag(hashtag: MaybeArray<string>, ...fns: MatchedMiddleware<C>): this;
81 cashtag(cashtag: MaybeArray<string>, ...fns: MatchedMiddleware<C>): this;
82 /**
83 * Registers a middleware for handling /start
84 */
85 start(...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, 'text'> & {
86 startPayload: string;
87 }>>): this;
88 /**
89 * Registers a middleware for handling /help
90 */
91 help(...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, 'text'>>>): this;
92 /**
93 * Registers a middleware for handling /settings
94 */
95 settings(...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, 'text'>>>): this;
96 middleware(): MiddlewareFn<C>;
97 static reply(...args: Parameters<Context['reply']>): MiddlewareFn<Context>;
98 static catch<C extends Context>(errorHandler: (err: unknown, ctx: C) => void, ...fns: ReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
99 /**
100 * Generates middleware that runs in the background.
101 */
102 static fork<C extends Context>(middleware: Middleware<C>): MiddlewareFn<C>;
103 static tap<C extends Context>(middleware: Middleware<C>): MiddlewareFn<C>;
104 /**
105 * Generates middleware that gives up control to the next middleware.
106 */
107 static passThru(): MiddlewareFn<Context>;
108 static lazy<C extends Context>(factoryFn: (ctx: C) => MaybePromise<Middleware<C>>): MiddlewareFn<C>;
109 static log(logFn?: (s: string) => void): MiddlewareFn<Context>;
110 /**
111 * @param trueMiddleware middleware to run if the predicate returns true
112 * @param falseMiddleware middleware to run if the predicate returns false
113 */
114 static branch<C extends Context>(predicate: Predicate<C> | AsyncPredicate<C>, trueMiddleware: Middleware<C>, falseMiddleware: Middleware<C>): MiddlewareFn<C>;
115 /**
116 * Generates optional middleware.
117 * @param predicate predicate to decide on a context object whether to run the middleware
118 * @param middleware middleware to run if the predicate returns true
119 */
120 static optional<C extends Context>(predicate: Predicate<C> | AsyncPredicate<C>, ...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
121 static filter<C extends Context>(predicate: Predicate<C>): MiddlewareFn<C>;
122 /**
123 * Generates middleware for dropping matching updates.
124 */
125 static drop<C extends Context>(predicate: Predicate<C>): MiddlewareFn<C>;
126 static dispatch<C extends Context, Handlers extends Record<string | number | symbol, Middleware<C>>>(routeFn: (ctx: C) => MaybePromise<keyof Handlers>, handlers: Handlers): Middleware<C>;
127 /**
128 * Generates optional middleware based on a predicate that only operates on `ctx.update`.
129 *
130 * Example:
131 * ```ts
132 * import { Composer, Update } from 'telegraf'
133 *
134 * const predicate = (u): u is Update.MessageUpdate => 'message' in u
135 * const middleware = Composer.guard(predicate, (ctx) => {
136 * const message = ctx.update.message
137 * })
138 * ```
139 *
140 * Note that `Composer.mount('message')` is preferred over this.
141 *
142 * @param guardFn predicate to decide whether to run the middleware based on the `ctx.update` object
143 * @param fns middleware to run if the predicate returns true
144 * @see `Composer.optional` for a more generic version of this method that allows the predicate to operate on `ctx` itself
145 */
146 static guard<C extends Context, U extends tg.Update>(guardFn: (u: tg.Update) => u is U, ...fns: NonemptyReadonlyArray<Middleware<NarrowedContext<C, U>>>): MiddlewareFn<C>;
147 /**
148 * Generates middleware for handling provided update types.
149 * @deprecated use `Composer.on`
150 */
151 static mount<C extends Context, T extends tt.UpdateType | tt.MessageSubType>(updateType: MaybeArray<T>, ...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, T>>>): MiddlewareFn<C>;
152 /**
153 * Generates middleware for handling provided update types.
154 */
155 static on<C extends Context, T extends tt.UpdateType | tt.MessageSubType>(updateType: MaybeArray<T>, ...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, T>>>): MiddlewareFn<C>;
156 private static entity;
157 static entityText<C extends Context>(entityType: MaybeArray<string>, predicate: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
158 static email<C extends Context>(email: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
159 static phone<C extends Context>(number: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
160 static url<C extends Context>(url: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
161 static textLink<C extends Context>(link: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
162 static textMention<C extends Context>(mention: Triggers<C>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
163 static mention<C extends Context>(mention: MaybeArray<string>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
164 static hashtag<C extends Context>(hashtag: MaybeArray<string>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
165 static cashtag<C extends Context>(cashtag: MaybeArray<string>, ...fns: MatchedMiddleware<C>): MiddlewareFn<C>;
166 private static match;
167 /**
168 * Generates middleware for handling matching text messages.
169 */
170 static hears<C extends Context>(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'text'>): MiddlewareFn<C>;
171 /**
172 * Generates middleware for handling specified commands.
173 */
174 static command<C extends Context>(command: MaybeArray<string>, ...fns: NonemptyReadonlyArray<Middleware<MatchedContext<C, 'text'>>>): MiddlewareFn<C>;
175 /**
176 * Generates middleware for handling matching callback queries.
177 */
178 static action<C extends Context>(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'callback_query'>): MiddlewareFn<C>;
179 /**
180 * Generates middleware for handling matching inline queries.
181 */
182 static inlineQuery<C extends Context>(triggers: Triggers<C>, ...fns: MatchedMiddleware<C, 'inline_query'>): MiddlewareFn<C>;
183 /**
184 * Generates middleware responding only to specified users.
185 */
186 static acl<C extends Context>(userId: MaybeArray<number>, ...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
187 private static memberStatus;
188 /**
189 * Generates middleware responding only to chat admins and chat creator.
190 */
191 static admin<C extends Context>(...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
192 /**
193 * Generates middleware responding only to chat creator.
194 */
195 static creator<C extends Context>(...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
196 /**
197 * Generates middleware running only in specified chat types.
198 */
199 static chatType<C extends Context>(type: MaybeArray<tg.Chat['type']>, ...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
200 /**
201 * Generates middleware running only in private chats.
202 */
203 static privateChat<C extends Context>(...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
204 /**
205 * Generates middleware running only in groups and supergroups.
206 */
207 static groupChat<C extends Context>(...fns: NonemptyReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
208 /**
209 * Generates middleware for handling game queries.
210 */
211 static gameQuery<C extends Context>(...fns: NonemptyReadonlyArray<Middleware<NarrowedContext<C, GameQueryUpdate>>>): MiddlewareFn<C>;
212 static unwrap<C extends Context>(handler: Middleware<C>): MiddlewareFn<C>;
213 static compose<C extends Context>(middlewares: ReadonlyArray<Middleware<C>>): MiddlewareFn<C>;
214}
215export default Composer;
216//# sourceMappingURL=composer.d.ts.map
\No newline at end of file