2017-09-04 18:31:47 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
json "github.com/pquerna/ffjson/ffjson"
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
APIEndpoint = "https://api.telegram.org/bot%s/%s"
|
|
|
|
FileEndpoind = "https://api.telegram.org/file/bot%s/%s"
|
|
|
|
)
|
|
|
|
|
2017-09-05 08:51:43 +00:00
|
|
|
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
|
2017-10-17 11:05:46 +00:00
|
|
|
_, 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) {
|
2017-10-13 13:26:07 +00:00
|
|
|
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
2017-10-17 11:05:46 +00:00
|
|
|
if &args != nil {
|
|
|
|
requestURI += fmt.Sprint("?", args.String())
|
2017-10-13 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var req http.Request
|
|
|
|
var resp http.Response
|
|
|
|
|
|
|
|
req.Header.SetMethod("POST")
|
2017-10-17 11:05:46 +00:00
|
|
|
req.Header.SetContentType("multipart/form-data")
|
|
|
|
req.Header.SetMultipartFormBoundary(boundary)
|
2017-10-13 13:26:07 +00:00
|
|
|
req.SetRequestURI(requestURI)
|
2017-10-17 11:05:46 +00:00
|
|
|
req.SetBody(dst)
|
2017-10-13 13:26:07 +00:00
|
|
|
|
|
|
|
if err := http.Do(&req, &resp); err != nil {
|
2017-09-04 18:31:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-13 13:26:07 +00:00
|
|
|
var data Response
|
|
|
|
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
2017-09-04 18:31:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-13 13:26:07 +00:00
|
|
|
if !data.Ok {
|
2017-10-17 11:05:46 +00:00
|
|
|
return nil, errors.New(data.Description)
|
2017-09-04 18:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 13:26:07 +00:00
|
|
|
return &data, nil
|
2017-09-04 18:31:47 +00:00
|
|
|
}
|