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