2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
2015-06-25 18:25:02 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-09-19 17:53:32 +02:00
|
|
|
"fmt"
|
2015-07-31 19:40:42 +02:00
|
|
|
"time"
|
2015-11-21 15:26:28 +01:00
|
|
|
"strings"
|
2015-06-25 18:25:02 +02:00
|
|
|
)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// APIResponse is a response from the Telegram API with the result stored raw.
|
|
|
|
type APIResponse struct {
|
2015-06-25 21:20:02 +02:00
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
ErrorCode int `json:"error_code"`
|
|
|
|
Description string `json:"description"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Update is an update response, from GetUpdates.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Update struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
UpdateID int `json:"update_id"`
|
2015-06-25 18:25:02 +02:00
|
|
|
Message Message `json:"message"`
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// User is a user, contained in Message and returned by GetSelf.
|
2015-06-25 18:25:02 +02:00
|
|
|
type User struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ID int `json:"id"`
|
2015-06-25 18:25:02 +02:00
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
UserName string `json:"username"`
|
|
|
|
}
|
|
|
|
|
2015-07-31 09:22:01 +02:00
|
|
|
// String displays a simple text version of a user.
|
|
|
|
// It is normally a user's username,
|
|
|
|
// but falls back to a first/last name as available.
|
|
|
|
func (u *User) String() string {
|
|
|
|
if u.UserName != "" {
|
|
|
|
return u.UserName
|
|
|
|
}
|
|
|
|
|
|
|
|
name := u.FirstName
|
|
|
|
if u.LastName != "" {
|
|
|
|
name += " " + u.LastName
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// GroupChat is a group chat, and not currently in use.
|
2015-06-25 18:25:02 +02:00
|
|
|
type GroupChat struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ID int `json:"id"`
|
2015-06-25 18:25:02 +02:00
|
|
|
Title string `json:"title"`
|
|
|
|
}
|
|
|
|
|
2015-10-09 16:26:38 +02:00
|
|
|
// Chat is returned in Message, it contains information about the Chat a message was sent in.
|
|
|
|
type Chat struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ID int `json:"id"`
|
2015-10-09 16:26:38 +02:00
|
|
|
Type string `json:"type"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
UserName string `json:"username"`
|
2015-06-25 18:25:02 +02:00
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
}
|
|
|
|
|
2015-10-09 16:31:00 +02:00
|
|
|
// IsPrivate returns true if the Chat is a private conversation
|
|
|
|
func (c *Chat) IsPrivate() bool {
|
|
|
|
return c.Type == "private"
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsGroup returns true if the Chat is a group conversation
|
|
|
|
func (c *Chat) IsGroup() bool {
|
|
|
|
return c.Type == "group"
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsChannel returns true if the Chat is a channel
|
|
|
|
func (c *Chat) IsChannel() bool {
|
|
|
|
return c.Type == "channel"
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Message is returned by almost every request, and contains data about almost anything.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Message struct {
|
2015-10-09 16:26:38 +02:00
|
|
|
MessageID int `json:"message_id"`
|
|
|
|
From User `json:"from"`
|
|
|
|
Date int `json:"date"`
|
|
|
|
Chat Chat `json:"chat"`
|
|
|
|
ForwardFrom User `json:"forward_from"`
|
|
|
|
ForwardDate int `json:"forward_date"`
|
|
|
|
ReplyToMessage *Message `json:"reply_to_message"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Audio Audio `json:"audio"`
|
|
|
|
Document Document `json:"document"`
|
|
|
|
Photo []PhotoSize `json:"photo"`
|
|
|
|
Sticker Sticker `json:"sticker"`
|
|
|
|
Video Video `json:"video"`
|
|
|
|
Voice Voice `json:"voice"`
|
|
|
|
Caption string `json:"caption"`
|
|
|
|
Contact Contact `json:"contact"`
|
|
|
|
Location Location `json:"location"`
|
|
|
|
NewChatParticipant User `json:"new_chat_participant"`
|
|
|
|
LeftChatParticipant User `json:"left_chat_participant"`
|
|
|
|
NewChatTitle string `json:"new_chat_title"`
|
|
|
|
NewChatPhoto []PhotoSize `json:"new_chat_photo"`
|
|
|
|
DeleteChatPhoto bool `json:"delete_chat_photo"`
|
|
|
|
GroupChatCreated bool `json:"group_chat_created"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 19:40:42 +02:00
|
|
|
// Time converts the message timestamp into a Time.
|
|
|
|
func (m *Message) Time() time.Time {
|
|
|
|
return time.Unix(int64(m.Date), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsGroup returns if the message was sent to a group.
|
|
|
|
func (m *Message) IsGroup() bool {
|
|
|
|
return m.From.ID != m.Chat.ID
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:43:39 +01:00
|
|
|
func (m *Message) IsCommand() bool {
|
|
|
|
return m.Text != "" && m.Text[0] == '/'
|
|
|
|
}
|
|
|
|
|
2015-11-21 15:26:28 +01:00
|
|
|
func (m *Message) Command() string {
|
|
|
|
return strings.Split(m.Text, " ")[0]
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// PhotoSize contains information about photos, including ID and Width and Height.
|
2015-06-25 18:25:02 +02:00
|
|
|
type PhotoSize struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string `json:"file_id"`
|
2015-06-25 18:25:02 +02:00
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
FileSize int `json:"file_size"`
|
|
|
|
}
|
|
|
|
|
2015-08-18 03:40:42 +02:00
|
|
|
// Audio contains information about audio,
|
|
|
|
// including ID, Duration, Performer and Title.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Audio struct {
|
2015-08-18 03:40:42 +02:00
|
|
|
FileID string `json:"file_id"`
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
Performer string `json:"performer"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
FileSize int `json:"file_size"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Document contains information about a document, including ID and a Thumbnail.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Document struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string `json:"file_id"`
|
|
|
|
Thumbnail PhotoSize `json:"thumb"`
|
|
|
|
FileName string `json:"file_name"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
FileSize int `json:"file_size"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Sticker contains information about a sticker, including ID and Thumbnail.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Sticker struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string `json:"file_id"`
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
Thumbnail PhotoSize `json:"thumb"`
|
|
|
|
FileSize int `json:"file_size"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Video contains information about a video, including ID and duration and Thumbnail.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Video struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string `json:"file_id"`
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
Thumbnail PhotoSize `json:"thumb"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
FileSize int `json:"file_size"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 03:40:42 +02:00
|
|
|
// Voice contains information about a voice, including ID and duration.
|
|
|
|
type Voice struct {
|
|
|
|
FileID string `json:"file_id"`
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
FileSize int `json:"file_size"`
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Contact contains information about a contact, such as PhoneNumber and UserId.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Contact struct {
|
|
|
|
PhoneNumber string `json:"phone_number"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
2015-08-05 03:44:09 +02:00
|
|
|
UserID int `json:"user_id"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Location contains information about a place, such as Longitude and Latitude.
|
2015-06-25 18:25:02 +02:00
|
|
|
type Location struct {
|
|
|
|
Longitude float32 `json:"longitude"`
|
|
|
|
Latitude float32 `json:"latitude"`
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// UserProfilePhotos contains information a set of user profile photos.
|
2015-06-25 18:25:02 +02:00
|
|
|
type UserProfilePhotos struct {
|
|
|
|
TotalCount int `json:"total_count"`
|
|
|
|
Photos []PhotoSize `json:"photos"`
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:46:20 +02:00
|
|
|
// File contains information about a file to download from Telegram
|
|
|
|
type File struct {
|
|
|
|
FileID string `json:"file_id"`
|
|
|
|
FileSize int `json:"file_size"`
|
|
|
|
FilePath string `json:"file_path"`
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:53:32 +02:00
|
|
|
// Link returns a full path to the download URL for a File.
|
|
|
|
//
|
|
|
|
// It requires the Bot Token to create the link.
|
|
|
|
func (f *File) Link(token string) string {
|
2015-09-19 20:42:50 +02:00
|
|
|
return fmt.Sprintf(FileEndpoint, token, f.FilePath)
|
2015-09-19 17:53:32 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
|
2015-06-25 18:25:02 +02:00
|
|
|
type ReplyKeyboardMarkup struct {
|
2015-06-25 23:15:28 +02:00
|
|
|
Keyboard [][]string `json:"keyboard"`
|
|
|
|
ResizeKeyboard bool `json:"resize_keyboard"`
|
|
|
|
OneTimeKeyboard bool `json:"one_time_keyboard"`
|
|
|
|
Selective bool `json:"selective"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
|
2015-06-25 18:25:02 +02:00
|
|
|
type ReplyKeyboardHide struct {
|
|
|
|
HideKeyboard bool `json:"hide_keyboard"`
|
|
|
|
Selective bool `json:"selective"`
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// ForceReply allows the Bot to have users directly reply to it without additional interaction.
|
2015-06-25 18:25:02 +02:00
|
|
|
type ForceReply struct {
|
|
|
|
ForceReply bool `json:"force_reply"`
|
2015-07-22 18:10:27 +02:00
|
|
|
Selective bool `json:"selective"`
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|