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// IsChannel returns true if the Chat is a channel
75func (c *Chat) IsChannel() bool {
76 return c.Type == "channel"
77}
78
79// Message is returned by almost every request, and contains data about almost anything.
80type Message struct {
81 MessageID int `json:"message_id"`
82 From User `json:"from"`
83 Date int `json:"date"`
84 Chat Chat `json:"chat"`
85 ForwardFrom User `json:"forward_from"`
86 ForwardDate int `json:"forward_date"`
87 ReplyToMessage *Message `json:"reply_to_message"`
88 Text string `json:"text"`
89 Audio Audio `json:"audio"`
90 Document Document `json:"document"`
91 Photo []PhotoSize `json:"photo"`
92 Sticker Sticker `json:"sticker"`
93 Video Video `json:"video"`
94 Voice Voice `json:"voice"`
95 Caption string `json:"caption"`
96 Contact Contact `json:"contact"`
97 Location Location `json:"location"`
98 NewChatParticipant User `json:"new_chat_participant"`
99 LeftChatParticipant User `json:"left_chat_participant"`
100 NewChatTitle string `json:"new_chat_title"`
101 NewChatPhoto []PhotoSize `json:"new_chat_photo"`
102 DeleteChatPhoto bool `json:"delete_chat_photo"`
103 GroupChatCreated bool `json:"group_chat_created"`
104}
105
106// Time converts the message timestamp into a Time.
107func (m *Message) Time() time.Time {
108 return time.Unix(int64(m.Date), 0)
109}
110
111// IsGroup returns if the message was sent to a group.
112func (m *Message) IsGroup() bool {
113 return m.From.ID != m.Chat.ID
114}
115
116func (m *Message) IsCommand() bool {
117 return m.Text != "" && m.Text[0] == '/'
118}
119
120func (m *Message) Command() string {
121 return strings.Split(m.Text, " ")[0]
122}
123
124// PhotoSize contains information about photos, including ID and Width and Height.
125type PhotoSize struct {
126 FileID string `json:"file_id"`
127 Width int `json:"width"`
128 Height int `json:"height"`
129 FileSize int `json:"file_size"`
130}
131
132// Audio contains information about audio,
133// including ID, Duration, Performer and Title.
134type Audio struct {
135 FileID string `json:"file_id"`
136 Duration int `json:"duration"`
137 Performer string `json:"performer"`
138 Title string `json:"title"`
139 MimeType string `json:"mime_type"`
140 FileSize int `json:"file_size"`
141}
142
143// Document contains information about a document, including ID and a Thumbnail.
144type Document struct {
145 FileID string `json:"file_id"`
146 Thumbnail PhotoSize `json:"thumb"`
147 FileName string `json:"file_name"`
148 MimeType string `json:"mime_type"`
149 FileSize int `json:"file_size"`
150}
151
152// Sticker contains information about a sticker, including ID and Thumbnail.
153type Sticker struct {
154 FileID string `json:"file_id"`
155 Width int `json:"width"`
156 Height int `json:"height"`
157 Thumbnail PhotoSize `json:"thumb"`
158 FileSize int `json:"file_size"`
159}
160
161// Video contains information about a video, including ID and duration and Thumbnail.
162type Video struct {
163 FileID string `json:"file_id"`
164 Width int `json:"width"`
165 Height int `json:"height"`
166 Duration int `json:"duration"`
167 Thumbnail PhotoSize `json:"thumb"`
168 MimeType string `json:"mime_type"`
169 FileSize int `json:"file_size"`
170}
171
172// Voice contains information about a voice, including ID and duration.
173type Voice struct {
174 FileID string `json:"file_id"`
175 Duration int `json:"duration"`
176 MimeType string `json:"mime_type"`
177 FileSize int `json:"file_size"`
178}
179
180// Contact contains information about a contact, such as PhoneNumber and UserId.
181type Contact struct {
182 PhoneNumber string `json:"phone_number"`
183 FirstName string `json:"first_name"`
184 LastName string `json:"last_name"`
185 UserID int `json:"user_id"`
186}
187
188// Location contains information about a place, such as Longitude and Latitude.
189type Location struct {
190 Longitude float32 `json:"longitude"`
191 Latitude float32 `json:"latitude"`
192}
193
194// UserProfilePhotos contains information a set of user profile photos.
195type UserProfilePhotos struct {
196 TotalCount int `json:"total_count"`
197 Photos []PhotoSize `json:"photos"`
198}
199
200// File contains information about a file to download from Telegram
201type File struct {
202 FileID string `json:"file_id"`
203 FileSize int `json:"file_size"`
204 FilePath string `json:"file_path"`
205}
206
207// Link returns a full path to the download URL for a File.
208//
209// It requires the Bot Token to create the link.
210func (f *File) Link(token string) string {
211 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
212}
213
214// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
215type ReplyKeyboardMarkup struct {
216 Keyboard [][]string `json:"keyboard"`
217 ResizeKeyboard bool `json:"resize_keyboard"`
218 OneTimeKeyboard bool `json:"one_time_keyboard"`
219 Selective bool `json:"selective"`
220}
221
222// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
223type ReplyKeyboardHide struct {
224 HideKeyboard bool `json:"hide_keyboard"`
225 Selective bool `json:"selective"`
226}
227
228// ForceReply allows the Bot to have users directly reply to it without additional interaction.
229type ForceReply struct {
230 ForceReply bool `json:"force_reply"`
231 Selective bool `json:"selective"`
232}