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// IsGroup returns if the message was sent to a group.
127//
128// Deprecated in favor of Chat.IsGroup.
129func (m *Message) IsGroup() bool {
130 log.Println("Message.IsGroup is deprecated.")
131 log.Println("Please use Chat.IsGroup instead.")
132 return m.Chat.IsGroup()
133}
134
135// IsCommand returns true if message starts with '/'.
136func (m *Message) IsCommand() bool {
137 return m.Text != "" && m.Text[0] == '/'
138}
139
140// Command checks if the message was a command and if it was, returns the
141// command. If the Message was not a command, it returns an empty string.
142func (m *Message) Command() string {
143 if !m.IsCommand() {
144 return ""
145 }
146
147 return strings.SplitN(m.Text, " ", 2)[0]
148}
149
150// CommandArguments checks if the message was a command and if it was,
151// returns all text after the command name. If the Message was not a
152// command, it returns an empty string.
153func (m *Message) CommandArguments() string {
154 if !m.IsCommand() {
155 return ""
156 }
157
158 split := strings.SplitN(m.Text, " ", 2)
159 if len(split) != 2 {
160 return ""
161 }
162
163 return strings.SplitN(m.Text, " ", 2)[1]
164}
165
166// PhotoSize contains information about photos.
167type PhotoSize struct {
168 FileID string `json:"file_id"`
169 Width int `json:"width"`
170 Height int `json:"height"`
171 FileSize int `json:"file_size"` // optional
172}
173
174// Audio contains information about audio.
175type Audio struct {
176 FileID string `json:"file_id"`
177 Duration int `json:"duration"`
178 Performer string `json:"performer"` // optional
179 Title string `json:"title"` // optional
180 MimeType string `json:"mime_type"` // optional
181 FileSize int `json:"file_size"` // optional
182}
183
184// Document contains information about a document.
185type Document struct {
186 FileID string `json:"file_id"`
187 Thumbnail PhotoSize `json:"thumb"` // optional
188 FileName string `json:"file_name"` // optional
189 MimeType string `json:"mime_type"` // optional
190 FileSize int `json:"file_size"` // optional
191}
192
193// Sticker contains information about a sticker.
194type Sticker struct {
195 FileID string `json:"file_id"`
196 Width int `json:"width"`
197 Height int `json:"height"`
198 Thumbnail PhotoSize `json:"thumb"` // optional
199 FileSize int `json:"file_size"` // optional
200}
201
202// Video contains information about a video.
203type Video struct {
204 FileID string `json:"file_id"`
205 Width int `json:"width"`
206 Height int `json:"height"`
207 Duration int `json:"duration"`
208 Thumbnail PhotoSize `json:"thumb"` // optional
209 MimeType string `json:"mime_type"` // optional
210 FileSize int `json:"file_size"` // optional
211}
212
213// Voice contains information about a voice.
214type Voice struct {
215 FileID string `json:"file_id"`
216 Duration int `json:"duration"`
217 MimeType string `json:"mime_type"` // optional
218 FileSize int `json:"file_size"` // optional
219}
220
221// Contact contains information about a contact.
222//
223// Note that LastName and UserID may be empty.
224type Contact struct {
225 PhoneNumber string `json:"phone_number"`
226 FirstName string `json:"first_name"`
227 LastName string `json:"last_name"` // optional
228 UserID int `json:"user_id"` // optional
229}
230
231// Location contains information about a place.
232type Location struct {
233 Longitude float32 `json:"longitude"`
234 Latitude float32 `json:"latitude"`
235}
236
237// UserProfilePhotos contains information a set of user profile photos.
238type UserProfilePhotos struct {
239 TotalCount int `json:"total_count"`
240 Photos []PhotoSize `json:"photos"`
241}
242
243// File contains information about a file to download from Telegram.
244type File struct {
245 FileID string `json:"file_id"`
246 FileSize int `json:"file_size"` // optional
247 FilePath string `json:"file_path"` // optional
248}
249
250// Link returns a full path to the download URL for a File.
251//
252// It requires the Bot Token to create the link.
253func (f *File) Link(token string) string {
254 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
255}
256
257// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
258type ReplyKeyboardMarkup struct {
259 Keyboard [][]string `json:"keyboard"`
260 ResizeKeyboard bool `json:"resize_keyboard"` // optional
261 OneTimeKeyboard bool `json:"one_time_keyboard"` // optional
262 Selective bool `json:"selective"` // optional
263}
264
265// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
266type ReplyKeyboardHide struct {
267 HideKeyboard bool `json:"hide_keyboard"`
268 Selective bool `json:"selective"` // optional
269}
270
271// ForceReply allows the Bot to have users directly reply to it without
272// additional interaction.
273type ForceReply struct {
274 ForceReply bool `json:"force_reply"`
275 Selective bool `json:"selective"` // optional
276}
277
278// InlineQuery is a Query from Telegram for an inline request.
279type InlineQuery struct {
280 ID string `json:"id"`
281 From User `json:"user"`
282 Query string `json:"query"`
283 Offset string `json:"offset"`
284}
285
286// InlineQueryResult is the base type that all InlineQuery Results have.
287type InlineQueryResult struct {
288 Type string `json:"type"` // required
289 ID string `json:"id"` // required
290}
291
292// InlineQueryResultArticle is an inline query response article.
293type InlineQueryResultArticle struct {
294 InlineQueryResult
295 Title string `json:"title"` // required
296 MessageText string `json:"message_text"` // required
297 ParseMode string `json:"parse_mode"`
298 DisableWebPagePreview bool `json:"disable_web_page_preview"`
299 URL string `json:"url"`
300 HideURL bool `json:"hide_url"`
301 Description string `json:"description"`
302 ThumbURL string `json:"thumb_url"`
303 ThumbWidth int `json:"thumb_width"`
304 ThumbHeight int `json:"thumb_height"`
305}
306
307// InlineQueryResultPhoto is an inline query response photo.
308type InlineQueryResultPhoto struct {
309 InlineQueryResult
310 URL string `json:"photo_url"` // required
311 MimeType string `json:"mime_type"`
312 Width int `json:"photo_width"`
313 Height int `json:"photo_height"`
314 ThumbURL string `json:"thumb_url"`
315 Title string `json:"title"`
316 Description string `json:"description"`
317 Caption string `json:"caption"`
318 MessageText string `json:"message_text"`
319 ParseMode string `json:"parse_mode"`
320 DisableWebPagePreview bool `json:"disable_web_page_preview"`
321}
322
323// InlineQueryResultGIF is an inline query response GIF.
324type InlineQueryResultGIF struct {
325 InlineQueryResult
326 URL string `json:"gif_url"` // required
327 Width int `json:"gif_width"`
328 Height int `json:"gif_height"`
329 ThumbURL string `json:"thumb_url"`
330 Title string `json:"title"`
331 Caption string `json:"caption"`
332 MessageText string `json:"message_text"`
333 ParseMode string `json:"parse_mode"`
334 DisableWebPagePreview bool `json:"disable_web_page_preview"`
335}
336
337// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
338type InlineQueryResultMPEG4GIF struct {
339 InlineQueryResult
340 URL string `json:"mpeg4_url"` // required
341 Width int `json:"mpeg4_width"`
342 Height int `json:"mpeg4_height"`
343 ThumbURL string `json:"thumb_url"`
344 Title string `json:"title"`
345 Caption string `json:"caption"`
346 MessageText string `json:"message_text"`
347 ParseMode string `json:"parse_mode"`
348 DisableWebPagePreview bool `json:"disable_web_page_preview"`
349}
350
351// InlineQueryResultVideo is an inline query response video.
352type InlineQueryResultVideo struct {
353 InlineQueryResult
354 URL string `json:"video_url"` // required
355 MimeType string `json:"mime_type"` // required
356 MessageText string `json:"message_text"` // required
357 ParseMode string `json:"parse_mode"`
358 DisableWebPagePreview bool `json:"disable_web_page_preview"`
359 Width int `json:"video_width"`
360 Height int `json:"video_height"`
361 ThumbURL string `json:"thumb_url"`
362 Title string `json:"title"`
363 Description string `json:"description"`
364}
365
366// ChosenInlineResult is an inline query result chosen by a User
367type ChosenInlineResult struct {
368 ResultID string `json:"result_id"`
369 From User `json:"from"`
370 Query string `json:"query"`
371}