2017-10-25 14:48:53 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-21 10:15:03 +00:00
|
|
|
"net/url"
|
2017-10-25 14:48:53 +00:00
|
|
|
|
|
|
|
json "github.com/pquerna/ffjson/ffjson"
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
|
2017-11-21 10:15:03 +00:00
|
|
|
requestURI := &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "api.telegram.org",
|
|
|
|
Path: fmt.Sprint("/bot", bot.AccessToken, "/", method),
|
|
|
|
}
|
2017-10-25 14:48:53 +00:00
|
|
|
if args != nil {
|
2017-11-21 10:15:03 +00:00
|
|
|
requestURI.RawQuery = args.String()
|
2017-10-25 14:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var req http.Request
|
|
|
|
var resp http.Response
|
|
|
|
|
|
|
|
req.Header.SetMethod("POST")
|
|
|
|
req.Header.SetContentType("application/json")
|
2017-11-21 10:15:03 +00:00
|
|
|
req.SetRequestURI(requestURI.String())
|
2017-10-25 14:48:53 +00:00
|
|
|
req.SetBody(dst)
|
|
|
|
|
|
|
|
if err := http.Do(&req, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data Response
|
|
|
|
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !data.Ok {
|
|
|
|
return nil, errors.New(data.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
}
|