bot: handle True response results

pull/302/head^2
Demian 4 years ago
parent 3e947d3392
commit c271d16dfc

@ -659,6 +659,9 @@ func (b *Bot) Forward(to Recipient, msg Editable, options ...interface{}) (*Mess
// Edit is magic, it lets you change already sent message.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// Use cases:
//
// b.Edit(msg, msg.Text, newMarkup)
@ -707,6 +710,9 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes
// EditReplyMarkup edits reply markup of already sent message.
// Pass nil or empty ReplyMarkup to delete it from the message.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// On success, returns edited message object.
// This function will panic upon nil Editable.
func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) {
@ -739,6 +745,9 @@ func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, erro
// EditCaption edits already sent photo caption with known recipient and message id.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// On success, returns edited message object.
// This function will panic upon nil Editable.
func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{}) (*Message, error) {
@ -768,6 +777,9 @@ func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{})
// EditMedia edits already sent media with known recipient and message id.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// Use cases:
//
// bot.EditMedia(msg, &tb.Photo{File: tb.FromDisk("chicken.jpg")})
@ -880,7 +892,7 @@ func (b *Bot) EditMedia(msg Editable, media InputMedia, options ...interface{})
data, _ := json.Marshal(result)
params["media"] = string(data)
if chatID == 0 { // If inline message.
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
@ -1113,6 +1125,9 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
// StopLiveLocation stops broadcasting live message location
// before Location.LivePeriod expires.
//
// If the message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// It supports tb.ReplyMarkup.
// This function will panic upon nil Editable.
func (b *Bot) StopLiveLocation(msg Editable, options ...interface{}) (*Message, error) {

@ -70,11 +70,8 @@ func (b *Bot) GetGameScores(user Recipient, msg Editable) ([]GameHighScore, erro
// SetGameScore sets the score of the specified user in a game.
//
// NOTE:
// If the message was sent by the bot, returns the edited Message,
// otherwise returns nil Message and ErrNoGameMessage.
// If you expect successful True result, you must check
// for `telebot: no game message` error.
// If the message was sent by the bot, returns the edited Message,
// otherwise returns nil and ErrTrueResult.
//
func (b *Bot) SetGameScore(user Recipient, msg Editable, score GameHighScore) (*Message, error) {
msgID, chatID := msg.MessageSig()
@ -97,13 +94,5 @@ func (b *Bot) SetGameScore(user Recipient, msg Editable, score GameHighScore) (*
if err != nil {
return nil, err
}
m, err := extractMessage(data)
if err != nil {
return nil, err
}
if m == nil {
return nil, ErrNoGameMessage
}
return m, nil
return extractMessage(data)
}

@ -34,7 +34,7 @@ var (
ErrBadRecipient = errors.New("telebot: recipient is nil")
ErrUnsupportedWhat = errors.New("telebot: unsupported what argument")
ErrCouldNotUpdate = errors.New("telebot: could not fetch new updates")
ErrNoGameMessage = errors.New("telebot: no game message")
ErrTrueResult = errors.New("telebot: result is True")
)
const DefaultApiURL = "https://api.telegram.org"

@ -70,6 +70,15 @@ func extractMessage(data []byte) (*Message, error) {
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

@ -13,6 +13,16 @@ func TestExtractOk(t *testing.T) {
data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
assert.Error(t, extractOk(data))
data = []byte(`{"ok":false,"error_code":400,"description":"Bad Request: reply message not found"}`) // Also check the old format
data = []byte(`{"ok":false,"error_code":400,"description":"Bad Request: reply message not found"}`)
assert.EqualError(t, extractOk(data), ErrToReplyNotFound.Error())
}
func TestExtractMessage(t *testing.T) {
data := []byte(`{"ok":true,"result":true}`)
_, err := extractMessage(data)
assert.Equal(t, ErrTrueResult, err)
data = []byte(`{"ok":true,"result":{"foo":"bar"}}`)
_, err = extractMessage(data)
assert.NoError(t, err)
}

Loading…
Cancel
Save