deno.land / x / grammy@v1.22.4 / convenience / inline_query.ts

inline_query.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import { type InlineQueryResult, type InlineQueryResultArticle, type InlineQueryResultAudio, type InlineQueryResultCachedAudio, type InlineQueryResultCachedDocument, type InlineQueryResultCachedGif, type InlineQueryResultCachedMpeg4Gif, type InlineQueryResultCachedPhoto, type InlineQueryResultCachedSticker, type InlineQueryResultCachedVideo, type InlineQueryResultCachedVoice, type InlineQueryResultContact, type InlineQueryResultDocument, type InlineQueryResultGame, type InlineQueryResultGif, type InlineQueryResultLocation, type InlineQueryResultMpeg4Gif, type InlineQueryResultPhoto, type InlineQueryResultVenue, type InlineQueryResultVideo, type InlineQueryResultVoice, type InputContactMessageContent, type InputInvoiceMessageContent, type InputLocationMessageContent, type InputTextMessageContent, type InputVenueMessageContent, type LabeledPrice,} from "../types.ts";
type InlineQueryResultOptions<T, K extends keyof T> = Omit< T, "type" | "id" | "input_message_content" | K>;
type OptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? K : never };type OptionalFields<T> = Pick<T, OptionalKeys<T>[keyof T]>;
function inputMessage<R extends InlineQueryResult>(queryTemplate: R) { return { ...queryTemplate, ...inputMessageMethods<R>(queryTemplate), };}function inputMessageMethods<R extends InlineQueryResult>( queryTemplate: Omit<R, "input_message_content">,) { return { text( message_text: string, options: OptionalFields<InputTextMessageContent> = {}, ) { const content: InputTextMessageContent = { message_text, ...options, }; return { ...queryTemplate, input_message_content: content } as R; }, location( latitude: number, longitude: number, options: OptionalFields<InputLocationMessageContent> = {}, ) { const content: InputLocationMessageContent = { latitude, longitude, ...options, }; return { ...queryTemplate, input_message_content: content } as R; }, venue( title: string, latitude: number, longitude: number, address: string, options: OptionalFields<InputVenueMessageContent>, ) { const content: InputVenueMessageContent = { title, latitude, longitude, address, ...options, }; return { ...queryTemplate, input_message_content: content } as R; }, contact( first_name: string, phone_number: string, options: OptionalFields<InputContactMessageContent> = {}, ) { const content: InputContactMessageContent = { first_name, phone_number, ...options, }; return { ...queryTemplate, input_message_content: content } as R; }, invoice( title: string, description: string, payload: string, provider_token: string, currency: string, prices: LabeledPrice[], options: OptionalFields<InputInvoiceMessageContent> = {}, ) { const content: InputInvoiceMessageContent = { title, description, payload, provider_token, currency, prices, ...options, }; return { ...queryTemplate, input_message_content: content } as R; }, };}
/** * Holds a number of helper methods for building `InlineQueryResult*` objects. * * For example, letting the user pick one out of three photos can be done like * this. * * ```ts * const results = [ * InlineQueryResultBuilder.photo('id0', 'https://grammy.dev/images/Y.png'), * InlineQueryResultBuilder.photo('id1', 'https://grammy.dev/images/Y.png'), * InlineQueryResultBuilder.photo('id2', 'https://grammy.dev/images/Y.png'), * ]; * await ctx.answerInlineQuery(results) * ``` * * If you want the message content to be different from the content in the * inline query result, you can perform another method call on the resulting * objects. * * ```ts * const results = [ * InlineQueryResultBuilder.photo("id0", "https://grammy.dev/images/Y.png") * .text("Picked photo 0!"), * InlineQueryResultBuilder.photo("id1", "https://grammy.dev/images/Y.png") * .text("Picked photo 1!"), * InlineQueryResultBuilder.photo("id2", "https://grammy.dev/images/Y.png") * .text("Picked photo 2!"), * ]; * await ctx.answerInlineQuery(results) * ``` * * Be sure to check the * [documentation](https://core.telegram.org/bots/api#inline-mode) on inline * mode. */export const InlineQueryResultBuilder = { /** * Builds an InlineQueryResultArticle object as specified by * https://core.telegram.org/bots/api#inlinequeryresultarticle. Requires you * to specify the actual message content by calling another function on the * object returned from this method. * * @param id Unique identifier for this result, 1-64 Bytes * @param title Title of the result * @param options Remaining options */ article( id: string, title: string, options: InlineQueryResultOptions< InlineQueryResultArticle, "title" > = {}, ) { return inputMessageMethods<InlineQueryResultArticle>( { type: "article", id, title, ...options }, ); }, /** * Builds an InlineQueryResultAudio object as specified by * https://core.telegram.org/bots/api#inlinequeryresultaudio. * * @param id Unique identifier for this result, 1-64 bytes * @param title Title * @param audio_url A valid URL for the audio file * @param options Remaining options */ audio( id: string, title: string, audio_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultAudio, "title" | "audio_url" > = {}, ) { return inputMessage<InlineQueryResultAudio>({ type: "audio", id, title, audio_url: typeof audio_url === "string" ? audio_url : audio_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedAudio object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedaudio. * * @param id Unique identifier for this result, 1-64 bytes * @param audio_file_id A valid file identifier for the audio file * @param options Remaining options */ audioCached( id: string, audio_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedAudio, "audio_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedAudio>( { type: "audio", id, audio_file_id, ...options }, ); }, /** * Builds an InlineQueryResultContact object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcontact. * * @param id Unique identifier for this result, 1-64 Bytes * @param phone_number Contact's phone number * @param first_name Contact's first name * @param options Remaining options */ contact( id: string, phone_number: string, first_name: string, options: InlineQueryResultOptions< InlineQueryResultContact, "phone_number" | "first_name" > = {}, ) { return inputMessage<InlineQueryResultContact>( { type: "contact", id, phone_number, first_name, ...options }, ); }, /** * Builds an InlineQueryResultDocument object as specified by * https://core.telegram.org/bots/api#inlinequeryresultdocument with * mime_type set to "application/pdf". * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param document_url A valid URL for the file * @param options Remaining options */ documentPdf( id: string, title: string, document_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultDocument, "mime_type" | "title" | "document_url" > = {}, ) { return inputMessage<InlineQueryResultDocument>({ type: "document", mime_type: "application/pdf", id, title, document_url: typeof document_url === "string" ? document_url : document_url.href, ...options, }); }, /** * Builds an InlineQueryResultDocument object as specified by * https://core.telegram.org/bots/api#inlinequeryresultdocument with * mime_type set to "application/zip". * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param document_url A valid URL for the file * @param options Remaining options */ documentZip( id: string, title: string, document_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultDocument, "mime_type" | "title" | "document_url" > = {}, ) { return inputMessage<InlineQueryResultDocument>({ type: "document", mime_type: "application/zip", id, title, document_url: typeof document_url === "string" ? document_url : document_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedDocument object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcacheddocument. * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param document_file_id A valid file identifier for the file * @param options Remaining options */ documentCached( id: string, title: string, document_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedDocument, "title" | "document_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedDocument>( { type: "document", id, title, document_file_id, ...options }, ); }, /** * Builds an InlineQueryResultGame object as specified by * https://core.telegram.org/bots/api#inlinequeryresultgame. * * @param id Unique identifier for this result, 1-64 bytes * @param game_short_name Short name of the game * @param options Remaining options */ game( id: string, game_short_name: string, options: InlineQueryResultOptions< InlineQueryResultGame, "game_short_name" > = {}, ) { return { type: "game", id, game_short_name, ...options }; }, /** * Builds an InlineQueryResultGif object as specified by * https://core.telegram.org/bots/api#inlinequeryresultgif. * * @param id Unique identifier for this result, 1-64 bytes * @param gif_url A valid URL for the GIF file. File size must not exceed 1MB * @param thumbnail_url URL of the static (JPEG or GIF) or animated (MPEG4) thumbnail for the result * @param options Remaining options */ gif( id: string, gif_url: string | URL, thumbnail_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultGif, "gif_url" | "thumbnail_url" > = {}, ) { return inputMessage<InlineQueryResultGif>({ type: "gif", id, gif_url: typeof gif_url === "string" ? gif_url : gif_url.href, thumbnail_url: typeof thumbnail_url === "string" ? thumbnail_url : thumbnail_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedGif object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedgif. * * @param id Unique identifier for this result, 1-64 bytes * @param gif_file_id A valid file identifier for the GIF file * @param options Remaining options */ gifCached( id: string, gif_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedGif, "gif_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedGif>( { type: "gif", id, gif_file_id, ...options }, ); }, /** * Builds an InlineQueryResultLocation object as specified by * https://core.telegram.org/bots/api#inlinequeryresultlocation. * * @param id Unique identifier for this result, 1-64 Bytes * @param title Location title * @param latitude Location latitude in degrees * @param longitude Location longitude in degrees * @param options Remaining options */ location( id: string, title: string, latitude: number, longitude: number, options: InlineQueryResultOptions< InlineQueryResultLocation, "title" | "latitude" | "longitude" > = {}, ) { return inputMessage<InlineQueryResultLocation>( { type: "location", id, title, latitude, longitude, ...options }, ); }, /** * Builds an InlineQueryResultMpeg4Gif object as specified by * https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif. * * @param id Unique identifier for this result, 1-64 bytes * @param mpeg4_url A valid URL for the MPEG4 file. File size must not exceed 1MB * @param thumbnail_url URL of the static (JPEG or GIF) or animated (MPEG4) thumbnail for the result * @param options Remaining options */ mpeg4gif( id: string, mpeg4_url: string | URL, thumbnail_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultMpeg4Gif, "mpeg4_url" | "thumbnail_url" > = {}, ) { return inputMessage<InlineQueryResultMpeg4Gif>({ type: "mpeg4_gif", id, mpeg4_url: typeof mpeg4_url === "string" ? mpeg4_url : mpeg4_url.href, thumbnail_url: typeof thumbnail_url === "string" ? thumbnail_url : thumbnail_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedMpeg4Gif object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedmpeg4gif. * * @param id Unique identifier for this result, 1-64 bytes * @param mpeg4_file_id A valid file identifier for the MPEG4 file * @param options Remaining options */ mpeg4gifCached( id: string, mpeg4_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedMpeg4Gif, "mpeg4_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedMpeg4Gif>( { type: "mpeg4_gif", id, mpeg4_file_id, ...options }, ); }, /** * Builds an InlineQueryResultPhoto object as specified by * https://core.telegram.org/bots/api#inlinequeryresultphoto with the * thumbnail defaulting to the photo itself. * * @param id Unique identifier for this result, 1-64 bytes * @param photo_url A valid URL of the photo. Photo must be in JPEG format. Photo size must not exceed 5MB * @param options Remaining options */ photo( id: string, photo_url: string | URL, options: InlineQueryResultOptions<InlineQueryResultPhoto, "photo_url"> = { // do not require thumbnail, default to the photo itself thumbnail_url: typeof photo_url === "string" ? photo_url : photo_url.href, }, ) { return inputMessage<InlineQueryResultPhoto>({ type: "photo", id, photo_url: typeof photo_url === "string" ? photo_url : photo_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedPhoto object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedphoto. * * @param id Unique identifier for this result, 1-64 bytes * @param photo_file_id A valid file identifier of the photo * @param options Remaining options */ photoCached( id: string, photo_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedPhoto, "photo_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedPhoto>( { type: "photo", id, photo_file_id, ...options }, ); }, /** * Builds an InlineQueryResultCachedSticker object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedsticker. * * @param id Unique identifier for this result, 1-64 bytes * @param sticker_file_id A valid file identifier of the sticker * @param options Remaining options */ stickerCached( id: string, sticker_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedSticker, "sticker_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedSticker>( { type: "sticker", id, sticker_file_id, ...options }, ); }, /** * Builds an InlineQueryResultVenue object as specified by * https://core.telegram.org/bots/api#inlinequeryresultvenue. * * @param id Unique identifier for this result, 1-64 Bytes * @param title Title of the venue * @param latitude Latitude of the venue location in degrees * @param longitude Longitude of the venue location in degrees * @param address Address of the venue * @param options Remaining options */ venue( id: string, title: string, latitude: number, longitude: number, address: string, options: InlineQueryResultOptions< InlineQueryResultVenue, "title" | "latitude" | "longitude" | "address" > = {}, ) { return inputMessage<InlineQueryResultVenue>({ type: "venue", id, title, latitude, longitude, address, ...options, }); }, /** * Builds an InlineQueryResultVideo object as specified by * https://core.telegram.org/bots/api#inlinequeryresultvideo with mime_type * set to "text/html". This will send an embedded video player. Requires you * to specify the actual message content by calling another function on the * object returned from this method. * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param video_url A valid URL for the embedded video player * @param thumbnail_url URL of the thumbnail (JPEG only) for the video * @param options Remaining options */ videoHtml( id: string, title: string, video_url: string | URL, thumbnail_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultVideo, "mime_type" | "title" | "video_url" | "thumbnail_url" > = {}, ) { // require input message content by only returning methods return inputMessageMethods<InlineQueryResultVideo>({ type: "video", mime_type: "text/html", id, title, video_url: typeof video_url === "string" ? video_url : video_url.href, thumbnail_url: typeof thumbnail_url === "string" ? thumbnail_url : thumbnail_url.href, ...options, }); }, /** * Builds an InlineQueryResultVideo object as specified by * https://core.telegram.org/bots/api#inlinequeryresultvideo with mime_type * set to "video/mp4". * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param video_url A valid URL for the video file * @param thumbnail_url URL of the thumbnail (JPEG only) for the video * @param options Remaining options */ videoMp4( id: string, title: string, video_url: string | URL, thumbnail_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultVideo, "mime_type" | "title" | "video_url" | "thumbnail_url" > = {}, ) { return inputMessage<InlineQueryResultVideo>({ type: "video", mime_type: "video/mp4", id, title, video_url: typeof video_url === "string" ? video_url : video_url.href, thumbnail_url: typeof thumbnail_url === "string" ? thumbnail_url : thumbnail_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedVideo object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedvideo. * * @param id Unique identifier for this result, 1-64 bytes * @param title Title for the result * @param video_file_id A valid file identifier for the video file * @param options Remaining options */ videoCached( id: string, title: string, video_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedVideo, "title" | "video_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedVideo>( { type: "video", id, title, video_file_id, ...options }, ); }, /** * Builds an InlineQueryResultVoice object as specified by * https://core.telegram.org/bots/api#inlinequeryresultvoice. * * @param id Unique identifier for this result, 1-64 bytes * @param title Voice message title * @param voice_url A valid URL for the voice recording * @param options Remaining options */ voice( id: string, title: string, voice_url: string | URL, options: InlineQueryResultOptions< InlineQueryResultVoice, "title" | "voice_url" > = {}, ) { return inputMessage<InlineQueryResultVoice>({ type: "voice", id, title, voice_url: typeof voice_url === "string" ? voice_url : voice_url.href, ...options, }); }, /** * Builds an InlineQueryResultCachedVoice object as specified by * https://core.telegram.org/bots/api#inlinequeryresultcachedvoice. * * @param id Unique identifier for this result, 1-64 bytes * @param title Voice message title * @param voice_file_id A valid file identifier for the voice message * @param options Remaining options */ voiceCached( id: string, title: string, voice_file_id: string, options: InlineQueryResultOptions< InlineQueryResultCachedVoice, "title" | "voice_file_id" > = {}, ) { return inputMessage<InlineQueryResultCachedVoice>( { type: "voice", id, title, voice_file_id, ...options }, ); },};
grammy

Version Info

Tagged at
a month ago