Fix editMedia and add editCaption

This commit is contained in:
Hamcha 2018-12-13 14:23:50 +01:00
parent dfbd036217
commit d7698a3c50
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
2 changed files with 45 additions and 4 deletions

View File

@ -51,6 +51,9 @@ const (
// CmdAnswerCallback requests the broker to reply to a incoming callback query
CmdAnswerCallback ClientCommandType = "answerCallback"
// CmdEditCaption requests to broker to replace the caption of a media (photo, doc, etc) message
CmdEditCaption ClientCommandType = "editCaption"
// CmdEditMedia requests to broker to replace the media contents of a message
CmdEditMedia ClientCommandType = "editMedia"
)
@ -102,6 +105,15 @@ type ClientAlbumData struct {
ReplyID *int64 `json:",omitempty"`
}
// ClientEditCaptionData is the required data for a CmdEditCaption request
type ClientEditCaptionData struct {
ChatID int64
MessageID int64
InlineID string
Caption string
ReplyMarkup interface{} `json:",omitempty"`
}
// ClientEditMediaData is the required data for a CmdEditMedia request
type ClientEditMediaData struct {
ChatID int64

View File

@ -213,11 +213,38 @@ func (t Telegram) AnswerCallback(data ClientCallbackQueryData) {
checkerr("AnswerCallback/http.PostForm", err)
}
// EditCaption modifies the caption of a photo/document/etc message
func (t Telegram) EditCaption(data ClientEditCaptionData) error {
postdata := url.Values{
"caption": {data.Caption},
"parse_mode": {"HTML"},
}
if data.InlineID != "" {
postdata.Set("inline_message_id", data.InlineID)
} else {
postdata.Set("chat_id", strconv.FormatInt(data.ChatID, 10))
postdata.Set("message_id", strconv.FormatInt(data.MessageID, 10))
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if checkerr("EditCaption/json.Marshal", err) {
return ErrMalformed
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err := http.PostForm(t.apiURL("editMessageCaption"), postdata)
checkerr("EditCaption/http.PostForm", err)
return nil
}
// EditMedia modifies the media content (like photo) of a message
func (t Telegram) EditMedia(data ClientEditMediaData) {
func (t Telegram) EditMedia(data ClientEditMediaData) error {
jsonmedia, err := json.Marshal(data.Media)
if err != nil {
checkerr("SendAlbum/json.Marshal", err)
checkerr("EditMedia/json.Marshal", err)
return ErrMalformed
}
postdata := url.Values{
"media": {string(jsonmedia)},
@ -230,14 +257,16 @@ func (t Telegram) EditMedia(data ClientEditMediaData) {
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if !checkerr("SendTextMessage/json.Marshal", err) {
return
if checkerr("EditMedia/json.Marshal", err) {
return ErrMalformed
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err = http.PostForm(t.apiURL("editMessageMedia"), postdata)
checkerr("EditMedia/http.PostForm", err)
return nil
}
// AnswerInlineQuery replies to an inline query