Skip to content

Commit

Permalink
POST /api/transport/v1/messages/history support
Browse files Browse the repository at this point in the history
  • Loading branch information
Neur0toxine authored Nov 3, 2022
2 parents 04ec837 + 8308d6b commit 7c96487
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
54 changes: 54 additions & 0 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"io/ioutil"
"net/http"
"time"
)

const MB = 1 << 20
Expand All @@ -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
}
21 changes: 21 additions & 0 deletions v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7c96487

Please sign in to comment.