Skip to content

Commit e9cc8dd

Browse files
committed
Add MessageGateway ID to MessageWebhookData and to requests for message operations
- Add the missing RestoreMessage operation
1 parent 4daacf3 commit e9cc8dd

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

v1/client.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,43 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
942942
return resp, status, err
943943
}
944944

945+
// RestoreMessage restores deleted message.
946+
//
947+
// Example:
948+
//
949+
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
950+
//
951+
// resp, status, err := client.RestoreMessage(RestoreMessageRequest{
952+
// Message: RestoreMessageRequestMessage{
953+
// ExternalID: "message_id_1",
954+
// },
955+
// ChannelID: 305,
956+
// })
957+
// if err != nil {
958+
// log.Fatalf("request error: %s (%d)", err, status)
959+
// }
960+
//
961+
// log.Printf("status: %d, message ID: %d", status, resp.MessageID)
962+
func (c *MgClient) RestoreMessage(request RestoreMessageRequest) (MessagesResponse, int, error) {
963+
var resp MessagesResponse
964+
outgoing, _ := json.Marshal(&request)
965+
966+
data, status, err := c.PostRequest("/messages/restore", bytes.NewBuffer(outgoing))
967+
if err != nil {
968+
return resp, status, err
969+
}
970+
971+
if e := json.Unmarshal(data, &resp); e != nil {
972+
return resp, status, e
973+
}
974+
975+
if status != http.StatusOK {
976+
return resp, status, NewAPIClientError(data)
977+
}
978+
979+
return resp, status, err
980+
}
981+
945982
// MakeTimestamp returns current unix timestamp in milliseconds.
946983
//
947984
// Example:

v1/client_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,3 +1123,26 @@ func (t *MGClientTest) Test_SuccessHandleError() {
11231123
t.Assert().IsType(new(HTTPClientError), err)
11241124
t.Assert().Equal("Channel not found", err.Error())
11251125
}
1126+
1127+
func (t *MGClientTest) Test_RestoreMessage() {
1128+
client := t.client()
1129+
t.gock().
1130+
Post(t.transportURL("messages/restore")).
1131+
Reply(http.StatusOK).
1132+
JSON(MessagesResponse{
1133+
MessageID: 1,
1134+
Time: time.Now(),
1135+
})
1136+
1137+
data, status, err := client.RestoreMessage(RestoreMessageRequest{
1138+
ChannelID: 30,
1139+
Message: RestoreMessageRequestMessage{
1140+
ExternalID: "external_1",
1141+
},
1142+
})
1143+
1144+
t.Require().NoError(err)
1145+
t.Assert().Equal(http.StatusOK, status)
1146+
t.Assert().NotEmpty(data.Time.String())
1147+
t.Assert().Equal(1, data.MessageID)
1148+
}

v1/types.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ type Utm struct {
319319

320320
// Message struct.
321321
type Message struct {
322-
ExternalID string `json:"external_id"`
322+
ID *int64 `json:"id,omitempty"`
323+
ExternalID string `json:"external_id,omitempty"`
323324
Type string `json:"type,omitempty"`
324325
Text string `json:"text,omitempty"`
325326
Note string `json:"note,omitempty"`
@@ -343,7 +344,8 @@ type EditMessageRequest struct {
343344

344345
// EditMessageRequestMessage type.
345346
type EditMessageRequestMessage struct {
346-
ExternalID string `json:"external_id"`
347+
ID *int64 `json:"id,omitempty"`
348+
ExternalID string `json:"external_id,omitempty"`
347349
Text string `json:"text"`
348350
EditedAt int64 `json:"edited_at"`
349351
PageLink string `json:"page_link,omitempty"`
@@ -366,7 +368,8 @@ type ReactionRequest struct {
366368
}
367369

368370
type ReactionMessageReference struct {
369-
ExternalID string `json:"external_id"`
371+
ID *int64 `json:"id,omitempty"`
372+
ExternalID string `json:"external_id,omitempty"`
370373
}
371374

372375
type SendMessageRequestMessage struct {
@@ -413,15 +416,23 @@ type MarkMessageReadRequest struct {
413416

414417
// MarkMessageReadRequestMessage type.
415418
type MarkMessageReadRequestMessage struct {
416-
ExternalID string `json:"external_id"`
419+
ID *int64 `json:"id,omitempty"`
420+
ExternalID string `json:"external_id,omitempty"`
417421
}
418422

419423
// AckMessageRequest type.
420424
type AckMessageRequest struct {
421-
ExternalMessageID string `json:"external_message_id"`
422-
TransportMessageID string `json:"transport_message_id,omitempty"`
423-
Channel uint64 `json:"channel"`
424-
Error *MessageSentError `json:"error,omitempty"`
425+
ExternalMessageID string `json:"external_message_id,omitempty"`
426+
TransportMessageID string `json:"transport_message_id,omitempty"`
427+
Channel uint64 `json:"channel"`
428+
Error *MessageSentError `json:"error,omitempty"`
429+
Message *AckMessageRequestMessage `json:"message,omitempty"`
430+
}
431+
432+
// AckMessageRequestMessage type.
433+
type AckMessageRequestMessage struct {
434+
ID *int64 `json:"id,omitempty"`
435+
ExternalID string `json:"external_id,omitempty"`
425436
}
426437

427438
// MarkMessagesReadUntilRequest type.
@@ -442,6 +453,18 @@ type DeleteData struct {
442453
Channel uint64 `json:"channel"`
443454
}
444455

456+
// RestoreMessageRequest type.
457+
type RestoreMessageRequest struct {
458+
ChannelID int64 `json:"channel_id"`
459+
Message RestoreMessageRequestMessage `json:"message"`
460+
}
461+
462+
// RestoreMessageRequestMessage type.
463+
type RestoreMessageRequestMessage struct {
464+
ID *int64 `json:"id,omitempty"`
465+
ExternalID string `json:"external_id,omitempty"`
466+
}
467+
445468
// MessagesResponse message event response.
446469
type MessagesResponse struct {
447470
MessageID int `json:"message_id,omitempty"`
@@ -468,6 +491,7 @@ type MessageSentError struct {
468491

469492
// MessageWebhookData request data.
470493
type MessageWebhookData struct {
494+
ID int64 `json:"id"`
471495
ExternalUserID string `json:"external_user_id"`
472496
ExternalMessageID string `json:"external_message_id,omitempty"`
473497
ExternalChatID string `json:"external_chat_id"`

v1/types_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ func TestSendData_MarshalJSON(t *testing.T) {
2121
"nickname": ""
2222
},
2323
"external_chat_id": "",
24-
"message": {
25-
"external_id": ""
26-
}
24+
"message": {}
2725
}`
2826
assert.JSONEq(t, expected, string(data))
2927
})
@@ -51,9 +49,7 @@ func TestSendData_MarshalJSON(t *testing.T) {
5149
"nickname": ""
5250
},
5351
"external_chat_id": "",
54-
"message": {
55-
"external_id": ""
56-
}
52+
"message": {}
5753
}`
5854

5955
for _, c := range cases {

0 commit comments

Comments
 (0)