all repos — telegram-bot-api @ 1ae7803be06a7a521004632d8aa02dbc38c068f8

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