types.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "time"
8)
9
10// APIResponse is a response from the Telegram API with the result stored raw.
11type APIResponse struct {
12 Ok bool `json:"ok"`
13 Result json.RawMessage `json:"result"`
14 ErrorCode int `json:"error_code"`
15 Description string `json:"description"`
16}
17
18// Update is an update response, from GetUpdates.
19type Update struct {
20 UpdateID int `json:"update_id"`
21 Message Message `json:"message"`
22}
23
24// User is a user, contained in Message and returned by GetSelf.
25type User struct {
26 ID int `json:"id"`
27 FirstName string `json:"first_name"`
28 LastName string `json:"last_name"`
29 UserName string `json:"username"`
30}
31
32// String displays a simple text version of a user.
33// It is normally a user's username,
34// but falls back to a first/last name as available.
35func (u *User) String() string {
36 if u.UserName != "" {
37 return u.UserName
38 }
39
40 name := u.FirstName
41 if u.LastName != "" {
42 name += " " + u.LastName
43 }
44
45 return name
46}
47
48// GroupChat is a group chat, and not currently in use.
49type GroupChat struct {
50 ID int `json:"id"`
51 Title string `json:"title"`
52}
53
54// Chat is returned in Message, it contains information about the Chat a message was sent in.
55type Chat struct {
56 ID int `json:"id"`
57 Type string `json:"type"`
58 Title string `json:"title"`
59 UserName string `json:"username"`
60 FirstName string `json:"first_name"`
61 LastName string `json:"last_name"`
62}
63
64// IsPrivate returns true if the Chat is a private conversation
65func (c *Chat) IsPrivate() bool {
66 return c.Type == "private"
67}
68
69// IsGroup returns true if the Chat is a group conversation
70func (c *Chat) IsGroup() bool {
71 return c.Type == "group"
72}
73
74// IsSuperGroup returns true if the Chat is a supergroup conversation
75func (c *Chat) IsSuperGroup() bool {
76 return c.Type == "supergroup"
77}
78
79// IsChannel returns true if the Chat is a channel
80func (c *Chat) IsChannel() bool {
81 return c.Type == "channel"
82}
83
84// Message is returned by almost every request, and contains data about almost anything.
85type Message struct {
86 MessageID int `json:"message_id"`
87 From User `json:"from"`
88 Date int `json:"date"`
89 Chat Chat `json:"chat"`
90 ForwardFrom User `json:"forward_from"`
91 ForwardDate int `json:"forward_date"`
92 ReplyToMessage *Message `json:"reply_to_message"`
93 Text string `json:"text"`
94 Audio Audio `json:"audio"`
95 Document Document `json:"document"`
96 Photo []PhotoSize `json:"photo"`
97 Sticker Sticker `json:"sticker"`
98 Video Video `json:"video"`
99 Voice Voice `json:"voice"`
100 Caption string `json:"caption"`
101 Contact Contact `json:"contact"`
102 Location Location `json:"location"`
103 NewChatParticipant User `json:"new_chat_participant"`
104 LeftChatParticipant User `json:"left_chat_participant"`
105 NewChatTitle string `json:"new_chat_title"`
106 NewChatPhoto []PhotoSize `json:"new_chat_photo"`
107 DeleteChatPhoto bool `json:"delete_chat_photo"`
108 GroupChatCreated bool `json:"group_chat_created"`
109 SuperGroupChatCreated bool `json:"supergroup_chat_created"`
110 ChannelChatCreated bool `json:"channel_chat_created"`
111 MigrateToChatID int `json:"migrate_to_chat_id"`
112 MigrateFromChatID int `json:"migrate_from_chat_id"`
113}
114
115// Time converts the message timestamp into a Time.
116func (m *Message) Time() time.Time {
117 return time.Unix(int64(m.Date), 0)
118}
119
120// IsGroup returns if the message was sent to a group.
121func (m *Message) IsGroup() bool {
122 return m.Chat.IsGroup()
123}
124
125// IsCommand returns true if message starts from /
126func (m *Message) IsCommand() bool {
127 return m.Text != "" && m.Text[0] == '/'
128}
129
130// Command returns first word from message
131func (m *Message) Command() string {
132 return strings.Split(m.Text, " ")[0]
133}
134
135// PhotoSize contains information about photos, including ID and Width and Height.
136type PhotoSize struct {
137 FileID string `json:"file_id"`
138 Width int `json:"width"`
139 Height int `json:"height"`
140 FileSize int `json:"file_size"`
141}
142
143// Audio contains information about audio,
144// including ID, Duration, Performer and Title.
145type Audio struct {
146 FileID string `json:"file_id"`
147 Duration int `json:"duration"`
148 Performer string `json:"performer"`
149 Title string `json:"title"`
150 MimeType string `json:"mime_type"`
151 FileSize int `json:"file_size"`
152}
153
154// Document contains information about a document, including ID and a Thumbnail.
155type Document struct {
156 FileID string `json:"file_id"`
157 Thumbnail PhotoSize `json:"thumb"`
158 FileName string `json:"file_name"`
159 MimeType string `json:"mime_type"`
160 FileSize int `json:"file_size"`
161}
162
163// Sticker contains information about a sticker, including ID and Thumbnail.
164type Sticker struct {
165 FileID string `json:"file_id"`
166 Width int `json:"width"`
167 Height int `json:"height"`
168 Thumbnail PhotoSize `json:"thumb"`
169 FileSize int `json:"file_size"`
170}
171
172// Video contains information about a video, including ID and duration and Thumbnail.
173type Video struct {
174 FileID string `json:"file_id"`
175 Width int `json:"width"`
176 Height int `json:"height"`
177 Duration int `json:"duration"`
178 Thumbnail PhotoSize `json:"thumb"`
179 MimeType string `json:"mime_type"`
180 FileSize int `json:"file_size"`
181}
182
183// Voice contains information about a voice, including ID and duration.
184type Voice struct {
185 FileID string `json:"file_id"`
186 Duration int `json:"duration"`
187 MimeType string `json:"mime_type"`
188 FileSize int `json:"file_size"`
189}
190
191// Contact contains information about a contact, such as PhoneNumber and UserId.
192type Contact struct {
193 PhoneNumber string `json:"phone_number"`
194 FirstName string `json:"first_name"`
195 LastName string `json:"last_name"`
196 UserID int `json:"user_id"`
197}
198
199// Location contains information about a place, such as Longitude and Latitude.
200type Location struct {
201 Longitude float32 `json:"longitude"`
202 Latitude float32 `json:"latitude"`
203}
204
205// UserProfilePhotos contains information a set of user profile photos.
206type UserProfilePhotos struct {
207 TotalCount int `json:"total_count"`
208 Photos []PhotoSize `json:"photos"`
209}
210
211// File contains information about a file to download from Telegram
212type File struct {
213 FileID string `json:"file_id"`
214 FileSize int `json:"file_size"`
215 FilePath string `json:"file_path"`
216}
217
218// Link returns a full path to the download URL for a File.
219//
220// It requires the Bot Token to create the link.
221func (f *File) Link(token string) string {
222 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
223}
224
225// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
226type ReplyKeyboardMarkup struct {
227 Keyboard [][]string `json:"keyboard"`
228 ResizeKeyboard bool `json:"resize_keyboard"`
229 OneTimeKeyboard bool `json:"one_time_keyboard"`
230 Selective bool `json:"selective"`
231}
232
233// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
234type ReplyKeyboardHide struct {
235 HideKeyboard bool `json:"hide_keyboard"`
236 Selective bool `json:"selective"`
237}
238
239// ForceReply allows the Bot to have users directly reply to it without additional interaction.
240type ForceReply struct {
241 ForceReply bool `json:"force_reply"`
242 Selective bool `json:"selective"`
243}