From 934ae1e733f374d6fe6dd88bd7d2a54e80399907 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Sun, 5 Jul 2015 00:43:12 -0700 Subject: [PATCH] Consolidated file sending types and methods. --- methods.go | 278 ++--------------------------------------------------- types.go | 57 +++-------- 2 files changed, 21 insertions(+), 314 deletions(-) diff --git a/methods.go b/methods.go index 9e01a12..a3e42f0 100644 --- a/methods.go +++ b/methods.go @@ -188,12 +188,15 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { // // Requires ChatID and FileID OR FilePath. // Caption, ReplyToMessageID, and ReplyMarkup are optional. -func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { +func (bot *BotAPI) SendFile(config FileConfig) (Message, error) { + if !config.FileType.ValidFileType() { + return Message{}, errors.New("Invalid file type.") + } if config.UseExistingPhoto { v := url.Values{} v.Add("chat_id", strconv.Itoa(config.ChatID)) - v.Add("photo", config.FileID) - if config.Caption != "" { + v.Add(config.FileType, config.FileID) + if config.FileType == "photo" && config.Caption != "" { v.Add("caption", config.Caption) } if config.ReplyToMessageID != 0 { @@ -256,275 +259,6 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { return message, nil } -// SendAudio sends or uploads an audio clip to a chat. -// If using a file, the file must be encoded as an .ogg with OPUS. -// -// Requires ChatID and FileID OR FilePath. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { - if config.UseExistingAudio { - v := url.Values{} - v.Add("chat_id", strconv.Itoa(config.ChatID)) - v.Add("audio", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - v.Add("reply_markup", string(data)) - } - - resp, err := bot.MakeRequest("sendAudio", v) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendAudio req : %+v\n", v) - log.Printf("sendAudio resp: %+v\n", message) - } - - return message, nil - } - - params := make(map[string]string) - - params["chat_id"] = strconv.Itoa(config.ChatID) - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - params["reply_markup"] = string(data) - } - - resp, err := bot.UploadFile("sendAudio", params, "audio", config.FilePath) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendAudio resp: %+v\n", message) - } - - return message, nil -} - -// SendDocument sends or uploads a document to a chat. -// -// Requires ChatID and FileID OR FilePath. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { - if config.UseExistingDocument { - v := url.Values{} - v.Add("chat_id", strconv.Itoa(config.ChatID)) - v.Add("document", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - v.Add("reply_markup", string(data)) - } - - resp, err := bot.MakeRequest("sendDocument", v) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendDocument req : %+v\n", v) - log.Printf("sendDocument resp: %+v\n", message) - } - - return message, nil - } - - params := make(map[string]string) - - params["chat_id"] = strconv.Itoa(config.ChatID) - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - params["reply_markup"] = string(data) - } - - resp, err := bot.UploadFile("sendDocument", params, "document", config.FilePath) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendDocument resp: %+v\n", message) - } - - return message, nil -} - -// SendSticker sends or uploads a sticker to a chat. -// -// Requires ChatID and FileID OR FilePath. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { - if config.UseExistingSticker { - v := url.Values{} - v.Add("chat_id", strconv.Itoa(config.ChatID)) - v.Add("sticker", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - v.Add("reply_markup", string(data)) - } - - resp, err := bot.MakeRequest("sendSticker", v) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendSticker req : %+v\n", v) - log.Printf("sendSticker resp: %+v\n", message) - } - - return message, nil - } - - params := make(map[string]string) - - params["chat_id"] = strconv.Itoa(config.ChatID) - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - params["reply_markup"] = string(data) - } - - resp, err := bot.UploadFile("sendSticker", params, "sticker", config.FilePath) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendSticker resp: %+v\n", message) - } - - return message, nil -} - -// SendVideo sends or uploads a video to a chat. -// -// Requires ChatID and FileID OR FilePath. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { - if config.UseExistingVideo { - v := url.Values{} - v.Add("chat_id", strconv.Itoa(config.ChatID)) - v.Add("video", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - v.Add("reply_markup", string(data)) - } - - resp, err := bot.MakeRequest("sendVideo", v) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendVideo req : %+v\n", v) - log.Printf("sendVideo resp: %+v\n", message) - } - - return message, nil - } - - params := make(map[string]string) - - params["chat_id"] = strconv.Itoa(config.ChatID) - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } - if config.ReplyMarkup != nil { - data, err := json.Marshal(config.ReplyMarkup) - if err != nil { - return Message{}, err - } - - params["reply_markup"] = string(data) - } - - resp, err := bot.UploadFile("sendVideo", params, "video", config.FilePath) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendVideo resp: %+v\n", message) - } - - return message, nil -} - // SendLocation sends a location to a chat. // // Requires ChatID, Latitude, and Longitude. diff --git a/types.go b/types.go index d31b78f..47eefd3 100644 --- a/types.go +++ b/types.go @@ -16,6 +16,18 @@ const ( ChatFindLocation = "find_location" ) +// FileType type which allows us to validate file types. +type FileType string + +func (f *FileType) ValidFileType() bool { + switch *f { + case "photo", "document", "video", "sticker", "audio": + return true + default: + return false + } +} + // APIResponse is a response from the Telegram API with the result stored raw. type APIResponse struct { Ok bool `json:"ok"` @@ -180,8 +192,8 @@ type ForwardConfig struct { MessageID int } -// PhotoConfig contains information about a SendPhoto request. -type PhotoConfig struct { +// FileConfig contains information about any Send request. +type FileConfig struct { ChatID int Caption string ReplyToMessageID int @@ -189,46 +201,7 @@ type PhotoConfig struct { UseExistingPhoto bool FilePath string FileID string -} - -// AudioConfig contains information about a SendAudio request. -type AudioConfig struct { - ChatID int - ReplyToMessageID int - ReplyMarkup interface{} - UseExistingAudio bool - FilePath string - FileID string -} - -// DocumentConfig contains information about a SendDocument request. -type DocumentConfig struct { - ChatID int - ReplyToMessageID int - ReplyMarkup interface{} - UseExistingDocument bool - FilePath string - FileID string -} - -// StickerConfig contains information about a SendSticker request. -type StickerConfig struct { - ChatID int - ReplyToMessageID int - ReplyMarkup interface{} - UseExistingSticker bool - FilePath string - FileID string -} - -// VideoConfig contains information about a SendVideo request. -type VideoConfig struct { - ChatID int - ReplyToMessageID int - ReplyMarkup interface{} - UseExistingVideo bool - FilePath string - FileID string + FileType FileType } // LocationConfig contains information about a SendLocation request.