all repos — telegram-bot-api @ 196f515db776397f82c925f1b26c1ad62a16b7d8

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	Contact             Contact         `json:"contact"`
 77	Location            Location        `json:"location"`
 78	NewChatParticipant  User            `json:"new_chat_participant"`
 79	LeftChatParticipant User            `json:"left_chat_participant"`
 80	NewChatTitle        string          `json:"new_chat_title"`
 81	NewChatPhoto        string          `json:"new_chat_photo"`
 82	DeleteChatPhoto     bool            `json:"delete_chat_photo"`
 83	GroupChatCreated    bool            `json:"group_chat_created"`
 84}
 85
 86// Time converts the message timestamp into a Time.
 87func (m *Message) Time() time.Time {
 88	return time.Unix(int64(m.Date), 0)
 89}
 90
 91// IsGroup returns if the message was sent to a group.
 92func (m *Message) IsGroup() bool {
 93	return m.From.ID != m.Chat.ID
 94}
 95
 96// PhotoSize contains information about photos, including ID and Width and Height.
 97type PhotoSize struct {
 98	FileID   string `json:"file_id"`
 99	Width    int    `json:"width"`
100	Height   int    `json:"height"`
101	FileSize int    `json:"file_size"`
102}
103
104// Audio contains information about audio, including ID and Duration.
105type Audio struct {
106	FileID   string `json:"file_id"`
107	Duration int    `json:"duration"`
108	MimeType string `json:"mime_type"`
109	FileSize int    `json:"file_size"`
110}
111
112// Document contains information about a document, including ID and a Thumbnail.
113type Document struct {
114	FileID    string    `json:"file_id"`
115	Thumbnail PhotoSize `json:"thumb"`
116	FileName  string    `json:"file_name"`
117	MimeType  string    `json:"mime_type"`
118	FileSize  int       `json:"file_size"`
119}
120
121// Sticker contains information about a sticker, including ID and Thumbnail.
122type Sticker struct {
123	FileID    string    `json:"file_id"`
124	Width     int       `json:"width"`
125	Height    int       `json:"height"`
126	Thumbnail PhotoSize `json:"thumb"`
127	FileSize  int       `json:"file_size"`
128}
129
130// Video contains information about a video, including ID and duration and Thumbnail.
131type Video struct {
132	FileID    string    `json:"file_id"`
133	Width     int       `json:"width"`
134	Height    int       `json:"height"`
135	Duration  int       `json:"duration"`
136	Thumbnail PhotoSize `json:"thumb"`
137	MimeType  string    `json:"mime_type"`
138	FileSize  int       `json:"file_size"`
139	Caption   string    `json:"caption"`
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      string `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}