@rilong/grammyjs-conversations-esm 2.0.2

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/out/form.d.ts ADDED
@@ -0,0 +1,530 @@
1
+ import type { Animation, Audio, Contact, Context, Dice, Document, File, Game, Location, MessageEntity, PaidMediaInfo, PhotoSize, Poll, Sticker, Story, Venue, Video, VideoNote, Voice } from "./deps.node.js";
2
+ /**
3
+ * An action to perform after a context object passed form valiation.
4
+ *
5
+ * A form action receives the validated context object as the first argument. It
6
+ * receives the extracted value as a second argument.
7
+ *
8
+ * @param ctx The current context object
9
+ * @param result The value returned by the form field
10
+ * @typeParam C A custom context type
11
+ * @typeParam T The type of form field
12
+ */
13
+ export type FormAction<C extends Context, T> = (ctx: C, result: T) => unknown | Promise<unknown>;
14
+ /**
15
+ * A callback function to be invoked after a context object failed form
16
+ * validation.
17
+ *
18
+ * A form action receives the rejected context object as the first argument.
19
+ *
20
+ * If validation fails and an error is specified, it is possible to use
21
+ * {@link OtherwiseWithReason} instead.
22
+ *
23
+ * @param ctx The current context object
24
+ * @typeParam C A custom context type
25
+ */
26
+ export type Otherwise<C extends Context> = (ctx: C) => unknown | Promise<unknown>;
27
+ /**
28
+ * A callback function to be invoked after a context object failed form
29
+ * validation.
30
+ *
31
+ * A form action receives the rejected context object as the first argument.
32
+ *
33
+ * If validation fails and a reason is specified as an error of the
34
+ * {@link Result} return value of the validation function, the form action
35
+ * receives the error as a second argument. If no reason was specified, use
36
+ * {@link Otherwise} instead.
37
+ *
38
+ * @param ctx The current context object
39
+ * @param reason A reason why validation failed
40
+ * @typeParam C A custom context type
41
+ * @typeParam R A type of reason defined by the validation function
42
+ */
43
+ export type OtherwiseWithReason<C extends Context, R> = (ctx: C, reason: R) => unknown | Promise<unknown>;
44
+ /**
45
+ * Options to pass to a form field. Can be either a {@link FormAction} function
46
+ * or a {@link FormConfig} object.
47
+ *
48
+ * If the validation function is able to provide a reason as to why validation
49
+ * has failed, it is possible use {@link FormOptionsWithReason} instead.
50
+ *
51
+ * @typeParam C A custom context type
52
+ * @typeParam C A form field type
53
+ */
54
+ export type FormOptions<C extends Context, T> = FormAction<C, T> | FormConfig<C, T>;
55
+ /**
56
+ * Options to pass to a form field with an enhanced validation function that can
57
+ * provide a reason as to why validation has failed. Can be either a
58
+ * {@link FormAction} function or a {@link FormConfigWithReason} object.
59
+ *
60
+ * If the validation function does not provide a reason, use {@link FormOptions}
61
+ * instead.
62
+ *
63
+ * @typeParam C A custom context type
64
+ * @typeParam T A type of success value for the validation function
65
+ * @typeParam R A type of error value for the validation function
66
+ */
67
+ export type FormOptionsWithReason<C extends Context, T, R> = FormAction<C, T> | FormConfigWithReason<C, T, R>;
68
+ /**
69
+ * A base options bag object for a form field. This holds all properties that
70
+ * are common among {@link FormConfig} and {@link FormConfigWithReason}.
71
+ *
72
+ * @typeParam C A custom context type
73
+ * @typeParam T A form field type
74
+ */
75
+ export interface FormConfigShared<C extends Context, T> {
76
+ /**
77
+ * Determines whether [the outside middleware
78
+ * system](https://grammy.dev/guide/middleware) should resume after the
79
+ * update is skipped in case that form validation fails.
80
+ *
81
+ * Specify `next: true` to make sure that subsequent handlers will run. This
82
+ * effectively causes `next` to be called by the plugin.
83
+ *
84
+ * Defaults to `false` unless the conversation is marked as parallel, in
85
+ * which case this option defaults to `true`.
86
+ */
87
+ next?: boolean;
88
+ /**
89
+ * Specifies a timeout for the wait call.
90
+ *
91
+ * When the form field is reached, `Date.now()` is called. When the form
92
+ * field resolves, `Date.now()` is called again, and the two values are
93
+ * compared. If the form field resolved more than the specified number of
94
+ * milliseconds after it was reached initially, then the conversation will
95
+ * be halted, any exit handlers will be called, and the surrounding
96
+ * middleware will resume normally so that subsequent handlers can run.
97
+ *
98
+ * To the outside middleware system, this will look like the conversation
99
+ * was never active.
100
+ */
101
+ maxMilliseconds?: number;
102
+ /**
103
+ * Collation key for the wait call, safety measure to protect against data
104
+ * corruption. This is used extensively by the plugin internally, but it is
105
+ * rarely useful to changes this behavior.
106
+ */
107
+ collationKey?: string;
108
+ /**
109
+ * A form action to perform on the context object after the form validation
110
+ * succeeds for a context object, and before the respective value is
111
+ * returned form the form field.
112
+ */
113
+ action?: FormAction<C, T>;
114
+ }
115
+ /**
116
+ * An options bag object for a form field. Contains all properties of
117
+ * {@link FormConfigShared} as well as an `otherwise` function to be invoked
118
+ * when form valiation fails.
119
+ *
120
+ * If a reason is specified by the form validation, it is possible to use
121
+ * {@link FormConfigWithReason} instead.
122
+ *
123
+ * @typeParam C A custom context type
124
+ * @typeParam T A form field type
125
+ */
126
+ export interface FormConfig<C extends Context, T> extends FormConfigShared<C, T> {
127
+ /**
128
+ * Callback that will be invoked when the form validation fails for a
129
+ * context object.
130
+ */
131
+ otherwise?: Otherwise<C>;
132
+ }
133
+ /**
134
+ * An options bag object for a form field. Contains all properties of
135
+ * {@link FormConfigShared} as well as an `otherwise` function to be invoked
136
+ * when form valiation fails with a known reason.
137
+ *
138
+ * If no reason is specified by the form validation, use {@link FormConfig}
139
+ * instead.
140
+ *
141
+ * @typeParam C A custom context type
142
+ * @typeParam T A type of success value for the validation function
143
+ * @typeParam R A type of error value for the validation function
144
+ */
145
+ export interface FormConfigWithReason<C extends Context, T, R> extends FormConfigShared<C, T> {
146
+ /**
147
+ * Callback that will be invoked when the form validation fails with a
148
+ * reason for a context object.
149
+ */
150
+ otherwise?: OtherwiseWithReason<C, R>;
151
+ }
152
+ /** A value that may be absent */
153
+ export type Maybe<T> = {
154
+ ok: false;
155
+ } | {
156
+ ok: true;
157
+ value: T;
158
+ };
159
+ /** A value or an error */
160
+ export type Result<T, E> = {
161
+ ok: false;
162
+ error: E;
163
+ } | {
164
+ ok: true;
165
+ value: T;
166
+ };
167
+ /**
168
+ * An object that fully specifies how to build a form field.
169
+ *
170
+ * Contains as properties of {@link FormConfig} as well as a function that can
171
+ * validate context objects.
172
+ *
173
+ * If the validation function can provide a reason as to why validation has
174
+ * failed, it is possible to use {@link FormBuilderWithReason} instead.
175
+ *
176
+ * @typeParam C A custom context type
177
+ * @typeParam T The type of value this form field returns
178
+ */
179
+ export interface FormBuilder<C extends Context, T> extends FormConfig<C, T> {
180
+ /**
181
+ * A function that validates a given context.
182
+ *
183
+ * When validation succeeds, a value can be extracted from the context
184
+ * object. The result of the validation as well as the data extraction needs
185
+ * to be encoded using a {@link Maybe} type.
186
+ *
187
+ * @param ctx A context object to validate
188
+ */
189
+ validate(ctx: C): Maybe<T> | Promise<Maybe<T>>;
190
+ }
191
+ /**
192
+ * An object that fully specifies how to build a form field.
193
+ *
194
+ * Contains as properties of {@link FormConfigWithReason} as well as a function
195
+ * that can validate context objects. If a context object does not pass
196
+ * validation, the validation function is able to provide a reason.
197
+ *
198
+ * If the validation function cannot provide reason as to why validation has
199
+ * failed, it is possible to use {@link FormBuilder} instead.
200
+ *
201
+ * @typeParam C A custom context type
202
+ * @typeParam T A type of success value for the validation function
203
+ * @typeParam R A type of error value for the validation function
204
+ */
205
+ export interface FormBuilderWithReason<C extends Context, T, R> extends FormConfigWithReason<C, T, R> {
206
+ /**
207
+ * A function that validates a given context.
208
+ *
209
+ * When validation succeeds, a value can be extracted from the context
210
+ * object. When validation fails, an error can be generated. The result of
211
+ * the validation needs to be encoded using a {@link Result} type.
212
+ *
213
+ * @param ctx A context object to validate
214
+ */
215
+ validate(ctx: C): Result<T, R> | Promise<Result<T, R>>;
216
+ }
217
+ /**
218
+ * A container for form building utilities.
219
+ *
220
+ * Each method on this class represents a differnt type of form field which can
221
+ * validate context objects and extract data from it.
222
+ */
223
+ export declare class ConversationForm<C extends Context> {
224
+ private readonly conversation;
225
+ /** Constructs a new form based on wait and skip callbacks */
226
+ constructor(conversation: {
227
+ wait: (opts: {
228
+ maxMilliseconds?: number;
229
+ collationKey?: string;
230
+ }) => Promise<C>;
231
+ skip: (opts: {
232
+ next?: boolean;
233
+ }) => Promise<never>;
234
+ });
235
+ /**
236
+ * Generic form field that can be used to build any other type of form
237
+ * field. This is heavily used internally.
238
+ *
239
+ * Most likely, you will not need this because there is a more convenient
240
+ * option. However, you can still use it if the type of form field you need
241
+ * is not supported out of the box.
242
+ *
243
+ * @param builder A form field definition object
244
+ * @typeParam T A type of value to be extracted from the form field
245
+ * @typeParam R A type of reason to be specified if validation fails
246
+ */
247
+ build<T, R>(builder: FormBuilderWithReason<C, T, R>): Promise<T>;
248
+ build<T, R>(builder: FormBuilder<C, T>): Promise<T>;
249
+ /**
250
+ * Form field that checks if the incoming update contains a message or
251
+ * channel post with text, and returns this text as string. Does not check
252
+ * for captions.
253
+ *
254
+ * Accepts an optional options object that lets you perform actions when
255
+ * text is received, when a non-text update is received, and more.
256
+ *
257
+ * @param options Optional options
258
+ */
259
+ text(options?: FormOptions<C, string>): Promise<string>;
260
+ /**
261
+ * Form field that checks if the incoming update contains a message or
262
+ * channel post with text that can be parsed to a number, and returns this
263
+ * number. Does not check captions.
264
+ *
265
+ * The conversion to number uses `parseFloat`.
266
+ *
267
+ * Accepts an optional options object that lets you perform actions when a
268
+ * number is received, when a non-number update is received, and more.
269
+ *
270
+ * @param options Optional options
271
+ */
272
+ number(options?: FormOptions<C, number>): Promise<number>;
273
+ /**
274
+ * Form field that checks if the incoming update contains a message or
275
+ * channel post with text that can be parsed to an integer, and returns this
276
+ * integer as a `number`. Does not check for captions.
277
+ *
278
+ * The conversion to number uses `parseInt`.
279
+ *
280
+ * Accepts an optional options object that lets you specify the radix to use
281
+ * as well as perform actions when a number is received, when a non-number
282
+ * update is received, and more.
283
+ *
284
+ * @param options Optional options
285
+ */
286
+ int(options?: FormOptions<C, number> & {
287
+ /** The radix to use for parsing the integer */
288
+ radix?: number;
289
+ }): Promise<number>;
290
+ /**
291
+ * Form field that checks if the incoming update contains a message or
292
+ * channel post with one of several predefined strings, and returns the
293
+ * actual text as string. Does not check captions.
294
+ *
295
+ * This is especially useful when working with custom keyboards.
296
+ *
297
+ * ```ts
298
+ * const keyboard = new Keyboard()
299
+ * .text("A").text("B")
300
+ * .text("C").text("D")
301
+ * .oneTime()
302
+ * await ctx.reply("A, B, C, or D?", { reply_markup: keyboard })
303
+ * const answer = await conversation.form.select(["A", "B", "C", "D"], {
304
+ * otherwise: ctx => ctx.reply("Please use one of the buttons!")
305
+ * })
306
+ * switch (answer) {
307
+ * case "A":
308
+ * case "B":
309
+ * case "C":
310
+ * case "D":
311
+ * // ...
312
+ * }
313
+ * ```
314
+ *
315
+ * Accepts an optional options object that lets you perform actions when
316
+ * text is received, when a non-text update is received, and more.
317
+ *
318
+ * @param entries A string array of accepted values
319
+ * @param options Optional options
320
+ */
321
+ select<E extends string>(entries: E[], options?: FormOptions<C, E>): Promise<E>;
322
+ /**
323
+ * Form field that checks if the incoming update contains a message or
324
+ * channel post with a given type of message entity, and returns this
325
+ * entity. The form field relies on `ctx.entities()` for data extraction, so
326
+ * both texts and captions are checked.
327
+ *
328
+ * Accepts an optional options object that lets you perform actions when
329
+ * text is received, when a non-text update is received, and more.
330
+ *
331
+ * @param type One or more types of message entities to accept
332
+ * @param options Optional options
333
+ */
334
+ entity<M extends MessageEntity>(type: M["type"] | M["type"][], options?: FormOptions<C, MessageEntity & {
335
+ text: string;
336
+ }>): Promise<MessageEntity & {
337
+ text: string;
338
+ }>;
339
+ /**
340
+ * Form field that checks if the incoming update contains a message or
341
+ * channel post with an animation, and returns the received animation
342
+ * object.
343
+ *
344
+ * Accepts an optional options object that lets you perform actions when an
345
+ * animation is received, when a non-animation update is received, and more.
346
+ *
347
+ * @param options Optional options
348
+ */
349
+ animation(options?: FormOptions<C, Animation>): Promise<Animation>;
350
+ /**
351
+ * Form field that checks if the incoming update contains a message or
352
+ * channel post with an audio message, and returns the received audio
353
+ * object.
354
+ *
355
+ * Accepts an optional options object that lets you perform actions when an
356
+ * audio message is received, when a non-audio update is received, and more.
357
+ *
358
+ * @param options Optional options
359
+ */
360
+ audio(options?: FormOptions<C, Audio>): Promise<Audio>;
361
+ /**
362
+ * Form field that checks if the incoming update contains a message or
363
+ * channel post with a document message, and returns the received document
364
+ * object.
365
+ *
366
+ * Accepts an optional options object that lets you perform actions when a
367
+ * document message is received, when a non-document update is received, and
368
+ * more.
369
+ *
370
+ * @param options Optional options
371
+ */
372
+ document(options?: FormOptions<C, Document>): Promise<Document>;
373
+ /**
374
+ * Form field that checks if the incoming update contains a message or
375
+ * channel post with paid media, and returns the received paid media object.
376
+ *
377
+ * Accepts an optional options object that lets you perform actions when a
378
+ * paid media message is received, when a non-paid media update is received,
379
+ * and more.
380
+ *
381
+ * @param options Optional options
382
+ */
383
+ paidMedia(options?: FormOptions<C, PaidMediaInfo>): Promise<PaidMediaInfo>;
384
+ /**
385
+ * Form field that checks if the incoming update contains a message or
386
+ * channel post with a photo, and returns the received array of `PhotoSize`
387
+ * objects.
388
+ *
389
+ * Accepts an optional options object that lets you perform actions when a
390
+ * photo is received, when a non-photo update is received, and more.
391
+ *
392
+ * @param options Optional options
393
+ */
394
+ photo(options?: FormOptions<C, PhotoSize[]>): Promise<PhotoSize[]>;
395
+ /**
396
+ * Form field that checks if the incoming update contains a message or
397
+ * channel post with a sticker, and returns the received sticker object.
398
+ *
399
+ * Accepts an optional options object that lets you perform actions when a
400
+ * sticker is received, when a non-sticker update is received, and more.
401
+ *
402
+ * @param options Optional options
403
+ */
404
+ sticker(options?: FormOptions<C, Sticker>): Promise<Sticker>;
405
+ /**
406
+ * Form field that checks if the incoming update contains a message or
407
+ * channel post with a story, and returns the received story object.
408
+ *
409
+ * Accepts an optional options object that lets you perform actions when a
410
+ * story is received, when a non-story update is received, and more.
411
+ *
412
+ * @param options Optional options
413
+ */
414
+ story(options?: FormOptions<C, Story>): Promise<Story>;
415
+ /**
416
+ * Form field that checks if the incoming update contains a message or
417
+ * channel post with a video, and returns the received video object.
418
+ *
419
+ * Accepts an optional options object that lets you perform actions when a
420
+ * video is received, when a non-video update is received, and more.
421
+ *
422
+ * @param options Optional options
423
+ */
424
+ video(options?: FormOptions<C, Video>): Promise<Video>;
425
+ /**
426
+ * Form field that checks if the incoming update contains a message or
427
+ * channel post with a video note, and returns the received video note
428
+ * object.
429
+ *
430
+ * Accepts an optional options object that lets you perform actions when a
431
+ * video note is received, when a non-video note update is received, and
432
+ * more.
433
+ *
434
+ * @param options Optional options
435
+ */
436
+ video_note(options?: FormOptions<C, VideoNote>): Promise<VideoNote>;
437
+ /**
438
+ * Form field that checks if the incoming update contains a message or
439
+ * channel post with a voice message, and returns the received voice object.
440
+ *
441
+ * Accepts an optional options object that lets you perform actions when a
442
+ * voice message is received, when a non-voice message update is received,
443
+ * and more.
444
+ *
445
+ * @param options Optional options
446
+ */
447
+ voice(options?: FormOptions<C, Voice>): Promise<Voice>;
448
+ /**
449
+ * Form field that checks if the incoming update contains a message or
450
+ * channel post with a contact, and returns the received contact object.
451
+ *
452
+ * Accepts an optional options object that lets you perform actions when a
453
+ * contact is received, when a non-contact update is received, and more.
454
+ *
455
+ * @param options Optional options
456
+ */
457
+ contact(options?: FormOptions<C, Contact>): Promise<Contact>;
458
+ /**
459
+ * Form field that checks if the incoming update contains a message or
460
+ * channel post with dice, and returns the received dice object.
461
+ *
462
+ * Accepts an optional options object that lets you perform actions when
463
+ * dice are received, when a non-dice update is received, and more.
464
+ *
465
+ * @param options Optional options
466
+ */
467
+ dice(options?: FormOptions<C, Dice>): Promise<Dice>;
468
+ /**
469
+ * Form field that checks if the incoming update contains a message or
470
+ * channel post with a game, and returns the received game object.
471
+ *
472
+ * Accepts an optional options object that lets you perform actions when a
473
+ * game is received, when a non-game update is received, and more.
474
+ *
475
+ * @param options Optional options
476
+ */
477
+ game(options?: FormOptions<C, Game>): Promise<Game>;
478
+ /**
479
+ * Form field that checks if the incoming update contains a message or
480
+ * channel post with a poll, and returns the received poll object.
481
+ *
482
+ * Accepts an optional options object that lets you perform actions when a
483
+ * poll is received, when a non-poll update is received, and more.
484
+ *
485
+ * @param options Optional options
486
+ */
487
+ poll(options?: FormOptions<C, Poll>): Promise<Poll>;
488
+ /**
489
+ * Form field that checks if the incoming update contains a message or
490
+ * channel post with a venue, and returns the received venue object.
491
+ *
492
+ * Accepts an optional options object that lets you perform actions when a
493
+ * venue is received, when a non-venue update is received, and more.
494
+ *
495
+ * @param options Optional options
496
+ */
497
+ venue(options?: FormOptions<C, Venue>): Promise<Venue>;
498
+ /**
499
+ * Form field that checks if the incoming update contains a message or
500
+ * channel post with a location, and returns the received location object.
501
+ *
502
+ * Accepts an optional options object that lets you perform actions when a
503
+ * location is received, when a non-location update is received, and more.
504
+ *
505
+ * @param options Optional options
506
+ */
507
+ location(options?: FormOptions<C, Location>): Promise<Location>;
508
+ /**
509
+ * Form field that checks if the incoming update contains a message or
510
+ * channel post with a photo or video, and returns the received media
511
+ * object.
512
+ *
513
+ * Accepts an optional options object that lets you perform actions when a
514
+ * media is received, when a non-media update is received, and more.
515
+ *
516
+ * @param options Optional options
517
+ */
518
+ media(options?: FormOptions<C, PhotoSize[] | Video>): Promise<PhotoSize[] | Video>;
519
+ /**
520
+ * Form field that checks if the incoming update contains a message or
521
+ * channel post with a file, calls `await ctx.getFile()`, and returns the
522
+ * received file object.
523
+ *
524
+ * Accepts an optional options object that lets you perform actions when a
525
+ * file is received, when a non-file update is received, and more.
526
+ *
527
+ * @param options Optional options
528
+ */
529
+ file(options?: FormOptions<C, File>): Promise<File>;
530
+ }