UNPKG

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