Lint issues fixed

bot-api-6.1
Gleb Sinyavsky 2015-11-21 19:43:24 +03:00
parent d638757359
commit 4037dbed02
3 changed files with 63 additions and 8 deletions

28
bot.go
View File

@ -81,7 +81,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse,
return apiResp, nil return apiResp, nil
} }
func (bot *BotAPI) MakeMessageRequest(endpoint string, params url.Values) (Message, error) { func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) {
resp, err := bot.MakeRequest(endpoint, params) resp, err := bot.MakeRequest(endpoint, params)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
@ -169,14 +169,17 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
return apiResp, nil return apiResp, nil
} }
func (this *BotAPI) GetFileDirectUrl(fileID string) (string, error) { // GetFileDirectURL returns direct URL to file
file, err := this.GetFile(FileConfig{fileID}) //
// Requires fileID
func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) {
file, err := bot.GetFile(FileConfig{fileID})
if err != nil { if err != nil {
return "", err return "", err
} }
return file.Link(this.Token), nil return file.Link(bot.Token), nil
} }
// GetMe fetches the currently authenticated bot. // GetMe fetches the currently authenticated bot.
@ -198,10 +201,16 @@ func (bot *BotAPI) GetMe() (User, error) {
return user, nil return user, nil
} }
// IsMessageToMe returns true if message directed to this bot
//
// Requires message
func (bot *BotAPI) IsMessageToMe(message Message) bool { func (bot *BotAPI) IsMessageToMe(message Message) bool {
return strings.Contains(message.Text, "@"+bot.Self.UserName) return strings.Contains(message.Text, "@"+bot.Self.UserName)
} }
// Send will send event(Message, Photo, Audio, ChatAction, anything) to Telegram
//
// Requires Chattable
func (bot *BotAPI) Send(c Chattable) (Message, error) { func (bot *BotAPI) Send(c Chattable) (Message, error) {
switch c.(type) { switch c.(type) {
case Fileable: case Fileable:
@ -225,7 +234,7 @@ func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error)
return Message{}, err return Message{}, err
} }
message, err := bot.MakeMessageRequest(method, v) message, err := bot.makeMessageRequest(method, v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -270,7 +279,7 @@ func (bot *BotAPI) sendChattable(config Chattable) (Message, error) {
return Message{}, err return Message{}, err
} }
message, err := bot.MakeMessageRequest(config.Method(), v) message, err := bot.makeMessageRequest(config.Method(), v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
@ -359,6 +368,9 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
return updates, nil return updates, nil
} }
// RemoveWebhook removes webhook
//
// There are no parameters for this method.
func (bot *BotAPI) RemoveWebhook() (APIResponse, error) { func (bot *BotAPI) RemoveWebhook() (APIResponse, error) {
return bot.MakeRequest("setWebhook", url.Values{}) return bot.MakeRequest("setWebhook", url.Values{})
} }
@ -393,7 +405,9 @@ func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
return apiResp, nil return apiResp, nil
} }
// UpdatesChan starts a channel for getting updates. // GetUpdatesChan starts and returns a channel for getting updates.
//
// Requires UpdateConfig
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (<-chan Update, error) { func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (<-chan Update, error) {
updatesChan := make(chan Update, 100) updatesChan := make(chan Update, 100)

View File

@ -38,11 +38,13 @@ const (
ModeMarkdown = "Markdown" ModeMarkdown = "Markdown"
) )
//Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others)
type Chattable interface { type Chattable interface {
Values() (url.Values, error) Values() (url.Values, error)
Method() string Method() string
} }
//Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
type Fileable interface { type Fileable interface {
Chattable Chattable
Params() (map[string]string, error) Params() (map[string]string, error)
@ -51,7 +53,7 @@ type Fileable interface {
UseExistingFile() bool UseExistingFile() bool
} }
// Base struct for all chat event(Message, Photo and so on) // BaseChat is base struct for all chat event(Message, Photo and so on)
type BaseChat struct { type BaseChat struct {
ChatID int ChatID int
ChannelUsername string ChannelUsername string
@ -59,6 +61,7 @@ type BaseChat struct {
ReplyMarkup interface{} ReplyMarkup interface{}
} }
// Values returns url.Values representation of BaseChat
func (chat *BaseChat) Values() (url.Values, error) { func (chat *BaseChat) Values() (url.Values, error) {
v := url.Values{} v := url.Values{}
if chat.ChannelUsername != "" { if chat.ChannelUsername != "" {
@ -83,6 +86,7 @@ func (chat *BaseChat) Values() (url.Values, error) {
return v, nil return v, nil
} }
// BaseFile is base struct for all file events(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
type BaseFile struct { type BaseFile struct {
BaseChat BaseChat
FilePath string FilePath string
@ -91,6 +95,7 @@ type BaseFile struct {
UseExisting bool UseExisting bool
} }
// Params returns map[string]string representation of BaseFile
func (file BaseFile) Params() (map[string]string, error) { func (file BaseFile) Params() (map[string]string, error) {
params := make(map[string]string) params := make(map[string]string)
@ -116,6 +121,7 @@ func (file BaseFile) Params() (map[string]string, error) {
return params, nil return params, nil
} }
// GetFile returns abstract representation of File inside BaseFile
func (file BaseFile) GetFile() interface{} { func (file BaseFile) GetFile() interface{} {
var result interface{} var result interface{}
if file.FilePath == "" { if file.FilePath == "" {
@ -127,6 +133,7 @@ func (file BaseFile) GetFile() interface{} {
return result return result
} }
// UseExistingFile returns true if BaseFile contains already uploaded file by FileID
func (file BaseFile) UseExistingFile() bool { func (file BaseFile) UseExistingFile() bool {
return file.UseExisting return file.UseExisting
} }
@ -140,6 +147,7 @@ type MessageConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
} }
// Values returns url.Values representation of MessageConfig
func (config MessageConfig) Values() (url.Values, error) { func (config MessageConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("text", config.Text) v.Add("text", config.Text)
@ -151,6 +159,7 @@ func (config MessageConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Method returns Telegram API method name for sending Message
func (config MessageConfig) Method() string { func (config MessageConfig) Method() string {
return "SendMessage" return "SendMessage"
} }
@ -163,6 +172,7 @@ type ForwardConfig struct {
MessageID int MessageID int
} }
// Values returns url.Values representation of ForwardConfig
func (config ForwardConfig) Values() (url.Values, error) { func (config ForwardConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("from_chat_id", strconv.Itoa(config.FromChatID)) v.Add("from_chat_id", strconv.Itoa(config.FromChatID))
@ -170,6 +180,7 @@ func (config ForwardConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Method returns Telegram API method name for sending Forward
func (config ForwardConfig) Method() string { func (config ForwardConfig) Method() string {
return "forwardMessage" return "forwardMessage"
} }
@ -180,6 +191,7 @@ type PhotoConfig struct {
Caption string Caption string
} }
// Params returns map[string]string representation of PhotoConfig
func (config PhotoConfig) Params() (map[string]string, error) { func (config PhotoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
@ -190,6 +202,7 @@ func (config PhotoConfig) Params() (map[string]string, error) {
return params, nil return params, nil
} }
// Values returns url.Values representation of PhotoConfig
func (config PhotoConfig) Values() (url.Values, error) { func (config PhotoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -200,10 +213,12 @@ func (config PhotoConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Name return field name for uploading file
func (config PhotoConfig) Name() string { func (config PhotoConfig) Name() string {
return "photo" return "photo"
} }
// Method returns Telegram API method name for sending Photo
func (config PhotoConfig) Method() string { func (config PhotoConfig) Method() string {
return "SendPhoto" return "SendPhoto"
} }
@ -216,6 +231,7 @@ type AudioConfig struct {
Title string Title string
} }
// Values returns url.Values representation of AudioConfig
func (config AudioConfig) Values() (url.Values, error) { func (config AudioConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -234,6 +250,7 @@ func (config AudioConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Params returns map[string]string representation of AudioConfig
func (config AudioConfig) Params() (map[string]string, error) { func (config AudioConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
@ -251,10 +268,12 @@ func (config AudioConfig) Params() (map[string]string, error) {
return params, nil return params, nil
} }
// Name return field name for uploading file
func (config AudioConfig) Name() string { func (config AudioConfig) Name() string {
return "audio" return "audio"
} }
// Method returns Telegram API method name for sending Audio
func (config AudioConfig) Method() string { func (config AudioConfig) Method() string {
return "SendAudio" return "SendAudio"
} }
@ -264,6 +283,7 @@ type DocumentConfig struct {
BaseFile BaseFile
} }
// Values returns url.Values representation of DocumentConfig
func (config DocumentConfig) Values() (url.Values, error) { func (config DocumentConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -272,16 +292,19 @@ func (config DocumentConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Params returns map[string]string representation of DocumentConfig
func (config DocumentConfig) Params() (map[string]string, error) { func (config DocumentConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
return params, nil return params, nil
} }
// Name return field name for uploading file
func (config DocumentConfig) Name() string { func (config DocumentConfig) Name() string {
return "document" return "document"
} }
// Method returns Telegram API method name for sending Document
func (config DocumentConfig) Method() string { func (config DocumentConfig) Method() string {
return "sendDocument" return "sendDocument"
} }
@ -291,6 +314,7 @@ type StickerConfig struct {
BaseFile BaseFile
} }
// Values returns url.Values representation of StickerConfig
func (config StickerConfig) Values() (url.Values, error) { func (config StickerConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -299,16 +323,19 @@ func (config StickerConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Params returns map[string]string representation of StickerConfig
func (config StickerConfig) Params() (map[string]string, error) { func (config StickerConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
return params, nil return params, nil
} }
// Name return field name for uploading file
func (config StickerConfig) Name() string { func (config StickerConfig) Name() string {
return "sticker" return "sticker"
} }
// Method returns Telegram API method name for sending Sticker
func (config StickerConfig) Method() string { func (config StickerConfig) Method() string {
return "sendSticker" return "sendSticker"
} }
@ -320,6 +347,7 @@ type VideoConfig struct {
Caption string Caption string
} }
// Values returns url.Values representation of VideoConfig
func (config VideoConfig) Values() (url.Values, error) { func (config VideoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -334,16 +362,19 @@ func (config VideoConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Params returns map[string]string representation of VideoConfig
func (config VideoConfig) Params() (map[string]string, error) { func (config VideoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
return params, nil return params, nil
} }
// Name return field name for uploading file
func (config VideoConfig) Name() string { func (config VideoConfig) Name() string {
return "video" return "video"
} }
// Method returns Telegram API method name for sending Video
func (config VideoConfig) Method() string { func (config VideoConfig) Method() string {
return "sendVideo" return "sendVideo"
} }
@ -354,6 +385,7 @@ type VoiceConfig struct {
Duration int Duration int
} }
// Values returns url.Values representation of VoiceConfig
func (config VoiceConfig) Values() (url.Values, error) { func (config VoiceConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -365,6 +397,7 @@ func (config VoiceConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Params returns map[string]string representation of VoiceConfig
func (config VoiceConfig) Params() (map[string]string, error) { func (config VoiceConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
@ -375,10 +408,12 @@ func (config VoiceConfig) Params() (map[string]string, error) {
return params, nil return params, nil
} }
// Name return field name for uploading file
func (config VoiceConfig) Name() string { func (config VoiceConfig) Name() string {
return "voice" return "voice"
} }
// Method returns Telegram API method name for sending Voice
func (config VoiceConfig) Method() string { func (config VoiceConfig) Method() string {
return "sendVoice" return "sendVoice"
} }
@ -390,6 +425,7 @@ type LocationConfig struct {
Longitude float64 Longitude float64
} }
// Values returns url.Values representation of LocationConfig
func (config LocationConfig) Values() (url.Values, error) { func (config LocationConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
@ -399,6 +435,7 @@ func (config LocationConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// Method returns Telegram API method name for sending Location
func (config LocationConfig) Method() string { func (config LocationConfig) Method() string {
return "sendLocation" return "sendLocation"
} }
@ -409,12 +446,14 @@ type ChatActionConfig struct {
Action string Action string
} }
// Values returns url.Values representation of ChatActionConfig
func (config ChatActionConfig) Values() (url.Values, error) { func (config ChatActionConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("action", config.Action) v.Add("action", config.Action)
return v, nil return v, nil
} }
// Method returns Telegram API method name for sending ChatAction
func (config ChatActionConfig) Method() string { func (config ChatActionConfig) Method() string {
return "sendChatAction" return "sendChatAction"
} }

View File

@ -113,10 +113,12 @@ func (m *Message) IsGroup() bool {
return m.From.ID != m.Chat.ID return m.From.ID != m.Chat.ID
} }
// IsCommand returns true if message starts from /
func (m *Message) IsCommand() bool { func (m *Message) IsCommand() bool {
return m.Text != "" && m.Text[0] == '/' return m.Text != "" && m.Text[0] == '/'
} }
// Command returns first word from message
func (m *Message) Command() string { func (m *Message) Command() string {
return strings.Split(m.Text, " ")[0] return strings.Split(m.Text, " ")[0]
} }