2017-11-17 06:20:36 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-04-06 13:04:25 +00:00
|
|
|
"fmt"
|
2019-03-29 12:42:29 +00:00
|
|
|
"log"
|
2020-11-03 19:50:27 +00:00
|
|
|
"net/http"
|
2019-07-22 18:10:18 +00:00
|
|
|
"strconv"
|
2020-11-15 17:05:32 +00:00
|
|
|
"bytes"
|
2017-11-17 06:20:36 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
func (b *Bot) debug(err error) {
|
2019-03-29 12:42:29 +00:00
|
|
|
err = errors.WithStack(err)
|
2017-11-27 15:58:41 +00:00
|
|
|
if b.reporter != nil {
|
2019-03-29 12:42:29 +00:00
|
|
|
b.reporter(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("%+v\n", err)
|
2017-11-20 23:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:58:41 +00:00
|
|
|
func (b *Bot) deferDebug() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if err, ok := r.(error); ok {
|
|
|
|
b.debug(err)
|
|
|
|
} else if str, ok := r.(string); ok {
|
2017-12-19 00:15:29 +00:00
|
|
|
b.debug(errors.Errorf("%s", str))
|
2017-11-23 02:13:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:53:44 +00:00
|
|
|
func (b *Bot) runHandler(handler func()) {
|
|
|
|
f := func() {
|
|
|
|
defer b.deferDebug()
|
|
|
|
handler()
|
|
|
|
}
|
|
|
|
if b.synchronous {
|
|
|
|
f()
|
|
|
|
} else {
|
|
|
|
go f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:23:51 +00:00
|
|
|
// wrapError returns new wrapped telebot-related error.
|
|
|
|
func wrapError(err error) error {
|
|
|
|
return errors.Wrap(err, "telebot")
|
2017-11-27 15:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +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 {
|
2020-11-15 16:41:52 +00:00
|
|
|
// Parse the error message as JSON
|
|
|
|
var tgramApiError struct {
|
2020-11-15 16:43:31 +00:00
|
|
|
Ok bool `json:"ok"`
|
|
|
|
ErrorCode int `json:"error_code"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Parameters map[string]interface{} `json:"parameters"`
|
2020-11-15 16:41:52 +00:00
|
|
|
}
|
2020-11-15 17:05:32 +00:00
|
|
|
jdecoder := json.NewDecoder(bytes.NewReader(data))
|
|
|
|
jdecoder.UseNumber()
|
|
|
|
|
|
|
|
err := jdecoder.Decode(&tgramApiError)
|
2020-11-15 16:41:52 +00:00
|
|
|
if err != nil {
|
|
|
|
//return errors.Wrap(err, "can't parse JSON reply, the Telegram server is mibehaving")
|
|
|
|
// FIXME / TODO: in this case the error might be at HTTP level, or the content is not JSON (eg. image?)
|
2020-04-06 13:04:25 +00:00
|
|
|
return nil
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:41:52 +00:00
|
|
|
if tgramApiError.Ok {
|
|
|
|
// No error
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-03 19:50:27 +00:00
|
|
|
|
2020-11-15 16:41:52 +00:00
|
|
|
err = ErrByDescription(tgramApiError.Description)
|
2020-11-15 16:57:42 +00:00
|
|
|
if err != nil {
|
|
|
|
apierr, _ := err.(*APIError)
|
|
|
|
// Formally this is wrong, as the error is not created on the fly
|
|
|
|
// However, given the current way of handling errors, this a working
|
|
|
|
// workaround which doesn't break the API
|
|
|
|
apierr.Parameters = tgramApiError.Parameters
|
|
|
|
return apierr
|
|
|
|
}
|
2020-11-15 16:41:52 +00:00
|
|
|
|
2020-11-15 16:57:42 +00:00
|
|
|
switch tgramApiError.ErrorCode {
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
retryAfter, ok := tgramApiError.Parameters["retry_after"]
|
|
|
|
if !ok {
|
|
|
|
return NewAPIError(429, tgramApiError.Description)
|
|
|
|
}
|
|
|
|
retryAfterInt, _ := strconv.Atoi(fmt.Sprint(retryAfter))
|
|
|
|
|
|
|
|
err = FloodError{
|
|
|
|
APIError: NewAPIError(429, tgramApiError.Description),
|
|
|
|
RetryAfter: retryAfterInt,
|
2020-11-03 19:50:27 +00:00
|
|
|
}
|
2020-11-15 16:57:42 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("telegram unknown: %s (%d)", tgramApiError.Description, tgramApiError.ErrorCode)
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2020-11-03 19:50:27 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return err
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
// extractMessage extracts common Message result from given data.
|
2020-04-16 15:56:47 +00:00
|
|
|
// Should be called after extractOk or b.Raw() to handle possible errors.
|
2020-04-06 13:04:25 +00:00
|
|
|
func extractMessage(data []byte) (*Message, error) {
|
2017-11-17 07:22:16 +00:00
|
|
|
var resp struct {
|
2020-04-06 13:04:25 +00:00
|
|
|
Result *Message
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
2020-05-31 16:11:42 +00:00
|
|
|
var resp struct {
|
|
|
|
Result bool
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
if resp.Result {
|
|
|
|
return nil, ErrTrueResult
|
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
return nil, wrapError(err)
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
return resp.Result, nil
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:20:36 +00:00
|
|
|
func extractOptions(how []interface{}) *SendOptions {
|
2017-11-17 14:29:44 +00:00
|
|
|
var opts *SendOptions
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2017-11-17 14:29:44 +00:00
|
|
|
for _, prop := range how {
|
|
|
|
switch opt := prop.(type) {
|
2017-11-17 06:20:36 +00:00
|
|
|
case *SendOptions:
|
2017-12-10 23:21:51 +00:00
|
|
|
opts = opt.copy()
|
2017-11-17 06:20:36 +00:00
|
|
|
case *ReplyMarkup:
|
2017-11-17 14:29:44 +00:00
|
|
|
if opts == nil {
|
|
|
|
opts = &SendOptions{}
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-12-10 23:21:51 +00:00
|
|
|
opts.ReplyMarkup = opt.copy()
|
2017-11-17 14:29:44 +00:00
|
|
|
case Option:
|
|
|
|
if opts == nil {
|
|
|
|
opts = &SendOptions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch opt {
|
|
|
|
case NoPreview:
|
|
|
|
opts.DisableWebPagePreview = true
|
|
|
|
case Silent:
|
|
|
|
opts.DisableNotification = true
|
|
|
|
case ForceReply:
|
|
|
|
if opts.ReplyMarkup == nil {
|
|
|
|
opts.ReplyMarkup = &ReplyMarkup{}
|
|
|
|
}
|
|
|
|
opts.ReplyMarkup.ForceReply = true
|
|
|
|
case OneTimeKeyboard:
|
|
|
|
if opts.ReplyMarkup == nil {
|
|
|
|
opts.ReplyMarkup = &ReplyMarkup{}
|
|
|
|
}
|
|
|
|
opts.ReplyMarkup.OneTimeKeyboard = true
|
|
|
|
default:
|
2017-11-27 15:58:41 +00:00
|
|
|
panic("telebot: unsupported flag-option")
|
2017-11-17 14:29:44 +00:00
|
|
|
}
|
|
|
|
case ParseMode:
|
|
|
|
if opts == nil {
|
|
|
|
opts = &SendOptions{}
|
|
|
|
}
|
|
|
|
opts.ParseMode = opt
|
2017-11-17 06:20:36 +00:00
|
|
|
default:
|
2017-11-27 14:19:42 +00:00
|
|
|
panic("telebot: unsupported send-option")
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:29:44 +00:00
|
|
|
return opts
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:12:49 +00:00
|
|
|
func (b *Bot) embedSendOptions(params map[string]string, opt *SendOptions) {
|
|
|
|
if b.parseMode != ModeDefault {
|
|
|
|
params["parse_mode"] = b.parseMode
|
|
|
|
}
|
|
|
|
|
2017-11-17 06:20:36 +00:00
|
|
|
if opt == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
if opt.ReplyTo != nil && opt.ReplyTo.ID != 0 {
|
2017-11-17 06:20:36 +00:00
|
|
|
params["reply_to_message_id"] = strconv.Itoa(opt.ReplyTo.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.DisableWebPagePreview {
|
|
|
|
params["disable_web_page_preview"] = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.DisableNotification {
|
|
|
|
params["disable_notification"] = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.ParseMode != ModeDefault {
|
2020-04-16 07:17:17 +00:00
|
|
|
params["parse_mode"] = opt.ParseMode
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if opt.ReplyMarkup != nil {
|
2017-12-26 22:39:15 +00:00
|
|
|
processButtons(opt.ReplyMarkup.InlineKeyboard)
|
|
|
|
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
|
|
|
|
params["reply_markup"] = string(replyMarkup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processButtons(keys [][]InlineButton) {
|
|
|
|
if keys == nil || len(keys) < 1 || len(keys[0]) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:04:03 +00:00
|
|
|
for i := range keys {
|
|
|
|
for j := range keys[i] {
|
2017-12-26 22:39:15 +00:00
|
|
|
key := &keys[i][j]
|
|
|
|
if key.Unique != "" {
|
|
|
|
// Format: "\f<callback_name>|<data>"
|
|
|
|
data := key.Data
|
|
|
|
if data == "" {
|
|
|
|
key.Data = "\f" + key.Unique
|
|
|
|
} else {
|
|
|
|
key.Data = "\f" + key.Unique + "|" + data
|
2017-11-26 02:33:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-21 15:28:22 +00:00
|
|
|
|
2020-06-24 01:07:53 +00:00
|
|
|
func embedRights(p map[string]interface{}, rights Rights) {
|
2020-04-06 13:04:25 +00:00
|
|
|
data, _ := json.Marshal(rights)
|
|
|
|
_ = json.Unmarshal(data, &p)
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
2018-12-15 23:54:13 +00:00
|
|
|
|
|
|
|
func thumbnailToFilemap(thumb *Photo) map[string]File {
|
|
|
|
if thumb != nil {
|
|
|
|
return map[string]File{"thumb": thumb.File}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
|
|
|
|
func isUserInList(user *User, list []User) bool {
|
|
|
|
for _, user2 := range list {
|
|
|
|
if user.ID == user2.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|