From 49c90f986bc8ba1abe7cedc24bd42522df4c3a2e Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 29 Mar 2019 18:52:22 +0300 Subject: Fix attachments --- api.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ models.go | 3 ++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index f3bc90e..3d727fd 100644 --- a/api.go +++ b/api.go @@ -351,6 +351,9 @@ func (a *Api) bytesToProperUpdate(b []byte) interface{} { case UpdateTypeMessageCreated: upd := UpdateMessageCreated{} _ = json.Unmarshal(b, &upd) + for _, att := range upd.Message.Body.RawAttachments { + upd.Message.Body.Attachments = append(upd.Message.Body.Attachments, a.bytesToProperAttachment(att)) + } return upd case UpdateTypeMessageRemoved: upd := UpdateMessageRemoved{} @@ -359,6 +362,9 @@ func (a *Api) bytesToProperUpdate(b []byte) interface{} { case UpdateTypeMessageEdited: upd := UpdateMessageEdited{} _ = json.Unmarshal(b, &upd) + for _, att := range upd.Message.Body.RawAttachments { + upd.Message.Body.Attachments = append(upd.Message.Body.Attachments, a.bytesToProperAttachment(att)) + } return upd case UpdateTypeMessageRestored: upd := UpdateMessageRestored{} @@ -392,6 +398,50 @@ func (a *Api) bytesToProperUpdate(b []byte) interface{} { return nil } +func (a *Api) bytesToProperAttachment(b []byte) interface{} { + attachment := new(Attachment) + _ = json.Unmarshal(b, attachment) + switch attachment.Type { + case AttachmentAudio: + res := &AudioAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentContact: + res := &ContactAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentFile: + res := &FileAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentImage: + res := &PhotoAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentKeyboard: + res := &InlineKeyboardAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentLocation: + res := &LocationAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentShare: + res := &ShareAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentSticker: + res := &StickerAttachment{} + _ = json.Unmarshal(b, &res) + return res + case AttachmentVideo: + res := &VideoAttachment{} + _ = json.Unmarshal(b, &res) + return res + } + return attachment +} + func (a *Api) getUpdates(limit int, timeout int, marker int64, types []string) (*UpdateList, error) { result := new(UpdateList) values := url.Values{} diff --git a/models.go b/models.go index 7ee7888..310baf6 100644 --- a/models.go +++ b/models.go @@ -387,7 +387,8 @@ type MessageBody struct { // Message text Text string `json:"text"` // Message attachments. Could be one of `Attachment` type. See description of this schema - Attachments []Attachment `json:"attachments"` + RawAttachments []json.RawMessage `json:"attachments"` + Attachments []interface{} // In case this message is repled to, it is the unique identifier of the replied message ReplyTo string `json:"reply_to,omitempty"` } -- cgit v1.2.3