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
116// IsCommand returns true if message starts from /
117func (m *Message) IsCommand() bool {
118 return m.Text != "" && m.Text[0] == '/'
119}
120
121// Command returns first word from message
122func (m *Message) Command() string {
123 return strings.Split(m.Text, " ")[0]
124}
125
126// PhotoSize contains information about photos, including ID and Width and Height.
127type PhotoSize struct {
128 FileID string `json:"file_id"`
129 Width int `json:"width"`
130 Height int `json:"height"`
131 FileSize int `json:"file_size"`
132}
133
134// Audio contains information about audio,
135// including ID, Duration, Performer and Title.
136type Audio struct {
137 FileID string `json:"file_id"`
138 Duration int `json:"duration"`
139 Performer string `json:"performer"`
140 Title string `json:"title"`
141 MimeType string `json:"mime_type"`
142 FileSize int `json:"file_size"`
143}
144
145// Document contains information about a document, including ID and a Thumbnail.
146type Document struct {
147 FileID string `json:"file_id"`
148 Thumbnail PhotoSize `json:"thumb"`
149 FileName string `json:"file_name"`
150 MimeType string `json:"mime_type"`
151 FileSize int `json:"file_size"`
152}
153
154// Sticker contains information about a sticker, including ID and Thumbnail.
155type Sticker struct {
156 FileID string `json:"file_id"`
157 Width int `json:"width"`
158 Height int `json:"height"`
159 Thumbnail PhotoSize `json:"thumb"`
160 FileSize int `json:"file_size"`
161}
162
163// Video contains information about a video, including ID and duration and Thumbnail.
164type Video struct {
165 FileID string `json:"file_id"`
166 Width int `json:"width"`
167 Height int `json:"height"`
168 Duration int `json:"duration"`
169 Thumbnail PhotoSize `json:"thumb"`
170 MimeType string `json:"mime_type"`
171 FileSize int `json:"file_size"`
172}
173
174// Voice contains information about a voice, including ID and duration.
175type Voice struct {
176 FileID string `json:"file_id"`
177 Duration int `json:"duration"`
178 MimeType string `json:"mime_type"`
179 FileSize int `json:"file_size"`
180}
181
182// Contact contains information about a contact, such as PhoneNumber and UserId.
183type Contact struct {
184 PhoneNumber string `json:"phone_number"`
185 FirstName string `json:"first_name"`
186 LastName string `json:"last_name"`
187 UserID int `json:"user_id"`
188}
189
190// Location contains information about a place, such as Longitude and Latitude.
191type Location struct {
192 Longitude float32 `json:"longitude"`
193 Latitude float32 `json:"latitude"`
194}
195
196// UserProfilePhotos contains information a set of user profile photos.
197type UserProfilePhotos struct {
198 TotalCount int `json:"total_count"`
199 Photos []PhotoSize `json:"photos"`
200}
201
202// File contains information about a file to download from Telegram
203type File struct {
204 FileID string `json:"file_id"`
205 FileSize int `json:"file_size"`
206 FilePath string `json:"file_path"`
207}
208
209// Link returns a full path to the download URL for a File.
210//
211// It requires the Bot Token to create the link.
212func (f *File) Link(token string) string {
213 return fmt.Sprintf(FileEndpoint, token, f.FilePath)
214}
215
216// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
217type ReplyKeyboardMarkup struct {
218 Keyboard [][]string `json:"keyboard"`
219 ResizeKeyboard bool `json:"resize_keyboard"`
220 OneTimeKeyboard bool `json:"one_time_keyboard"`
221 Selective bool `json:"selective"`
222}
223
224// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
225type ReplyKeyboardHide struct {
226 HideKeyboard bool `json:"hide_keyboard"`
227 Selective bool `json:"selective"`
228}
229
230// ForceReply allows the Bot to have users directly reply to it without additional interaction.
231type ForceReply struct {
232 ForceReply bool `json:"force_reply"`
233 Selective bool `json:"selective"`
234}