all repos — telegram-bot-api @ 10c5261d919e9311450a5ddb950c759b31f8ec0d

Golang bindings for the Telegram Bot API

types.go (view raw)

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