telebot/api.go

197 lines
4.2 KiB
Go
Raw Normal View History

package telebot
import (
2015-07-02 18:39:39 +00:00
"bytes"
"encoding/json"
2015-07-02 18:39:39 +00:00
"io"
"io/ioutil"
2015-07-02 18:39:39 +00:00
"mime/multipart"
"net/http"
2015-07-02 18:39:39 +00:00
"os"
2015-06-26 16:12:54 +00:00
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// Raw lets you call any method of Bot API manually.
2020-04-06 13:04:25 +00:00
// It also handles API errors, so you only need to unwrap
// result field from json data.
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
2019-09-30 15:57:54 +00:00
url := b.URL + "/bot" + b.Token + "/" + method
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
2020-04-06 13:04:25 +00:00
return nil, err
}
resp, err := b.client.Post(url, "application/json", &buf)
if err != nil {
2020-04-06 13:04:25 +00:00
return nil, errors.Wrap(err, "http.Post failed")
}
resp.Close = true
defer resp.Body.Close()
2019-09-30 15:57:54 +00:00
2020-04-05 17:23:51 +00:00
data, err := ioutil.ReadAll(resp.Body)
2015-06-26 19:20:08 +00:00
if err != nil {
2020-04-06 13:04:25 +00:00
return nil, wrapError(err)
}
2020-04-06 13:04:25 +00:00
// returning data as well
return data, extractOk(data)
}
2020-04-06 13:04:25 +00:00
func (b *Bot) sendFiles(method string, files map[string]File, params map[string]string) ([]byte, error) {
rawFiles := map[string]interface{}{}
for name, f := range files {
switch {
case f.InCloud():
params[name] = f.FileID
case f.FileURL != "":
params[name] = f.FileURL
case f.OnDisk():
rawFiles[name] = f.FileLocal
case f.FileReader != nil:
rawFiles[name] = f.FileReader
default:
2020-04-06 13:04:25 +00:00
return nil, errors.Errorf("telebot: File for field %s doesn't exist", name)
}
}
2015-07-02 18:39:39 +00:00
if len(rawFiles) == 0 {
return b.Raw(method, params)
}
2020-04-06 13:04:25 +00:00
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
2017-12-25 11:50:21 +00:00
for field, file := range rawFiles {
2019-09-30 15:57:54 +00:00
if err := addFileToWriter(writer, params["file_name"], field, file); err != nil {
2020-04-05 17:23:51 +00:00
return nil, wrapError(err)
}
2015-07-02 18:39:39 +00:00
}
for field, value := range params {
if err := writer.WriteField(field, value); err != nil {
2020-04-05 17:23:51 +00:00
return nil, wrapError(err)
}
2015-07-02 18:39:39 +00:00
}
if err := writer.Close(); err != nil {
2020-04-05 17:23:51 +00:00
return nil, wrapError(err)
2015-07-02 18:39:39 +00:00
}
2019-09-30 15:57:54 +00:00
url := b.URL + "/bot" + b.Token + "/" + method
2015-07-02 18:39:39 +00:00
2020-04-06 13:04:25 +00:00
resp, err := b.client.Post(url, writer.FormDataContentType(), &buf)
2015-07-02 18:39:39 +00:00
if err != nil {
return nil, errors.Wrap(err, "http.Post failed")
2015-07-02 18:39:39 +00:00
}
2020-04-06 13:04:25 +00:00
resp.Close = true
2019-12-25 23:03:10 +00:00
defer resp.Body.Close()
2015-07-02 18:39:39 +00:00
if resp.StatusCode == http.StatusInternalServerError {
2020-04-06 13:04:25 +00:00
return nil, ErrInternal
}
2020-04-05 17:23:51 +00:00
data, err := ioutil.ReadAll(resp.Body)
2015-07-02 18:39:39 +00:00
if err != nil {
2020-04-05 17:23:51 +00:00
return nil, wrapError(err)
2015-07-02 18:39:39 +00:00
}
2020-04-06 13:04:25 +00:00
return data, extractOk(data)
}
func addFileToWriter(writer *multipart.Writer, filename, field string, file interface{}) error {
var reader io.Reader
if r, ok := file.(io.Reader); ok {
reader = r
} else if path, ok := file.(string); ok {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
reader = f
} else {
return errors.Errorf("telebot: File for field %v should be an io.ReadCloser or string", field)
}
part, err := writer.CreateFormFile(field, filename)
if err != nil {
return err
}
_, err = io.Copy(part, reader)
return err
}
func (b *Bot) sendText(to Recipient, text string, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"text": text,
}
embedSendOptions(params, opt)
data, err := b.Raw("sendMessage", params)
if err != nil {
return nil, err
}
return extractMessage(data)
2015-07-02 18:39:39 +00:00
}
2018-12-15 23:54:13 +00:00
func (b *Bot) sendObject(f *File, what string, params map[string]string, files map[string]File) (*Message, error) {
sendWhat := "send" + strings.Title(what)
if what == "videoNote" {
what = "video_note"
}
2018-12-15 23:54:13 +00:00
sendFiles := map[string]File{what: *f}
for k, v := range files {
sendFiles[k] = v
}
2020-04-06 13:04:25 +00:00
data, err := b.sendFiles(sendWhat, sendFiles, params)
if err != nil {
return nil, err
2015-09-14 03:15:16 +00:00
}
2020-04-06 13:04:25 +00:00
return extractMessage(data)
}
func (b *Bot) getMe() (*User, error) {
2020-04-06 13:04:25 +00:00
data, err := b.Raw("getMe", nil)
if err != nil {
return nil, err
}
2020-04-05 18:29:45 +00:00
var resp struct {
2020-04-06 13:04:25 +00:00
Result *User
}
2020-04-06 13:04:25 +00:00
if err := json.Unmarshal(data, &resp); err != nil {
2020-04-05 18:29:45 +00:00
return nil, wrapError(err)
}
2020-04-05 18:29:45 +00:00
return resp.Result, nil
}
2020-04-06 13:04:25 +00:00
func (b *Bot) getUpdates(offset int, timeout time.Duration) ([]Update, error) {
params := map[string]string{
"offset": strconv.Itoa(offset),
"timeout": strconv.Itoa(int(timeout / time.Second)),
}
2020-04-06 13:04:25 +00:00
data, err := b.Raw("getUpdates", params)
2015-06-26 16:12:54 +00:00
if err != nil {
2020-04-06 13:04:25 +00:00
return nil, err
2015-06-26 16:12:54 +00:00
}
2020-04-06 13:04:25 +00:00
var resp struct {
Result []Update
}
2020-04-06 13:04:25 +00:00
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
2020-03-27 23:05:46 +00:00
}