diff --git a/bot.go b/bot.go index 7a5115e..f93a9f5 100644 --- a/bot.go +++ b/bot.go @@ -81,6 +81,17 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, return apiResp, nil } +func (bot *BotAPI) MakeMessageRequest(endpoint string, params url.Values) (Message, error) { + resp, err := bot.MakeRequest(endpoint, params) + if err != nil { + return Message{}, err + } + + var message Message + json.Unmarshal(resp.Result, &message) + return message, nil +} + // UploadFile makes a request to the API with a file. // // Requires the parameter to hold the file not be in the params. @@ -189,14 +200,12 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("SendMessage", v) + message, err := bot.MakeMessageRequest("SendMessage", v) + if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("SendMessage req : %+v\n", v) log.Printf("SendMessage resp: %+v\n", message) @@ -209,16 +218,13 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { // // Requires ChatID (destination), FromChatID (source), and MessageID. func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { - v, _:= config.Values() + v, _ := config.Values() - resp, err := bot.MakeRequest("forwardMessage", v) + message, err := bot.MakeMessageRequest("forwardMessage", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("forwardMessage req : %+v\n", v) log.Printf("forwardMessage resp: %+v\n", message) @@ -240,14 +246,11 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("SendPhoto", v) + message, err := bot.MakeMessageRequest("SendPhoto", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("SendPhoto req : %+v\n", v) log.Printf("SendPhoto resp: %+v\n", message) @@ -317,14 +320,11 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendAudio", v) + message, err := bot.MakeMessageRequest("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) @@ -395,14 +395,11 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendDocument", v) + message, err := bot.MakeMessageRequest("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) @@ -466,14 +463,11 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendVoice", v) + message, err := bot.MakeMessageRequest("sendVoice", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("SendVoice req : %+v\n", v) log.Printf("SendVoice resp: %+v\n", message) @@ -538,14 +532,11 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendSticker", v) + message, err := bot.MakeMessageRequest("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) @@ -607,14 +598,11 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendVideo", v) + message, err := bot.MakeMessageRequest("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) @@ -674,14 +662,11 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { return Message{}, err } - resp, err := bot.MakeRequest("sendLocation", v) + message, err := bot.MakeMessageRequest("sendLocation", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("sendLocation req : %+v\n", v) log.Printf("sendLocation resp: %+v\n", message) diff --git a/configs.go b/configs.go index 4bc1d38..21a13e1 100644 --- a/configs.go +++ b/configs.go @@ -1,10 +1,10 @@ package tgbotapi import ( + "encoding/json" "io" "net/url" "strconv" - "encoding/json" ) // Telegram constants @@ -44,7 +44,7 @@ type Chattable struct { ChannelUsername string } -func (chattable *Chattable) Values() (url.Values, error){ +func (chattable *Chattable) Values() (url.Values, error) { v := url.Values{} if chattable.ChannelUsername != "" { v.Add("chat_id", chattable.ChannelUsername) @@ -83,7 +83,6 @@ func (config *MessageConfig) Values() (url.Values, error) { v.Add("reply_markup", string(data)) } - return v, nil } @@ -330,6 +329,7 @@ func (config *LocationConfig) Values() (url.Values, error) { v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64)) v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64)) + if config.ReplyToMessageID != 0 { v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) }