Skip to content

Commit

Permalink
bot: handle True response results
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 31, 2020
1 parent 3e947d3 commit c271d16
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
17 changes: 16 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 3 additions & 14 deletions games.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion telebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit c271d16

Please sign in to comment.