From 299838450e6b78774bd2dad75bee3ccb98debd5d Mon Sep 17 00:00:00 2001 From: Demian Date: Thu, 23 Apr 2020 21:56:27 +0300 Subject: [PATCH] tests: implement b.EditReplyMarkup() function test --- bot.go | 17 +++++++++++------ bot_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/bot.go b/bot.go index 23c495f..3e750b1 100644 --- a/bot.go +++ b/bot.go @@ -726,24 +726,29 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes } // EditReplyMarkup used to edit reply markup of already sent message. +// Pass nil or empty ReplyMarkup to delete it from the message. // -// On success, returns edited message object +// On success, returns edited message object. +// This function will panic upon nil Editable. func (b *Bot) EditReplyMarkup(message Editable, markup *ReplyMarkup) (*Message, error) { messageID, chatID := message.MessageSig() - params := map[string]string{} - // if inline message - if chatID == 0 { + if chatID == 0 { // if inline message params["inline_message_id"] = messageID } else { params["chat_id"] = strconv.FormatInt(chatID, 10) params["message_id"] = messageID } + if markup == nil { + // will delete reply markup + markup = &ReplyMarkup{} + } + processButtons(markup.InlineKeyboard) - jsonMarkup, _ := json.Marshal(markup) - params["reply_markup"] = string(jsonMarkup) + data, _ := json.Marshal(markup) + params["reply_markup"] = string(data) data, err := b.Raw("editMessageReplyMarkup", params) if err != nil { diff --git a/bot_test.go b/bot_test.go index a09d8d5..dfe04b1 100644 --- a/bot_test.go +++ b/bot_test.go @@ -4,6 +4,7 @@ import ( "net/http" "os" "strconv" + "strings" "testing" "time" @@ -332,4 +333,30 @@ func TestBot(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, msg.Location) }) + + t.Run("EditReplyMarkup()", func(t *testing.T) { + markup := &ReplyMarkup{ + InlineKeyboard: [][]InlineButton{{{ + Data: "btn", + Text: "Hi Telebot!", + }}}, + } + badMarkup := &ReplyMarkup{ + InlineKeyboard: [][]InlineButton{{{ + Data: strings.Repeat("*", 65), + Text: "Bad Button", + }}}, + } + + msg, err := b.EditReplyMarkup(msg, markup) + assert.NoError(t, err) + assert.Equal(t, msg.ReplyMarkup.InlineKeyboard, markup.InlineKeyboard) + + msg, err = b.EditReplyMarkup(msg, nil) + assert.NoError(t, err) + assert.Nil(t, msg.ReplyMarkup.InlineKeyboard) + + _, err = b.EditReplyMarkup(msg, badMarkup) + assert.Equal(t, ErrButtonDataInvalid, err) + }) }