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