✨ Added upload method
This commit is contained in:
parent
f719e391e4
commit
5e907b296d
29
telegram.go
29
telegram.go
|
@ -14,18 +14,37 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
|
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)
|
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
||||||
if args != nil {
|
if &args != nil {
|
||||||
requestURI += args.String()
|
requestURI += fmt.Sprint("?", args.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
var req http.Request
|
var req http.Request
|
||||||
var resp http.Response
|
var resp http.Response
|
||||||
|
|
||||||
req.Header.SetMethod("POST")
|
req.Header.SetMethod("POST")
|
||||||
req.Header.SetContentType("application/json")
|
req.Header.SetContentType("multipart/form-data")
|
||||||
req.SetBody(dst)
|
req.Header.SetMultipartFormBoundary(boundary)
|
||||||
req.SetRequestURI(requestURI)
|
req.SetRequestURI(requestURI)
|
||||||
|
req.SetBody(dst)
|
||||||
|
|
||||||
if err := http.Do(&req, &resp); err != nil {
|
if err := http.Do(&req, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -37,7 +56,7 @@ func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !data.Ok {
|
if !data.Ok {
|
||||||
return &data, errors.New(data.Description)
|
return nil, errors.New(data.Description)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &data, nil
|
return &data, nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user