From 7b25148c1a1398c5e624281dc4e3cdfd5e180059 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Thu, 8 Nov 2018 17:17:24 +0300 Subject: [PATCH 1/3] add files & images support --- v1/client.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ v1/types.go | 61 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 5 deletions(-) diff --git a/v1/client.go b/v1/client.go index 01a934a..6d6aaa4 100644 --- a/v1/client.go +++ b/v1/client.go @@ -384,6 +384,81 @@ func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, err return resp, status, err } +// GetFile implement get file url +// +// Example: +// +// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6") +// +// data, status, err := client.GetFile("file_ID") +// +// if err != nil { +// fmt.Printf("%v", err) +// } +// +// fmt.Printf("%s\n", data.MessageID) +func (c *MgClient) GetFile(request string) (FullFileResponse, int, error) { + var resp FullFileResponse + var b []byte + + data, status, err := c.GetRequest(fmt.Sprintf("/files/%s", request), b) + + if err != nil { + return resp, status, err + } + + if e := json.Unmarshal(data, &resp); e != nil { + return resp, status, e + } + + if status != http.StatusOK { + return resp, status, c.Error(data) + } + + return resp, status, err +} + +// UploadFile upload file +func (c *MgClient) UploadFile(request []byte) (UploadFileResponse, int, error) { + var resp UploadFileResponse + + data, status, err := c.PostRequest("/files/upload", request) + if err != nil { + return resp, status, err + } + + if e := json.Unmarshal(data, &resp); e != nil { + return resp, status, e + } + + if status != http.StatusOK { + return resp, status, c.Error(data) + } + + return resp, status, err +} + +// UploadFileByURL upload file by url +func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileResponse, int, error) { + var resp UploadFileResponse + outgoing, _ := json.Marshal(&request) + + data, status, err := c.PostRequest("/files/upload_by_url", []byte(outgoing)) + if err != nil { + return resp, status, err + } + + if e := json.Unmarshal(data, &resp); e != nil { + return resp, status, e + } + + if status != http.StatusOK { + return resp, status, c.Error(data) + } + + return resp, status, err +} + func (c *MgClient) Error(info []byte) error { var data map[string]interface{} diff --git a/v1/types.go b/v1/types.go index 3fa5b72..c4267fc 100644 --- a/v1/types.go +++ b/v1/types.go @@ -39,6 +39,8 @@ const ( MsgOrderStatusCodeComplete = "complete" // MsgOrderStatusCodeCancel order status group cancel MsgOrderStatusCodeCancel = "cancel" + + FileSizeLimit = 20 * 1024 * 1024 ) // MgClient type @@ -59,11 +61,13 @@ type Channel struct { // ChannelSettings struct type ChannelSettings struct { - SpamAllowed bool `json:"spam_allowed"` - Status Status `json:"status"` - Text ChannelSettingsText `json:"text"` - Product Product `json:"product"` - Order Order `json:"order"` + SpamAllowed bool `json:"spam_allowed"` + Status Status `json:"status"` + Text ChannelSettingsText `json:"text"` + Product Product `json:"product"` + Order Order `json:"order"` + Files ChannelSettingsFilesBase `json:"files"` + Images ChannelSettingsFilesBase `json:"images"` } // Product type @@ -94,6 +98,44 @@ type ChannelSettingsText struct { Deleting string `json:"deleting"` } +// ChannelSettingsFilesBase struct +type ChannelSettingsFilesBase struct { + Creating string `json:"creating"` + Editing string `json:"editing"` + Quoting string `json:"quoting"` + Deleting string `json:"deleting"` + Max uint64 `json:"max"` +} + +// FullFileResponse uploaded file data +type FullFileResponse struct { + UploadFileResponse + Link string `json:"link,omitempty"` +} + +// UploadFileResponse uploaded file data +type UploadFileResponse struct { + ID []byte `json:"id"` + Hash string `json:"hash"` + Type string `json:"type"` + Meta FileMeta `json:"meta"` + MimeType string `json:"mime_type"` + Size int `json:"size"` + Url *string `json:"source_url"` + CreatedAt time.Time `json:"created_at"` +} + +// FileMeta file metadata +type FileMeta struct { + Width *int `json:"width,omitempty"` + Height *int `json:"height,omitempty"` +} + +// UploadFileByUrlRequest file url to upload +type UploadFileByUrlRequest struct { + Url string `json:"url"` +} + // ActivateResponse channel activation response type ActivateResponse struct { ChannelID uint64 `json:"id"` @@ -238,6 +280,15 @@ type WebhookData struct { Bot *MessageDataBot `json:"bot,omitempty"` Product *MessageDataProduct `json:"product,omitempty"` Order *MessageDataOrder `json:"order,omitempty"` + Images *[]FileItem `json:"images,omitempty"` + Files *[]FileItem `json:"files,omitempty"` +} + +// FileItem struct +type FileItem struct { + ID string `json:"id"` + Name string `json:"name"` + Size int `json:"size"` } // MessageDataUser user data from webhook From 82d2f2fc46295b0b757f5cfab4b61e592c8c4f94 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Tue, 13 Nov 2018 12:52:01 +0300 Subject: [PATCH 2/3] improve file and image elem in channel settings struct, minor fixes --- v1/client_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++- v1/types.go | 17 +++++++++++--- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/v1/client_test.go b/v1/client_test.go index 83e2ded..3311e7e 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -57,6 +57,12 @@ func TestMgClient_ActivateTransportChannel(t *testing.T) { Creating: ChannelFeatureBoth, Deleting: ChannelFeatureSend, }, + Image: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, + File: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, }, } @@ -94,6 +100,12 @@ func TestMgClient_ActivateNewTransportChannel(t *testing.T) { Creating: ChannelFeatureBoth, Deleting: ChannelFeatureSend, }, + Image: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, + File: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, }, } @@ -143,6 +155,12 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) { Creating: ChannelFeatureBoth, Deleting: ChannelFeatureSend, }, + Image: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, + File: ChannelSettingsFilesBase{ + Creating: ChannelFeatureBoth, + }, }, } @@ -155,7 +173,7 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) { t.Logf("Update selected channel: %v", data.ChannelID) } -func TestMgClient_Messages(t *testing.T) { +func TestMgClient_TextMessages(t *testing.T) { c := client() t.Logf("%v", ext) @@ -187,6 +205,46 @@ func TestMgClient_Messages(t *testing.T) { t.Logf("Message %v is sent", data.MessageID) } +func TestMgClient_ImageMessages(t *testing.T) { + c := client() + t.Logf("%v", ext) + + uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{ + Url: "https://via.placeholder.com/300", + }) + + if st != http.StatusOK { + t.Errorf("%v", err) + } + + snd := SendData{ + Message: Message{ + ExternalID: ext, + Type: MsgTypeImage, + Items: []Item{{ID: uploadFileResponse.ID}}, + }, + User: User{ + ExternalID: "6", + Nickname: "octopus", + Firstname: "Joe", + }, + Channel: channelID, + ExternalChatID: "24798237492374", + } + + data, status, err := c.Messages(snd) + + if status != http.StatusOK { + t.Errorf("%v", err) + } + + if data.Time.String() == "" { + t.Errorf("%v", err) + } + + t.Logf("Message %v is sent", data.MessageID) +} + func TestMgClient_UpdateMessages(t *testing.T) { c := client() t.Logf("%v", ext) diff --git a/v1/types.go b/v1/types.go index c4267fc..f9fdc70 100644 --- a/v1/types.go +++ b/v1/types.go @@ -26,6 +26,10 @@ const ( MsgTypeOrder string = "order" // MsgTypeProduct product card MsgTypeProduct string = "product" + // MsgTypeFile file card + MsgTypeFile string = "file" + // MsgTypeImage image card + MsgTypeImage string = "image" // MsgOrderStatusCodeNew order status group new MsgOrderStatusCodeNew = "new" @@ -66,8 +70,8 @@ type ChannelSettings struct { Text ChannelSettingsText `json:"text"` Product Product `json:"product"` Order Order `json:"order"` - Files ChannelSettingsFilesBase `json:"files"` - Images ChannelSettingsFilesBase `json:"images"` + File ChannelSettingsFilesBase `json:"file"` + Image ChannelSettingsFilesBase `json:"image"` } // Product type @@ -115,7 +119,7 @@ type FullFileResponse struct { // UploadFileResponse uploaded file data type UploadFileResponse struct { - ID []byte `json:"id"` + ID string `json:"id"` Hash string `json:"hash"` Type string `json:"type"` Meta FileMeta `json:"meta"` @@ -198,6 +202,7 @@ type Message struct { ExternalID string `json:"external_id"` Type string `json:"type,omitempty"` Text string `json:"text,omitempty"` + Items []Item `json:"items,omitempty"` } // SendMessage struct @@ -228,6 +233,12 @@ type SendData struct { Quote *SendMessageRequestQuote `json:"quote,omitempty"` } +// Item struct +type Item struct { + ID string `json:"id"` + Caption string `json:"caption"` +} + // SendMessageRequestQuote type type SendMessageRequestQuote struct { ExternalID string `json:"external_id"` From e11dcc8312095e6952042887cc76b9814f7f43a8 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 15 Nov 2018 14:11:11 +0300 Subject: [PATCH 3/3] improve FullFileResponse struct --- v1/types.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/v1/types.go b/v1/types.go index f9fdc70..54a7edd 100644 --- a/v1/types.go +++ b/v1/types.go @@ -108,13 +108,15 @@ type ChannelSettingsFilesBase struct { Editing string `json:"editing"` Quoting string `json:"quoting"` Deleting string `json:"deleting"` - Max uint64 `json:"max"` + Max uint64 `json:"max_items_count"` } // FullFileResponse uploaded file data type FullFileResponse struct { - UploadFileResponse - Link string `json:"link,omitempty"` + ID string `json:"id,omitempty"` + Type string `json:"type,omitempty"` + Size int `json:"size,omitempty"` + Url string `json:"url,omitempty"` } // UploadFileResponse uploaded file data