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