Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added template information to the message webhook structure #108

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
27 changes: 21 additions & 6 deletions v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
129 changes: 102 additions & 27 deletions v1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 wh struct {
Data struct {
Template TemplateInfo `json:"template"`
} `json:"data"`
}
assert.NoError(t, json.Unmarshal([]byte(tmplJSON), &wh))

var tmpl TemplateInfo
assert.NoError(t, json.Unmarshal([]byte(tmplJSON), &tmpl))
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)
}
Loading