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 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'api.go') 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{} -- cgit v1.2.3