Skip to content

Commit b613053

Browse files
author
Syfaro
committed
Add methods for editing messages.
1 parent a92f88c commit b613053

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

bot_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,28 @@ func TestSendChatConfig(t *testing.T) {
340340
}
341341
}
342342

343+
func TestSendEditMessage(t *testing.T) {
344+
bot, _ := getBot(t)
345+
346+
msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
347+
if err != nil {
348+
t.Fail()
349+
}
350+
351+
edit := tgbotapi.EditMessageTextConfig{
352+
BaseEdit: tgbotapi.BaseEdit{
353+
ChatID: ChatID,
354+
MessageID: msg.MessageID,
355+
},
356+
Text: "Updated text.",
357+
}
358+
359+
_, err = bot.Send(edit)
360+
if err != nil {
361+
t.Fail()
362+
}
363+
}
364+
343365
func TestGetUserProfilePhotos(t *testing.T) {
344366
bot, _ := getBot(t)
345367

configs.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,37 @@ func (file BaseFile) useExistingFile() bool {
153153
return file.UseExisting
154154
}
155155

156+
// BaseEdit is base type of all chat edits.
157+
type BaseEdit struct {
158+
ChatID int64
159+
ChannelUsername string
160+
MessageID int
161+
InlineMessageID string
162+
ReplyMarkup *InlineKeyboardMarkup
163+
}
164+
165+
func (edit BaseEdit) values() (url.Values, error) {
166+
v := url.Values{}
167+
168+
if edit.ChannelUsername != "" {
169+
v.Add("chat_id", edit.ChannelUsername)
170+
} else {
171+
v.Add("chat_id", strconv.FormatInt(edit.ChatID, 10))
172+
}
173+
v.Add("message_id", strconv.Itoa(edit.MessageID))
174+
v.Add("inline_message_id", edit.InlineMessageID)
175+
176+
if edit.ReplyMarkup != nil {
177+
data, err := json.Marshal(edit.ReplyMarkup)
178+
if err != nil {
179+
return v, err
180+
}
181+
v.Add("reply_markup", string(data))
182+
}
183+
184+
return v, nil
185+
}
186+
156187
// MessageConfig contains information about a SendMessage request.
157188
type MessageConfig struct {
158189
BaseChat
@@ -500,6 +531,63 @@ func (config ChatActionConfig) method() string {
500531
return "sendChatAction"
501532
}
502533

534+
// EditMessageTextConfig allows you to modify the text in a message.
535+
type EditMessageTextConfig struct {
536+
BaseEdit
537+
Text string
538+
ParseMode string
539+
DisableWebPagePreview bool
540+
ReplyMarkup InlineKeyboardMarkup
541+
}
542+
543+
func (config EditMessageTextConfig) values() (url.Values, error) {
544+
v, _ := config.BaseEdit.values()
545+
546+
v.Add("text", config.Text)
547+
v.Add("parse_mode", config.ParseMode)
548+
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
549+
550+
return v, nil
551+
}
552+
553+
func (config EditMessageTextConfig) method() string {
554+
return "editMessageText"
555+
}
556+
557+
// EditMessageCaptionConfig allows you to modify the caption of a message.
558+
type EditMessageCaptionConfig struct {
559+
BaseEdit
560+
Caption string
561+
ReplyMarkup InlineKeyboardMarkup
562+
}
563+
564+
func (config EditMessageCaptionConfig) values() (url.Values, error) {
565+
v, _ := config.BaseEdit.values()
566+
567+
v.Add("caption", config.Caption)
568+
569+
return v, nil
570+
}
571+
572+
func (config EditMessageCaptionConfig) method() string {
573+
return "editMessageCaption"
574+
}
575+
576+
// EditMessageReplyMarkup allows you to modify the reply markup
577+
// of a message.
578+
type EditMessageReplyMarkup struct {
579+
BaseEdit
580+
ReplyMarkup InlineKeyboardMarkup
581+
}
582+
583+
func (config EditMessageReplyMarkup) values() (url.Values, error) {
584+
return config.BaseEdit.values()
585+
}
586+
587+
func (config EditMessageReplyMarkup) method() string {
588+
return "editMessageReplyMarkup"
589+
}
590+
503591
// UserProfilePhotosConfig contains information about a
504592
// GetUserProfilePhotos request.
505593
type UserProfilePhotosConfig struct {

0 commit comments

Comments
 (0)