support for template webhooks
This commit is contained in:
commit
dbab908b1d
51
v1/types.go
51
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"`
|
||||
@ -659,3 +652,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"
|
||||
)
|
||||
|
54
v1/webhook.go
Normal file
54
v1/webhook.go
Normal file
@ -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
|
||||
}
|
85
v1/webhook_test.go
Normal file
85
v1/webhook_test.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user