From da026b435e78db6e13663d1af658db812e868cdc Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Fri, 20 Nov 2015 17:55:32 +0300 Subject: [PATCH] Totally new, universal API --- bot.go | 162 +++++++++++------------------------------------------ configs.go | 44 ++++++++++++--- 2 files changed, 70 insertions(+), 136 deletions(-) diff --git a/bot.go b/bot.go index e9b6cd6..8a5c96a 100644 --- a/bot.go +++ b/bot.go @@ -90,7 +90,7 @@ func (bot *BotAPI) MakeMessageRequest(endpoint string, params url.Values) (Messa var message Message json.Unmarshal(resp.Result, &message) - bot.DebugLog(endpoint, params, message) + bot.debugLog(endpoint, params, message) return message, nil } @@ -188,11 +188,16 @@ func (bot *BotAPI) GetMe() (User, error) { return user, nil } -func (bot *BotAPI) Send(c BaseChat) error { - return nil +func (bot *BotAPI) Send(c Chattable) (Message, error) { + switch c.(type) { + case Fileable: + return bot.sendFile(c.(Fileable)) + default: + return bot.sendChattable(c) + } } -func (bot *BotAPI) DebugLog(context string, v url.Values, message interface{}) { +func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { if bot.Debug { log.Printf("%s req : %+v\n", context, v) log.Printf("%s resp: %+v\n", context, message) @@ -237,61 +242,22 @@ func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error return message, nil } -func (bot *BotAPI) sendFile(method string, config Fileable) (Message, error) { +func (bot *BotAPI) sendFile(config Fileable) (Message, error) { if config.UseExistingFile() { - return bot.sendExisting(method, config) + return bot.sendExisting(config.Method(), config) } - return bot.uploadAndSend(method, config) + return bot.uploadAndSend(config.Method(), config) } -// SendMessage sends a Message to a chat. -// -// Requires ChatID and Text. -// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional. -func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { +func (bot *BotAPI) sendChattable(config Chattable) (Message, error) { v, err := config.Values() if err != nil { return Message{}, err } - message, err := bot.MakeMessageRequest("SendMessage", v) - - if err != nil { - return Message{}, err - } - - return message, nil -} - -// ForwardMessage forwards a message from one chat to another. -// -// Requires ChatID (destination), FromChatID (source), and MessageID. -func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { - v, err := config.Values() - if err != nil { - return Message{}, err - } - - message, err := bot.MakeMessageRequest("forwardMessage", v) - if err != nil { - return Message{}, err - } - - return message, nil -} + message, err := bot.MakeMessageRequest(config.Method(), v) -// SendLocation sends a location to a chat. -// -// Requires ChatID, Latitude, and Longitude. -// ReplyToMessageID and ReplyMarkup are optional. -func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { - v, err := config.Values() - if err != nil { - return Message{}, err - } - - message, err := bot.MakeMessageRequest("sendLocation", v) if err != nil { return Message{}, err } @@ -299,85 +265,6 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { return message, nil } -// SendPhoto sends or uploads a photo to a chat. -// -// Requires ChatID and FileID OR File. -// Caption, ReplyToMessageID, and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { - return bot.sendFile("SendPhoto", config) -} - -// SendAudio sends or uploads an audio clip to a chat. -// If using a file, the file must be in the .mp3 format. -// -// When the fields title and performer are both empty and -// the mime-type of the file to be sent is not audio/mpeg, -// the file must be an .ogg file encoded with OPUS. -// You may use the tgutils.EncodeAudio func to assist you with this, if needed. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { - return bot.sendFile("sendAudio", config) -} - -// SendDocument sends or uploads a document to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { - return bot.sendFile("sendDocument", config) -} - -// SendVoice sends or uploads a playable voice to a chat. -// If using a file, the file must be encoded as an .ogg with OPUS. -// You may use the tgutils.EncodeAudio func to assist you with this, if needed. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { - return bot.sendFile("sendVoice", config) -} - -// SendSticker sends or uploads a sticker to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { - return bot.sendFile("sendSticker", config) -} - -// SendVideo sends or uploads a video to a chat. -// -// Requires ChatID and FileID OR File. -// ReplyToMessageID and ReplyMarkup are optional. -// File should be either a string, FileBytes, or FileReader. -func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { - return bot.sendFile("sendVideo", config) -} - -// SendChatAction sets a current action in a chat. -// -// Requires ChatID and a valid Action (see Chat constants). -func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { - v, err := config.Values() - if err != nil { - return err - } - - _, err = bot.MakeRequest("sendChatAction", v) - if err != nil { - return err - } - - return nil -} - // GetUserProfilePhotos gets a user's profile photos. // // Requires UserID. @@ -400,7 +287,7 @@ func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserPro var profilePhotos UserProfilePhotos json.Unmarshal(resp.Result, &profilePhotos) - bot.DebugLog("GetUserProfilePhoto", v, profilePhotos) + bot.debugLog("GetUserProfilePhoto", v, profilePhotos) return profilePhotos, nil } @@ -420,7 +307,7 @@ func (bot *BotAPI) GetFile(config FileConfig) (File, error) { var file File json.Unmarshal(resp.Result, &file) - bot.DebugLog("GetFile", v, file) + bot.debugLog("GetFile", v, file) return file, nil } @@ -530,3 +417,20 @@ func (bot *BotAPI) ListenForWebhook(pattern string) { bot.Updates <- update }) } + +// SendChatAction sets a current action in a chat. +// +// Requires ChatID and a valid Action (see Chat constants). +func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { + v, err := config.Values() + if err != nil { + return err + } + + _, err = bot.MakeRequest("sendChatAction", v) + if err != nil { + return err + } + + return nil +} diff --git a/configs.go b/configs.go index 3a65a6f..d05a954 100644 --- a/configs.go +++ b/configs.go @@ -40,6 +40,7 @@ const ( type Chattable interface { Values() (url.Values, error) + Method() string } type Fileable interface { @@ -133,6 +134,10 @@ func (config MessageConfig) Values() (url.Values, error) { return v, nil } +func (config MessageConfig) Method() string { + return "SendMessage" +} + // ForwardConfig contains information about a ForwardMessage request. type ForwardConfig struct { BaseChat @@ -143,17 +148,14 @@ type ForwardConfig struct { func (config ForwardConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() - - if config.FromChannelUsername != "" { - v.Add("chat_id", config.FromChannelUsername) - } else { - v.Add("chat_id", strconv.Itoa(config.FromChatID)) - } v.Add("message_id", strconv.Itoa(config.MessageID)) - return v, nil } +func (config ForwardConfig) Method() string { + return "forwardMessage" +} + // PhotoConfig contains information about a SendPhoto request. type PhotoConfig struct { BaseFile @@ -209,6 +211,10 @@ func (config PhotoConfig) Name() string { return "photo" } +func (config PhotoConfig) Method() string { + return "SendPhoto" +} + // AudioConfig contains information about a SendAudio request. type AudioConfig struct { BaseFile @@ -278,6 +284,10 @@ func (config AudioConfig) Name() string { return "audio" } +func (config AudioConfig) Method() string { + return "SendAudio" +} + // DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile @@ -326,6 +336,10 @@ func (config DocumentConfig) Name() string { return "document" } +func (config DocumentConfig) Method() string { + return "sendDocument" +} + // StickerConfig contains information about a SendSticker request. type StickerConfig struct { BaseFile @@ -374,6 +388,10 @@ func (config StickerConfig) Name() string { return "sticker" } +func (config StickerConfig) Method() string { + return "sendSticker" +} + // VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile @@ -430,6 +448,10 @@ func (config VideoConfig) Name() string { return "viceo" } +func (config VideoConfig) Method() string { + return "sendVideo" +} + // VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile @@ -485,6 +507,10 @@ func (config VoiceConfig) Name() string { return "voice" } +func (config VoiceConfig) Method() string { + return "sendVoice" +} + // LocationConfig contains information about a SendLocation request. type LocationConfig struct { BaseChat @@ -515,6 +541,10 @@ func (config LocationConfig) Values() (url.Values, error) { return v, nil } +func (config LocationConfig) Method() string { + return "sendLocation" +} + // ChatActionConfig contains information about a SendChatAction request. type ChatActionConfig struct { BaseChat