all repos — telegram-bot-api @ d3f7ac7197622f37672b08a9fec9743132cbf802

Golang bindings for the Telegram Bot API

Refactorings
Gleb Sinyavsky zhulik.gleb@gmail.com
Fri, 20 Nov 2015 17:31:01 +0300
commit

d3f7ac7197622f37672b08a9fec9743132cbf802

parent

9361631c6d3a7a10da24e4172d471026560b6e97

3 files changed, 71 insertions(+), 148 deletions(-)

jump to
M bot.gobot.go

@@ -214,6 +214,37 @@

return message, nil } +func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) { + params, err := config.Params() + if err != nil { + return Message{}, err + } + + file := config.GetFile() + + resp, err := bot.UploadFile(method, params, config.Name(), file) + if err != nil { + return Message{}, err + } + + var message Message + json.Unmarshal(resp.Result, &message) + + if bot.Debug { + log.Printf("%s resp: %+v\n", method, message) + } + + return message, nil +} + +func (bot *BotAPI) sendFile(method string, config Fileable) (Message, error) { + if config.UseExistingFile() { + return bot.sendExisting(method, config) + } + + return bot.uploadAndSend(method, config) +} + // SendMessage sends a Message to a chat. // // Requires ChatID and Text.

@@ -274,30 +305,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("SendPhoto", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("SendPhoto", params, "photo", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("SendPhoto resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("SendPhoto", config) } // SendAudio sends or uploads an audio clip to a chat.

@@ -312,30 +320,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("sendAudio", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendAudio", params, "audio", file) - 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 + return bot.sendFile("sendAudio", config) } // SendDocument sends or uploads a document to a chat.

@@ -344,30 +329,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("sendDocument", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendDocument", params, "document", file) - 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 + return bot.sendFile("sendDocument", config) } // SendVoice sends or uploads a playable voice to a chat.

@@ -378,30 +340,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("sendVoice", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("SendVoice", params, "voice", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("SendVoice resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendVoice", config) } // SendSticker sends or uploads a sticker to a chat.

@@ -410,30 +349,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("sendSticker", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendSticker", params, "sticker", file) - 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 + return bot.sendFile("sendSticker", config) } // SendVideo sends or uploads a video to a chat.

@@ -442,30 +358,7 @@ // 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) { - if config.UseExisting { - return bot.sendExisting("sendVideo", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendVideo", params, "video", file) - 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 + return bot.sendFile("sendVideo", config) } // SendChatAction sets a current action in a chat.
M configs.goconfigs.go

@@ -45,7 +45,9 @@

type Fileable interface { Chattable Params() (map[string]string, error) + Name() string GetFile() interface{} + UseExistingFile() bool } // Base struct for all chat event(Message, Photo and so on)

@@ -93,6 +95,10 @@ result = file.FilePath

} return result +} + +func (file BaseFile) UseExistingFile() bool { + return file.UseExisting } // MessageConfig contains information about a SendMessage request.

@@ -199,6 +205,10 @@

return v, nil } +func (config PhotoConfig) Name() string { + return "photo" +} + // AudioConfig contains information about a SendAudio request. type AudioConfig struct { BaseFile

@@ -264,6 +274,10 @@

return params, nil } +func (config AudioConfig) Name() string { + return "audio" +} + // DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile

@@ -308,6 +322,10 @@

return params, nil } +func (config DocumentConfig) Name() string { + return "document" +} + // StickerConfig contains information about a SendSticker request. type StickerConfig struct { BaseFile

@@ -350,6 +368,10 @@ params["reply_markup"] = string(data)

} return params, nil +} + +func (config StickerConfig) Name() string { + return "sticker" } // VideoConfig contains information about a SendVideo request.

@@ -404,6 +426,10 @@

return params, nil } +func (config VideoConfig) Name() string { + return "viceo" +} + // VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile

@@ -453,6 +479,10 @@ params["reply_markup"] = string(data)

} return params, nil +} + +func (config VoiceConfig) Name() string { + return "voice" } // LocationConfig contains information about a SendLocation request.
M helpers.gohelpers.go

@@ -11,7 +11,7 @@ // chatID is where to send it, text is the message text.

func NewMessage(chatID int, text string) MessageConfig { return MessageConfig{ BaseChat: BaseChat{ChatID: chatID}, - Text: text, + Text: text, DisableWebPagePreview: false, ReplyToMessageID: 0, }

@@ -23,7 +23,7 @@ // chatID is where to send it, fromChatID is the source chat,

// and messageID is the ID of the original message. func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig { return ForwardConfig{ - BaseChat: BaseChat{ChatID: chatID}, + BaseChat: BaseChat{ChatID: chatID}, FromChatID: fromChatID, MessageID: messageID, }

@@ -160,7 +160,7 @@ //

// chatID is where to send it, latitude and longitude are coordinates. func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig { return LocationConfig{ - BaseChat: BaseChat{ChatID: chatID}, + BaseChat: BaseChat{ChatID: chatID}, Latitude: latitude, Longitude: longitude, ReplyToMessageID: 0,

@@ -175,7 +175,7 @@ // chatID is where to send it, action should be set via CHAT constants.

func NewChatAction(chatID int, action string) ChatActionConfig { return ChatActionConfig{ BaseChat: BaseChat{ChatID: chatID}, - Action: action, + Action: action, } }