/
telegram-types.ts
219 lines (205 loc) · 6.49 KB
/
telegram-types.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
/** @format */
import { Expand } from './core/helpers/util'
import {
Message,
Opts,
Telegram,
Update,
InputMediaAudio,
InputMediaDocument,
InputMediaPhoto,
InputMediaVideo,
} from './core/types/typegram'
import { UnionKeys } from './core/helpers/deunionize'
import { FmtString } from './format'
export { Markup } from './markup'
// tiny helper types
export type ChatAction = Opts<'sendChatAction'>['action']
// Modify type so caption, if exists, can be FmtString
export type WrapCaption<T> = T extends { caption?: string }
? Expand<Omit<T, 'caption'> & { caption?: string | FmtString }>
: T
// extra types
/**
* Create an `Extra*` type from the arguments of a given method `M extends keyof Telegram` but `Omit`ting fields with key `K` from it.
*
* Note that `chat_id` may not be specified in `K` because it is `Omit`ted by default.
*/
type MakeExtra<
M extends keyof Telegram,
K extends keyof Omit<Opts<M>, 'chat_id'> = never,
> = WrapCaption<Omit<Opts<M>, 'chat_id' | K>>
export type ExtraAddStickerToSet = MakeExtra<
'addStickerToSet',
'name' | 'user_id'
>
export type ExtraAnimation = MakeExtra<'sendAnimation', 'animation'>
export type ExtraAnswerCbQuery = MakeExtra<
'answerCallbackQuery',
'text' | 'callback_query_id'
>
export type ExtraAnswerInlineQuery = MakeExtra<
'answerInlineQuery',
'inline_query_id' | 'results'
>
export type ExtraSetChatPermissions = MakeExtra<
'setChatPermissions',
'permissions'
>
export type ExtraAudio = MakeExtra<'sendAudio', 'audio'>
export type ExtraContact = MakeExtra<
'sendContact',
'phone_number' | 'first_name'
>
export type ExtraCopyMessage = MakeExtra<
'copyMessage',
'from_chat_id' | 'message_id'
>
export type ExtraCopyMessages = MakeExtra<
'copyMessages',
'from_chat_id' | 'message_ids'
>
export type ExtraCreateChatInviteLink = MakeExtra<'createChatInviteLink'>
export type NewInvoiceLinkParameters = MakeExtra<'createInvoiceLink'>
export type ExtraCreateNewStickerSet = MakeExtra<
'createNewStickerSet',
'name' | 'title' | 'user_id'
>
export type ExtraDice = MakeExtra<'sendDice'>
export type ExtraDocument = MakeExtra<'sendDocument', 'document'>
export type ExtraEditChatInviteLink = MakeExtra<
'editChatInviteLink',
'invite_link'
>
export type ExtraEditMessageCaption = MakeExtra<
'editMessageCaption',
'message_id' | 'inline_message_id' | 'caption'
>
export type ExtraEditMessageLiveLocation = MakeExtra<
'editMessageLiveLocation',
'message_id' | 'inline_message_id' | 'latitude' | 'longitude'
>
export type ExtraEditMessageMedia = MakeExtra<
'editMessageMedia',
'message_id' | 'inline_message_id' | 'media'
>
export type ExtraEditMessageText = MakeExtra<
'editMessageText',
'message_id' | 'inline_message_id' | 'text'
>
export type ExtraGame = MakeExtra<'sendGame', 'game_short_name'>
export type NewInvoiceParameters = MakeExtra<
'sendInvoice',
| 'disable_notification'
| 'reply_parameters'
| 'reply_markup'
| 'message_thread_id'
>
export type ExtraInvoice = MakeExtra<'sendInvoice', keyof NewInvoiceParameters>
export type ExtraBanChatMember = MakeExtra<
'banChatMember',
'user_id' | 'until_date'
>
export type ExtraKickChatMember = ExtraBanChatMember
export type ExtraLocation = MakeExtra<'sendLocation', 'latitude' | 'longitude'>
export type ExtraMediaGroup = MakeExtra<'sendMediaGroup', 'media'>
export type ExtraPhoto = MakeExtra<'sendPhoto', 'photo'>
export type ExtraPoll = MakeExtra<'sendPoll', 'question' | 'options' | 'type'>
export type ExtraPromoteChatMember = MakeExtra<'promoteChatMember', 'user_id'>
export type ExtraReplyMessage = MakeExtra<'sendMessage', 'text'>
export type ExtraForwardMessage = MakeExtra<
'forwardMessage',
'from_chat_id' | 'message_id'
>
export type ExtraForwardMessages = MakeExtra<
'forwardMessages',
'from_chat_id' | 'message_ids'
>
export type ExtraSendChatAction = MakeExtra<'sendChatAction', 'action'>
export type ExtraRestrictChatMember = MakeExtra<'restrictChatMember', 'user_id'>
export type ExtraSetMyCommands = MakeExtra<'setMyCommands', 'commands'>
export type ExtraSetWebhook = MakeExtra<'setWebhook', 'url'>
export type ExtraSticker = MakeExtra<'sendSticker', 'sticker'>
export type ExtraStopPoll = MakeExtra<'stopPoll', 'message_id'>
export type ExtraVenue = MakeExtra<
'sendVenue',
'latitude' | 'longitude' | 'title' | 'address'
>
export type ExtraVideo = MakeExtra<'sendVideo', 'video'>
export type ExtraVideoNote = MakeExtra<'sendVideoNote', 'video_note'>
export type ExtraVoice = MakeExtra<'sendVoice', 'voice'>
export type ExtraBanChatSenderChat = MakeExtra<
'banChatSenderChat',
'sender_chat_id'
>
export type ExtraCreateForumTopic = MakeExtra<'createForumTopic', 'name'>
export type ExtraEditForumTopic = MakeExtra<
'editForumTopic',
'message_thread_id'
>
export type MediaGroup =
| readonly (InputMediaPhoto | InputMediaVideo)[]
| readonly InputMediaAudio[]
| readonly InputMediaDocument[]
// types used for inference of ctx object
/** Possible update types */
export type UpdateType = Exclude<UnionKeys<Update>, keyof Update>
/** Possible message subtypes. Same as the properties on a message object */
export type MessageSubType =
| 'forward_date'
| Exclude<
UnionKeys<Message>,
keyof Message.CaptionableMessage | 'entities' | 'media_group_id'
>
type ExtractPartial<T extends object, U extends object> = T extends unknown
? Required<T> extends U
? T
: never
: never
/**
* Maps [[`Composer.on`]]'s `updateType` or `messageSubType` to a `tt.Update` subtype.
* @deprecated
*/
export type MountMap = {
[T in UpdateType]: Extract<Update, Record<T, object>>
} & {
[T in MessageSubType]: {
message: ExtractPartial<Update.MessageUpdate['message'], Record<T, unknown>>
update_id: number
}
}
export interface CommandContextExtn {
match: RegExpExecArray
/**
* Matched command. This will always be the actual command, excluding preceeding slash and `@botname`
*
* Examples:
* ```
* /command abc -> command
* /command@xyzbot abc -> command
* ```
*/
command: string
/**
* The unparsed payload part of the command
*
* Examples:
* ```
* /command abc def -> "abc def"
* /command "token1 token2" -> "\"token1 token2\""
* ```
*/
payload: string
/**
* Command args parsed into an array.
*
* Examples:
* ```
* /command token1 token2 -> [ "token1", "token2" ]
* /command "token1 token2" -> [ "token1 token2" ]
* /command token1 "token2 token3" -> [ "token1" "token2 token3" ]
* ```
* @unstable Parser implementation might vary until considered stable
* */
args: string[]
}