types.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "fmt"
6 "log"
7 "strings"
8 "time"
9)
10
11// APIResponse is a response from the Telegram API with the result
12// stored raw.
13type APIResponse struct {
14 Ok bool `json:"ok"`
15 Result json.RawMessage `json:"result"`
16 ErrorCode int `json:"error_code"`
17 Description string `json:"description"`
18}
19
20// Update is an update response, from GetUpdates.
21type Update struct {
22 UpdateID int `json:"update_id"`
23 Message Message `json:"message"`
24 InlineQuery InlineQuery `json:"inline_query"`
25 ChosenInlineResult ChosenInlineResult `json:"chosen_inline_result"`
26}
27
28// User is a user on Telegram.
29type User struct {
30 ID int `json:"id"`
31 FirstName string `json:"first_name"`
32 LastName string `json:"last_name"` // optional
33 UserName string `json:"username"` // optional
34}
35
36// String displays a simple text version of a user.
37//
38// It is normally a user's username, but falls back to a first/last
39// name as available.
40func (u *User) String() string {
41 if u.UserName != "" {
42 return u.UserName
43 }
44
45 name := u.FirstName
46 if u.LastName != "" {
47 name += " " + u.LastName
48 }
49
50 return name
51}
52
53// GroupChat is a group chat.
54type GroupChat struct {
55 ID int `json:"id"`
56 Title string `json:"title"`
57}
58
59// Chat contains information about the place a message was sent.
60type Chat struct {
61 ID int `json:"id"`
62 Type string `json:"type"`
63 Title string `json:"title"` // optional
64 UserName string `json:"username"` // optional
65 FirstName string `json:"first_name"` // optional
66 LastName string `json:"last_name"` // optional
67}
68
69// IsPrivate returns if the Chat is a private conversation.
70func (c *Chat) IsPrivate() bool {
71 return c.Type == "private"
72}
73
74// IsGroup returns if the Chat is a group.
75func (c *Chat) IsGroup() bool {
76 return c.Type == "group"
77}
78
79// IsSuperGroup returns if the Chat is a supergroup.
80func (c *Chat) IsSuperGroup() bool {
81 return c.Type == "supergroup"
82}
83
84// IsChannel returns if the Chat is a channel.
85func (c *Chat) IsChannel() bool {
86 return c.Type == "channel"
87}
88
89// Message is returned by almost every request, and contains data about
90// almost anything.
91type Message struct {
92 MessageID int `json:"message_id"`
93 From User `json:"from"` // optional
94 Date int `json:"date"`
95 Chat Chat `json:"chat"`
96 ForwardFrom User `json:"forward_from"` // optional
97 ForwardDate int `json:"forward_date"` // optional
98 ReplyToMessage *Message `json:"reply_to_message"` // optional
99 Text string `json:"text"` // optional
100 Audio Audio `json:"audio"` // optional
101 Document Document `json:"document"` // optional
102 Photo []PhotoSize `json:"photo"` // optional
103 Sticker Sticker `json:"sticker"` // optional
104 Video Video `json:"video"` // optional
105 Voice Voice `json:"voice"` // optional
106 Caption string `json:"caption"` // optional
107 Contact Contact `json:"contact"` // optional
108 Location Location `json:"location"` // optional
109 NewChatParticipant User `json:"new_chat_participant"` // optional
110 LeftChatParticipant User `json:"left_chat_participant"` // optional
111 NewChatTitle string `json:"new_chat_title"` // optional
112 NewChatPhoto []PhotoSize `json:"new_chat_photo"` // optional
113 DeleteChatPhoto bool `json:"delete_chat_photo"` // optional
114 GroupChatCreated bool `json:"group_chat_created"` // optional
115 SuperGroupChatCreated bool `json:"supergroup_chat_created"` // optional
116 ChannelChatCreated bool `json:"channel_chat_created"` // optional
117 MigrateToChatID int `json:"migrate_to_chat_id"` // optional
118 MigrateFromChatID int `json:"migrate_from_chat_id"` // optional
119}
120
121// Time converts the message timestamp into a Time.
122func (m *Message) Time() time.Time {
123 return time.Unix(int64(m.Date), 0)
124}
125
126// IsCommand returns true if message starts with '/'.
127func (m *Message) IsCommand() bool {
128 return m.Text != "" && m.Text[0] == '/'
129}
130
131// Command checks if the message was a command and if it was, returns the
132// command. If the Message was not a command, it returns an empty string.
133func (m *Message) Command() string {
134 if !m.IsCommand() {
135 return ""
136 }
137
138 return strings.SplitN(m.Text, " ", 2)[0]
139}
140
141// CommandArguments checks if the message was a command and if it was,
142// returns all text after the command name. If the Message was not a
143// command, it returns an empty string.
144func (m *Message) CommandArguments() string {
145 if !m.IsCommand() {
146 return ""
147 }
148
149 split := strings.SplitN(m.Text, " ", 2)
150 if len(split) != 2 {
151 return ""
152 }
153
154 return strings.SplitN(m.Text, " ", 2)[1]
155}
156
157// PhotoSize contains information about photos.
158type PhotoSize struct {
159 FileID string `json:"file_id"`
160 Width int `json:"width"`
161 Height int `json:"height"`
162 FileSize int `json:"file_size"` // optional
163}
164
165// Audio contains information about audio.
166type Audio struct {
167 FileID string `json:"file_id"`
168 Duration int `json:"duration"`
169 Performer string `json:"performer"` // optional
170 Title string `json:"title"` // optional
171 MimeType string `json:"mime_type"` // optional
172 FileSize int `json:"file_size"` // optional
173}
174
175// Document contains information about a document.
176type Document struct {
177 FileID string `json:"file_id"`
178 Thumbnail PhotoSize `json:"thumb"` // optional
179 FileName string `json:"file_name"` // optional
180 MimeType string `json:"mime_type"` // optional
181 FileSize int `json:"file_size"` // optional
182}
183
184// Sticker contains information about a sticker.
185type Sticker struct {
186 FileID string `json:"file_id"`
187 Width int `json:"width"`
188 Height int `json:"height"`
189 Thumbnail PhotoSize `json:"thumb"` // optional
190 FileSize int `json:"file_size"` // optional
191}
192
193// Video contains information about a video.
194type Video struct {
195 FileID string `json:"file_id"`
196 Width int `json:"width"`
197 Height int `json:"height"`
198 Duration int `json:"duration"`
199 Thumbnail PhotoSize `json:"thumb"` // optional
200 MimeType string `json:"mime_type"` // optional
201 FileSize int `json:"file_size"` // optional
202}
203
204// Voice contains information about a voice.
205type Voice struct {
206 FileID string `json:"file_id"`
207 Duration int `json:"duration"`
208 MimeType string `json:"mime_type"` // optional
209 FileSize int `json:"file_size"` // optional
210}
211
212// Contact contains information about a contact.
213//
214// Note that LastName and UserID may be empty.
215type Contact struct {
216 PhoneNumber string `json:"phone_number"`
217 FirstName string `json:"first_name"`
218 LastName string `json:"last_name"` // optional
219 UserID int `json:"user_id"` // optional
220}
221
222// Location contains information about a place.
223type Location struct {
224 Longitude float32 `json:"longitude"`
225 Latitude float32 `json:"latitude"`
226}
227
228// UserProfilePhotos contains information a set of user profile photos.
229type UserProfilePhotos struct {
230 TotalCount int `json:"total_count"`
231 Photos []PhotoSize `json:"photos"`
232}
233
234// File contains information about a file to download from Telegram.
235type File struct {
236 FileID string `json:"file_id"`
237 FileSize int `json:"file_size"` // optional
238 FilePath string `json:"file_path"` // optional
239}
240
241// Link returns a full path to the download URL for a File.
242//
243// It requires the Bot Token to create the link.
244func (f *File) Link(token string) string {
245 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
246}
247
248// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
249type ReplyKeyboardMarkup struct {
250 Keyboard [][]string `json:"keyboard"`
251 ResizeKeyboard bool `json:"resize_keyboard"` // optional
252 OneTimeKeyboard bool `json:"one_time_keyboard"` // optional
253 Selective bool `json:"selective"` // optional
254}
255
256// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
257type ReplyKeyboardHide struct {
258 HideKeyboard bool `json:"hide_keyboard"`
259 Selective bool `json:"selective"` // optional
260}
261
262// ForceReply allows the Bot to have users directly reply to it without
263// additional interaction.
264type ForceReply struct {
265 ForceReply bool `json:"force_reply"`
266 Selective bool `json:"selective"` // optional
267}
268
269// InlineQuery is a Query from Telegram for an inline request.
270type InlineQuery struct {
271 ID string `json:"id"`
272 From User `json:"user"`
273 Query string `json:"query"`
274 Offset string `json:"offset"`
275}
276
277// InlineQueryResult is the base type that all InlineQuery Results have.
278type InlineQueryResult struct {
279 Type string `json:"type"` // required
280 ID string `json:"id"` // required
281}
282
283// InlineQueryResultArticle is an inline query response article.
284type InlineQueryResultArticle struct {
285 InlineQueryResult
286 Title string `json:"title"` // required
287 MessageText string `json:"message_text"` // required
288 ParseMode string `json:"parse_mode"`
289 DisableWebPagePreview bool `json:"disable_web_page_preview"`
290 URL string `json:"url"`
291 HideURL bool `json:"hide_url"`
292 Description string `json:"description"`
293 ThumbURL string `json:"thumb_url"`
294 ThumbWidth int `json:"thumb_width"`
295 ThumbHeight int `json:"thumb_height"`
296}
297
298// InlineQueryResultPhoto is an inline query response photo.
299type InlineQueryResultPhoto struct {
300 InlineQueryResult
301 URL string `json:"photo_url"` // required
302 MimeType string `json:"mime_type"`
303 Width int `json:"photo_width"`
304 Height int `json:"photo_height"`
305 ThumbURL string `json:"thumb_url"`
306 Title string `json:"title"`
307 Description string `json:"description"`
308 Caption string `json:"caption"`
309 MessageText string `json:"message_text"`
310 ParseMode string `json:"parse_mode"`
311 DisableWebPagePreview bool `json:"disable_web_page_preview"`
312}
313
314// InlineQueryResultGIF is an inline query response GIF.
315type InlineQueryResultGIF struct {
316 InlineQueryResult
317 URL string `json:"gif_url"` // required
318 Width int `json:"gif_width"`
319 Height int `json:"gif_height"`
320 ThumbURL string `json:"thumb_url"`
321 Title string `json:"title"`
322 Caption string `json:"caption"`
323 MessageText string `json:"message_text"`
324 ParseMode string `json:"parse_mode"`
325 DisableWebPagePreview bool `json:"disable_web_page_preview"`
326}
327
328// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
329type InlineQueryResultMPEG4GIF struct {
330 InlineQueryResult
331 URL string `json:"mpeg4_url"` // required
332 Width int `json:"mpeg4_width"`
333 Height int `json:"mpeg4_height"`
334 ThumbURL string `json:"thumb_url"`
335 Title string `json:"title"`
336 Caption string `json:"caption"`
337 MessageText string `json:"message_text"`
338 ParseMode string `json:"parse_mode"`
339 DisableWebPagePreview bool `json:"disable_web_page_preview"`
340}
341
342// InlineQueryResultVideo is an inline query response video.
343type InlineQueryResultVideo struct {
344 InlineQueryResult
345 URL string `json:"video_url"` // required
346 MimeType string `json:"mime_type"` // required
347 MessageText string `json:"message_text"` // required
348 ParseMode string `json:"parse_mode"`
349 DisableWebPagePreview bool `json:"disable_web_page_preview"`
350 Width int `json:"video_width"`
351 Height int `json:"video_height"`
352 ThumbURL string `json:"thumb_url"`
353 Title string `json:"title"`
354 Description string `json:"description"`
355}
356
357// ChosenInlineResult is an inline query result chosen by a User
358type ChosenInlineResult struct {
359 ResultID string `json:"result_id"`
360 From User `json:"from"`
361 Query string `json:"query"`
362}