From 7c974998747752aa76ad7b590cb82edf10d99000 Mon Sep 17 00:00:00 2001 From: Vlasov Date: Thu, 21 Dec 2023 15:02:09 +0300 Subject: [PATCH] Added attachments field to template model --- v1/template.go | 12 ++++++--- v1/template_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/v1/template.go b/v1/template.go index ff973b2..6de806b 100644 --- a/v1/template.go +++ b/v1/template.go @@ -54,9 +54,10 @@ type Template struct { } type TemplateExample struct { - Body []string `json:"body,omitempty"` - Header []string `json:"header,omitempty"` - Buttons [][]string `json:"buttons,omitempty"` + Body []string `json:"body,omitempty"` + Header []string `json:"header,omitempty"` + Buttons [][]string `json:"buttons,omitempty"` + Attachments []TemplateExampleAttachment `json:"attachments,omitempty"` } type TemplateButtons struct { @@ -353,3 +354,8 @@ func (t *TemplateItem) UnmarshalJSON(b []byte) error { return nil } + +type TemplateExampleAttachment struct { + ID string `json:"id"` + Caption string `json:"caption"` +} diff --git a/v1/template_test.go b/v1/template_test.go index 7d94e3f..cdfd165 100644 --- a/v1/template_test.go +++ b/v1/template_test.go @@ -254,3 +254,69 @@ func TestUnmarshalInteractiveTemplate_VideoHeader(t *testing.T) { assert.Nil(t, template.Header) assert.Empty(t, template.Buttons) } + +func TestUnmarshalInteractiveTemplate_Examples(t *testing.T) { + var template Template + input := `{ + "code":"aaa#bbb#ru", + "phone": "79252223456", + "channel_id": 1, + "header": { + "content": { + "type": "text", + "body": "Hello, {{1}}!" + } + }, + "body": "Order {{1}} successfully delivered", + "buttons": { + "items": [ + { + "type": "url", + "label": "Go to website", + "url": "https://test.com/{{1}}" + }, + { + "type": "plain", + "label": "OK" + } + ] + }, + "verification_status": "approved", + "example": { + "header": ["Alex"], + "body": ["ORDER-111"], + "buttons": [["id123"], []] + } +}` + assert.NoError(t, json.Unmarshal([]byte(input), &template)) + + assert.NotNil(t, template.Example) + assert.Equal(t, []string{"Alex"}, template.Example.Header) + assert.Equal(t, []string{"ORDER-111"}, template.Example.Body) + assert.Equal(t, [][]string{{"id123"}, {}}, template.Example.Buttons) +} + +func TestUnmarshalInteractiveTemplate_Attachments(t *testing.T) { + var template Template + input := `{ + "code":"aaa#bbb#ru", + "phone": "79252223456", + "channel_id": 1, + "header": { + "content": { + "type": "image" + } + }, + "body": "Welcome to new delivery point", + "verification_status": "approved", + "example": { + "attachments": [{"id": "a6cf882e-6915-410a-8672-ed3a28a7875d", "caption": "test-cats.png"}] + } +}` + assert.NoError(t, json.Unmarshal([]byte(input), &template)) + + assert.NotNil(t, template.Example) + assert.Len(t, template.Example.Attachments, 1) + assert.Equal(t, "a6cf882e-6915-410a-8672-ed3a28a7875d", template.Example.Attachments[0].ID) + assert.Equal(t, "test-cats.png", template.Example.Attachments[0].Caption) +}