all repos — telegram-bot-api @ af934e3e10c054a3c58e0737434165797d62184c

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.
132//
133// If the command contains the at bot syntax, it removes the bot name.
134func (m *Message) Command() string {
135	if !m.IsCommand() {
136		return ""
137	}
138
139	command := strings.SplitN(m.Text, " ", 2)[0][1:]
140
141	if i := strings.Index(command, "@"); i != -1 {
142		command = command[:i]
143	}
144
145	return command
146}
147
148// CommandArguments checks if the message was a command and if it was,
149// returns all text after the command name. If the Message was not a
150// command, it returns an empty string.
151func (m *Message) CommandArguments() string {
152	if !m.IsCommand() {
153		return ""
154	}
155
156	split := strings.SplitN(m.Text, " ", 2)
157	if len(split) != 2 {
158		return ""
159	}
160
161	return strings.SplitN(m.Text, " ", 2)[1]
162}
163
164// PhotoSize contains information about photos.
165type PhotoSize struct {
166	FileID   string `json:"file_id"`
167	Width    int    `json:"width"`
168	Height   int    `json:"height"`
169	FileSize int    `json:"file_size"` // optional
170}
171
172// Audio contains information about audio.
173type Audio struct {
174	FileID    string `json:"file_id"`
175	Duration  int    `json:"duration"`
176	Performer string `json:"performer"` // optional
177	Title     string `json:"title"`     // optional
178	MimeType  string `json:"mime_type"` // optional
179	FileSize  int    `json:"file_size"` // optional
180}
181
182// Document contains information about a document.
183type Document struct {
184	FileID    string    `json:"file_id"`
185	Thumbnail PhotoSize `json:"thumb"`     // optional
186	FileName  string    `json:"file_name"` // optional
187	MimeType  string    `json:"mime_type"` // optional
188	FileSize  int       `json:"file_size"` // optional
189}
190
191// Sticker contains information about a sticker.
192type Sticker struct {
193	FileID    string    `json:"file_id"`
194	Width     int       `json:"width"`
195	Height    int       `json:"height"`
196	Thumbnail PhotoSize `json:"thumb"`     // optional
197	FileSize  int       `json:"file_size"` // optional
198}
199
200// Video contains information about a video.
201type Video struct {
202	FileID    string    `json:"file_id"`
203	Width     int       `json:"width"`
204	Height    int       `json:"height"`
205	Duration  int       `json:"duration"`
206	Thumbnail PhotoSize `json:"thumb"`     // optional
207	MimeType  string    `json:"mime_type"` // optional
208	FileSize  int       `json:"file_size"` // optional
209}
210
211// Voice contains information about a voice.
212type Voice struct {
213	FileID   string `json:"file_id"`
214	Duration int    `json:"duration"`
215	MimeType string `json:"mime_type"` // optional
216	FileSize int    `json:"file_size"` // optional
217}
218
219// Contact contains information about a contact.
220//
221// Note that LastName and UserID may be empty.
222type Contact struct {
223	PhoneNumber string `json:"phone_number"`
224	FirstName   string `json:"first_name"`
225	LastName    string `json:"last_name"` // optional
226	UserID      int    `json:"user_id"`   // optional
227}
228
229// Location contains information about a place.
230type Location struct {
231	Longitude float32 `json:"longitude"`
232	Latitude  float32 `json:"latitude"`
233}
234
235// UserProfilePhotos contains information a set of user profile photos.
236type UserProfilePhotos struct {
237	TotalCount int         `json:"total_count"`
238	Photos     []PhotoSize `json:"photos"`
239}
240
241// File contains information about a file to download from Telegram.
242type File struct {
243	FileID   string `json:"file_id"`
244	FileSize int    `json:"file_size"` // optional
245	FilePath string `json:"file_path"` // optional
246}
247
248// Link returns a full path to the download URL for a File.
249//
250// It requires the Bot Token to create the link.
251func (f *File) Link(token string) string {
252	return fmt.Sprintf(FileEndpoint, token, f.FilePath)
253}
254
255// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
256type ReplyKeyboardMarkup struct {
257	Keyboard        [][]string `json:"keyboard"`
258	ResizeKeyboard  bool       `json:"resize_keyboard"`   // optional
259	OneTimeKeyboard bool       `json:"one_time_keyboard"` // optional
260	Selective       bool       `json:"selective"`         // optional
261}
262
263// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
264type ReplyKeyboardHide struct {
265	HideKeyboard bool `json:"hide_keyboard"`
266	Selective    bool `json:"selective"` // optional
267}
268
269// ForceReply allows the Bot to have users directly reply to it without
270// additional interaction.
271type ForceReply struct {
272	ForceReply bool `json:"force_reply"`
273	Selective  bool `json:"selective"` // optional
274}
275
276// InlineQuery is a Query from Telegram for an inline request.
277type InlineQuery struct {
278	ID     string `json:"id"`
279	From   User   `json:"from"`
280	Query  string `json:"query"`
281	Offset string `json:"offset"`
282}
283
284// InlineQueryResultArticle is an inline query response article.
285type InlineQueryResultArticle struct {
286	Type                  string `json:"type"`         // required
287	ID                    string `json:"id"`           // required
288	Title                 string `json:"title"`        // required
289	MessageText           string `json:"message_text"` // required
290	ParseMode             string `json:"parse_mode"`
291	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
292	URL                   string `json:"url"`
293	HideURL               bool   `json:"hide_url"`
294	Description           string `json:"description"`
295	ThumbURL              string `json:"thumb_url"`
296	ThumbWidth            int    `json:"thumb_width"`
297	ThumbHeight           int    `json:"thumb_height"`
298}
299
300// InlineQueryResultPhoto is an inline query response photo.
301type InlineQueryResultPhoto struct {
302	Type                  string `json:"type"`      // required
303	ID                    string `json:"id"`        // required
304	URL                   string `json:"photo_url"` // required
305	MimeType              string `json:"mime_type"`
306	Width                 int    `json:"photo_width"`
307	Height                int    `json:"photo_height"`
308	ThumbURL              string `json:"thumb_url"`
309	Title                 string `json:"title"`
310	Description           string `json:"description"`
311	Caption               string `json:"caption"`
312	MessageText           string `json:"message_text"`
313	ParseMode             string `json:"parse_mode"`
314	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
315}
316
317// InlineQueryResultGIF is an inline query response GIF.
318type InlineQueryResultGIF struct {
319	Type                  string `json:"type"`    // required
320	ID                    string `json:"id"`      // required
321	URL                   string `json:"gif_url"` // required
322	Width                 int    `json:"gif_width"`
323	Height                int    `json:"gif_height"`
324	ThumbURL              string `json:"thumb_url"`
325	Title                 string `json:"title"`
326	Caption               string `json:"caption"`
327	MessageText           string `json:"message_text"`
328	ParseMode             string `json:"parse_mode"`
329	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
330}
331
332// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
333type InlineQueryResultMPEG4GIF struct {
334	Type                  string `json:"type"`      // required
335	ID                    string `json:"id"`        // required
336	URL                   string `json:"mpeg4_url"` // required
337	Width                 int    `json:"mpeg4_width"`
338	Height                int    `json:"mpeg4_height"`
339	ThumbURL              string `json:"thumb_url"`
340	Title                 string `json:"title"`
341	Caption               string `json:"caption"`
342	MessageText           string `json:"message_text"`
343	ParseMode             string `json:"parse_mode"`
344	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
345}
346
347// InlineQueryResultVideo is an inline query response video.
348type InlineQueryResultVideo struct {
349	Type                  string `json:"type"`         // required
350	ID                    string `json:"id"`           // required
351	URL                   string `json:"video_url"`    // required
352	MimeType              string `json:"mime_type"`    // required
353	MessageText           string `json:"message_text"` // required
354	ParseMode             string `json:"parse_mode"`
355	DisableWebPagePreview bool   `json:"disable_web_page_preview"`
356	Width                 int    `json:"video_width"`
357	Height                int    `json:"video_height"`
358	ThumbURL              string `json:"thumb_url"`
359	Title                 string `json:"title"`
360	Description           string `json:"description"`
361}
362
363// ChosenInlineResult is an inline query result chosen by a User
364type ChosenInlineResult struct {
365	ResultID string `json:"result_id"`
366	From     User   `json:"from"`
367	Query    string `json:"query"`
368}