diff --git a/v1/types.go b/v1/types.go index fe7b1af..11e565f 100644 --- a/v1/types.go +++ b/v1/types.go @@ -388,13 +388,6 @@ type MessagesResponse struct { Warnings []string `json:"warnings"` } -// WebhookRequest type. -type WebhookRequest struct { - Type string `json:"type"` - Meta TransportRequestMeta `json:"meta"` - Data WebhookData `json:"data"` -} - // WebhookMessageSentResponse type // Consider using this structure while processing webhook request. type WebhookMessageSentResponse struct { @@ -409,8 +402,8 @@ type MessageSentError struct { Message string `json:"message"` } -// WebhookData request data. -type WebhookData struct { +// MessageWebhookData request data. +type MessageWebhookData struct { ExternalUserID string `json:"external_user_id"` ExternalMessageID string `json:"external_message_id,omitempty"` ExternalChatID string `json:"external_chat_id"` @@ -429,6 +422,10 @@ type WebhookData struct { InAppID int32 `json:"in_app_id,omitempty"` } +// TemplateData request data. +type TemplateData struct { +} + type Attachments struct { Suggestions []Suggestion `json:"suggestions,omitempty"` } @@ -659,3 +656,43 @@ type ButtonParam struct { Text string `json:"text,omitempty"` URLParameter string `json:"urlParameter,omitempty"` } + +type TemplateContent struct { + Name string `json:"name"` + Lang string `json:"lang"` + Category string `json:"category"` + Body string `json:"body"` + Example struct { + Body []string `json:"body"` + } `json:"example"` +} + +type TemplateCreateWebhookData struct { + TemplateContent + ChannelID int64 `json:"channel_id"` +} + +type TemplateCreateWebhookResponse struct { + Code string `json:"code" binding:"required"` + VerificationStatus TemplateVerificationStatus `json:"verification_status" binding:"required"` +} + +type TemplateUpdateWebhookData struct { + TemplateContent + ChannelID int64 `json:"channel_id"` + Code string `json:"code"` +} + +type TemplateDeleteWebhookData struct { + ChannelID int64 `json:"channel_id"` + Code string `json:"code"` +} + +type TemplateVerificationStatus string + +const ( + TemplateStatusApproved TemplateVerificationStatus = "approved" + TemplateStatusPending TemplateVerificationStatus = "pending" + TemplateStatusRejected TemplateVerificationStatus = "rejected" + TemplateStatusNew TemplateVerificationStatus = "new" +) diff --git a/v1/webhook.go b/v1/webhook.go new file mode 100644 index 0000000..6b11e4f --- /dev/null +++ b/v1/webhook.go @@ -0,0 +1,54 @@ +package v1 + +import "encoding/json" + +type WebhookType string + +const ( + MessageSendWebhookType WebhookType = "message_sent" + MessageUpdateWebhookType WebhookType = "message_updated" + MessageDeleteWebhookType WebhookType = "message_deleted" + MessageReadWebhookType WebhookType = "message_read" + TemplateCreateWebhookType WebhookType = "template_create" + TemplateUpdateWebhookType WebhookType = "template_update" + TemplateDeleteWebhookType WebhookType = "template_delete" +) + +// WebhookRequest type. +type WebhookRequest struct { + Type WebhookType `json:"type"` + Meta TransportRequestMeta `json:"meta"` + Data json.RawMessage `json:"data"` +} + +// IsMessageWebhook returns true if current webhook contains data related to chat messages. +func (w WebhookRequest) IsMessageWebhook() bool { + return w.Type == MessageReadWebhookType || w.Type == MessageDeleteWebhookType || + w.Type == MessageSendWebhookType || w.Type == MessageUpdateWebhookType +} + +func (w WebhookRequest) IsTemplateWebhook() bool { + return w.Type == TemplateCreateWebhookType || + w.Type == TemplateUpdateWebhookType || + w.Type == TemplateDeleteWebhookType +} + +func (w WebhookRequest) MessageWebhookData() (wd MessageWebhookData) { + _ = json.Unmarshal(w.Data, &wd) + return +} + +func (w WebhookRequest) TemplateCreateWebhookData() (wd TemplateCreateWebhookData) { + _ = json.Unmarshal(w.Data, &wd) + return +} + +func (w WebhookRequest) TemplateUpdateWebhookData() (wd TemplateUpdateWebhookData) { + _ = json.Unmarshal(w.Data, &wd) + return +} + +func (w WebhookRequest) TemplateDeleteWebhookData() (wd TemplateDeleteWebhookData) { + _ = json.Unmarshal(w.Data, &wd) + return +} diff --git a/v1/webhook_test.go b/v1/webhook_test.go new file mode 100644 index 0000000..6d9a8e5 --- /dev/null +++ b/v1/webhook_test.go @@ -0,0 +1,85 @@ +package v1 + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestWebhookRequest_IsMessageWebhook(t *testing.T) { + assert.True(t, WebhookRequest{Type: MessageSendWebhookType}.IsMessageWebhook()) + assert.True(t, WebhookRequest{Type: MessageUpdateWebhookType}.IsMessageWebhook()) + assert.True(t, WebhookRequest{Type: MessageDeleteWebhookType}.IsMessageWebhook()) + assert.True(t, WebhookRequest{Type: MessageReadWebhookType}.IsMessageWebhook()) + assert.False(t, WebhookRequest{}.IsMessageWebhook()) +} + +func TestWebhookRequest_IsTemplateWebhook(t *testing.T) { + assert.True(t, WebhookRequest{Type: TemplateCreateWebhookType}.IsTemplateWebhook()) + assert.True(t, WebhookRequest{Type: TemplateUpdateWebhookType}.IsTemplateWebhook()) + assert.True(t, WebhookRequest{Type: TemplateDeleteWebhookType}.IsTemplateWebhook()) + assert.False(t, WebhookRequest{}.IsTemplateWebhook()) +} + +func TestWebhookData_MessageWebhookData(t *testing.T) { + wh := WebhookRequest{ + Type: MessageSendWebhookType, + Data: mustMarshalJSON(MessageWebhookData{ + ExternalUserID: "1", + ExternalMessageID: "1", + ExternalChatID: "1", + ChannelID: 1, + Content: "test", + Type: MsgTypeText, + }), + }.MessageWebhookData() + assert.Equal(t, "test", wh.Content) +} + +func TestWebhookData_TemplateCreateWebhookData(t *testing.T) { + wh := WebhookRequest{ + Type: TemplateCreateWebhookType, + Data: mustMarshalJSON(TemplateCreateWebhookData{ + TemplateContent: TemplateContent{ + Name: "template", + }, + ChannelID: 1, + }), + }.TemplateCreateWebhookData() + assert.Equal(t, "template", wh.TemplateContent.Name) +} + +func TestWebhookData_TemplateEditWebhookData(t *testing.T) { + wh := WebhookRequest{ + Type: TemplateUpdateWebhookType, + Data: mustMarshalJSON(TemplateUpdateWebhookData{ + TemplateContent: TemplateContent{ + Name: "template", + }, + ChannelID: 1, + Code: "code", + }), + }.TemplateUpdateWebhookData() + assert.Equal(t, "template", wh.TemplateContent.Name) + assert.Equal(t, "code", wh.Code) +} + +func TestWebhookData_TemplateDeleteWebhookData(t *testing.T) { + wh := WebhookRequest{ + Type: TemplateDeleteWebhookType, + Data: mustMarshalJSON(TemplateDeleteWebhookData{ + ChannelID: 1, + Code: "code", + }), + }.TemplateDeleteWebhookData() + assert.Equal(t, "code", wh.Code) +} + +func mustMarshalJSON(v interface{}) []byte { + data, err := json.Marshal(v) + if err != nil { + panic(err) + } + return data +}