all repos — telegram-bot-api @ 1cede9cf458b162a92832af661b53b9b2f1ed221

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}
 23
 24// User is a user, contained in Message and returned by GetSelf.
 25type User struct {
 26	ID        int    `json:"id"`
 27	FirstName string `json:"first_name"`
 28	LastName  string `json:"last_name"`
 29	UserName  string `json:"username"`
 30}
 31
 32// String displays a simple text version of a user.
 33// It is normally a user's username,
 34// but falls back to a first/last name as available.
 35func (u *User) String() string {
 36	if u.UserName != "" {
 37		return u.UserName
 38	}
 39
 40	name := u.FirstName
 41	if u.LastName != "" {
 42		name += " " + u.LastName
 43	}
 44
 45	return name
 46}
 47
 48// GroupChat is a group chat, and not currently in use.
 49type GroupChat struct {
 50	ID    int    `json:"id"`
 51	Title string `json:"title"`
 52}
 53
 54// Chat is returned in Message, it contains information about the Chat a message was sent in.
 55type Chat struct {
 56	ID        int    `json:"id"`
 57	Type      string `json:"type"`
 58	Title     string `json:"title"`
 59	UserName  string `json:"username"`
 60	FirstName string `json:"first_name"`
 61	LastName  string `json:"last_name"`
 62}
 63
 64// IsPrivate returns true if the Chat is a private conversation
 65func (c *Chat) IsPrivate() bool {
 66	return c.Type == "private"
 67}
 68
 69// IsGroup returns true if the Chat is a group conversation
 70func (c *Chat) IsGroup() bool {
 71	return c.Type == "group"
 72}
 73
 74// IsSuperGroup returns true if the Chat is a supergroup conversation
 75func (c *Chat) IsSuperGroup() bool {
 76	return c.Type == "supergroup"
 77}
 78
 79// IsChannel returns true if the Chat is a channel
 80func (c *Chat) IsChannel() bool {
 81	return c.Type == "channel"
 82}
 83
 84// Message is returned by almost every request, and contains data about almost anything.
 85type Message struct {
 86	MessageID             int         `json:"message_id"`
 87	From                  User        `json:"from"`
 88	Date                  int         `json:"date"`
 89	Chat                  Chat        `json:"chat"`
 90	ForwardFrom           User        `json:"forward_from"`
 91	ForwardDate           int         `json:"forward_date"`
 92	ReplyToMessage        *Message    `json:"reply_to_message"`
 93	Text                  string      `json:"text"`
 94	Audio                 Audio       `json:"audio"`
 95	Document              Document    `json:"document"`
 96	Photo                 []PhotoSize `json:"photo"`
 97	Sticker               Sticker     `json:"sticker"`
 98	Video                 Video       `json:"video"`
 99	Voice                 Voice       `json:"voice"`
100	Caption               string      `json:"caption"`
101	Contact               Contact     `json:"contact"`
102	Location              Location    `json:"location"`
103	NewChatParticipant    User        `json:"new_chat_participant"`
104	LeftChatParticipant   User        `json:"left_chat_participant"`
105	NewChatTitle          string      `json:"new_chat_title"`
106	NewChatPhoto          []PhotoSize `json:"new_chat_photo"`
107	DeleteChatPhoto       bool        `json:"delete_chat_photo"`
108	GroupChatCreated      bool        `json:"group_chat_created"`
109	SuperGroupChatCreated bool        `json:"supergroup_chat_created"`
110	ChannelChatCreated    bool        `json:"channel_chat_created"`
111	MigrateToChatID       int         `json:"migrate_to_chat_id"`
112	MigrateFromChatID     int         `json:"migrate_from_chat_id"`
113}
114
115// Time converts the message timestamp into a Time.
116func (m *Message) Time() time.Time {
117	return time.Unix(int64(m.Date), 0)
118}
119
120// IsGroup returns if the message was sent to a group.
121func (m *Message) IsGroup() bool {
122	return m.Chat.IsGroup()
123}
124
125// IsCommand returns true if message starts from /
126func (m *Message) IsCommand() bool {
127	return m.Text != "" && m.Text[0] == '/'
128}
129
130// Command if message is command returns first word from message(entire command)
131// otherwise returns empty string
132func (m *Message) Command() string {
133	if m.IsCommand() {
134		return strings.SplitN(m.Text, " ", 2)[0]
135	}
136	return ""
137}
138
139// CommandArguments if message is command, returns all text after command, excluding the command itself
140// otherwise returns empty string
141func (m *Message) CommandArguments() string {
142	if m.IsCommand() {
143		split := strings.SplitN(m.Text, " ", 2)
144		if len(split) == 2 {
145			return strings.SplitN(m.Text, " ", 2)[1]
146		}
147	}
148	return ""
149}
150
151// PhotoSize contains information about photos, including ID and Width and Height.
152type PhotoSize struct {
153	FileID   string `json:"file_id"`
154	Width    int    `json:"width"`
155	Height   int    `json:"height"`
156	FileSize int    `json:"file_size"`
157}
158
159// Audio contains information about audio,
160// including ID, Duration, Performer and Title.
161type Audio struct {
162	FileID    string `json:"file_id"`
163	Duration  int    `json:"duration"`
164	Performer string `json:"performer"`
165	Title     string `json:"title"`
166	MimeType  string `json:"mime_type"`
167	FileSize  int    `json:"file_size"`
168}
169
170// Document contains information about a document, including ID and a Thumbnail.
171type Document struct {
172	FileID    string    `json:"file_id"`
173	Thumbnail PhotoSize `json:"thumb"`
174	FileName  string    `json:"file_name"`
175	MimeType  string    `json:"mime_type"`
176	FileSize  int       `json:"file_size"`
177}
178
179// Sticker contains information about a sticker, including ID and Thumbnail.
180type Sticker struct {
181	FileID    string    `json:"file_id"`
182	Width     int       `json:"width"`
183	Height    int       `json:"height"`
184	Thumbnail PhotoSize `json:"thumb"`
185	FileSize  int       `json:"file_size"`
186}
187
188// Video contains information about a video, including ID and duration and Thumbnail.
189type Video struct {
190	FileID    string    `json:"file_id"`
191	Width     int       `json:"width"`
192	Height    int       `json:"height"`
193	Duration  int       `json:"duration"`
194	Thumbnail PhotoSize `json:"thumb"`
195	MimeType  string    `json:"mime_type"`
196	FileSize  int       `json:"file_size"`
197}
198
199// Voice contains information about a voice, including ID and duration.
200type Voice struct {
201	FileID   string `json:"file_id"`
202	Duration int    `json:"duration"`
203	MimeType string `json:"mime_type"`
204	FileSize int    `json:"file_size"`
205}
206
207// Contact contains information about a contact, such as PhoneNumber and UserId.
208type Contact struct {
209	PhoneNumber string `json:"phone_number"`
210	FirstName   string `json:"first_name"`
211	LastName    string `json:"last_name"`
212	UserID      int    `json:"user_id"`
213}
214
215// Location contains information about a place, such as Longitude and Latitude.
216type Location struct {
217	Longitude float32 `json:"longitude"`
218	Latitude  float32 `json:"latitude"`
219}
220
221// UserProfilePhotos contains information a set of user profile photos.
222type UserProfilePhotos struct {
223	TotalCount int         `json:"total_count"`
224	Photos     []PhotoSize `json:"photos"`
225}
226
227// File contains information about a file to download from Telegram
228type File struct {
229	FileID   string `json:"file_id"`
230	FileSize int    `json:"file_size"`
231	FilePath string `json:"file_path"`
232}
233
234// Link returns a full path to the download URL for a File.
235//
236// It requires the Bot Token to create the link.
237func (f *File) Link(token string) string {
238	return fmt.Sprintf(FileEndpoint, token, f.FilePath)
239}
240
241// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
242type ReplyKeyboardMarkup struct {
243	Keyboard        [][]string `json:"keyboard"`
244	ResizeKeyboard  bool       `json:"resize_keyboard"`
245	OneTimeKeyboard bool       `json:"one_time_keyboard"`
246	Selective       bool       `json:"selective"`
247}
248
249// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
250type ReplyKeyboardHide struct {
251	HideKeyboard bool `json:"hide_keyboard"`
252	Selective    bool `json:"selective"`
253}
254
255// ForceReply allows the Bot to have users directly reply to it without additional interaction.
256type ForceReply struct {
257	ForceReply bool `json:"force_reply"`
258	Selective  bool `json:"selective"`
259}