diff --git a/bot_test.go b/bot_test.go index 215e08b..200404a 100644 --- a/bot_test.go +++ b/bot_test.go @@ -340,6 +340,28 @@ func TestSendChatConfig(t *testing.T) { } } +func TestSendEditMessage(t *testing.T) { + bot, _ := getBot(t) + + msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing.")) + if err != nil { + t.Fail() + } + + edit := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ + ChatID: ChatID, + MessageID: msg.MessageID, + }, + Text: "Updated text.", + } + + _, err = bot.Send(edit) + if err != nil { + t.Fail() + } +} + func TestGetUserProfilePhotos(t *testing.T) { bot, _ := getBot(t) diff --git a/configs.go b/configs.go index 5f9f6aa..614e99b 100644 --- a/configs.go +++ b/configs.go @@ -153,6 +153,37 @@ func (file BaseFile) useExistingFile() bool { return file.UseExisting } +// BaseEdit is base type of all chat edits. +type BaseEdit struct { + ChatID int64 + ChannelUsername string + MessageID int + InlineMessageID string + ReplyMarkup *InlineKeyboardMarkup +} + +func (edit BaseEdit) values() (url.Values, error) { + v := url.Values{} + + if edit.ChannelUsername != "" { + v.Add("chat_id", edit.ChannelUsername) + } else { + v.Add("chat_id", strconv.FormatInt(edit.ChatID, 10)) + } + v.Add("message_id", strconv.Itoa(edit.MessageID)) + v.Add("inline_message_id", edit.InlineMessageID) + + if edit.ReplyMarkup != nil { + data, err := json.Marshal(edit.ReplyMarkup) + if err != nil { + return v, err + } + v.Add("reply_markup", string(data)) + } + + return v, nil +} + // MessageConfig contains information about a SendMessage request. type MessageConfig struct { BaseChat @@ -500,6 +531,63 @@ func (config ChatActionConfig) method() string { return "sendChatAction" } +// EditMessageTextConfig allows you to modify the text in a message. +type EditMessageTextConfig struct { + BaseEdit + Text string + ParseMode string + DisableWebPagePreview bool + ReplyMarkup InlineKeyboardMarkup +} + +func (config EditMessageTextConfig) values() (url.Values, error) { + v, _ := config.BaseEdit.values() + + v.Add("text", config.Text) + v.Add("parse_mode", config.ParseMode) + v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview)) + + return v, nil +} + +func (config EditMessageTextConfig) method() string { + return "editMessageText" +} + +// EditMessageCaptionConfig allows you to modify the caption of a message. +type EditMessageCaptionConfig struct { + BaseEdit + Caption string + ReplyMarkup InlineKeyboardMarkup +} + +func (config EditMessageCaptionConfig) values() (url.Values, error) { + v, _ := config.BaseEdit.values() + + v.Add("caption", config.Caption) + + return v, nil +} + +func (config EditMessageCaptionConfig) method() string { + return "editMessageCaption" +} + +// EditMessageReplyMarkup allows you to modify the reply markup +// of a message. +type EditMessageReplyMarkup struct { + BaseEdit + ReplyMarkup InlineKeyboardMarkup +} + +func (config EditMessageReplyMarkup) values() (url.Values, error) { + return config.BaseEdit.values() +} + +func (config EditMessageReplyMarkup) method() string { + return "editMessageReplyMarkup" +} + // UserProfilePhotosConfig contains information about a // GetUserProfilePhotos request. type UserProfilePhotosConfig struct {