telegram-bot-api-nodejs 1.0.0

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/index.ts ADDED
@@ -0,0 +1,1444 @@
1
+ const headers = {
2
+ "Content-Type": "application/json",
3
+ "Accept": "application/json",
4
+ }
5
+
6
+ export type ChatType = "private" | "group" | "supergroup" | "channel";
7
+ export type ChatAction = "typing" | "upload_photo" | "record_video" | "upload_video" | "record_audio" | "upload_audio" | "upload_document" | "find_location" | "record_video_note" | "upload_video_note";
8
+ export type ChatMemberStatus = "creator" | "administrator" | "member" | "restricted" | "left" | "kicked";
9
+ export type DocumentMimeType = "application/pdf" | "application/zip";
10
+ export type MessageType =
11
+ "text" |
12
+ "animation" |
13
+ "audio" |
14
+ "channel_chat_created" |
15
+ "contact" |
16
+ "delete_chat_photo" |
17
+ "document" |
18
+ "game" |
19
+ "group_chat_created" |
20
+ "invoice" |
21
+ "left_chat_member" |
22
+ "location" |
23
+ "migrate_from_chat_id" |
24
+ "migrate_to_chat_id" |
25
+ "new_chat_members" |
26
+ "new_chat_photo" |
27
+ "new_chat_title" |
28
+ "passport_data" |
29
+ "photo" |
30
+ "pinned_message" |
31
+ "sticker" |
32
+ "successful_payment" |
33
+ "supergroup_chat_created" |
34
+ "video" |
35
+ "video_note" |
36
+ "voice";
37
+ type MessageEntityType = "mention" | "hashtag" | "bot_command" | "url" | "email" | "bold" | "italic" | "code" | "pre" | "text_link" | "text_mention";
38
+ type ParseMode = "Markdown" | "MarkdownV2" | "HTML";
39
+
40
+ export interface SetWebHookOptions {
41
+ url?: string;
42
+ max_connections?: number;
43
+ allowed_updates?: string[];
44
+ }
45
+
46
+ interface SendBasicOptions {
47
+ chat_id: number | string
48
+ disable_notification?: boolean;
49
+ reply_to_message_id?: number | string;
50
+ reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
51
+ }
52
+
53
+ export interface SendMessageOptions extends SendBasicOptions {
54
+ text: string
55
+ parse_mode?: ParseMode;
56
+ disable_web_page_preview?: boolean;
57
+ entities?: MessageEntity[]
58
+ }
59
+
60
+ interface AnswerInlineQueryOptions {
61
+ cache_time?: number;
62
+ is_personal?: boolean;
63
+ next_offset?: string;
64
+ switch_pm_text?: string;
65
+ switch_pm_parameter?: string;
66
+ }
67
+
68
+ interface ForwardMessageOptions {
69
+ disable_notification?: boolean;
70
+ }
71
+
72
+ export interface SendPhotoOptions extends SendBasicOptions {
73
+ photo: string
74
+ parse_mode?: ParseMode;
75
+ caption?: string;
76
+ }
77
+
78
+ export interface SendAnimationOptions extends SendBasicOptions {
79
+ animation: string
80
+ parse_mode?: ParseMode;
81
+ caption?: string;
82
+ }
83
+
84
+ interface SendAudioOptions extends SendBasicOptions {
85
+ parse_mode?: ParseMode;
86
+ caption?: string;
87
+ duration?: number;
88
+ performer?: string;
89
+ title?: string;
90
+ }
91
+
92
+ interface SendDocumentOptions extends SendBasicOptions {
93
+ document: string
94
+ parse_mode?: ParseMode;
95
+ caption?: string;
96
+ }
97
+
98
+ interface SendDocumentStreamOptions extends SendBasicOptions {
99
+ document: Blob
100
+ documentName: string
101
+ parse_mode?: ParseMode;
102
+ caption?: string;
103
+ }
104
+
105
+ interface SendMediaGroupOptions {
106
+ disable_notification?: boolean;
107
+ reply_to_message_id?: number;
108
+ }
109
+
110
+ type SendStickerOptions = SendBasicOptions;
111
+
112
+ interface SendVideoOptions extends SendBasicOptions {
113
+ parse_mode?: ParseMode;
114
+ duration?: number;
115
+ width?: number;
116
+ height?: number;
117
+ caption?: string;
118
+ }
119
+
120
+ interface SendVoiceOptions extends SendBasicOptions {
121
+ parse_mode?: ParseMode;
122
+ caption?: string;
123
+ duration?: number;
124
+ }
125
+
126
+ interface SendVideoNoteOptions extends SendBasicOptions {
127
+ duration?: number;
128
+ length?: number;
129
+ }
130
+
131
+ type SendLocationOptions = SendBasicOptions;
132
+
133
+ type EditMessageLiveLocationOptions = EditMessageCaptionOptions;
134
+
135
+ type StopMessageLiveLocationOptions = EditMessageCaptionOptions;
136
+
137
+ interface SendVenueOptions extends SendBasicOptions {
138
+ foursquare_id?: string;
139
+ }
140
+
141
+ interface SendContactOptions extends SendBasicOptions {
142
+ last_name?: string;
143
+ }
144
+
145
+ type SendGameOptions = SendBasicOptions;
146
+
147
+ interface SendInvoiceOptions extends SendBasicOptions {
148
+ provider_data?: string;
149
+ photo_url?: string;
150
+ photo_size?: number;
151
+ photo_width?: number;
152
+ photo_height?: number;
153
+ need_name?: boolean;
154
+ need_phone_number?: boolean;
155
+ need_email?: boolean;
156
+ need_shipping_address?: boolean;
157
+ is_flexible?: boolean;
158
+ }
159
+
160
+ interface RestrictChatMemberOptions {
161
+ until_date?: number;
162
+ can_send_messages?: boolean;
163
+ can_send_media_messages?: boolean;
164
+ can_send_other_messages?: boolean;
165
+ can_add_web_page_previews?: boolean;
166
+ }
167
+
168
+ interface PromoteChatMemberOptions {
169
+ can_change_info?: boolean
170
+ can_post_messages?: boolean
171
+ can_edit_messages?: boolean
172
+ can_delete_messages?: boolean
173
+ can_invite_users?: boolean
174
+ can_restrict_members?: boolean
175
+ can_pin_messages?: boolean
176
+ can_promote_members?: boolean
177
+ }
178
+
179
+ export interface AnswerCallbackQueryOptions {
180
+ callback_query_id: string
181
+ text?: string
182
+ show_alert?: boolean
183
+ url?: string
184
+ cache_time?: number
185
+ }
186
+
187
+ export interface GetChatOptions {
188
+ chat_id: string
189
+ }
190
+
191
+ interface SetMyCommandsOptions {
192
+ commands: BotCommand[]
193
+ }
194
+
195
+ interface DeleteMyCommandsOptions { }
196
+
197
+ interface EditMessageTextOptions extends EditMessageReplyMarkupOptions {
198
+ parse_mode?: ParseMode;
199
+ disable_web_page_preview?: boolean;
200
+ text: string
201
+ }
202
+
203
+ interface EditMessageCaptionOptions extends EditMessageReplyMarkupOptions {
204
+ reply_markup?: InlineKeyboardMarkup;
205
+ }
206
+
207
+ interface EditMessageReplyMarkupOptions {
208
+ chat_id?: number | string;
209
+ message_id?: number | string;
210
+ inline_message_id?: string;
211
+ }
212
+
213
+ export interface GetUserProfilePhotosOptions {
214
+ user_id: number | string
215
+ offset?: number;
216
+ limit?: number;
217
+ }
218
+
219
+ interface SetGameScoreOptions {
220
+ force?: boolean;
221
+ disable_edit_message?: boolean;
222
+ chat_id?: number;
223
+ message_id?: number;
224
+ inline_message_id?: string;
225
+ }
226
+
227
+ interface GetGameHighScoresOptions {
228
+ chat_id?: number;
229
+ message_id?: number;
230
+ inline_message_id?: string;
231
+ }
232
+
233
+ interface AnswerShippingQueryOptions {
234
+ shipping_options?: ShippingOption[];
235
+ error_message?: string;
236
+ }
237
+
238
+ interface AnswerPreCheckoutQueryOptions {
239
+ error_message?: string;
240
+ }
241
+
242
+ /// TELEGRAM TYPES ///
243
+ interface PassportFile {
244
+ file_id: string;
245
+ file_size: number;
246
+ file_date: number;
247
+ }
248
+
249
+ interface EncryptedPassportElement {
250
+ type: string;
251
+ data?: string;
252
+ phone_number?: string;
253
+ email?: string;
254
+ files?: PassportFile[];
255
+ front_side?: PassportFile;
256
+ reverse_side?: PassportFile;
257
+ selfie?: PassportFile;
258
+ translation?: PassportFile[];
259
+ hash: string;
260
+ }
261
+
262
+ interface EncryptedCredentials {
263
+ data: string;
264
+ hash: string;
265
+ secret: string;
266
+ }
267
+
268
+ interface PassportData {
269
+ data: EncryptedPassportElement[];
270
+ credentials: EncryptedCredentials;
271
+ }
272
+
273
+ export interface Update {
274
+ update_id: number;
275
+ message?: Message;
276
+ edited_message?: Message;
277
+ channel_post?: Message;
278
+ edited_channel_post?: Message;
279
+ inline_query?: InlineQuery;
280
+ chosen_inline_result?: ChosenInlineResult;
281
+ callback_query?: CallbackQuery;
282
+ shipping_query?: ShippingQuery;
283
+ pre_checkout_query?: PreCheckoutQuery;
284
+ }
285
+
286
+ export interface WebhookInfo {
287
+ url: string;
288
+ has_custom_certificate: boolean;
289
+ pending_update_count: number;
290
+ last_error_date?: number;
291
+ last_error_message?: string;
292
+ max_connections?: number;
293
+ allowed_updates?: string[];
294
+ }
295
+
296
+ export interface User {
297
+ id: string;
298
+ is_bot: boolean;
299
+ first_name: string;
300
+ last_name?: string;
301
+ username?: string;
302
+ language_code?: string;
303
+ can_join_groups?: boolean
304
+ supports_inline_queries?: boolean
305
+ can_read_all_group_messages?: boolean
306
+ }
307
+
308
+ export interface Chat {
309
+ id: number;
310
+ type: ChatType;
311
+ title?: string;
312
+ username?: string;
313
+ first_name?: string;
314
+ last_name?: string;
315
+ photo?: ChatPhoto;
316
+ description?: string;
317
+ invite_link?: string;
318
+ pinned_message?: Message;
319
+ permissions?: ChatPermissions;
320
+ can_set_sticker_set?: boolean;
321
+ sticker_set_name?: string;
322
+ }
323
+
324
+ export interface Message {
325
+ message_id: number;
326
+ from?: User;
327
+ date: number;
328
+ chat: Chat;
329
+ forward_from?: User;
330
+ forward_from_chat?: Chat;
331
+ forward_from_message_id?: number;
332
+ forward_signature?: string;
333
+ forward_sender_name?: string;
334
+ forward_date?: number;
335
+ reply_to_message?: Message;
336
+ edit_date?: number;
337
+ media_group_id?: string;
338
+ author_signature?: string;
339
+ text?: string;
340
+ entities?: MessageEntity[];
341
+ caption_entities?: MessageEntity[];
342
+ audio?: Audio;
343
+ document?: Document;
344
+ animation?: Animation;
345
+ game?: Game;
346
+ photo?: PhotoSize[];
347
+ sticker?: Sticker;
348
+ video?: Video;
349
+ voice?: Voice;
350
+ video_note?: VideoNote;
351
+ caption?: string;
352
+ contact?: Contact;
353
+ location?: Location;
354
+ venue?: Venue;
355
+ poll?: Poll;
356
+ new_chat_members?: User[];
357
+ left_chat_member?: User;
358
+ new_chat_title?: string;
359
+ new_chat_photo?: PhotoSize[];
360
+ delete_chat_photo?: boolean;
361
+ group_chat_created?: boolean;
362
+ supergroup_chat_created?: boolean;
363
+ channel_chat_created?: boolean;
364
+ migrate_to_chat_id?: number;
365
+ migrate_from_chat_id?: number;
366
+ pinned_message?: Message;
367
+ invoice?: Invoice;
368
+ successful_payment?: SuccessfulPayment;
369
+ connected_website?: string;
370
+ passport_data?: PassportData;
371
+ reply_markup?: InlineKeyboardMarkup;
372
+ sender_chat?: Chat
373
+ }
374
+
375
+ export interface MessageEntity {
376
+ type: MessageEntityType;
377
+ offset: number;
378
+ length: number;
379
+ url?: string;
380
+ user?: User;
381
+ }
382
+
383
+ interface FileBase {
384
+ file_id: string;
385
+ file_size?: number;
386
+ }
387
+
388
+ export interface PhotoSize extends FileBase {
389
+ width: number;
390
+ height: number;
391
+ }
392
+
393
+ export interface Audio extends FileBase {
394
+ duration: number;
395
+ performer?: string;
396
+ title?: string;
397
+ file_name?: string;
398
+ mime_type?: string;
399
+ thumb?: PhotoSize;
400
+ }
401
+
402
+ export interface Document extends FileBase {
403
+ thumb?: PhotoSize;
404
+ file_name?: string;
405
+ mime_type?: string;
406
+ file_size?: number;
407
+ }
408
+
409
+ export interface Video extends FileBase {
410
+ width: number;
411
+ height: number;
412
+ duration: number;
413
+ thumb?: PhotoSize;
414
+ mime_type?: string;
415
+ file_name?: string;
416
+ }
417
+
418
+ export interface Voice extends FileBase {
419
+ duration: number;
420
+ mime_type?: string;
421
+ }
422
+
423
+ interface InputMediaBase {
424
+ media: string;
425
+ caption?: string;
426
+ parse_mode?: ParseMode;
427
+ }
428
+
429
+ interface InputMediaPhoto extends InputMediaBase {
430
+ type: "photo";
431
+ }
432
+
433
+ interface InputMediaVideo extends InputMediaBase {
434
+ type: "video";
435
+ width?: number;
436
+ height?: number;
437
+ duration?: number;
438
+ supports_streaming?: boolean;
439
+ }
440
+
441
+ type InputMedia = InputMediaPhoto | InputMediaVideo;
442
+
443
+ export interface VideoNote extends FileBase {
444
+ length: number;
445
+ duration: number;
446
+ thumb?: PhotoSize;
447
+ }
448
+
449
+ export interface Contact {
450
+ phone_number: string;
451
+ first_name: string;
452
+ last_name?: string;
453
+ user_id?: number;
454
+ vcard?: string;
455
+ }
456
+
457
+ export interface Location {
458
+ longitude: number;
459
+ latitude: number;
460
+ }
461
+
462
+ export interface Venue {
463
+ location: Location;
464
+ title: string;
465
+ address: string;
466
+ foursquare_id?: string;
467
+ foursquare_type?: string;
468
+ google_place_id?: string;
469
+ google_place_type?: string;
470
+ }
471
+
472
+ export interface PollOption {
473
+ text: string;
474
+ voter_count: number;
475
+ }
476
+
477
+ export interface Poll {
478
+ id: string;
479
+ question: string;
480
+ options: PollOption[];
481
+ is_closed: boolean;
482
+ }
483
+
484
+ export interface UserProfilePhotos {
485
+ total_count: number;
486
+ photos: PhotoSize[][];
487
+ }
488
+
489
+ export interface File extends FileBase {
490
+ file_path?: string;
491
+ }
492
+
493
+ interface ReplyKeyboardMarkup {
494
+ keyboard: KeyboardButton[][];
495
+ resize_keyboard?: boolean;
496
+ one_time_keyboard?: boolean;
497
+ selective?: boolean;
498
+ }
499
+
500
+ interface KeyboardButton {
501
+ text: string;
502
+ request_contact?: boolean;
503
+ request_location?: boolean;
504
+ }
505
+
506
+ interface ReplyKeyboardRemove {
507
+ remove_keyboard: boolean;
508
+ selective?: boolean;
509
+ }
510
+
511
+ export interface InlineKeyboardMarkup {
512
+ inline_keyboard: InlineKeyboardButton[][];
513
+ }
514
+
515
+ export interface InlineKeyboardButton {
516
+ text: string;
517
+ url?: string;
518
+ login_url?: LoginUrl;
519
+ callback_data?: string;
520
+ switch_inline_query?: string;
521
+ switch_inline_query_current_chat?: string;
522
+ callback_game?: CallbackGame;
523
+ pay?: boolean;
524
+ }
525
+
526
+ interface LoginUrl {
527
+ url: string;
528
+ forward_text?: string;
529
+ bot_username?: string;
530
+ request_write_acces?: boolean;
531
+ }
532
+
533
+ export interface CallbackQuery {
534
+ id: string;
535
+ from: User;
536
+ message?: Message;
537
+ inline_message_id?: string;
538
+ chat_instance: string;
539
+ data?: string;
540
+ game_short_name?: string;
541
+ }
542
+
543
+ interface ForceReply {
544
+ force_reply: boolean;
545
+ selective?: boolean;
546
+ }
547
+
548
+ interface ChatPhoto {
549
+ small_file_id: string
550
+ small_file_unique_id: string
551
+ big_file_id: string
552
+ big_file_unique_id: string
553
+ }
554
+
555
+ interface ChatMember {
556
+ user: User;
557
+ status: ChatMemberStatus;
558
+ until_date?: number;
559
+ can_be_edited?: boolean;
560
+ can_post_messages?: boolean;
561
+ can_edit_messages?: boolean;
562
+ can_delete_messages?: boolean;
563
+ can_restrict_members?: boolean;
564
+ can_promote_members?: boolean;
565
+ can_change_info?: boolean;
566
+ can_invite_users?: boolean;
567
+ can_pin_messages?: boolean;
568
+ is_member?: boolean;
569
+ can_send_messages?: boolean;
570
+ can_send_media_messages?: boolean;
571
+ can_send_polls: boolean;
572
+ can_send_other_messages?: boolean;
573
+ can_add_web_page_previews?: boolean;
574
+ }
575
+
576
+ interface ChatPermissions {
577
+ can_send_messages?: boolean;
578
+ can_send_media_messages?: boolean;
579
+ can_send_polls?: boolean;
580
+ can_send_other_messages?: boolean;
581
+ can_add_web_page_previews?: boolean;
582
+ can_change_info?: boolean;
583
+ can_invite_users?: boolean;
584
+ can_pin_messages?: boolean;
585
+ }
586
+
587
+ export interface Sticker {
588
+ file_id: string;
589
+ width: number;
590
+ height: number;
591
+ thumb?: PhotoSize;
592
+ emoji?: string;
593
+ set_name?: string;
594
+ mask_position?: MaskPosition;
595
+ file_size?: number;
596
+ }
597
+
598
+ interface StickerSet {
599
+ name: string;
600
+ title: string;
601
+ contains_masks: boolean;
602
+ stickers: Sticker[];
603
+ }
604
+
605
+ interface MaskPosition {
606
+ point: string;
607
+ x_shift: number;
608
+ y_shift: number;
609
+ scale: number;
610
+ }
611
+
612
+ interface InlineQuery {
613
+ id: string;
614
+ from: User;
615
+ location?: Location;
616
+ query: string;
617
+ offset: string;
618
+ }
619
+
620
+ interface InlineQueryResultBase {
621
+ id: string;
622
+ reply_markup?: InlineKeyboardMarkup;
623
+ }
624
+
625
+ interface InlineQueryResultArticle extends InlineQueryResultBase {
626
+ type: "article";
627
+ title: string;
628
+ input_message_content: InputMessageContent;
629
+ url?: string;
630
+ hide_url?: boolean;
631
+ description?: string;
632
+ thumb_url?: string;
633
+ thumb_width?: number;
634
+ thumb_height?: number;
635
+ }
636
+
637
+ interface InlineQueryResultPhoto extends InlineQueryResultBase {
638
+ type: "photo";
639
+ photo_url: string;
640
+ thumb_url: string;
641
+ photo_width?: number;
642
+ photo_height?: number;
643
+ title?: string;
644
+ description?: string;
645
+ caption?: string;
646
+ input_message_content?: InputMessageContent;
647
+ }
648
+
649
+ interface InlineQueryResultGif extends InlineQueryResultBase {
650
+ type: "gif";
651
+ gif_url: string;
652
+ gif_width?: number;
653
+ gif_height?: number;
654
+ gif_duration?: number;
655
+ thumb_url?: string;
656
+ title?: string;
657
+ caption?: string;
658
+ input_message_content?: InputMessageContent;
659
+ }
660
+
661
+ interface InlineQueryResultMpeg4Gif extends InlineQueryResultBase {
662
+ type: "mpeg4_gif";
663
+ mpeg4_url: string;
664
+ mpeg4_width?: number;
665
+ mpeg4_height?: number;
666
+ mpeg4_duration?: number;
667
+ thumb_url?: string;
668
+ title?: string;
669
+ caption?: string;
670
+ input_message_content?: InputMessageContent;
671
+ }
672
+
673
+ interface InlineQueryResultVideo extends InlineQueryResultBase {
674
+ type: "video";
675
+ video_url: string;
676
+ mime_type: string;
677
+ thumb_url: string;
678
+ title: string;
679
+ caption?: string;
680
+ video_width?: number;
681
+ video_height?: number;
682
+ video_duration?: number;
683
+ description?: string;
684
+ input_message_content?: InputMessageContent;
685
+ }
686
+
687
+ interface InlineQueryResultAudio extends InlineQueryResultBase {
688
+ type: "audio";
689
+ audio_url: string;
690
+ title: string;
691
+ caption?: string;
692
+ performer?: string;
693
+ audio_duration?: number;
694
+ input_message_content?: InputMessageContent;
695
+ }
696
+
697
+ interface InlineQueryResultVoice extends InlineQueryResultBase {
698
+ type: "voice";
699
+ voice_url: string;
700
+ title: string;
701
+ caption?: string;
702
+ voice_duration?: number;
703
+ input_message_content?: InputMessageContent;
704
+ }
705
+
706
+ interface InlineQueryResultDocument extends InlineQueryResultBase {
707
+ type: "document";
708
+ title: string;
709
+ caption?: string;
710
+ document_url: string;
711
+ mime_type: string;
712
+ description?: string;
713
+ input_message_content?: InputMessageContent;
714
+ thumb_url?: string;
715
+ thumb_width?: number;
716
+ thumb_height?: number;
717
+ }
718
+
719
+ interface InlineQueryResultLocationBase extends InlineQueryResultBase {
720
+ latitude: number;
721
+ longitude: number;
722
+ title: string;
723
+ input_message_content?: InputMessageContent;
724
+ thumb_url?: string;
725
+ thumb_width?: number;
726
+ thumb_height?: number;
727
+ }
728
+
729
+ interface InlineQueryResultLocation extends InlineQueryResultLocationBase {
730
+ type: "location";
731
+ }
732
+
733
+ interface InlineQueryResultVenue extends InlineQueryResultLocationBase {
734
+ type: "venue";
735
+ address: string;
736
+ foursquare_id?: string;
737
+ }
738
+
739
+ interface InlineQueryResultContact extends InlineQueryResultBase {
740
+ type: "contact";
741
+ phone_number: string;
742
+ first_name: string;
743
+ last_name?: string;
744
+ input_message_content?: InputMessageContent;
745
+ thumb_url?: string;
746
+ thumb_width?: number;
747
+ thumb_height?: number;
748
+ }
749
+
750
+ interface InlineQueryResultGame extends InlineQueryResultBase {
751
+ type: "game";
752
+ game_short_name: string;
753
+ }
754
+
755
+ interface InlineQueryResultCachedPhoto extends InlineQueryResultBase {
756
+ type: "photo";
757
+ photo_file_id: string;
758
+ title?: string;
759
+ description?: string;
760
+ caption?: string;
761
+ input_message_content?: InputMessageContent;
762
+ }
763
+
764
+ interface InlineQueryResultCachedGif extends InlineQueryResultBase {
765
+ type: "gif";
766
+ gif_file_id: string;
767
+ title?: string;
768
+ caption?: string;
769
+ input_message_content?: InputMessageContent;
770
+ }
771
+
772
+ interface InlineQueryResultCachedMpeg4Gif extends InlineQueryResultBase {
773
+ type: "mpeg4_gif";
774
+ mpeg4_file_id: string;
775
+ title?: string;
776
+ caption?: string;
777
+ input_message_content?: InputMessageContent;
778
+ }
779
+
780
+ interface InlineQueryResultCachedSticker extends InlineQueryResultBase {
781
+ type: "sticker";
782
+ sticker_file_id: string;
783
+ input_message_content?: InputMessageContent;
784
+ }
785
+
786
+ interface InlineQueryResultCachedDocument extends InlineQueryResultBase {
787
+ type: "document";
788
+ title: string;
789
+ document_file_id: string;
790
+ description?: string;
791
+ caption?: string;
792
+ input_message_content?: InputMessageContent;
793
+ }
794
+
795
+ interface InlineQueryResultCachedVideo extends InlineQueryResultBase {
796
+ type: "video";
797
+ video_file_id: string;
798
+ title: string;
799
+ description?: string;
800
+ caption?: string;
801
+ input_message_content?: InputMessageContent;
802
+ }
803
+
804
+ interface InlineQueryResultCachedVoice extends InlineQueryResultBase {
805
+ type: "voice";
806
+ voice_file_id: string;
807
+ title: string;
808
+ caption?: string;
809
+ input_message_content?: InputMessageContent;
810
+ }
811
+
812
+ interface InlineQueryResultCachedAudio extends InlineQueryResultBase {
813
+ type: "audio";
814
+ audio_file_id: string;
815
+ caption?: string;
816
+ input_message_content?: InputMessageContent;
817
+ }
818
+
819
+ type InlineQueryResult =
820
+ InlineQueryResultCachedAudio |
821
+ InlineQueryResultCachedDocument |
822
+ InlineQueryResultCachedGif |
823
+ InlineQueryResultCachedMpeg4Gif |
824
+ InlineQueryResultCachedPhoto |
825
+ InlineQueryResultCachedSticker |
826
+ InlineQueryResultCachedVideo |
827
+ InlineQueryResultCachedVoice |
828
+ InlineQueryResultArticle |
829
+ InlineQueryResultAudio |
830
+ InlineQueryResultContact |
831
+ InlineQueryResultGame |
832
+ InlineQueryResultDocument |
833
+ InlineQueryResultGif |
834
+ InlineQueryResultLocation |
835
+ InlineQueryResultMpeg4Gif |
836
+ InlineQueryResultPhoto |
837
+ InlineQueryResultVenue |
838
+ InlineQueryResultVideo |
839
+ InlineQueryResultVoice;
840
+
841
+ type InputMessageContent = object;
842
+
843
+ interface InputTextMessageContent extends InputMessageContent {
844
+ message_text: string;
845
+ parse_mode?: ParseMode;
846
+ disable_web_page_preview?: boolean;
847
+ }
848
+
849
+ interface InputLocationMessageContent extends InputMessageContent {
850
+ latitude: number;
851
+ longitude: number;
852
+ }
853
+
854
+ interface InputVenueMessageContent extends InputLocationMessageContent {
855
+ title: string;
856
+ address: string;
857
+ foursquare_id?: string;
858
+ }
859
+
860
+ interface InputContactMessageContent extends InputMessageContent {
861
+ phone_number: string;
862
+ first_name: string;
863
+ last_name?: string;
864
+ }
865
+
866
+ interface ChosenInlineResult {
867
+ result_id: string;
868
+ from: User;
869
+ location?: Location;
870
+ inline_message_id?: string;
871
+ query: string;
872
+ }
873
+
874
+ interface ResponseParameters {
875
+ migrate_to_chat_id?: number;
876
+ retry_after?: number;
877
+ }
878
+
879
+ interface LabeledPrice {
880
+ label: string;
881
+ amount: number;
882
+ }
883
+
884
+ interface Invoice {
885
+ title: string;
886
+ description: string;
887
+ start_parameter: string;
888
+ currency: string;
889
+ total_amount: number;
890
+ }
891
+
892
+ interface ShippingAddress {
893
+ country_code: string;
894
+ state: string;
895
+ city: string;
896
+ street_line1: string;
897
+ street_line2: string;
898
+ post_code: string;
899
+ }
900
+
901
+ interface OrderInfo {
902
+ name?: string;
903
+ phone_number?: string;
904
+ email?: string;
905
+ shipping_address?: ShippingAddress;
906
+ }
907
+
908
+ interface ShippingOption {
909
+ id: string;
910
+ title: string;
911
+ prices: LabeledPrice[];
912
+ }
913
+
914
+ interface SuccessfulPayment {
915
+ currency: string;
916
+ total_amount: number;
917
+ invoice_payload: string;
918
+ shipping_option_id?: string;
919
+ order_info?: OrderInfo;
920
+ telegram_payment_charge_id: string;
921
+ provider_payment_charge_id: string;
922
+ }
923
+
924
+ interface ShippingQuery {
925
+ id: string;
926
+ from: User;
927
+ invoice_payload: string;
928
+ shipping_address: ShippingAddress;
929
+ }
930
+
931
+ interface PreCheckoutQuery {
932
+ id: string;
933
+ from: User;
934
+ currency: string;
935
+ total_amount: number;
936
+ invoice_payload: string;
937
+ shipping_option_id?: string;
938
+ order_info?: OrderInfo;
939
+ }
940
+
941
+ interface Game {
942
+ title: string;
943
+ description: string;
944
+ photo: PhotoSize[];
945
+ text?: string;
946
+ text_entities?: MessageEntity[];
947
+ animation?: Animation;
948
+ }
949
+
950
+ export interface Animation extends FileBase {
951
+ width: number;
952
+ height: number;
953
+ duration: number;
954
+ thumb?: PhotoSize;
955
+ file_name?: string;
956
+ mime_type?: string;
957
+ }
958
+
959
+ type CallbackGame = object;
960
+
961
+ interface GameHighScore {
962
+ position: number;
963
+ user: User;
964
+ score: number;
965
+ }
966
+
967
+ interface Metadata {
968
+ type?: MessageType;
969
+ }
970
+
971
+ interface BotCommand {
972
+ command: string
973
+ description: string
974
+ }
975
+
976
+ /**
977
+ * Methods
978
+ */
979
+
980
+ export function setWebhook(token: string, body: SetWebHookOptions, signal: any) {
981
+ return fetch(`https://api.telegram.org/bot${token}/setWebhook`, {
982
+ method: "POST",
983
+ body: JSON.stringify(body),
984
+ headers,
985
+ signal
986
+ }).then(parseResponse)
987
+ }
988
+
989
+ export function deleteWebhook(token: string, signal: any) {
990
+ return fetch(`https://api.telegram.org/bot${token}/deleteWebhook`, {
991
+ method: "POST",
992
+ body: JSON.stringify({}),
993
+ headers,
994
+ signal
995
+ }).then(parseResponse)
996
+ }
997
+
998
+ /** @todo cehck if response is the same as in typings */
999
+ export function getWebhookInfo(token: string, signal: any): Promise<WebhookInfo> {
1000
+ return fetch(`https://api.telegram.org/bot${token}/getWebhookInfo`, {
1001
+ method: "POST",
1002
+ body: JSON.stringify({}),
1003
+ headers,
1004
+ signal
1005
+ }).then(parseResponse)
1006
+ }
1007
+
1008
+ export function getMe(token: string, signal: any): Promise<User> {
1009
+ return fetch(`https://api.telegram.org/bot${token}/getMe`, {
1010
+ method: "POST",
1011
+ body: JSON.stringify({}),
1012
+ headers,
1013
+ signal
1014
+ }).then(parseResponse)
1015
+ }
1016
+
1017
+ export function sendMessage(token: string, payload: SendMessageOptions, signal: any): Promise<Message> {
1018
+ return fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
1019
+ method: "POST",
1020
+ body: JSON.stringify(payload),
1021
+ headers,
1022
+ signal
1023
+ }).then(parseResponse)
1024
+ }
1025
+
1026
+ export function editMessageText(token: string, payload: EditMessageTextOptions, signal: any): Promise<Message> {
1027
+ return fetch(`https://api.telegram.org/bot${token}/editMessageText`, {
1028
+ method: "POST",
1029
+ body: JSON.stringify(payload),
1030
+ headers,
1031
+ signal
1032
+ }).then(parseResponse)
1033
+ }
1034
+
1035
+ export function sendPhoto(token: string, payload: SendPhotoOptions, signal: any): Promise<Message> {
1036
+ return fetch(`https://api.telegram.org/bot${token}/sendPhoto`, {
1037
+ method: "POST",
1038
+ body: JSON.stringify(payload),
1039
+ headers,
1040
+ signal
1041
+ }).then(parseResponse)
1042
+ }
1043
+
1044
+ export function sendAnimation(token: string, payload: SendAnimationOptions, signal: any): Promise<Message> {
1045
+ return fetch(`https://api.telegram.org/bot${token}/sendAnimation`, {
1046
+ method: "POST",
1047
+ body: JSON.stringify(payload),
1048
+ headers,
1049
+ signal
1050
+ }).then(parseResponse)
1051
+ }
1052
+
1053
+ export function sendDocument(token: string, payload: SendDocumentOptions, signal: any): Promise<Message> {
1054
+ return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
1055
+ method: "POST",
1056
+ body: JSON.stringify(payload),
1057
+ headers,
1058
+ signal
1059
+ }).then(parseResponse)
1060
+ }
1061
+
1062
+ export function sendDocumentStream(token: string, payload: SendDocumentStreamOptions, signal: any): Promise<Message> {
1063
+ const body = new FormData()
1064
+
1065
+ body.append("chat_id", String(payload.chat_id))
1066
+
1067
+ if (payload.caption) {
1068
+ body.append("caption", payload.caption)
1069
+ }
1070
+
1071
+ body.append("document", payload.document, payload.documentName)
1072
+
1073
+ if (payload.parse_mode) {
1074
+ body.append("parse_mode", payload.parse_mode)
1075
+ }
1076
+
1077
+ return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
1078
+ method: "POST",
1079
+ body: body,
1080
+ signal
1081
+ }).then(parseResponse)
1082
+ }
1083
+
1084
+ export function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal: any): Promise<UserProfilePhotos> {
1085
+ return fetch(`https://api.telegram.org/bot${token}/getUserProfilePhotos`, {
1086
+ method: "POST",
1087
+ body: JSON.stringify(payload),
1088
+ headers,
1089
+ signal
1090
+ }).then(parseResponse)
1091
+ }
1092
+
1093
+ export function getFile(token: string, file_id: string, signal: any): Promise<File> {
1094
+ return fetch(`https://api.telegram.org/bot${token}/getFile`, {
1095
+ method: "POST",
1096
+ body: JSON.stringify({ file_id }),
1097
+ headers,
1098
+ signal
1099
+ }).then(parseResponse)
1100
+ }
1101
+
1102
+ export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: any): Promise<File> {
1103
+ return fetch(`https://api.telegram.org/bot${token}/answerCallbackQuery`, {
1104
+ method: "POST",
1105
+ body: JSON.stringify(payload),
1106
+ headers,
1107
+ signal
1108
+ }).then(parseResponse)
1109
+ }
1110
+
1111
+ export function getChat(token: string, payload: GetChatOptions, signal: any): Promise<Chat> {
1112
+ return fetch(`https://api.telegram.org/bot${token}/getChat`, {
1113
+ method: "POST",
1114
+ body: JSON.stringify(payload),
1115
+ headers,
1116
+ signal
1117
+ }).then(parseResponse)
1118
+ }
1119
+
1120
+ export function getChatMember(token: string, payload: { chat_id: string, user_id: string }, signal: AbortSignal): Promise<ChatMember> {
1121
+ return fetch(`https://api.telegram.org/bot${token}/getChatMember`, {
1122
+ method: "POST",
1123
+ body: JSON.stringify(payload),
1124
+ headers,
1125
+ signal: signal
1126
+ }).then(parseResponse)
1127
+ }
1128
+
1129
+ export function setMyCommands(token: string, payload: SetMyCommandsOptions, signal: any): Promise<Chat> {
1130
+ return fetch(`https://api.telegram.org/bot${token}/setMyCommands`, {
1131
+ method: "POST",
1132
+ body: JSON.stringify(payload),
1133
+ headers,
1134
+ signal
1135
+ }).then(parseResponse)
1136
+ }
1137
+
1138
+ export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal: any): Promise<Chat> {
1139
+ return fetch(`https://api.telegram.org/bot${token}/deleteMyCommands`, {
1140
+ method: "POST",
1141
+ body: JSON.stringify(payload),
1142
+ headers,
1143
+ signal
1144
+ }).then(parseResponse)
1145
+ }
1146
+
1147
+ /**
1148
+ * Parsers
1149
+ */
1150
+
1151
+ export function parseUpdate(update: any): Update {
1152
+ const update_id = Number(update.update_id);
1153
+
1154
+ return {
1155
+ update_id,
1156
+ message: parseMessage(update.message),
1157
+ edited_message: parseMessage(update.edited_message),
1158
+ callback_query: parseCallbackQuery(update.callback_query),
1159
+ }
1160
+ }
1161
+
1162
+ export function parseMessage(m?: any): Message | undefined {
1163
+ if (!m) {
1164
+ return
1165
+ }
1166
+
1167
+ return {
1168
+ message_id: Number(m.message_id),
1169
+ from: m.from ? parseUser(m.from) : void 0,
1170
+ date: Number(m.date),
1171
+ chat: parseChat(m.chat),
1172
+ text: String(m.text ?? ""),
1173
+ forward_from: m.forward_from ? parseUser(m.forward_from) : void 0, // User;
1174
+ forward_from_chat: m.forward_from_chat ? parseChat(m.forward_from_chat) : void 0, // Chat;
1175
+ forward_from_message_id: m.forward_from_message_id ? Number(m.forward_from_message_id) : void 0, // number;
1176
+ forward_signature: m.forward_signature ? String(m.forward_signature) : void 0, // string;
1177
+ forward_sender_name: m.forward_sender_name ? String(m.forward_sender_name) : void 0, // string;
1178
+ forward_date: m.forward_date ? Number(m.forward_date) : void 0, // number;
1179
+ // reply_to_message: m.reply_to_message ? parseMessage(m.reply_to_message) : void 0, // Message;
1180
+ edit_date: m.edit_date ? Number(m.edit_date) : void 0, // number;
1181
+ media_group_id: m.media_group_id ? String(m.media_group_id) : void 0, // string;
1182
+ author_signature: m.author_signature ? String(m.author_signature) : void 0, // string;
1183
+ entities: m.entities ? parseEntities(m.entities) : void 0, // MessageEntity[];
1184
+ // caption_entities: m.caption_entities ? parseSometh(m.caption_entities) : void 0, // MessageEntity[];
1185
+ audio: m.audio ? parseAudio(m.audio) : void 0,
1186
+ document: m.document ? parseDocument(m.document) : void 0,
1187
+ animation: m.animation ? parseAnimation(m.animation) : void 0,
1188
+ // game?: Game;
1189
+ photo: m.photo ? parsePhotoSizes(m.photo) : void 0,
1190
+ sticker: m.sticker ? parseSticker(m.sticker) : void 0,
1191
+ video: m.video ? parseVideo(m.video) : void 0,
1192
+ voice: m.voice ? parseVoice(m.voice) : void 0,
1193
+ video_note: m.video_note ? parseVideoNote(m.video_note) : void 0,
1194
+ caption: m.caption ? String(m.caption) : void 0,
1195
+ contact: m.contact ? parseContact(m.contact) : void 0,
1196
+ location: m.location ? parseLocation(m.location) : void 0,
1197
+ venue: m.venue ? parseVenue(m.venue) : void 0,
1198
+ poll: m.poll ? parsePoll(m.poll) : void 0,
1199
+ // new_chat_members?: User[];
1200
+ // left_chat_member?: User;
1201
+ // new_chat_title?: string;
1202
+ // new_chat_photo?: PhotoSize[];
1203
+ // delete_chat_photo?: boolean;
1204
+ // group_chat_created?: boolean;
1205
+ // supergroup_chat_created?: boolean;
1206
+ // channel_chat_created?: boolean;
1207
+ // migrate_to_chat_id?: number;
1208
+ // migrate_from_chat_id?: number;
1209
+ // pinned_message?: Message;
1210
+ // invoice?: Invoice;
1211
+ // successful_payment?: SuccessfulPayment;
1212
+ // connected_website?: string;
1213
+ // passport_data?: PassportData;
1214
+ // reply_markup?: InlineKeyboardMarkup;
1215
+ sender_chat: m.sender_chat ? parseChat(m.sender_chat) : void 0,
1216
+ }
1217
+ }
1218
+
1219
+ export function parseUser(u: User) {
1220
+ return {
1221
+ id: String(u.id),
1222
+ is_bot: Boolean(u.is_bot),
1223
+ first_name: String(u.first_name),
1224
+ last_name: String(u.last_name ?? ""),
1225
+ username: String(u.username ?? ""),
1226
+ language_code: String(u.language_code ?? ""),
1227
+ }
1228
+ }
1229
+
1230
+ export function parseChat(c: Chat) {
1231
+ return {
1232
+ id: Number(c.id),
1233
+ type: c.type,
1234
+ title: String(c.title ?? ""),
1235
+ username: String(c.username ?? ""),
1236
+ first_name: String(c.first_name ?? ""),
1237
+ last_name: String(c.last_name ?? ""),
1238
+ }
1239
+ }
1240
+
1241
+ export function parsePhotoSizes(p: PhotoSize[]) {
1242
+ if (!Array.isArray(p)) {
1243
+ return void 0
1244
+ }
1245
+
1246
+ return p.map(parsePhotoSize)
1247
+ }
1248
+
1249
+ export function parsePhotoSize(p: PhotoSize) {
1250
+ return {
1251
+ file_id: String(p.file_id),
1252
+ file_size: Number(p.file_size),
1253
+ width: Number(p.width),
1254
+ height: Number(p.height),
1255
+ }
1256
+ }
1257
+
1258
+ export function parseSticker(v: Sticker): Sticker {
1259
+ return {
1260
+ file_id: String(v.file_id),
1261
+ width: Number(v.width),
1262
+ height: Number(v.height),
1263
+ thumb: v.thumb ? parsePhotoSize(v.thumb) : void 0,
1264
+ emoji: String(v.emoji),
1265
+ }
1266
+ }
1267
+
1268
+ export function parseVideo(v: Video): Video {
1269
+ return {
1270
+ file_id: String(v.file_id),
1271
+ width: Number(v.width),
1272
+ height: Number(v.height),
1273
+ duration: Number(v.duration),
1274
+ thumb: v.thumb ? parsePhotoSize(v.thumb) : void 0,
1275
+ mime_type: String(v.mime_type ?? ""),
1276
+ file_name: String(v.file_name ?? ""),
1277
+ file_size: Number(v.file_size ?? ""),
1278
+ }
1279
+ }
1280
+
1281
+ export function parseVoice(v: Voice): Voice {
1282
+ return {
1283
+ file_id: String(v.file_id),
1284
+ file_size: Number(v.file_size),
1285
+ duration: Number(v.duration),
1286
+ mime_type: String(v.mime_type)
1287
+ }
1288
+ }
1289
+
1290
+ export function parseVideoNote(v: VideoNote): VideoNote {
1291
+ return {
1292
+ file_id: String(v.file_id),
1293
+ length: Number(v.length),
1294
+ duration: Number(v.duration),
1295
+ thumb: v.thumb ? parsePhotoSize(v.thumb) : void 0,
1296
+ }
1297
+ }
1298
+
1299
+ export function parseEntities(v: MessageEntity[]): MessageEntity[] {
1300
+ if (!Array.isArray(v)) {
1301
+ return []
1302
+ }
1303
+
1304
+ return v.map(function (entity) {
1305
+ return {
1306
+ type: entity.type,
1307
+ offset: entity.offset,
1308
+ length: entity.length,
1309
+ url: String(entity.url ?? ""),
1310
+ user: entity.user ? parseUser(entity.user) : void 0,
1311
+ }
1312
+ })
1313
+ }
1314
+
1315
+ export function parseAudio(v: Audio): Audio {
1316
+ return {
1317
+ file_id: String(v.file_id),
1318
+ duration: Number(v.duration),
1319
+ title: String(v.title ?? ""),
1320
+ performer: String(v.performer ?? ""),
1321
+ file_name: String(v.file_name ?? ""),
1322
+ mime_type: String(v.mime_type ?? ""),
1323
+ }
1324
+ }
1325
+
1326
+ export function parseDocument(v: Document): Document {
1327
+ return {
1328
+ file_id: String(v.file_id),
1329
+ file_name: String(v.file_name || ""),
1330
+ mime_type: String(v.mime_type || ""),
1331
+ file_size: Number(v.file_size || 0),
1332
+ }
1333
+ }
1334
+
1335
+ export function parseAnimation(v: Animation) {
1336
+ return {
1337
+ file_id: String(v.file_id),
1338
+ width: Number(v.width),
1339
+ height: Number(v.height),
1340
+ duration: Number(v.duration),
1341
+ file_name: String(v.file_name),
1342
+ mime_type: String(v.mime_type || ""),
1343
+ }
1344
+ }
1345
+
1346
+ export function parseContact(v: Contact) {
1347
+ return {
1348
+ phone_number: String(v.phone_number),
1349
+ first_name: String(v.first_name),
1350
+ last_name: String(v.last_name ?? ""),
1351
+ user_id: Number(v.user_id ?? ""),
1352
+ vcard: String(v.vcard ?? ""),
1353
+ }
1354
+ }
1355
+
1356
+ export function parseLocation(v: Location) {
1357
+ return {
1358
+ longitude: Number(v.longitude),
1359
+ latitude: Number(v.latitude),
1360
+ }
1361
+ }
1362
+
1363
+ export function parseVenue(v: Venue) {
1364
+ return {
1365
+ location: parseLocation(v.location),
1366
+ title: String(v.title),
1367
+ address: String(v.address),
1368
+ foursquare_id: String(v.foursquare_id ?? ""),
1369
+ foursquare_type: String(v.foursquare_type ?? ""),
1370
+ google_place_id: String(v.google_place_id ?? ""),
1371
+ google_place_type: String(v.google_place_type ?? ""),
1372
+ }
1373
+ }
1374
+
1375
+ export function parsePoll(v: Poll) {
1376
+ return {
1377
+ id: String(v.id),
1378
+ question: String(v.question),
1379
+ options: parsePollOptions(v.options),
1380
+ is_closed: Boolean(v.is_closed),
1381
+ }
1382
+ }
1383
+
1384
+ export function parsePollOptions(v: PollOption[]) {
1385
+ return Array.isArray(v) ? v.map(parsePollOption) : []
1386
+ }
1387
+
1388
+ export function parsePollOption(v: PollOption) {
1389
+ return {
1390
+ text: String(v.text),
1391
+ voter_count: Number(v.voter_count),
1392
+ }
1393
+ }
1394
+
1395
+ /**
1396
+ * @see https://core.telegram.org/bots/api#callbackquery
1397
+ */
1398
+ export function parseCallbackQuery(v?: CallbackQuery): CallbackQuery | undefined {
1399
+ if (!v) {
1400
+ return
1401
+ }
1402
+
1403
+ return {
1404
+ id: String(v.id),
1405
+ from: parseUser(v.from),
1406
+ data: String(v.data ?? ""),
1407
+ message: parseMessage(v.message),
1408
+ chat_instance: String(v.chat_instance ?? ""),
1409
+ }
1410
+ }
1411
+
1412
+ function parseResponse<T>(response: Response) {
1413
+ return response.text().then(function (text) {
1414
+ let data: any
1415
+
1416
+ try {
1417
+ data = JSON.parse(text)
1418
+ }
1419
+ catch (err) {
1420
+ throw new TelegramError(`Invalid response: ${text}`, response.status)
1421
+ }
1422
+
1423
+ const ok = Boolean(data.ok)
1424
+
1425
+ if (ok) {
1426
+ return data.result || {} as T
1427
+ }
1428
+
1429
+ const error_code = Number(data.error_code ?? 0)
1430
+ const description = String(data.description ?? "")
1431
+
1432
+ throw new TelegramError(description, response.status)
1433
+ })
1434
+ }
1435
+
1436
+ export class TelegramError extends Error {
1437
+ status: number
1438
+
1439
+ constructor(message: string, status: number) {
1440
+ super(message)
1441
+
1442
+ this.status = status
1443
+ }
1444
+ }