all repos — telegram-bot-api @ 91e1825b161873895b340ec24ff64c8ceb66898c

Golang bindings for the Telegram Bot API

types.go (view raw)

  1package tgbotapi
  2
  3import (
  4	"encoding/json"
  5	"time"
  6)
  7
  8// APIResponse is a response from the Telegram API with the result stored raw.
  9type APIResponse struct {
 10	Ok          bool            `json:"ok"`
 11	Result      json.RawMessage `json:"result"`
 12	ErrorCode   int             `json:"error_code"`
 13	Description string          `json:"description"`
 14}
 15
 16// Update is an update response, from GetUpdates.
 17type Update struct {
 18	UpdateID int     `json:"update_id"`
 19	Message  Message `json:"message"`
 20}
 21
 22// User is a user, contained in Message and returned by GetSelf.
 23type User struct {
 24	ID        int    `json:"id"`
 25	FirstName string `json:"first_name"`
 26	LastName  string `json:"last_name"`
 27	UserName  string `json:"username"`
 28}
 29
 30// String displays a simple text version of a user.
 31// It is normally a user's username,
 32// but falls back to a first/last name as available.
 33func (u *User) String() string {
 34	if u.UserName != "" {
 35		return u.UserName
 36	}
 37
 38	name := u.FirstName
 39	if u.LastName != "" {
 40		name += " " + u.LastName
 41	}
 42
 43	return name
 44}
 45
 46// GroupChat is a group chat, and not currently in use.
 47type GroupChat struct {
 48	ID    int    `json:"id"`
 49	Title string `json:"title"`
 50}
 51
 52// UserOrGroupChat is returned in Message, because it's not clear which it is.
 53type UserOrGroupChat struct {
 54	ID        int    `json:"id"`
 55	FirstName string `json:"first_name"`
 56	LastName  string `json:"last_name"`
 57	UserName  string `json:"username"`
 58	Title     string `json:"title"`
 59}
 60
 61// Message is returned by almost every request, and contains data about almost anything.
 62type Message struct {
 63	MessageID           int             `json:"message_id"`
 64	From                User            `json:"from"`
 65	Date                int             `json:"date"`
 66	Chat                UserOrGroupChat `json:"chat"`
 67	ForwardFrom         User            `json:"forward_from"`
 68	ForwardDate         int             `json:"forward_date"`
 69	ReplyToMessage      *Message        `json:"reply_to_message"`
 70	Text                string          `json:"text"`
 71	Audio               Audio           `json:"audio"`
 72	Document            Document        `json:"document"`
 73	Photo               []PhotoSize     `json:"photo"`
 74	Sticker             Sticker         `json:"sticker"`
 75	Video               Video           `json:"video"`
 76	Caption             string          `json:"caption"`
 77	Contact             Contact         `json:"contact"`
 78	Location            Location        `json:"location"`
 79	NewChatParticipant  User            `json:"new_chat_participant"`
 80	LeftChatParticipant User            `json:"left_chat_participant"`
 81	NewChatTitle        string          `json:"new_chat_title"`
 82	NewChatPhoto        []PhotoSize           `json:"new_chat_photo"`
 83	DeleteChatPhoto     bool            `json:"delete_chat_photo"`
 84	GroupChatCreated    bool            `json:"group_chat_created"`
 85}
 86
 87// Time converts the message timestamp into a Time.
 88func (m *Message) Time() time.Time {
 89	return time.Unix(int64(m.Date), 0)
 90}
 91
 92// IsGroup returns if the message was sent to a group.
 93func (m *Message) IsGroup() bool {
 94	return m.From.ID != m.Chat.ID
 95}
 96
 97// PhotoSize contains information about photos, including ID and Width and Height.
 98type PhotoSize struct {
 99	FileID   string `json:"file_id"`
100	Width    int    `json:"width"`
101	Height   int    `json:"height"`
102	FileSize int    `json:"file_size"`
103}
104
105// Audio contains information about audio, including ID and Duration.
106type Audio struct {
107	FileID   string `json:"file_id"`
108	Duration int    `json:"duration"`
109	MimeType string `json:"mime_type"`
110	FileSize int    `json:"file_size"`
111}
112
113// Document contains information about a document, including ID and a Thumbnail.
114type Document struct {
115	FileID    string    `json:"file_id"`
116	Thumbnail PhotoSize `json:"thumb"`
117	FileName  string    `json:"file_name"`
118	MimeType  string    `json:"mime_type"`
119	FileSize  int       `json:"file_size"`
120}
121
122// Sticker contains information about a sticker, including ID and Thumbnail.
123type Sticker struct {
124	FileID    string    `json:"file_id"`
125	Width     int       `json:"width"`
126	Height    int       `json:"height"`
127	Thumbnail PhotoSize `json:"thumb"`
128	FileSize  int       `json:"file_size"`
129}
130
131// Video contains information about a video, including ID and duration and Thumbnail.
132type Video struct {
133	FileID    string    `json:"file_id"`
134	Width     int       `json:"width"`
135	Height    int       `json:"height"`
136	Duration  int       `json:"duration"`
137	Thumbnail PhotoSize `json:"thumb"`
138	MimeType  string    `json:"mime_type"`
139	FileSize  int       `json:"file_size"`
140}
141
142// Contact contains information about a contact, such as PhoneNumber and UserId.
143type Contact struct {
144	PhoneNumber string `json:"phone_number"`
145	FirstName   string `json:"first_name"`
146	LastName    string `json:"last_name"`
147	UserID      int    `json:"user_id"`
148}
149
150// Location contains information about a place, such as Longitude and Latitude.
151type Location struct {
152	Longitude float32 `json:"longitude"`
153	Latitude  float32 `json:"latitude"`
154}
155
156// UserProfilePhotos contains information a set of user profile photos.
157type UserProfilePhotos struct {
158	TotalCount int         `json:"total_count"`
159	Photos     []PhotoSize `json:"photos"`
160}
161
162// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
163type ReplyKeyboardMarkup struct {
164	Keyboard        [][]string `json:"keyboard"`
165	ResizeKeyboard  bool       `json:"resize_keyboard"`
166	OneTimeKeyboard bool       `json:"one_time_keyboard"`
167	Selective       bool       `json:"selective"`
168}
169
170// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
171type ReplyKeyboardHide struct {
172	HideKeyboard bool `json:"hide_keyboard"`
173	Selective    bool `json:"selective"`
174}
175
176// ForceReply allows the Bot to have users directly reply to it without additional interaction.
177type ForceReply struct {
178	ForceReply bool `json:"force_reply"`
179	Selective  bool `json:"selective"`
180}