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 URL channel message suggestion #107

Merged
merged 1 commit into from
Feb 6, 2024
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
7 changes: 5 additions & 2 deletions v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type ChannelSettingsSuggestions struct {
Text string `json:"text,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
URL string `json:"url,omitempty"`
}

type ChannelSettingsTemplate struct {
Expand Down Expand Up @@ -437,13 +438,15 @@ const (
SuggestionTypeText SuggestionType = "text"
SuggestionTypeEmail SuggestionType = "email"
SuggestionTypePhone SuggestionType = "phone"
SuggestionTypeURL SuggestionType = "url"
)

type SuggestionType string

type Suggestion struct {
Type SuggestionType `json:"type"`
Title string `json:"title,omitempty"` // required for type=text and ignored for others
Type SuggestionType `json:"type"`
Title string `json:"title,omitempty"` // required for type=text and optional for others
Payload string `json:"payload,omitempty"` // ignored for type=text
}

type TemplateInfo struct {
Expand Down
79 changes: 79 additions & 0 deletions v1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,82 @@ func TestTemplateInfoUnmarshal(t *testing.T) {
assert.Equal(t, []string{"BABA", "JABA"}, tmpl.Variables.Body)
assert.Equal(t, [][]string{{"button1"}, {}, {"button2"}}, tmpl.Variables.Buttons)
}

func TestUnmarshalMessageWebhook(t *testing.T) {
msgJSON := `{
"type": "message_sent",
"meta": {
"timestamp": 1703523308
},
"data": {
"external_user_id": "79998887766",
"external_chat_id": "",
"channel_id": 83,
"type": "text",
"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": 638,
"bot": {
"id": 760,
"name": "Bot system",
"avatar": ""
},
"customer": {
"first_name": "",
"last_name": "",
"avatar": ""
},
"template": {
"code": "f87e678f_660b_461a_b60a_a6194e2e9094#thanks_for_order#ru",
"args": [
"8061C",
"17400"
],
"variables": {
"body": [
"8061C",
"17400"
],
"buttons": [
[],
[
"8061"
]
]
}
},
"attachments": {
"suggestions": [
{
"type": "text",
"title": "ОК"
},
{
"type": "url",
"title": "Our site",
"payload": "https://site.com/8061"
}
]
}
}
}`

var wh struct {
Data MessageWebhookData `json:"data"`
}
assert.NoError(t, json.Unmarshal([]byte(msgJSON), &wh))

assert.NotNil(t, wh.Data.Attachments)
assert.Len(t, wh.Data.Attachments.Suggestions, 2)

okButton := wh.Data.Attachments.Suggestions[0]
assert.Equal(t, SuggestionTypeText, okButton.Type)
assert.Equal(t, "ОК", okButton.Title)
assert.Empty(t, okButton.Payload)

urlButton := wh.Data.Attachments.Suggestions[1]
assert.Equal(t, SuggestionTypeURL, urlButton.Type)
assert.Equal(t, "Our site", urlButton.Title)
assert.Equal(t, "https://site.com/8061", urlButton.Payload)
}
Loading