all repos — telegram-bot-api @ 5df7aae78f6647147f98154e618cc7af818cad26

Golang bindings for the Telegram Bot API

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
 11// stored raw.
 12type APIResponse struct {
 13	Ok          bool            `json:"ok"`
 14	Result      json.RawMessage `json:"result"`
 15	ErrorCode   int             `json:"error_code"`
 16	Description string          `json:"description"`
 17}
 18
 19// Update is an update response, from GetUpdates.
 20type Update struct {
 21	UpdateID           int                `json:"update_id"`
 22	Message            Message            `json:"message"`
 23	InlineQuery        InlineQuery        `json:"inline_query"`
 24	ChosenInlineResult ChosenInlineResult `json:"chosen_inline_result"`
 25}
 26
 27// User is a user on Telegram.
 28type User struct {
 29	ID        int    `json:"id"`
 30	FirstName string `json:"first_name"`
 31	LastName  string `json:"last_name"` // optional
 32	UserName  string `json:"username"`  // optional
 33}
 34
 35// String displays a simple text version of a user.
 36//
 37// It is normally a user's username, but falls back to a first/last
 38// name as available.
 39func (u *User) String() string {
 40	if u.UserName != "" {
 41		return u.UserName
 42	}
 43
 44	name := u.FirstName
 45	if u.LastName != "" {
 46		name += " " + u.LastName
 47	}
 48
 49	return name
 50}
 51
 52// GroupChat is a group chat.
 53type GroupChat struct {
 54	ID    int    `json:"id"`
 55	Title string `json:"title"`
 56}
 57
 58// Chat contains information about the place a message was sent.
 59type Chat struct {
 60	ID        int    `json:"id"`
 61	Type      string `json:"type"`
 62	Title     string `json:"title"`      // optional
 63	UserName  string `json:"username"`   // optional
 64	FirstName string `json:"first_name"` // optional
 65	LastName  string `json:"last_name"`  // optional
 66}
 67
 68// IsPrivate returns if the Chat is a private conversation.
 69func (c *Chat) IsPrivate() bool {
 70	return c.Type == "private"
 71}
 72
 73// IsGroup returns if the Chat is a group.
 74func (c *Chat) IsGroup() bool {
 75	return c.Type == "group"
 76}
 77
 78// IsSuperGroup returns if the Chat is a supergroup.
 79func (c *Chat) IsSuperGroup() bool {
 80	return c.Type == "supergroup"
 81}
 82
 83// IsChannel returns if the Chat is a channel.
 84func (c *Chat) IsChannel() bool {
 85	return c.Type == "channel"
 86}
 87
 88// Message is returned by almost every request, and contains data about
 89// almost anything.
 90type Message struct {
 91	MessageID             int         `json:"message_id"`
 92	From                  User        `json:"from"` // optional
 93	Date                  int         `json:"date"`
 94	Chat                  Chat        `json:"chat"`
 95	ForwardFrom           User        `json:"forward_from"`            // optional
 96	ForwardDate           int         `json:"forward_date"`            // optional
 97	ReplyToMessage        *Message    `json:"reply_to_message"`        // optional
 98	Text                  string      `json:"text"`                    // optional
 99	Audio                 Audio       `json:"audio"`                   // optional
100	Document              Document    `json:"document"`                // optional
101	Photo                 []PhotoSize `json:"photo"`                   // optional
102	Sticker               Sticker     `json:"sticker"`                 // optional
103	Video                 Video       `json:"video"`                   // optional
104	Voice                 Voice       `json:"voice"`                   // optional
105	Caption               string      `json:"caption"`                 // optional
106	Contact               Contact     `json:"contact"`                 // optional
107	Location              Location    `json:"location"`                // optional
108	NewChatParticipant    User        `json:"new_chat_participant"`    // optional
109	LeftChatParticipant   User        `json:"left_chat_participant"`   // optional
110	NewChatTitle          string      `json:"new_chat_title"`          // optional
111	NewChatPhoto          []PhotoSize `json:"new_chat_photo"`          // optional
112	DeleteChatPhoto       bool        `json:"delete_chat_photo"`       // optional
113	GroupChatCreated      bool        `json:"group_chat_created"`      // optional
114	SuperGroupChatCreated bool        `json:"supergroup_chat_created"` // optional
115	ChannelChatCreated    bool        `json:"channel_chat_created"`    // optional
116	MigrateToChatID       int         `json:"migrate_to_chat_id"`      // optional
117	MigrateFromChatID     int         `json:"migrate_from_chat_id"`    // optional
118}
119
120// Time converts the message timestamp into a Time.
121func (m *Message) Time() time.Time {
122	return time.Unix(int64(m.Date), 0)
123}
124
125// IsCommand returns true if message starts with '/'.
126func (m *Message) IsCommand() bool {
127	return m.Text != "" && m.Text[0] == '/'
128}
129
130// Command checks if the message was a command and if it was, returns the
131// command. If the Message was not a command, it returns an empty string.
132func (m *Message) Command() string {
133	if !m.IsCommand() {
134		return ""
135	}
136
137	return strings.SplitN(m.Text, " ", 2)[0]
138}
139
140// CommandArguments checks if the message was a command and if it was,
141// returns all text after the command name. If the Message was not a
142// command, it returns an empty string.
143func (m *Message) CommandArguments() string {
144	if !m.IsCommand() {
145		return ""
146	}
147
148	split := strings.SplitN(m.Text, " ", 2)
149	if len(split) != 2 {
150		return ""
151	}
152
153	return strings.SplitN(m.Text, " ", 2)[1]
154}
155
156// PhotoSize contains information about photos.
157type PhotoSize struct {
158	FileID   string `json:"file_id"`
159	Width    int    `json:"width"`
160	Height   int    `json:"height"`
161	FileSize int    `json:"file_size"` // optional
162}
163
164// Audio contains information about audio.
165type Audio struct {
166	FileID    string `json:"file_id"`
167	Duration  int    `json:"duration"`
168	Performer string `json:"performer"` // optional
169	Title     string `json:"title"`     // optional
170	MimeType  string `json:"mime_type"` // optional
171	FileSize  int    `json:"file_size"` // optional
172}
173
174// Document contains information about a document.
175type Document struct {
176	FileID    string    `json:"file_id"`
177	Thumbnail PhotoSize `json:"thumb"`     // optional
178	FileName  string    `json:"file_name"` // optional
179	MimeType  string    `json:"mime_type"` // optional
180	FileSize  int       `json:"file_size"` // optional
181}
182
183// Sticker contains information about a sticker.
184type Sticker struct {
185	FileID    string    `json:"file_id"`
186	Width     int       `json:"width"`
187	Height    int       `json:"height"`
188	Thumbnail PhotoSize `json:"thumb"`     // optional
189	FileSize  int       `json:"file_size"` // optional
190}
191
192// Video contains information about a video.
193type Video struct {
194	FileID    string    `json:"file_id"`
195	Width     int       `json:"width"`
196	Height    int       `json:"height"`
197	Duration  int       `json:"duration"`
198	Thumbnail PhotoSize `json:"thumb"`     // optional
199	MimeType  string    `json:"mime_type"` // optional
200	FileSize  int       `json:"file_size"` // optional
201}
202
203// Voice contains information about a voice.
204type Voice struct {
205	FileID   string `json:"file_id"`
206	Duration int    `json:"duration"`
207	MimeType string `json:"mime_type"` // optional
208	FileSize int    `json:"file_size"` // optional
209}
210
211// Contact contains information about a contact.
212//
213// Note that LastName and UserID may be empty.
214type Contact struct {
215	PhoneNumber string `json:"phone_number"`
216	FirstName   string `json:"first_name"`
217	LastName    string `json:"last_name"` // optional
218	UserID      int    `json:"user_id"`   // optional
219}
220
221// Location contains information about a place.
222type Location struct {
223	Longitude float32 `json:"longitude"`
224	Latitude  float32 `json:"latitude"`
225}
226
227// UserProfilePhotos contains information a set of user profile photos.
228type UserProfilePhotos struct {
229	TotalCount int         `json:"total_count"`
230	Photos     []PhotoSize `json:"photos"`
231}
232
233// File contains information about a file to download from Telegram.
234type File struct {
235	FileID   string `json:"file_id"`
236	FileSize int    `json:"file_size"` // optional
237	FilePath string `json:"file_path"` // optional
238}
239
240// Link returns a full path to the download URL for a File.
241//
242// It requires the Bot Token to create the link.
243func (f *File) Link(token string) string {
244	return fmt.Sprintf(FileEndpoint, token, f.FilePath)
245}
246
247// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
248type ReplyKeyboardMarkup struct {
249	Keyboard        [][]string `json:"keyboard"`
250	ResizeKeyboard  bool       `json:"resize_keyboard"`   // optional
251	OneTimeKeyboard bool       `json:"one_time_keyboard"` // optional
252	Selective       bool       `json:"selective"`         // optional
253}
254
255// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
256type ReplyKeyboardHide struct {
257	HideKeyboard bool `json:"hide_keyboard"`
258	Selective    bool `json:"selective"` // optional
259}
260
261// ForceReply allows the Bot to have users directly reply to it without
262// additional interaction.
263type ForceReply struct {
264	ForceReply bool `json:"force_reply"`
265	Selective  bool `json:"selective"` // optional
266}
267
268// InlineQuery is a Query from Telegram for an inline request.
269type InlineQuery struct {
270	ID     string `json:"id"`
271	From   User   `json:"user"`
272	Query  string `json:"query"`
273	Offset string `json:"offset"`
274}
275
276// InlineQueryResult is the base type that all InlineQuery Results have.
277type InlineQueryResult struct {
278	Type string `json:"type"` // required
279	ID   string `json:"id"`   // required
280}
281
282// InlineQueryResultArticle is an inline query response article.
283type InlineQueryResultArticle struct {
284	InlineQueryResult
285	Title                 string `json:"title"`        // required
286	MessageText           string `json:"message_text"` // required
287	ParseMode             string `json:"parse_mode"`
288	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
289	URL                   string `json:"url"`
290	HideURL               bool   `json:"hide_url"`
291	Description           string `json:"description"`
292	ThumbURL              string `json:"thumb_url"`
293	ThumbWidth            int    `json:"thumb_width"`
294	ThumbHeight           int    `json:"thumb_height"`
295}
296
297// InlineQueryResultPhoto is an inline query response photo.
298type InlineQueryResultPhoto struct {
299	InlineQueryResult
300	URL                   string `json:"photo_url"` // required
301	MimeType              string `json:"mime_type"`
302	Width                 int    `json:"photo_width"`
303	Height                int    `json:"photo_height"`
304	ThumbURL              string `json:"thumb_url"`
305	Title                 string `json:"title"`
306	Description           string `json:"description"`
307	Caption               string `json:"caption"`
308	MessageText           string `json:"message_text"`
309	ParseMode             string `json:"parse_mode"`
310	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
311}
312
313// InlineQueryResultGIF is an inline query response GIF.
314type InlineQueryResultGIF struct {
315	InlineQueryResult
316	URL                   string `json:"gif_url"` // required
317	Width                 int    `json:"gif_width"`
318	Height                int    `json:"gif_height"`
319	ThumbURL              string `json:"thumb_url"`
320	Title                 string `json:"title"`
321	Caption               string `json:"caption"`
322	MessageText           string `json:"message_text"`
323	ParseMode             string `json:"parse_mode"`
324	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
325}
326
327// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
328type InlineQueryResultMPEG4GIF struct {
329	InlineQueryResult
330	URL                   string `json:"mpeg4_url"` // required
331	Width                 int    `json:"mpeg4_width"`
332	Height                int    `json:"mpeg4_height"`
333	ThumbURL              string `json:"thumb_url"`
334	Title                 string `json:"title"`
335	Caption               string `json:"caption"`
336	MessageText           string `json:"message_text"`
337	ParseMode             string `json:"parse_mode"`
338	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
339}
340
341// InlineQueryResultVideo is an inline query response video.
342type InlineQueryResultVideo struct {
343	InlineQueryResult
344	URL                   string `json:"video_url"`    // required
345	MimeType              string `json:"mime_type"`    // required
346	MessageText           string `json:"message_text"` // required
347	ParseMode             string `json:"parse_mode"`
348	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
349	Width                 int    `json:"video_width"`
350	Height                int    `json:"video_height"`
351	ThumbURL              string `json:"thumb_url"`
352	Title                 string `json:"title"`
353	Description           string `json:"description"`
354}
355
356// ChosenInlineResult is an inline query result chosen by a User
357type ChosenInlineResult struct {
358	ResultID string `json:"result_id"`
359	From     User   `json:"from"`
360	Query    string `json:"query"`
361}