2015-06-26 07:34:10 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2015-07-02 18:39:39 +00:00
|
|
|
"bytes"
|
2022-06-24 15:25:25 +00:00
|
|
|
"context"
|
2015-06-26 07:34:10 +00:00
|
|
|
"encoding/json"
|
2022-01-22 17:42:45 +00:00
|
|
|
"fmt"
|
2015-07-02 18:39:39 +00:00
|
|
|
"io"
|
2015-06-26 07:34:10 +00:00
|
|
|
"io/ioutil"
|
2022-10-05 21:45:27 +00:00
|
|
|
"log"
|
2015-07-02 18:39:39 +00:00
|
|
|
"mime/multipart"
|
2015-06-26 07:34:10 +00:00
|
|
|
"net/http"
|
2015-07-02 18:39:39 +00:00
|
|
|
"os"
|
2015-06-26 16:12:54 +00:00
|
|
|
"strconv"
|
2017-11-17 06:20:36 +00:00
|
|
|
"strings"
|
2017-08-15 12:05:54 +00:00
|
|
|
"time"
|
2015-06-26 07:34:10 +00:00
|
|
|
)
|
|
|
|
|
2017-11-28 22:15:50 +00:00
|
|
|
// 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.
|
2017-11-28 22:15:50 +00:00
|
|
|
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
|
2016-06-26 14:05:37 +00:00
|
|
|
|
2017-08-15 13:44:01 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
|
2020-04-06 13:04:25 +00:00
|
|
|
return nil, err
|
2016-06-26 14:05:37 +00:00
|
|
|
}
|
2015-06-26 07:34:10 +00:00
|
|
|
|
2022-06-24 15:25:25 +00:00
|
|
|
// Cancel the request immediately without waiting for the timeout when bot is about to stop.
|
|
|
|
// This may become important if doing long polling with long timeout.
|
|
|
|
exit := make(chan struct{})
|
|
|
|
defer close(exit)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-b.stopClient:
|
|
|
|
cancel()
|
|
|
|
case <-exit:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-04 21:47:25 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)
|
2022-06-24 15:25:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := b.client.Do(req)
|
2015-06-26 07:34:10 +00:00
|
|
|
if err != nil {
|
2020-06-09 20:28:28 +00:00
|
|
|
return nil, wrapError(err)
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
2017-08-11 02:29:55 +00:00
|
|
|
resp.Close = true
|
2015-06-26 07:34:10 +00:00
|
|
|
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)
|
2018-10-11 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 12:00:30 +00:00
|
|
|
if b.verbose {
|
2022-10-04 21:47:25 +00:00
|
|
|
verbose(method, payload, data)
|
2020-05-31 12:00:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
// returning data as well
|
|
|
|
return data, extractOk(data)
|
2018-10-11 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
func (b *Bot) sendFiles(method string, files map[string]File, params map[string]string) ([]byte, error) {
|
2020-06-19 20:34:37 +00:00
|
|
|
rawFiles := make(map[string]interface{})
|
2018-10-11 11:39:07 +00:00
|
|
|
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:
|
2022-01-22 17:42:45 +00:00
|
|
|
return nil, fmt.Errorf("telebot: file for field %s doesn't exist", name)
|
2018-10-11 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 18:39:39 +00:00
|
|
|
|
2018-10-11 11:39:07 +00:00
|
|
|
if len(rawFiles) == 0 {
|
|
|
|
return b.Raw(method, params)
|
|
|
|
}
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2020-06-12 20:11:35 +00:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
writer := multipart.NewWriter(pipeWriter)
|
2021-11-07 16:28:21 +00:00
|
|
|
|
2020-06-12 20:11:35 +00:00
|
|
|
go func() {
|
|
|
|
defer pipeWriter.Close()
|
|
|
|
|
|
|
|
for field, file := range rawFiles {
|
2021-11-07 16:28:21 +00:00
|
|
|
if err := addFileToWriter(writer, files[field].fileName, field, file); err != nil {
|
2020-06-15 18:32:19 +00:00
|
|
|
pipeWriter.CloseWithError(err)
|
2020-06-12 20:11:35 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
2020-06-12 20:11:35 +00:00
|
|
|
for field, value := range params {
|
|
|
|
if err := writer.WriteField(field, value); err != nil {
|
2020-06-15 18:32:19 +00:00
|
|
|
pipeWriter.CloseWithError(err)
|
2020-06-12 20:11:35 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-11 11:39:07 +00:00
|
|
|
}
|
2020-06-12 20:11:35 +00:00
|
|
|
if err := writer.Close(); err != nil {
|
2020-06-15 18:32:19 +00:00
|
|
|
pipeWriter.CloseWithError(err)
|
2020-06-12 20:11:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
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-06-12 20:11:35 +00:00
|
|
|
resp, err := b.client.Post(url, writer.FormDataContentType(), pipeReader)
|
2015-07-02 18:39:39 +00:00
|
|
|
if err != nil {
|
2020-06-15 18:32:19 +00:00
|
|
|
err = wrapError(err)
|
|
|
|
pipeReader.CloseWithError(err)
|
|
|
|
return nil, err
|
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
|
|
|
|
2015-10-11 17:47:39 +00:00
|
|
|
if resp.StatusCode == http.StatusInternalServerError {
|
2020-04-06 13:04:25 +00:00
|
|
|
return nil, ErrInternal
|
2015-10-11 17:47:39 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-01-22 17:42:45 +00:00
|
|
|
return fmt.Errorf("telebot: file for field %v should be io.ReadCloser or string", field)
|
2020-04-06 13:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, opt)
|
2020-04-06 13:04:25 +00:00
|
|
|
|
|
|
|
data, err := b.Raw("sendMessage", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractMessage(data)
|
2015-07-02 18:39:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 17:31:43 +00:00
|
|
|
func (b *Bot) sendMedia(media Media, params map[string]string, files map[string]File) (*Message, error) {
|
|
|
|
kind := media.MediaType()
|
|
|
|
what := "send" + strings.Title(kind)
|
2015-07-06 13:46:19 +00:00
|
|
|
|
2021-11-07 17:31:43 +00:00
|
|
|
if kind == "videoNote" {
|
|
|
|
kind = "video_note"
|
2017-11-17 13:10:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 17:31:43 +00:00
|
|
|
sendFiles := map[string]File{kind: *media.MediaFile()}
|
2018-12-15 23:54:13 +00:00
|
|
|
for k, v := range files {
|
|
|
|
sendFiles[k] = v
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:31:43 +00:00
|
|
|
data, err := b.sendFiles(what, sendFiles, params)
|
2017-11-17 06:20:36 +00:00
|
|
|
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)
|
2015-07-06 13:46:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
func (b *Bot) getMe() (*User, error) {
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getMe", nil)
|
2015-06-26 07:34:10 +00:00
|
|
|
if err != nil {
|
2017-11-20 23:41:39 +00:00
|
|
|
return nil, err
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 18:29:45 +00:00
|
|
|
var resp struct {
|
2020-04-06 13:04:25 +00:00
|
|
|
Result *User
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
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)
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
2020-04-05 18:29:45 +00:00
|
|
|
return resp.Result, nil
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 17:43:17 +00:00
|
|
|
func (b *Bot) getUpdates(offset, limit int, timeout time.Duration, allowed []string) ([]Update, error) {
|
2016-06-26 14:05:37 +00:00
|
|
|
params := map[string]string{
|
2017-11-20 23:41:39 +00:00
|
|
|
"offset": strconv.Itoa(offset),
|
|
|
|
"timeout": strconv.Itoa(int(timeout / time.Second)),
|
2016-06-26 14:05:37 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
|
2023-03-07 18:25:55 +00:00
|
|
|
data, _ := json.Marshal(allowed)
|
|
|
|
params["allowed_updates"] = string(data)
|
|
|
|
|
2020-04-26 17:43:17 +00:00
|
|
|
if limit != 0 {
|
|
|
|
params["limit"] = strconv.Itoa(limit)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-06-26 07:34:10 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
var resp struct {
|
|
|
|
Result []Update
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-10-05 21:45:27 +00:00
|
|
|
|
|
|
|
// extractOk checks given result for error. If result is ok returns nil.
|
|
|
|
// In other cases it extracts API error. If error is not presented
|
|
|
|
// in errors.go, it will be prefixed with `unknown` keyword.
|
|
|
|
func extractOk(data []byte) error {
|
|
|
|
var e struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Code int `json:"error_code"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Parameters map[string]interface{} `json:"parameters"`
|
|
|
|
}
|
|
|
|
if json.NewDecoder(bytes.NewReader(data)).Decode(&e) != nil {
|
|
|
|
return nil // FIXME
|
|
|
|
}
|
|
|
|
if e.Ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Err(e.Description)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case ErrGroupMigrated:
|
|
|
|
migratedTo, ok := e.Parameters["migrate_to_chat_id"]
|
|
|
|
if !ok {
|
|
|
|
return NewError(e.Code, e.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GroupError{
|
|
|
|
err: err.(*Error),
|
|
|
|
MigratedTo: int64(migratedTo.(float64)),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Code {
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
retryAfter, ok := e.Parameters["retry_after"]
|
|
|
|
if !ok {
|
|
|
|
return NewError(e.Code, e.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = FloodError{
|
|
|
|
err: NewError(e.Code, e.Description),
|
|
|
|
RetryAfter: int(retryAfter.(float64)),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("telegram: %s (%d)", e.Description, e.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractMessage extracts common Message result from given data.
|
|
|
|
// Should be called after extractOk or b.Raw() to handle possible errors.
|
|
|
|
func extractMessage(data []byte) (*Message, error) {
|
|
|
|
var resp struct {
|
|
|
|
Result *Message
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
var resp struct {
|
|
|
|
Result bool
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
if resp.Result {
|
|
|
|
return nil, ErrTrueResult
|
|
|
|
}
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func verbose(method string, payload interface{}, data []byte) {
|
|
|
|
body, _ := json.Marshal(payload)
|
|
|
|
body = bytes.ReplaceAll(body, []byte(`\"`), []byte(`"`))
|
|
|
|
body = bytes.ReplaceAll(body, []byte(`"{`), []byte(`{`))
|
|
|
|
body = bytes.ReplaceAll(body, []byte(`}"`), []byte(`}`))
|
|
|
|
|
|
|
|
indent := func(b []byte) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
json.Indent(&buf, b, "", " ")
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[verbose] telebot: sent request\nMethod: %v\nParams: %v\nResponse: %v",
|
|
|
|
method, indent(body), indent(data),
|
|
|
|
)
|
|
|
|
}
|