Major documentation and code cleanup.

All documention is now less than 80 characters wide. Old methods now
show deprecated warnings. The Values/Params/Method functions are now
private. Types and configs have required and optional comments on
them. Simplified some function logic.
This commit is contained in:
Syfaro 2016-01-03 16:54:24 -06:00
parent 1ae7803be0
commit e8dfdeeeb9
5 changed files with 414 additions and 306 deletions

199
types.go
View file

@ -3,11 +3,13 @@ package tgbotapi
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// APIResponse is a response from the Telegram API with the result stored raw.
// APIResponse is a response from the Telegram API with the result
// stored raw.
type APIResponse struct {
Ok bool `json:"ok"`
Result json.RawMessage `json:"result"`
@ -22,17 +24,18 @@ type Update struct {
InlineQuery InlineQuery `json:"inline_query"`
}
// User is a user, contained in Message and returned by GetSelf.
// User is a user on Telegram.
type User struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
UserName string `json:"username"`
LastName string `json:"last_name"` // optional
UserName string `json:"username"` // optional
}
// 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.
//
// 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
@ -46,71 +49,72 @@ func (u *User) String() string {
return name
}
// GroupChat is a group chat, and not currently in use.
// GroupChat is a group chat.
type GroupChat struct {
ID int `json:"id"`
Title string `json:"title"`
}
// Chat is returned in Message, it contains information about the Chat a message was sent in.
// Chat contains information about the place a message was sent.
type Chat struct {
ID int `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
UserName string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Title string `json:"title"` // optional
UserName string `json:"username"` // optional
FirstName string `json:"first_name"` // optional
LastName string `json:"last_name"` // optional
}
// IsPrivate returns true if the Chat is a private conversation
// IsPrivate returns 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
// IsGroup returns if the Chat is a group.
func (c *Chat) IsGroup() bool {
return c.Type == "group"
}
// IsSuperGroup returns true if the Chat is a supergroup conversation
// IsSuperGroup returns if the Chat is a supergroup.
func (c *Chat) IsSuperGroup() bool {
return c.Type == "supergroup"
}
// IsChannel returns true if the Chat is a channel
// IsChannel returns if the Chat is a channel.
func (c *Chat) IsChannel() bool {
return c.Type == "channel"
}
// Message is returned by almost every request, and contains data about almost anything.
// Message is returned by almost every request, and contains data about
// almost anything.
type Message struct {
MessageID int `json:"message_id"`
From User `json:"from"`
From User `json:"from"` // optional
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"`
SuperGroupChatCreated bool `json:"supergroup_chat_created"`
ChannelChatCreated bool `json:"channel_chat_created"`
MigrateToChatID int `json:"migrate_to_chat_id"`
MigrateFromChatID int `json:"migrate_from_chat_id"`
ForwardFrom User `json:"forward_from"` // optional
ForwardDate int `json:"forward_date"` // optional
ReplyToMessage *Message `json:"reply_to_message"` // optional
Text string `json:"text"` // optional
Audio Audio `json:"audio"` // optional
Document Document `json:"document"` // optional
Photo []PhotoSize `json:"photo"` // optional
Sticker Sticker `json:"sticker"` // optional
Video Video `json:"video"` // optional
Voice Voice `json:"voice"` // optional
Caption string `json:"caption"` // optional
Contact Contact `json:"contact"` // optional
Location Location `json:"location"` // optional
NewChatParticipant User `json:"new_chat_participant"` // optional
LeftChatParticipant User `json:"left_chat_participant"` // optional
NewChatTitle string `json:"new_chat_title"` // optional
NewChatPhoto []PhotoSize `json:"new_chat_photo"` // optional
DeleteChatPhoto bool `json:"delete_chat_photo"` // optional
GroupChatCreated bool `json:"group_chat_created"` // optional
SuperGroupChatCreated bool `json:"supergroup_chat_created"` // optional
ChannelChatCreated bool `json:"channel_chat_created"` // optional
MigrateToChatID int `json:"migrate_to_chat_id"` // optional
MigrateFromChatID int `json:"migrate_from_chat_id"` // optional
}
// Time converts the message timestamp into a Time.
@ -119,101 +123,111 @@ func (m *Message) Time() time.Time {
}
// IsGroup returns if the message was sent to a group.
//
// Deprecated in favor of Chat.IsGroup.
func (m *Message) IsGroup() bool {
log.Println("Message.IsGroup is deprecated.")
log.Println("Please use Chat.IsGroup instead.")
return m.Chat.IsGroup()
}
// IsCommand returns true if message starts from /
// IsCommand returns true if message starts with '/'.
func (m *Message) IsCommand() bool {
return m.Text != "" && m.Text[0] == '/'
}
// Command if message is command returns first word from message(entire command)
// otherwise returns empty string
// Command checks if the message was a command and if it was, returns the
// command. If the Message was not a command, it returns an empty string.
func (m *Message) Command() string {
if m.IsCommand() {
return strings.SplitN(m.Text, " ", 2)[0]
if !m.IsCommand() {
return ""
}
return ""
return strings.SplitN(m.Text, " ", 2)[0]
}
// CommandArguments if message is command, returns all text after command, excluding the command itself
// otherwise returns empty string
// CommandArguments checks if the message was a command and if it was,
// returns all text after the command name. If the Message was not a
// command, it returns an empty string.
func (m *Message) CommandArguments() string {
if m.IsCommand() {
split := strings.SplitN(m.Text, " ", 2)
if len(split) == 2 {
return strings.SplitN(m.Text, " ", 2)[1]
}
if !m.IsCommand() {
return ""
}
return ""
split := strings.SplitN(m.Text, " ", 2)
if len(split) != 2 {
return ""
}
return strings.SplitN(m.Text, " ", 2)[1]
}
// PhotoSize contains information about photos, including ID and Width and Height.
// PhotoSize contains information about photos.
type PhotoSize struct {
FileID string `json:"file_id"`
Width int `json:"width"`
Height int `json:"height"`
FileSize int `json:"file_size"`
FileSize int `json:"file_size"` // optional
}
// Audio contains information about audio,
// including ID, Duration, Performer and Title.
// Audio contains information about audio.
type Audio struct {
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"`
Performer string `json:"performer"` // optional
Title string `json:"title"` // optional
MimeType string `json:"mime_type"` // optional
FileSize int `json:"file_size"` // optional
}
// Document contains information about a document, including ID and a Thumbnail.
// Document contains information about a document.
type Document struct {
FileID string `json:"file_id"`
Thumbnail PhotoSize `json:"thumb"`
FileName string `json:"file_name"`
MimeType string `json:"mime_type"`
FileSize int `json:"file_size"`
Thumbnail PhotoSize `json:"thumb"` // optional
FileName string `json:"file_name"` // optional
MimeType string `json:"mime_type"` // optional
FileSize int `json:"file_size"` // optional
}
// Sticker contains information about a sticker, including ID and Thumbnail.
// Sticker contains information about a sticker.
type Sticker struct {
FileID string `json:"file_id"`
Width int `json:"width"`
Height int `json:"height"`
Thumbnail PhotoSize `json:"thumb"`
FileSize int `json:"file_size"`
Thumbnail PhotoSize `json:"thumb"` // optional
FileSize int `json:"file_size"` // optional
}
// Video contains information about a video, including ID and duration and Thumbnail.
// Video contains information about a video.
type Video struct {
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"`
Thumbnail PhotoSize `json:"thumb"` // optional
MimeType string `json:"mime_type"` // optional
FileSize int `json:"file_size"` // optional
}
// Voice contains information about a voice, including ID and duration.
// Voice contains information about a voice.
type Voice struct {
FileID string `json:"file_id"`
Duration int `json:"duration"`
MimeType string `json:"mime_type"`
FileSize int `json:"file_size"`
MimeType string `json:"mime_type"` // optional
FileSize int `json:"file_size"` // optional
}
// Contact contains information about a contact, such as PhoneNumber and UserId.
// Contact contains information about a contact.
//
// Note that LastName and UserID may be empty.
type Contact struct {
PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
UserID int `json:"user_id"`
LastName string `json:"last_name"` // optional
UserID int `json:"user_id"` // optional
}
// Location contains information about a place, such as Longitude and Latitude.
// Location contains information about a place.
type Location struct {
Longitude float32 `json:"longitude"`
Latitude float32 `json:"latitude"`
@ -225,11 +239,11 @@ type UserProfilePhotos struct {
Photos []PhotoSize `json:"photos"`
}
// File contains information about a file to download from Telegram
// 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"`
FileSize int `json:"file_size"` // optional
FilePath string `json:"file_path"` // optional
}
// Link returns a full path to the download URL for a File.
@ -242,24 +256,25 @@ func (f *File) Link(token string) string {
// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
type ReplyKeyboardMarkup struct {
Keyboard [][]string `json:"keyboard"`
ResizeKeyboard bool `json:"resize_keyboard"`
OneTimeKeyboard bool `json:"one_time_keyboard"`
Selective bool `json:"selective"`
ResizeKeyboard bool `json:"resize_keyboard"` // optional
OneTimeKeyboard bool `json:"one_time_keyboard"` // optional
Selective bool `json:"selective"` // optional
}
// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
type ReplyKeyboardHide struct {
HideKeyboard bool `json:"hide_keyboard"`
Selective bool `json:"selective"`
Selective bool `json:"selective"` // optional
}
// ForceReply allows the Bot to have users directly reply to it without additional interaction.
// ForceReply allows the Bot to have users directly reply to it without
// additional interaction.
type ForceReply struct {
ForceReply bool `json:"force_reply"`
Selective bool `json:"selective"`
Selective bool `json:"selective"` // optional
}
// InlineQuery is a Query from Telegram for an inline request
// InlineQuery is a Query from Telegram for an inline request.
type InlineQuery struct {
ID string `json:"id"`
From User `json:"user"`