diff --git a/telegram.go b/telegram.go index 0aabdf3..adeb105 100644 --- a/telegram.go +++ b/telegram.go @@ -14,18 +14,37 @@ const ( ) func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) { + _, resp, err := http.Post(dst, fmt.Sprintf(APIEndpoint, bot.AccessToken, method), args) + if err != nil { + return nil, err + } + + var data Response + if err := json.Unmarshal(resp, &data); err != nil { + return nil, err + } + + if !data.Ok { + return &data, errors.New(data.Description) + } + + return &data, nil +} + +func (bot *Bot) upload(dst []byte, boundary, method string, args *http.Args) (*Response, error) { requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method) - if args != nil { - requestURI += args.String() + if &args != nil { + requestURI += fmt.Sprint("?", args.String()) } var req http.Request var resp http.Response req.Header.SetMethod("POST") - req.Header.SetContentType("application/json") - req.SetBody(dst) + req.Header.SetContentType("multipart/form-data") + req.Header.SetMultipartFormBoundary(boundary) req.SetRequestURI(requestURI) + req.SetBody(dst) if err := http.Do(&req, &resp); err != nil { return nil, err @@ -37,7 +56,7 @@ func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, } if !data.Ok { - return &data, errors.New(data.Description) + return nil, errors.New(data.Description) } return &data, nil