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