1
0
mirror of synced 2024-11-22 04:56:06 +03:00

POST /api/transport/v1/messages/history support

This commit is contained in:
Pavel 2022-11-03 17:30:46 +03:00 committed by GitHub
commit 7c964878ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 0 deletions

View File

@ -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:

View File

@ -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()

View File

@ -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
}

View File

@ -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.