From a27bf6410159c902c2833fc7d69b9b9324b19054 Mon Sep 17 00:00:00 2001 From: Vlasov Date: Wed, 27 Dec 2023 17:21:29 +0300 Subject: [PATCH] Added template information to the message webhook structure --- v1/client_test.go | 2 +- v1/types.go | 27 +++++++--- v1/types_test.go | 129 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 124 insertions(+), 34 deletions(-) diff --git a/v1/client_test.go b/v1/client_test.go index 321cb10..fb06b40 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -499,7 +499,7 @@ func (t *MGClientTest) Test_TextMessages() { ExternalID: "external_id", Type: MsgTypeText, Text: "hello!", - PageLink: "https://example.loca/catalog/1", + PageLink: "https://example.loca/catalog/1", }, Originator: OriginatorCustomer, Customer: Customer{ diff --git a/v1/types.go b/v1/types.go index da074d6..9aa95d6 100644 --- a/v1/types.go +++ b/v1/types.go @@ -448,14 +448,29 @@ type Suggestion struct { type TemplateInfo struct { Code string `json:"code"` - Variables TemplateVariables `json:"variables,omitempty"` + Variables TemplateArguments `json:"variables,omitempty"` } -type TemplateVariables struct { - Header []string `json:"header,omitempty"` - Attachments []TemplateAttachment `json:"attachments,omitempty"` - Body []string `json:"body,omitempty"` - Buttons [][]string `json:"buttons,omitempty"` +type TemplateArguments struct { + Header *TemplateHeaderArguments `json:"header,omitempty"` + Body TemplateBodyArguments `json:"body"` + Buttons []TemplateButtonArguments `json:"buttons,omitempty"` +} + +type TemplateHeaderArguments struct { + Type string `json:"type"` + Args []string `json:"args,omitempty"` // for type="text" only + Attachments []TemplateAttachment `json:"attachments,omitempty"` // for media templates only +} + +type TemplateBodyArguments struct { + Args []string `json:"args"` +} + +type TemplateButtonArguments struct { + Type string `json:"type"` + Title string `json:"title"` + Args []string `json:"args,omitempty"` } type TemplateAttachment struct { diff --git a/v1/types_test.go b/v1/types_test.go index 80076dd..4b0f87a 100644 --- a/v1/types_test.go +++ b/v1/types_test.go @@ -173,33 +173,108 @@ func TestTransportErrorResponse(t *testing.T) { func TestTemplateInfoUnmarshal(t *testing.T) { tmplJSON := `{ - "code": "namespace#BABA_JABA#ru", - "variables": { - "header": [ - "header1", - "header2" - ], - "attachments": [ - {"caption":"test-caption", "id":"550e8400-e29b-41d4-a716-446655440000"} - ], - "body": [ - "BABA", - "JABA" - ], - "buttons": [ - ["button1"], - [], - ["button2"] - ] - } - }` + "type": "message_sent", + "meta": { + "timestamp": 1703686050 + }, + "data": { + "external_user_id": "79998887766", + "external_chat_id": "", + "channel_id": 83, + "type": "image", + "content": "Thank you for your order\n\nYou have placed order No. 8061C in the amount of 17400. We have already started working on it and will soon notify you of a change in status.\n\nStay with us", + "quote_external_id": null, + "quote_content": null, + "in_app_id": 999, + "user": { + "id": 222, + "first_name": "Alex", + "last_name": "", + "avatar": "" + }, + "customer": { + "first_name": "", + "last_name": "", + "avatar": "" + }, + "items": [ + { + "id": "aa4ff988-bafb-43a0-9551-b8e00f677e34", + "size": 71984, + "caption": "test.png", + "height": 742, + "width": 305 + } + ], + "template": { + "code": "namespace#BABA_JABA#ru", + "args": [ + "var0", + "var1" + ], + "variables": { + "header": { + "type": "image", + "attachments": [ + { + "id": "aa4ff988-bafb-43a0-9551-b8e00f677e34", + "caption": "test.png" + } + ] + }, + "body": { + "args": [ + "var0", + "var1" + ] + }, + "buttons": [ + { + "type": "plain", + "title": "OK" + }, + { + "type": "url", + "title": "Our site", + "args": [ + "id0" + ] + }, + { + "type": "phone", + "title": "Our phone" + } + ] + } + } + } +}` - var tmpl TemplateInfo - assert.NoError(t, json.Unmarshal([]byte(tmplJSON), &tmpl)) + var wh struct { + Data struct { + Template TemplateInfo `json:"template"` + } `json:"data"` + } + assert.NoError(t, json.Unmarshal([]byte(tmplJSON), &wh)) + + tmpl := wh.Data.Template assert.Equal(t, "namespace#BABA_JABA#ru", tmpl.Code) - assert.Equal(t, []string{"header1", "header2"}, tmpl.Variables.Header) - assert.Equal(t, "550e8400-e29b-41d4-a716-446655440000", tmpl.Variables.Attachments[0].ID) - assert.Equal(t, "test-caption", tmpl.Variables.Attachments[0].Caption) - assert.Equal(t, []string{"BABA", "JABA"}, tmpl.Variables.Body) - assert.Equal(t, [][]string{{"button1"}, {}, {"button2"}}, tmpl.Variables.Buttons) + + assert.NotNil(t, tmpl.Variables.Header) + assert.Empty(t, tmpl.Variables.Header.Args) + assert.Equal(t, "aa4ff988-bafb-43a0-9551-b8e00f677e34", tmpl.Variables.Header.Attachments[0].ID) + assert.Equal(t, "test.png", tmpl.Variables.Header.Attachments[0].Caption) + + assert.Equal(t, []string{"var0", "var1"}, tmpl.Variables.Body.Args) + + assert.Len(t, tmpl.Variables.Buttons, 3) + assert.Equal(t, "plain", tmpl.Variables.Buttons[0].Type) + assert.Equal(t, "OK", tmpl.Variables.Buttons[0].Title) + assert.Empty(t, tmpl.Variables.Buttons[0].Args) + assert.Equal(t, "url", tmpl.Variables.Buttons[1].Type) + assert.Equal(t, "Our site", tmpl.Variables.Buttons[1].Title) + assert.Equal(t, []string{"id0"}, tmpl.Variables.Buttons[1].Args) + assert.Equal(t, "phone", tmpl.Variables.Buttons[2].Type) + assert.Equal(t, "Our phone", tmpl.Variables.Buttons[2].Title) + assert.Empty(t, tmpl.Variables.Buttons[2].Args) }