all repos — telegram-bot-api @ 3cfc52c9c2c86b6df6a0b54d25c634abd9487cf4

Golang bindings for the Telegram Bot API

print full response when debug
zhuharev kirill@zhuharev.ru
Mon, 30 Oct 2017 00:03:48 +0300
commit

3cfc52c9c2c86b6df6a0b54d25c634abd9487cf4

parent

846d467e14e674846aea543559900b7832e0b435

1 files changed, 30 insertions(+), 11 deletions(-)

jump to
M bot.gobot.go

@@ -7,6 +7,7 @@ "bytes"

"encoding/json" "errors" "fmt" + "io" "io/ioutil" "log" "net/http"

@@ -67,6 +68,15 @@ return APIResponse{}, err

} defer resp.Body.Close() + var apiResp APIResponse + bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp) + if err != nil { + return apiResp, err + } + if bot.Debug { + log.Printf("%s %s", endpoint, bytes) + } + if resp.StatusCode == http.StatusForbidden { return APIResponse{}, errors.New(ErrAPIForbidden) }

@@ -75,23 +85,32 @@ if resp.StatusCode != http.StatusOK {

return APIResponse{}, errors.New(http.StatusText(resp.StatusCode)) } - bytes, err := ioutil.ReadAll(resp.Body) - if err != nil { - return APIResponse{}, err + if !apiResp.Ok { + return apiResp, errors.New(apiResp.Description) } - if bot.Debug { - log.Println(endpoint, string(bytes)) + return apiResp, nil +} + +func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) (_ []byte, err error) { + if !bot.Debug { + dec := json.NewDecoder(responseBody) + err = dec.Decode(resp) + return } - var apiResp APIResponse - json.Unmarshal(bytes, &apiResp) + // if debug, read reponse body + data, err := ioutil.ReadAll(responseBody) + if err != nil { + return + } - if !apiResp.Ok { - return apiResp, errors.New(apiResp.Description) + err = json.Unmarshal(data, resp) + if err != nil { + return } - return apiResp, nil + return } // makeMessageRequest makes a request to a method that returns a Message.

@@ -875,4 +894,4 @@

bot.debugLog(config.method(), v, nil) return bot.MakeRequest(config.method(), v) -}+}