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