tests: implement b.EditReplyMarkup() function test

This commit is contained in:
Demian 2020-04-23 21:56:27 +03:00
parent ad254b61d9
commit 299838450e
2 changed files with 38 additions and 6 deletions

17
bot.go
View File

@ -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. // 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) { func (b *Bot) EditReplyMarkup(message Editable, markup *ReplyMarkup) (*Message, error) {
messageID, chatID := message.MessageSig() messageID, chatID := message.MessageSig()
params := map[string]string{} params := map[string]string{}
// if inline message if chatID == 0 { // if inline message
if chatID == 0 {
params["inline_message_id"] = messageID params["inline_message_id"] = messageID
} else { } else {
params["chat_id"] = strconv.FormatInt(chatID, 10) params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID params["message_id"] = messageID
} }
if markup == nil {
// will delete reply markup
markup = &ReplyMarkup{}
}
processButtons(markup.InlineKeyboard) processButtons(markup.InlineKeyboard)
jsonMarkup, _ := json.Marshal(markup) data, _ := json.Marshal(markup)
params["reply_markup"] = string(jsonMarkup) params["reply_markup"] = string(data)
data, err := b.Raw("editMessageReplyMarkup", params) data, err := b.Raw("editMessageReplyMarkup", params)
if err != nil { if err != nil {

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"testing" "testing"
"time" "time"
@ -332,4 +333,30 @@ func TestBot(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, msg.Location) 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)
})
} }