types.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "net/url"
8 "strings"
9 "time"
10)
11
12// APIResponse is a response from the Telegram API with the result
13// stored raw.
14type APIResponse struct {
15 Ok bool `json:"ok"`
16 Result json.RawMessage `json:"result"`
17 ErrorCode int `json:"error_code"`
18 Description string `json:"description"`
19 Parameters *ResponseParameters `json:"parameters"`
20}
21
22// ResponseParameters are various errors that can be returned in APIResponse.
23type ResponseParameters struct {
24 MigrateToChatID int64 `json:"migrate_to_chat_id"` // optional
25 RetryAfter int `json:"retry_after"` // optional
26}
27
28// Update is an update response, from GetUpdates.
29type Update struct {
30 UpdateID int `json:"update_id"`
31 Message *Message `json:"message"`
32 EditedMessage *Message `json:"edited_message"`
33 ChannelPost *Message `json:"channel_post"`
34 EditedChannelPost *Message `json:"edited_channel_post"`
35 InlineQuery *InlineQuery `json:"inline_query"`
36 ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result"`
37 CallbackQuery *CallbackQuery `json:"callback_query"`
38 ShippingQuery *ShippingQuery `json:"shipping_query"`
39 PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"`
40}
41
42// UpdatesChannel is the channel for getting updates.
43type UpdatesChannel <-chan Update
44
45// Clear discards all unprocessed incoming updates.
46func (ch UpdatesChannel) Clear() {
47 for len(ch) != 0 {
48 <-ch
49 }
50}
51
52// User represents a Telegram user or bot.
53type User struct {
54 // ID is a unique identifier for this user or bot
55 ID int `json:"id"`
56 // FirstName user's or bot's first name
57 FirstName string `json:"first_name"`
58 // LastName user's or bot's last name
59 // optional
60 LastName string `json:"last_name"`
61 // UserName user's or bot's username
62 // optional
63 UserName string `json:"username"`
64 // LanguageCode IETF language tag of the user's language
65 // more info: https://en.wikipedia.org/wiki/IETF_language_tag
66 // optional
67 LanguageCode string `json:"language_code"`
68 // IsBot true, if this user is a bot
69 // optional
70 IsBot bool `json:"is_bot"`
71}
72
73// String displays a simple text version of a user.
74//
75// It is normally a user's username, but falls back to a first/last
76// name as available.
77func (u *User) String() string {
78 if u == nil {
79 return ""
80 }
81 if u.UserName != "" {
82 return u.UserName
83 }
84
85 name := u.FirstName
86 if u.LastName != "" {
87 name += " " + u.LastName
88 }
89
90 return name
91}
92
93// GroupChat is a group chat.
94type GroupChat struct {
95 ID int `json:"id"`
96 Title string `json:"title"`
97}
98
99// ChatPhoto represents a chat photo.
100type ChatPhoto struct {
101 SmallFileID string `json:"small_file_id"`
102 BigFileID string `json:"big_file_id"`
103}
104
105// Chat contains information about the place a message was sent.
106type Chat struct {
107 // ID is a unique identifier for this chat
108 ID int64 `json:"id"`
109 // Type of chat, can be either “private”, “group”, “supergroup” or “channel”
110 Type string `json:"type"`
111 // Title for supergroups, channels and group chats
112 // optional
113 Title string `json:"title"`
114 // UserName for private chats, supergroups and channels if available
115 // optional
116 UserName string `json:"username"`
117 // FirstName of the other party in a private chat
118 // optional
119 FirstName string `json:"first_name"`
120 // LastName of the other party in a private chat
121 // optional
122 LastName string `json:"last_name"`
123 // AllMembersAreAdmins
124 // optional
125 AllMembersAreAdmins bool `json:"all_members_are_administrators"`
126 // Photo is a chat photo
127 Photo *ChatPhoto `json:"photo"`
128 // Description for groups, supergroups and channel chats
129 // optional
130 Description string `json:"description,omitempty"`
131 // InviteLink is a chat invite link, for groups, supergroups and channel chats.
132 // Each administrator in a chat generates their own invite links,
133 // so the bot must first generate the link using exportChatInviteLink
134 // optional
135 InviteLink string `json:"invite_link,omitempty"`
136 // PinnedMessage Pinned message, for groups, supergroups and channels
137 // optional
138 PinnedMessage *Message `json:"pinned_message"`
139}
140
141// IsPrivate returns if the Chat is a private conversation.
142func (c Chat) IsPrivate() bool {
143 return c.Type == "private"
144}
145
146// IsGroup returns if the Chat is a group.
147func (c Chat) IsGroup() bool {
148 return c.Type == "group"
149}
150
151// IsSuperGroup returns if the Chat is a supergroup.
152func (c Chat) IsSuperGroup() bool {
153 return c.Type == "supergroup"
154}
155
156// IsChannel returns if the Chat is a channel.
157func (c Chat) IsChannel() bool {
158 return c.Type == "channel"
159}
160
161// ChatConfig returns a ChatConfig struct for chat related methods.
162func (c Chat) ChatConfig() ChatConfig {
163 return ChatConfig{ChatID: c.ID}
164}
165
166// Message is returned by almost every request, and contains data about
167// almost anything.
168type Message struct {
169 // MessageID is a unique message identifier inside this chat
170 MessageID int `json:"message_id"`
171 // From is a sender, empty for messages sent to channels;
172 // optional
173 From *User `json:"from"`
174 // Date of the message was sent in Unix time
175 Date int `json:"date"`
176 // Chat is the conversation the message belongs to
177 Chat *Chat `json:"chat"`
178 // ForwardFrom for forwarded messages, sender of the original message;
179 // optional
180 ForwardFrom *User `json:"forward_from"`
181 // ForwardFromChat for messages forwarded from channels,
182 // information about the original channel;
183 // optional
184 ForwardFromChat *Chat `json:"forward_from_chat"`
185 // ForwardFromMessageID for messages forwarded from channels,
186 // identifier of the original message in the channel;
187 // optional
188 ForwardFromMessageID int `json:"forward_from_message_id"`
189 // ForwardDate for forwarded messages, date the original message was sent in Unix time;
190 // optional
191 ForwardDate int `json:"forward_date"`
192 // ReplyToMessage for replies, the original message.
193 // Note that the Message object in this field will not contain further ReplyToMessage fields
194 // even if it itself is a reply;
195 // optional
196 ReplyToMessage *Message `json:"reply_to_message"`
197 // ViaBot through which the message was sent;
198 // optional
199 ViaBot *User `json:"via_bot"`
200 // EditDate of the message was last edited in Unix time;
201 // optional
202 EditDate int `json:"edit_date"`
203 // MediaGroupID is the unique identifier of a media message group this message belongs to;
204 // optional
205 MediaGroupID string `json:"media_group_id"`
206 // AuthorSignature is the signature of the post author for messages in channels;
207 // optional
208 AuthorSignature string `json:"author_signature"`
209 // Text is for text messages, the actual UTF-8 text of the message, 0-4096 characters;
210 // optional
211 Text string `json:"text"`
212 // Entities is for text messages, special entities like usernames,
213 // URLs, bot commands, etc. that appear in the text;
214 // optional
215 Entities *[]MessageEntity `json:"entities"`
216 // CaptionEntities;
217 // optional
218 CaptionEntities *[]MessageEntity `json:"caption_entities"`
219 // Audio message is an audio file, information about the file;
220 // optional
221 Audio *Audio `json:"audio"`
222 // Document message is a general file, information about the file;
223 // optional
224 Document *Document `json:"document"`
225 // Animation message is an animation, information about the animation.
226 // For backward compatibility, when this field is set, the document field will also be set;
227 // optional
228 Animation *ChatAnimation `json:"animation"`
229 // Game message is a game, information about the game;
230 // optional
231 Game *Game `json:"game"`
232 // Photo message is a photo, available sizes of the photo;
233 // optional
234 Photo *[]PhotoSize `json:"photo"`
235 // Sticker message is a sticker, information about the sticker;
236 // optional
237 Sticker *Sticker `json:"sticker"`
238 // Video message is a video, information about the video;
239 // optional
240 Video *Video `json:"video"`
241 // VideoNote message is a video note, information about the video message;
242 // optional
243 VideoNote *VideoNote `json:"video_note"`
244 // Voice message is a voice message, information about the file;
245 // optional
246 Voice *Voice `json:"voice"`
247 // Caption for the animation, audio, document, photo, video or voice, 0-1024 characters;
248 // optional
249 Caption string `json:"caption"`
250 // Contact message is a shared contact, information about the contact;
251 // optional
252 Contact *Contact `json:"contact"`
253 // Location message is a shared location, information about the location;
254 // optional
255 Location *Location `json:"location"`
256 // Venue message is a venue, information about the venue.
257 // For backward compatibility, when this field is set, the location field will also be set;
258 // optional
259 Venue *Venue `json:"venue"`
260 // NewChatMembers that were added to the group or supergroup
261 // and information about them (the bot itself may be one of these members);
262 // optional
263 NewChatMembers *[]User `json:"new_chat_members"`
264 // LeftChatMember is a member was removed from the group,
265 // information about them (this member may be the bot itself);
266 // optional
267 LeftChatMember *User `json:"left_chat_member"`
268 // NewChatTitle is a chat title was changed to this value;
269 // optional
270 NewChatTitle string `json:"new_chat_title"`
271 // NewChatPhoto is a chat photo was change to this value;
272 // optional
273 NewChatPhoto *[]PhotoSize `json:"new_chat_photo"`
274 // DeleteChatPhoto is a service message: the chat photo was deleted;
275 // optional
276 DeleteChatPhoto bool `json:"delete_chat_photo"`
277 // GroupChatCreated is a service message: the group has been created;
278 // optional
279 GroupChatCreated bool `json:"group_chat_created"`
280 // SuperGroupChatCreated is a service message: the supergroup has been created.
281 // This field can't be received in a message coming through updates,
282 // because bot can't be a member of a supergroup when it is created.
283 // It can only be found in ReplyToMessage if someone replies to a very first message
284 // in a directly created supergroup;
285 // optional
286 SuperGroupChatCreated bool `json:"supergroup_chat_created"`
287 // ChannelChatCreated is a service message: the channel has been created.
288 // This field can't be received in a message coming through updates,
289 // because bot can't be a member of a channel when it is created.
290 // It can only be found in ReplyToMessage
291 // if someone replies to a very first message in a channel;
292 // optional
293 ChannelChatCreated bool `json:"channel_chat_created"`
294 // MigrateToChatID is the group has been migrated to a supergroup with the specified identifier.
295 // This number may be greater than 32 bits and some programming languages
296 // may have difficulty/silent defects in interpreting it.
297 // But it is smaller than 52 bits, so a signed 64 bit integer
298 // or double-precision float type are safe for storing this identifier;
299 // optional
300 MigrateToChatID int64 `json:"migrate_to_chat_id"`
301 // MigrateFromChatID is the supergroup has been migrated from a group with the specified identifier.
302 // This number may be greater than 32 bits and some programming languages
303 // may have difficulty/silent defects in interpreting it.
304 // But it is smaller than 52 bits, so a signed 64 bit integer
305 // or double-precision float type are safe for storing this identifier;
306 // optional
307 MigrateFromChatID int64 `json:"migrate_from_chat_id"`
308 // PinnedMessage is a specified message was pinned.
309 // Note that the Message object in this field will not contain further ReplyToMessage
310 // fields even if it is itself a reply;
311 // optional
312 PinnedMessage *Message `json:"pinned_message"`
313 // Invoice message is an invoice for a payment;
314 // optional
315 Invoice *Invoice `json:"invoice"`
316 // SuccessfulPayment message is a service message about a successful payment,
317 // information about the payment;
318 // optional
319 SuccessfulPayment *SuccessfulPayment `json:"successful_payment"`
320 // PassportData is a Telegram Passport data;
321 // optional
322 PassportData *PassportData `json:"passport_data,omitempty"`
323}
324
325// Time converts the message timestamp into a Time.
326func (m *Message) Time() time.Time {
327 return time.Unix(int64(m.Date), 0)
328}
329
330// IsCommand returns true if message starts with a "bot_command" entity.
331func (m *Message) IsCommand() bool {
332 if m.Entities == nil || len(*m.Entities) == 0 {
333 return false
334 }
335
336 entity := (*m.Entities)[0]
337 return entity.Offset == 0 && entity.IsCommand()
338}
339
340// Command checks if the message was a command and if it was, returns the
341// command. If the Message was not a command, it returns an empty string.
342//
343// If the command contains the at name syntax, it is removed. Use
344// CommandWithAt() if you do not want that.
345func (m *Message) Command() string {
346 command := m.CommandWithAt()
347
348 if i := strings.Index(command, "@"); i != -1 {
349 command = command[:i]
350 }
351
352 return command
353}
354
355// CommandWithAt checks if the message was a command and if it was, returns the
356// command. If the Message was not a command, it returns an empty string.
357//
358// If the command contains the at name syntax, it is not removed. Use Command()
359// if you want that.
360func (m *Message) CommandWithAt() string {
361 if !m.IsCommand() {
362 return ""
363 }
364
365 // IsCommand() checks that the message begins with a bot_command entity
366 entity := (*m.Entities)[0]
367 return m.Text[1:entity.Length]
368}
369
370// CommandArguments checks if the message was a command and if it was,
371// returns all text after the command name. If the Message was not a
372// command, it returns an empty string.
373//
374// Note: The first character after the command name is omitted:
375// - "/foo bar baz" yields "bar baz", not " bar baz"
376// - "/foo-bar baz" yields "bar baz", too
377// Even though the latter is not a command conforming to the spec, the API
378// marks "/foo" as command entity.
379func (m *Message) CommandArguments() string {
380 if !m.IsCommand() {
381 return ""
382 }
383
384 // IsCommand() checks that the message begins with a bot_command entity
385 entity := (*m.Entities)[0]
386 if len(m.Text) == entity.Length {
387 return "" // The command makes up the whole message
388 }
389
390 return m.Text[entity.Length+1:]
391}
392
393// MessageEntity contains information about data in a Message.
394type MessageEntity struct {
395 Type string `json:"type"`
396 Offset int `json:"offset"`
397 Length int `json:"length"`
398 URL string `json:"url"` // optional
399 User *User `json:"user"` // optional
400}
401
402// ParseURL attempts to parse a URL contained within a MessageEntity.
403func (e MessageEntity) ParseURL() (*url.URL, error) {
404 if e.URL == "" {
405 return nil, errors.New(ErrBadURL)
406 }
407
408 return url.Parse(e.URL)
409}
410
411// IsMention returns true if the type of the message entity is "mention" (@username).
412func (e MessageEntity) IsMention() bool {
413 return e.Type == "mention"
414}
415
416// IsHashtag returns true if the type of the message entity is "hashtag".
417func (e MessageEntity) IsHashtag() bool {
418 return e.Type == "hashtag"
419}
420
421// IsCommand returns true if the type of the message entity is "bot_command".
422func (e MessageEntity) IsCommand() bool {
423 return e.Type == "bot_command"
424}
425
426// IsUrl returns true if the type of the message entity is "url".
427func (e MessageEntity) IsUrl() bool {
428 return e.Type == "url"
429}
430
431// IsEmail returns true if the type of the message entity is "email".
432func (e MessageEntity) IsEmail() bool {
433 return e.Type == "email"
434}
435
436// IsBold returns true if the type of the message entity is "bold" (bold text).
437func (e MessageEntity) IsBold() bool {
438 return e.Type == "bold"
439}
440
441// IsItalic returns true if the type of the message entity is "italic" (italic text).
442func (e MessageEntity) IsItalic() bool {
443 return e.Type == "italic"
444}
445
446// IsCode returns true if the type of the message entity is "code" (monowidth string).
447func (e MessageEntity) IsCode() bool {
448 return e.Type == "code"
449}
450
451// IsPre returns true if the type of the message entity is "pre" (monowidth block).
452func (e MessageEntity) IsPre() bool {
453 return e.Type == "pre"
454}
455
456// IsTextLink returns true if the type of the message entity is "text_link" (clickable text URL).
457func (e MessageEntity) IsTextLink() bool {
458 return e.Type == "text_link"
459}
460
461// PhotoSize contains information about photos.
462type PhotoSize struct {
463 FileID string `json:"file_id"`
464 Width int `json:"width"`
465 Height int `json:"height"`
466 FileSize int `json:"file_size"` // optional
467}
468
469// Audio contains information about audio.
470type Audio struct {
471 FileID string `json:"file_id"`
472 Duration int `json:"duration"`
473 Performer string `json:"performer"` // optional
474 Title string `json:"title"` // optional
475 MimeType string `json:"mime_type"` // optional
476 FileSize int `json:"file_size"` // optional
477}
478
479// Document contains information about a document.
480type Document struct {
481 FileID string `json:"file_id"`
482 Thumbnail *PhotoSize `json:"thumb"` // optional
483 FileName string `json:"file_name"` // optional
484 MimeType string `json:"mime_type"` // optional
485 FileSize int `json:"file_size"` // optional
486}
487
488// Sticker contains information about a sticker.
489type Sticker struct {
490 FileUniqueID string `json:"file_unique_id"`
491 FileID string `json:"file_id"`
492 Width int `json:"width"`
493 Height int `json:"height"`
494 Thumbnail *PhotoSize `json:"thumb"` // optional
495 Emoji string `json:"emoji"` // optional
496 FileSize int `json:"file_size"` // optional
497 SetName string `json:"set_name"` // optional
498 IsAnimated bool `json:"is_animated"` // optional
499}
500
501// StickerSet contains information about an sticker set.
502type StickerSet struct {
503 Name string `json:"name"`
504 Title string `json:"title"`
505 IsAnimated bool `json:"is_animated"`
506 ContainsMasks bool `json:"contains_masks"`
507 Stickers []Sticker `json:"stickers"`
508}
509
510// ChatAnimation contains information about an animation.
511type ChatAnimation struct {
512 FileID string `json:"file_id"`
513 Width int `json:"width"`
514 Height int `json:"height"`
515 Duration int `json:"duration"`
516 Thumbnail *PhotoSize `json:"thumb"` // optional
517 FileName string `json:"file_name"` // optional
518 MimeType string `json:"mime_type"` // optional
519 FileSize int `json:"file_size"` // optional
520}
521
522// Video contains information about a video.
523type Video struct {
524 FileID string `json:"file_id"`
525 Width int `json:"width"`
526 Height int `json:"height"`
527 Duration int `json:"duration"`
528 Thumbnail *PhotoSize `json:"thumb"` // optional
529 MimeType string `json:"mime_type"` // optional
530 FileSize int `json:"file_size"` // optional
531}
532
533// VideoNote contains information about a video.
534type VideoNote struct {
535 FileID string `json:"file_id"`
536 Length int `json:"length"`
537 Duration int `json:"duration"`
538 Thumbnail *PhotoSize `json:"thumb"` // optional
539 FileSize int `json:"file_size"` // optional
540}
541
542// Voice contains information about a voice.
543type Voice struct {
544 FileID string `json:"file_id"`
545 Duration int `json:"duration"`
546 MimeType string `json:"mime_type"` // optional
547 FileSize int `json:"file_size"` // optional
548}
549
550// Contact contains information about a contact.
551//
552// Note that LastName and UserID may be empty.
553type Contact struct {
554 PhoneNumber string `json:"phone_number"`
555 FirstName string `json:"first_name"`
556 LastName string `json:"last_name"` // optional
557 UserID int `json:"user_id"` // optional
558}
559
560// Location contains information about a place.
561type Location struct {
562 Longitude float64 `json:"longitude"`
563 Latitude float64 `json:"latitude"`
564}
565
566// Venue contains information about a venue, including its Location.
567type Venue struct {
568 Location Location `json:"location"`
569 Title string `json:"title"`
570 Address string `json:"address"`
571 FoursquareID string `json:"foursquare_id"` // optional
572}
573
574// UserProfilePhotos contains a set of user profile photos.
575type UserProfilePhotos struct {
576 TotalCount int `json:"total_count"`
577 Photos [][]PhotoSize `json:"photos"`
578}
579
580// File contains information about a file to download from Telegram.
581type File struct {
582 FileID string `json:"file_id"`
583 FileSize int `json:"file_size"` // optional
584 FilePath string `json:"file_path"` // optional
585}
586
587// Link returns a full path to the download URL for a File.
588//
589// It requires the Bot Token to create the link.
590func (f *File) Link(token string) string {
591 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
592}
593
594// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
595type ReplyKeyboardMarkup struct {
596 Keyboard [][]KeyboardButton `json:"keyboard"`
597 ResizeKeyboard bool `json:"resize_keyboard"` // optional
598 OneTimeKeyboard bool `json:"one_time_keyboard"` // optional
599 Selective bool `json:"selective"` // optional
600}
601
602// KeyboardButton is a button within a custom keyboard.
603type KeyboardButton struct {
604 Text string `json:"text"`
605 RequestContact bool `json:"request_contact"`
606 RequestLocation bool `json:"request_location"`
607}
608
609// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
610type ReplyKeyboardHide struct {
611 HideKeyboard bool `json:"hide_keyboard"`
612 Selective bool `json:"selective"` // optional
613}
614
615// ReplyKeyboardRemove allows the Bot to hide a custom keyboard.
616type ReplyKeyboardRemove struct {
617 RemoveKeyboard bool `json:"remove_keyboard"`
618 Selective bool `json:"selective"`
619}
620
621// InlineKeyboardMarkup is a custom keyboard presented for an inline bot.
622type InlineKeyboardMarkup struct {
623 InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
624}
625
626// InlineKeyboardButton is a button within a custom keyboard for
627// inline query responses.
628//
629// Note that some values are references as even an empty string
630// will change behavior.
631//
632// CallbackGame, if set, MUST be first button in first row.
633type InlineKeyboardButton struct {
634 Text string `json:"text"`
635 URL *string `json:"url,omitempty"` // optional
636 CallbackData *string `json:"callback_data,omitempty"` // optional
637 SwitchInlineQuery *string `json:"switch_inline_query,omitempty"` // optional
638 SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"` // optional
639 CallbackGame *CallbackGame `json:"callback_game,omitempty"` // optional
640 Pay bool `json:"pay,omitempty"` // optional
641}
642
643// CallbackQuery is data sent when a keyboard button with callback data
644// is clicked.
645type CallbackQuery struct {
646 ID string `json:"id"`
647 From *User `json:"from"`
648 Message *Message `json:"message"` // optional
649 InlineMessageID string `json:"inline_message_id"` // optional
650 ChatInstance string `json:"chat_instance"`
651 Data string `json:"data"` // optional
652 GameShortName string `json:"game_short_name"` // optional
653}
654
655// ForceReply allows the Bot to have users directly reply to it without
656// additional interaction.
657type ForceReply struct {
658 ForceReply bool `json:"force_reply"`
659 Selective bool `json:"selective"` // optional
660}
661
662// ChatMember is information about a member in a chat.
663type ChatMember struct {
664 User *User `json:"user"`
665 Status string `json:"status"`
666 CustomTitle string `json:"custom_title,omitempty"` // optional
667 UntilDate int64 `json:"until_date,omitempty"` // optional
668 CanBeEdited bool `json:"can_be_edited,omitempty"` // optional
669 CanChangeInfo bool `json:"can_change_info,omitempty"` // optional
670 CanPostMessages bool `json:"can_post_messages,omitempty"` // optional
671 CanEditMessages bool `json:"can_edit_messages,omitempty"` // optional
672 CanDeleteMessages bool `json:"can_delete_messages,omitempty"` // optional
673 CanInviteUsers bool `json:"can_invite_users,omitempty"` // optional
674 CanRestrictMembers bool `json:"can_restrict_members,omitempty"` // optional
675 CanPinMessages bool `json:"can_pin_messages,omitempty"` // optional
676 CanPromoteMembers bool `json:"can_promote_members,omitempty"` // optional
677 CanSendMessages bool `json:"can_send_messages,omitempty"` // optional
678 CanSendMediaMessages bool `json:"can_send_media_messages,omitempty"` // optional
679 CanSendOtherMessages bool `json:"can_send_other_messages,omitempty"` // optional
680 CanAddWebPagePreviews bool `json:"can_add_web_page_previews,omitempty"` // optional
681}
682
683// IsCreator returns if the ChatMember was the creator of the chat.
684func (chat ChatMember) IsCreator() bool { return chat.Status == "creator" }
685
686// IsAdministrator returns if the ChatMember is a chat administrator.
687func (chat ChatMember) IsAdministrator() bool { return chat.Status == "administrator" }
688
689// IsMember returns if the ChatMember is a current member of the chat.
690func (chat ChatMember) IsMember() bool { return chat.Status == "member" }
691
692// HasLeft returns if the ChatMember left the chat.
693func (chat ChatMember) HasLeft() bool { return chat.Status == "left" }
694
695// WasKicked returns if the ChatMember was kicked from the chat.
696func (chat ChatMember) WasKicked() bool { return chat.Status == "kicked" }
697
698// Game is a game within Telegram.
699type Game struct {
700 Title string `json:"title"`
701 Description string `json:"description"`
702 Photo []PhotoSize `json:"photo"`
703 Text string `json:"text"`
704 TextEntities []MessageEntity `json:"text_entities"`
705 Animation Animation `json:"animation"`
706}
707
708// Animation is a GIF animation demonstrating the game.
709type Animation struct {
710 FileID string `json:"file_id"`
711 Thumb PhotoSize `json:"thumb"`
712 FileName string `json:"file_name"`
713 MimeType string `json:"mime_type"`
714 FileSize int `json:"file_size"`
715}
716
717// GameHighScore is a user's score and position on the leaderboard.
718type GameHighScore struct {
719 Position int `json:"position"`
720 User User `json:"user"`
721 Score int `json:"score"`
722}
723
724// CallbackGame is for starting a game in an inline keyboard button.
725type CallbackGame struct{}
726
727// WebhookInfo is information about a currently set webhook.
728type WebhookInfo struct {
729 URL string `json:"url"`
730 HasCustomCertificate bool `json:"has_custom_certificate"`
731 PendingUpdateCount int `json:"pending_update_count"`
732 LastErrorDate int `json:"last_error_date"` // optional
733 LastErrorMessage string `json:"last_error_message"` // optional
734 MaxConnections int `json:"max_connections"` // optional
735}
736
737// IsSet returns true if a webhook is currently set.
738func (info WebhookInfo) IsSet() bool {
739 return info.URL != ""
740}
741
742// InputMediaPhoto contains a photo for displaying as part of a media group.
743type InputMediaPhoto struct {
744 Type string `json:"type"`
745 Media string `json:"media"`
746 Caption string `json:"caption"`
747 ParseMode string `json:"parse_mode"`
748}
749
750// InputMediaVideo contains a video for displaying as part of a media group.
751type InputMediaVideo struct {
752 Type string `json:"type"`
753 Media string `json:"media"`
754 // thumb intentionally missing as it is not currently compatible
755 Caption string `json:"caption"`
756 ParseMode string `json:"parse_mode"`
757 Width int `json:"width"`
758 Height int `json:"height"`
759 Duration int `json:"duration"`
760 SupportsStreaming bool `json:"supports_streaming"`
761}
762
763// InlineQuery is a Query from Telegram for an inline request.
764type InlineQuery struct {
765 ID string `json:"id"`
766 From *User `json:"from"`
767 Location *Location `json:"location"` // optional
768 Query string `json:"query"`
769 Offset string `json:"offset"`
770}
771
772// InlineQueryResultArticle is an inline query response article.
773type InlineQueryResultArticle struct {
774 Type string `json:"type"` // required
775 ID string `json:"id"` // required
776 Title string `json:"title"` // required
777 InputMessageContent interface{} `json:"input_message_content,omitempty"` // required
778 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
779 URL string `json:"url"`
780 HideURL bool `json:"hide_url"`
781 Description string `json:"description"`
782 ThumbURL string `json:"thumb_url"`
783 ThumbWidth int `json:"thumb_width"`
784 ThumbHeight int `json:"thumb_height"`
785}
786
787// InlineQueryResultPhoto is an inline query response photo.
788type InlineQueryResultPhoto struct {
789 Type string `json:"type"` // required
790 ID string `json:"id"` // required
791 URL string `json:"photo_url"` // required
792 MimeType string `json:"mime_type"`
793 Width int `json:"photo_width"`
794 Height int `json:"photo_height"`
795 ThumbURL string `json:"thumb_url"`
796 Title string `json:"title"`
797 Description string `json:"description"`
798 Caption string `json:"caption"`
799 ParseMode string `json:"parse_mode"`
800 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
801 InputMessageContent interface{} `json:"input_message_content,omitempty"`
802}
803
804// InlineQueryResultCachedPhoto is an inline query response with cached photo.
805type InlineQueryResultCachedPhoto struct {
806 Type string `json:"type"` // required
807 ID string `json:"id"` // required
808 PhotoID string `json:"photo_file_id"` // required
809 Title string `json:"title"`
810 Description string `json:"description"`
811 Caption string `json:"caption"`
812 ParseMode string `json:"parse_mode"`
813 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
814 InputMessageContent interface{} `json:"input_message_content,omitempty"`
815}
816
817// InlineQueryResultGIF is an inline query response GIF.
818type InlineQueryResultGIF struct {
819 Type string `json:"type"` // required
820 ID string `json:"id"` // required
821 URL string `json:"gif_url"` // required
822 ThumbURL string `json:"thumb_url"` // required
823 Width int `json:"gif_width,omitempty"`
824 Height int `json:"gif_height,omitempty"`
825 Duration int `json:"gif_duration,omitempty"`
826 Title string `json:"title,omitempty"`
827 Caption string `json:"caption,omitempty"`
828 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
829 InputMessageContent interface{} `json:"input_message_content,omitempty"`
830}
831
832// InlineQueryResultCachedGIF is an inline query response with cached gif.
833type InlineQueryResultCachedGIF struct {
834 Type string `json:"type"` // required
835 ID string `json:"id"` // required
836 GifID string `json:"gif_file_id"` // required
837 Title string `json:"title"`
838 Caption string `json:"caption"`
839 ParseMode string `json:"parse_mode"`
840 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
841 InputMessageContent interface{} `json:"input_message_content,omitempty"`
842}
843
844// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
845type InlineQueryResultMPEG4GIF struct {
846 Type string `json:"type"` // required
847 ID string `json:"id"` // required
848 URL string `json:"mpeg4_url"` // required
849 Width int `json:"mpeg4_width"`
850 Height int `json:"mpeg4_height"`
851 Duration int `json:"mpeg4_duration"`
852 ThumbURL string `json:"thumb_url"`
853 Title string `json:"title"`
854 Caption string `json:"caption"`
855 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
856 InputMessageContent interface{} `json:"input_message_content,omitempty"`
857}
858
859// InlineQueryResultCachedMpeg4Gif is an inline query response with cached
860// H.264/MPEG-4 AVC video without sound gif.
861type InlineQueryResultCachedMpeg4Gif struct {
862 Type string `json:"type"` // required
863 ID string `json:"id"` // required
864 MGifID string `json:"mpeg4_file_id"` // required
865 Title string `json:"title"`
866 Caption string `json:"caption"`
867 ParseMode string `json:"parse_mode"`
868 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
869 InputMessageContent interface{} `json:"input_message_content,omitempty"`
870}
871
872// InlineQueryResultVideo is an inline query response video.
873type InlineQueryResultVideo struct {
874 Type string `json:"type"` // required
875 ID string `json:"id"` // required
876 URL string `json:"video_url"` // required
877 MimeType string `json:"mime_type"` // required
878 ThumbURL string `json:"thumb_url"`
879 Title string `json:"title"`
880 Caption string `json:"caption"`
881 Width int `json:"video_width"`
882 Height int `json:"video_height"`
883 Duration int `json:"video_duration"`
884 Description string `json:"description"`
885 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
886 InputMessageContent interface{} `json:"input_message_content,omitempty"`
887}
888
889// InlineQueryResultCachedVideo is an inline query response with cached video.
890type InlineQueryResultCachedVideo struct {
891 Type string `json:"type"` // required
892 ID string `json:"id"` // required
893 VideoID string `json:"video_file_id"` // required
894 Title string `json:"title"` // required
895 Description string `json:"description"`
896 Caption string `json:"caption"`
897 ParseMode string `json:"parse_mode"`
898 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
899 InputMessageContent interface{} `json:"input_message_content,omitempty"`
900}
901
902// InlineQueryResultCachedSticker is an inline query response with cached sticker.
903type InlineQueryResultCachedSticker struct {
904 Type string `json:"type"` // required
905 ID string `json:"id"` // required
906 StickerID string `json:"sticker_file_id"` // required
907 Title string `json:"title"` // required
908 ParseMode string `json:"parse_mode"`
909 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
910 InputMessageContent interface{} `json:"input_message_content,omitempty"`
911}
912
913// InlineQueryResultAudio is an inline query response audio.
914type InlineQueryResultAudio struct {
915 Type string `json:"type"` // required
916 ID string `json:"id"` // required
917 URL string `json:"audio_url"` // required
918 Title string `json:"title"` // required
919 Caption string `json:"caption"`
920 Performer string `json:"performer"`
921 Duration int `json:"audio_duration"`
922 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
923 InputMessageContent interface{} `json:"input_message_content,omitempty"`
924}
925
926// InlineQueryResultCachedAudio is an inline query response with cached audio.
927type InlineQueryResultCachedAudio struct {
928 Type string `json:"type"` // required
929 ID string `json:"id"` // required
930 AudioID string `json:"audio_file_id"` // required
931 Caption string `json:"caption"`
932 ParseMode string `json:"parse_mode"`
933 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
934 InputMessageContent interface{} `json:"input_message_content,omitempty"`
935}
936
937// InlineQueryResultVoice is an inline query response voice.
938type InlineQueryResultVoice struct {
939 Type string `json:"type"` // required
940 ID string `json:"id"` // required
941 URL string `json:"voice_url"` // required
942 Title string `json:"title"` // required
943 Caption string `json:"caption"`
944 Duration int `json:"voice_duration"`
945 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
946 InputMessageContent interface{} `json:"input_message_content,omitempty"`
947}
948
949// InlineQueryResultCachedVoice is an inline query response with cached voice.
950type InlineQueryResultCachedVoice struct {
951 Type string `json:"type"` // required
952 ID string `json:"id"` // required
953 VoiceID string `json:"voice_file_id"` // required
954 Title string `json:"title"` // required
955 Caption string `json:"caption"`
956 ParseMode string `json:"parse_mode"`
957 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
958 InputMessageContent interface{} `json:"input_message_content,omitempty"`
959}
960
961// InlineQueryResultDocument is an inline query response document.
962type InlineQueryResultDocument struct {
963 Type string `json:"type"` // required
964 ID string `json:"id"` // required
965 Title string `json:"title"` // required
966 Caption string `json:"caption"`
967 URL string `json:"document_url"` // required
968 MimeType string `json:"mime_type"` // required
969 Description string `json:"description"`
970 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
971 InputMessageContent interface{} `json:"input_message_content,omitempty"`
972 ThumbURL string `json:"thumb_url"`
973 ThumbWidth int `json:"thumb_width"`
974 ThumbHeight int `json:"thumb_height"`
975}
976
977// InlineQueryResultCachedDocument is an inline query response with cached document.
978type InlineQueryResultCachedDocument struct {
979 Type string `json:"type"` // required
980 ID string `json:"id"` // required
981 DocumentID string `json:"document_file_id"` // required
982 Title string `json:"title"` // required
983 Caption string `json:"caption"`
984 Description string `json:"description"`
985 ParseMode string `json:"parse_mode"`
986 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
987 InputMessageContent interface{} `json:"input_message_content,omitempty"`
988}
989
990// InlineQueryResultLocation is an inline query response location.
991type InlineQueryResultLocation struct {
992 Type string `json:"type"` // required
993 ID string `json:"id"` // required
994 Latitude float64 `json:"latitude"` // required
995 Longitude float64 `json:"longitude"` // required
996 Title string `json:"title"` // required
997 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
998 InputMessageContent interface{} `json:"input_message_content,omitempty"`
999 ThumbURL string `json:"thumb_url"`
1000 ThumbWidth int `json:"thumb_width"`
1001 ThumbHeight int `json:"thumb_height"`
1002}
1003
1004// InlineQueryResultVenue is an inline query response venue.
1005type InlineQueryResultVenue struct {
1006 Type string `json:"type"` // required
1007 ID string `json:"id"` // required
1008 Latitude float64 `json:"latitude"` // required
1009 Longitude float64 `json:"longitude"` // required
1010 Title string `json:"title"` // required
1011 Address string `json:"address"` // required
1012 FoursquareID string `json:"foursquare_id"`
1013 FoursquareType string `json:"foursquare_type"`
1014 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
1015 InputMessageContent interface{} `json:"input_message_content,omitempty"`
1016 ThumbURL string `json:"thumb_url"`
1017 ThumbWidth int `json:"thumb_width"`
1018 ThumbHeight int `json:"thumb_height"`
1019}
1020
1021// InlineQueryResultGame is an inline query response game.
1022type InlineQueryResultGame struct {
1023 Type string `json:"type"`
1024 ID string `json:"id"`
1025 GameShortName string `json:"game_short_name"`
1026 ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
1027}
1028
1029// ChosenInlineResult is an inline query result chosen by a User
1030type ChosenInlineResult struct {
1031 ResultID string `json:"result_id"`
1032 From *User `json:"from"`
1033 Location *Location `json:"location"`
1034 InlineMessageID string `json:"inline_message_id"`
1035 Query string `json:"query"`
1036}
1037
1038// InputTextMessageContent contains text for displaying
1039// as an inline query result.
1040type InputTextMessageContent struct {
1041 Text string `json:"message_text"`
1042 ParseMode string `json:"parse_mode"`
1043 DisableWebPagePreview bool `json:"disable_web_page_preview"`
1044}
1045
1046// InputLocationMessageContent contains a location for displaying
1047// as an inline query result.
1048type InputLocationMessageContent struct {
1049 Latitude float64 `json:"latitude"`
1050 Longitude float64 `json:"longitude"`
1051}
1052
1053// InputVenueMessageContent contains a venue for displaying
1054// as an inline query result.
1055type InputVenueMessageContent struct {
1056 Latitude float64 `json:"latitude"`
1057 Longitude float64 `json:"longitude"`
1058 Title string `json:"title"`
1059 Address string `json:"address"`
1060 FoursquareID string `json:"foursquare_id"`
1061}
1062
1063// InputContactMessageContent contains a contact for displaying
1064// as an inline query result.
1065type InputContactMessageContent struct {
1066 PhoneNumber string `json:"phone_number"`
1067 FirstName string `json:"first_name"`
1068 LastName string `json:"last_name"`
1069}
1070
1071// Invoice contains basic information about an invoice.
1072type Invoice struct {
1073 Title string `json:"title"`
1074 Description string `json:"description"`
1075 StartParameter string `json:"start_parameter"`
1076 Currency string `json:"currency"`
1077 TotalAmount int `json:"total_amount"`
1078}
1079
1080// LabeledPrice represents a portion of the price for goods or services.
1081type LabeledPrice struct {
1082 Label string `json:"label"`
1083 Amount int `json:"amount"`
1084}
1085
1086// ShippingAddress represents a shipping address.
1087type ShippingAddress struct {
1088 CountryCode string `json:"country_code"`
1089 State string `json:"state"`
1090 City string `json:"city"`
1091 StreetLine1 string `json:"street_line1"`
1092 StreetLine2 string `json:"street_line2"`
1093 PostCode string `json:"post_code"`
1094}
1095
1096// OrderInfo represents information about an order.
1097type OrderInfo struct {
1098 Name string `json:"name,omitempty"`
1099 PhoneNumber string `json:"phone_number,omitempty"`
1100 Email string `json:"email,omitempty"`
1101 ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
1102}
1103
1104// ShippingOption represents one shipping option.
1105type ShippingOption struct {
1106 ID string `json:"id"`
1107 Title string `json:"title"`
1108 Prices *[]LabeledPrice `json:"prices"`
1109}
1110
1111// SuccessfulPayment contains basic information about a successful payment.
1112type SuccessfulPayment struct {
1113 Currency string `json:"currency"`
1114 TotalAmount int `json:"total_amount"`
1115 InvoicePayload string `json:"invoice_payload"`
1116 ShippingOptionID string `json:"shipping_option_id,omitempty"`
1117 OrderInfo *OrderInfo `json:"order_info,omitempty"`
1118 TelegramPaymentChargeID string `json:"telegram_payment_charge_id"`
1119 ProviderPaymentChargeID string `json:"provider_payment_charge_id"`
1120}
1121
1122// ShippingQuery contains information about an incoming shipping query.
1123type ShippingQuery struct {
1124 ID string `json:"id"`
1125 From *User `json:"from"`
1126 InvoicePayload string `json:"invoice_payload"`
1127 ShippingAddress *ShippingAddress `json:"shipping_address"`
1128}
1129
1130// PreCheckoutQuery contains information about an incoming pre-checkout query.
1131type PreCheckoutQuery struct {
1132 ID string `json:"id"`
1133 From *User `json:"from"`
1134 Currency string `json:"currency"`
1135 TotalAmount int `json:"total_amount"`
1136 InvoicePayload string `json:"invoice_payload"`
1137 ShippingOptionID string `json:"shipping_option_id,omitempty"`
1138 OrderInfo *OrderInfo `json:"order_info,omitempty"`
1139}
1140
1141// Error is an error containing extra information returned by the Telegram API.
1142type Error struct {
1143 Code int
1144 Message string
1145 ResponseParameters
1146}
1147
1148func (e Error) Error() string {
1149 return e.Message
1150}
1151
1152// BotCommand represents a bot command.
1153type BotCommand struct {
1154 Command string `json:"command"`
1155 Description string `json:"description"`
1156}