From 8308d6ba83ff5a47824560f2463a8355c8a61df2 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 3 Nov 2022 17:26:48 +0300 Subject: [PATCH] `POST /api/transport/v1/messages/history` support --- v1/client.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ v1/client_test.go | 37 ++++++++++++++++++++++++++++++++ v1/helpers.go | 9 ++++++++ v1/types.go | 21 ++++++++++++++++++ 4 files changed, 121 insertions(+) diff --git a/v1/client.go b/v1/client.go index 06c3fa9..eddce10 100644 --- a/v1/client.go +++ b/v1/client.go @@ -435,6 +435,60 @@ func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) { return resp, status, err } +// MessagesHistory implement history message sending. +// +// Example: +// +// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6") +// msg := v1.SendHistoryMessageRequest{ +// Message: v1.SendMessageRequestMessage{ +// Type: v1.MsgTypeText, +// ExternalID: "external_id", +// CreatedAt: v1.TimePtr(time.Now()), +// IsComment: false, +// Text: "Test message", +// }, +// ChannelID: 1, +// ExternalChatID: "chat_id", +// Customer: &v1.Customer{ +// ExternalID: "1", +// Nickname: "@john_doe", +// Firstname: "John", +// Lastname: "Doe", +// }, +// Originator: v1.OriginatorCustomer, +// ReplyDeadline: v1.TimePtr(time.Now().Add(time.Hour * 24)), +// } +// +// data, status, err := client.MessagesHistory(msg) +// if err != nil { +// fmt.Printf("[%d]: %v", status, err) +// } +// +// fmt.Printf("%d\n", data.MessageID) +func (c *MgClient) MessagesHistory(request SendHistoryMessageRequest) (MessagesResponse, int, error) { + var ( + resp MessagesResponse + outgoing = &bytes.Buffer{} + ) + _ = json.NewEncoder(outgoing).Encode(request) + + data, status, err := c.PostRequest("/messages/history", 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, NewAPIClientError(data) + } + + return resp, status, err +} + // UpdateMessages implement edit message // // Example: diff --git a/v1/client_test.go b/v1/client_test.go index e20e5f2..b99a49f 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -636,6 +636,43 @@ func (t *MGClientTest) Test_ReadUntil() { t.Assert().Equal([]int64{1}, resp.IDs) } +func (t *MGClientTest) Test_MessagesHistory() { + c := t.client() + + snd := SendHistoryMessageRequest{ + Message: SendMessageRequestMessage{ + ExternalID: "external_id", + Type: MsgTypeText, + Text: "hello!", + }, + Originator: OriginatorCustomer, + Customer: &Customer{ + ExternalID: "6", + Nickname: "octopus", + Firstname: "Joe", + }, + ChannelID: 1, + ExternalChatID: "24798237492374", + } + + defer gock.Off() + t.gock(). + Post(t.transportURL("messages/history")). + Reply(http.StatusOK). + JSON( + MessagesResponse{ + MessageID: 1, + Time: time.Now(), + }, + ) + + data, status, err := c.MessagesHistory(snd) + t.Require().NoError(err) + t.Assert().Equal(http.StatusOK, status) + t.Assert().NotEmpty(data.Time.String()) + t.Assert().Equal(1, data.MessageID) +} + func (t *MGClientTest) Test_MarkMessageReadAndDelete() { c := t.client() diff --git a/v1/helpers.go b/v1/helpers.go index 24fec0e..f7f7904 100644 --- a/v1/helpers.go +++ b/v1/helpers.go @@ -4,6 +4,7 @@ import ( "io" "io/ioutil" "net/http" + "time" ) const MB = 1 << 20 @@ -20,3 +21,11 @@ func buildLimitedRawResponse(resp *http.Response) ([]byte, error) { return body, nil } + +func BoolPtr(v bool) *bool { + return &v +} + +func TimePtr(v time.Time) *time.Time { + return &v +} diff --git a/v1/types.go b/v1/types.go index 1881b7c..bc6ea0b 100644 --- a/v1/types.go +++ b/v1/types.go @@ -286,6 +286,26 @@ type EditMessageRequestMessage struct { EditedAt int64 `json:"edited_at"` } +type SendHistoryMessageRequest struct { + Message SendMessageRequestMessage `json:"message"` + ChannelID uint64 `json:"channel_id"` + ExternalChatID string `json:"external_chat_id"` + Customer *Customer `json:"customer"` + Quote *SendMessageRequestQuote `json:"quote,omitempty"` + Originator Originator `json:"originator,omitempty"` + ReplyDeadline *time.Time `json:"reply_deadline,omitempty"` +} + +type SendMessageRequestMessage struct { + Type string `json:"type"` + ExternalID string `json:"external_id,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + IsComment bool `json:"is_comment,omitempty"` + Text string `json:"text"` + Items []Item `json:"items"` + Note string `json:"note,omitempty"` +} + // SendData struct. type SendData struct { Message Message `json:"message"` @@ -352,6 +372,7 @@ type DeleteData struct { type MessagesResponse struct { MessageID int `json:"message_id,omitempty"` Time time.Time `json:"time,omitempty"` + Warnings []string `json:"warnings"` } // WebhookRequest type.