UNPKG

4.45 kBPlain TextView Raw
1/** @format */
2
3import { Message, Opts, Telegram, Update } from './core/types/typegram'
4import { UnionKeys } from './deunionize'
5
6export { Markup } from './markup'
7
8// tiny helper types
9export type ChatAction = Opts<'sendChatAction'>['action']
10
11// extra types
12/**
13 * Create an `Extra*` type from the arguments of a given method `M extends keyof Telegram` but `Omit`ting fields with key `K` from it.
14 *
15 * Note that `chat_id` may not be specified in `K` because it is `Omit`ted by default.
16 */
17type MakeExtra<
18 M extends keyof Telegram,
19 K extends keyof Omit<Opts<M>, 'chat_id'> = never
20> = Omit<Opts<M>, 'chat_id' | K>
21
22export type ExtraAddStickerToSet = MakeExtra<
23 'addStickerToSet',
24 'name' | 'user_id'
25>
26export type ExtraAnimation = MakeExtra<'sendAnimation', 'animation'>
27export type ExtraAnswerCbQuery = MakeExtra<
28 'answerCallbackQuery',
29 'text' | 'callback_query_id'
30>
31export type ExtraAnswerInlineQuery = MakeExtra<
32 'answerInlineQuery',
33 'inline_query_id' | 'results'
34>
35export type ExtraAudio = MakeExtra<'sendAudio', 'audio'>
36export type ExtraContact = MakeExtra<
37 'sendContact',
38 'phone_number' | 'first_name'
39>
40export type ExtraCopyMessage = MakeExtra<
41 'copyMessage',
42 'from_chat_id' | 'message_id'
43>
44export type ExtraCreateChatInviteLink = MakeExtra<'createChatInviteLink'>
45export type ExtraCreateNewStickerSet = MakeExtra<
46 'createNewStickerSet',
47 'name' | 'title' | 'user_id'
48>
49export type ExtraDice = MakeExtra<'sendDice'>
50export type ExtraDocument = MakeExtra<'sendDocument', 'document'>
51export type ExtraEditChatInviteLink = MakeExtra<
52 'editChatInviteLink',
53 'invite_link'
54>
55export type ExtraEditMessageCaption = MakeExtra<
56 'editMessageCaption',
57 'message_id' | 'inline_message_id' | 'caption'
58>
59export type ExtraEditMessageLiveLocation = MakeExtra<
60 'editMessageLiveLocation',
61 'message_id' | 'inline_message_id' | 'latitude' | 'longitude'
62>
63export type ExtraEditMessageMedia = MakeExtra<
64 'editMessageMedia',
65 'message_id' | 'inline_message_id' | 'media'
66>
67export type ExtraEditMessageText = MakeExtra<
68 'editMessageText',
69 'message_id' | 'inline_message_id' | 'text'
70>
71export type ExtraGame = MakeExtra<'sendGame', 'game_short_name'>
72export type NewInvoiceParameters = MakeExtra<
73 'sendInvoice',
74 | 'disable_notification'
75 | 'reply_to_message_id'
76 | 'allow_sending_without_reply'
77 | 'reply_markup'
78>
79export type ExtraInvoice = MakeExtra<'sendInvoice', keyof NewInvoiceParameters>
80export type ExtraKickChatMember = MakeExtra<
81 'kickChatMember',
82 'user_id' | 'until_date'
83>
84export type ExtraLocation = MakeExtra<'sendLocation', 'latitude' | 'longitude'>
85export type ExtraMediaGroup = MakeExtra<'sendMediaGroup', 'media'>
86export type ExtraPhoto = MakeExtra<'sendPhoto', 'photo'>
87export type ExtraPoll = MakeExtra<'sendPoll', 'question' | 'options' | 'type'>
88export type ExtraPromoteChatMember = MakeExtra<'promoteChatMember', 'user_id'>
89export type ExtraReplyMessage = MakeExtra<'sendMessage', 'text'>
90export type ExtraRestrictChatMember = MakeExtra<'restrictChatMember', 'user_id'>
91export type ExtraSetMyCommands = MakeExtra<'setMyCommands', 'commands'>
92export type ExtraSetWebhook = MakeExtra<'setWebhook', 'url'>
93export type ExtraSticker = MakeExtra<'sendSticker', 'sticker'>
94export type ExtraStopPoll = MakeExtra<'stopPoll', 'message_id'>
95export type ExtraVenue = MakeExtra<
96 'sendVenue',
97 'latitude' | 'longitude' | 'title' | 'address'
98>
99export type ExtraVideo = MakeExtra<'sendVideo', 'video'>
100export type ExtraVideoNote = MakeExtra<'sendVideoNote', 'video_note'>
101export type ExtraVoice = MakeExtra<'sendVoice', 'voice'>
102
103// types used for inference of ctx object
104
105/** Possible update types */
106export type UpdateType = Exclude<UnionKeys<Update>, keyof Update>
107
108/** Possible message subtypes. Same as the properties on a message object */
109export type MessageSubType =
110 | 'forward_date'
111 | Exclude<
112 UnionKeys<Message>,
113 keyof Message.CaptionableMessage | 'entities' | 'media_group_id'
114 >
115
116type ExtractPartial<T extends object, U extends object> = T extends unknown
117 ? Required<T> extends U
118 ? T
119 : never
120 : never
121
122/**
123 * Maps [[`Composer.on`]]'s `updateType` or `messageSubType` to a `tt.Update` subtype.
124 */
125export type MountMap = {
126 [T in UpdateType]: Extract<Update, Record<T, object>>
127} &
128 {
129 [T in MessageSubType]: {
130 message: ExtractPartial<
131 Update.MessageUpdate['message'],
132 Record<T, unknown>
133 >
134 update_id: number
135 }
136 }