mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-21 20:36:03 +03:00
correct templates methods
This commit is contained in:
parent
16e2bc304c
commit
9bd3d646fc
@ -7796,12 +7796,22 @@ func TestClient_ListMGChannelTemplates(t *testing.T) {
|
||||
assert.Equal(t, 1, template.Channel.ID)
|
||||
assert.Equal(t, 1, template.Channel.ExternalID)
|
||||
assert.Equal(t, "NAMEAAA", template.Name)
|
||||
|
||||
assert.Equal(t, TemplateItemTypeText, template.BodyTemplate[0].Type)
|
||||
assert.Equal(t, "Text_0", template.BodyTemplate[0].Text)
|
||||
|
||||
assert.Equal(t, TemplateItemTypeVar, template.BodyTemplate[1].Type)
|
||||
assert.Equal(t, TemplateVarCustom, template.BodyTemplate[1].VarType)
|
||||
assert.Equal(t, []string{"Text_1"}, template.BodyTemplateExample)
|
||||
assert.Equal(t, "en", template.Lang)
|
||||
assert.Equal(t, "test_0", template.Category)
|
||||
assert.Equal(t, []string{"Hello,", "{{1}}"}, template.Header.Text.Parts)
|
||||
|
||||
assert.Equal(t, TemplateItemTypeText, template.Header.Text.Parts[0].Type)
|
||||
assert.Equal(t, "JABAAA", template.Header.Text.Parts[0].Text)
|
||||
assert.Equal(t, TemplateItemTypeVar, template.Header.Text.Parts[1].Type)
|
||||
assert.Equal(t, TemplateVarCustom, template.Header.Text.Parts[1].VarType)
|
||||
assert.Equal(t, []string{"AAAAAA"}, template.Header.Text.Example)
|
||||
|
||||
assert.Equal(t, "https://example.com/file/123.png", template.Header.Image.Example)
|
||||
assert.Equal(t, "https://example.com/file/123.pdf", template.Header.Document.Example)
|
||||
assert.Equal(t, "https://example.com/file/123.mp4", template.Header.Video.Example)
|
||||
@ -7823,7 +7833,7 @@ func TestClient_ListMGChannelTemplates(t *testing.T) {
|
||||
|
||||
data, err := json.Marshal(template.BodyTemplate)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, `["Text_0",{}]`, string(data))
|
||||
assert.Equal(t, `["Text_0",{"var":"custom"}]`, string(data))
|
||||
}
|
||||
|
||||
func TestClient_ListMGChannelEmptyHeaderButtons(t *testing.T) {
|
||||
|
26
template.go
26
template.go
@ -36,7 +36,7 @@ var templateVarAssoc = map[string]interface{}{
|
||||
}
|
||||
|
||||
type Text struct {
|
||||
Parts []string `json:"parts"`
|
||||
Parts []TextTemplateItem `json:"parts"`
|
||||
Example []string `json:"example,omitempty"`
|
||||
}
|
||||
|
||||
@ -51,17 +51,17 @@ type Header struct {
|
||||
Video *Media `json:"video,omitempty"`
|
||||
}
|
||||
|
||||
type TemplateItemList []BodyTemplateItem
|
||||
type TemplateItemList []TextTemplateItem
|
||||
|
||||
// BodyTemplateItem is a part of template.
|
||||
type BodyTemplateItem struct {
|
||||
// TextTemplateItem is a part of template.
|
||||
type TextTemplateItem struct {
|
||||
Text string
|
||||
VarType string
|
||||
Type uint8
|
||||
}
|
||||
|
||||
// MarshalJSON controls how BodyTemplateItem will be marshaled into JSON.
|
||||
func (t BodyTemplateItem) MarshalJSON() ([]byte, error) {
|
||||
// MarshalJSON controls how TextTemplateItem will be marshaled into JSON.
|
||||
func (t TextTemplateItem) MarshalJSON() ([]byte, error) {
|
||||
switch t.Type {
|
||||
case TemplateItemTypeText:
|
||||
return json.Marshal(t.Text)
|
||||
@ -71,11 +71,11 @@ func (t BodyTemplateItem) MarshalJSON() ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
return nil, errors.New("unknown BodyTemplateItem type")
|
||||
return nil, errors.New("unknown TextTemplateItem type")
|
||||
}
|
||||
|
||||
// UnmarshalJSON will correctly unmarshal BodyTemplateItem.
|
||||
func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error {
|
||||
// UnmarshalJSON will correctly unmarshal TextTemplateItem.
|
||||
func (t *TextTemplateItem) UnmarshalJSON(b []byte) error {
|
||||
var obj interface{}
|
||||
err := json.Unmarshal(b, &obj)
|
||||
if err != nil {
|
||||
@ -89,8 +89,8 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error {
|
||||
case map[string]interface{}:
|
||||
// {} case
|
||||
if len(bodyPart) == 0 {
|
||||
t.Type = TemplateItemTypeVar
|
||||
t.VarType = TemplateVarCustom
|
||||
t.Type = TemplateItemTypeText
|
||||
t.Text = "{}"
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -102,10 +102,10 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error {
|
||||
t.Type = TemplateItemTypeVar
|
||||
t.VarType = varTypeCurr
|
||||
} else {
|
||||
return errors.New("invalid BodyTemplateItem")
|
||||
return errors.New("invalid TextTemplateItem")
|
||||
}
|
||||
default:
|
||||
return errors.New("invalid BodyTemplateItem")
|
||||
return errors.New("invalid TextTemplateItem")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -464,14 +464,17 @@ func getMGTemplatesResponse() string {
|
||||
"var": "custom"
|
||||
}
|
||||
],
|
||||
"templateExample": ["Text_1"],
|
||||
"namespace": "namespace_0",
|
||||
"lang": "en",
|
||||
"category": "test_0",
|
||||
"header": {
|
||||
"text": {
|
||||
"parts": [
|
||||
"Hello,",
|
||||
"{{1}}"
|
||||
"JABAAA",
|
||||
{
|
||||
"var": "custom"
|
||||
}
|
||||
],
|
||||
"example": [
|
||||
"AAAAAA"
|
||||
@ -514,5 +517,5 @@ func getMGTemplatesResponse() string {
|
||||
}
|
||||
|
||||
func getMGTemplatesForEdit() string {
|
||||
return `[{"header":{"text":{"parts":["Hello,","{{1}}"],"example":["Henry"]},"document":{"example":"https://example.com/file/123.pdf"},"image":{"example":"https://example.com/file/123.png"},"video":{"example":"https://example.com/file/123.mp4"}},"lang":"en","category":"test_0","code":"namespace#name_0#ru","name":"name_0","namespace":"namespace_0","footer":"footer_0","verificationStatus":"REJECTED","template":["Text_0",{"var":"custom"}],"buttons":[{"type":"PHONE_NUMBER","text":"your-phone-button-text","phoneNumber":"+79895553535"},{"type":"QUICK_REPLY","text":"Yes"},{"type":"URL","url":"https://example.com/file/{{1}}","text":"button","example":["https://www.website.com/dynamic-url-example"]}],"channel":{"type":"fbmessenger","name":"JABAAAAAAAAAA","id":1,"externalId":1,"allowedSendByPhone":false,"active":true},"id":1,"externalId":10,"active":true}]`
|
||||
return `[{"header":{"text":{"parts":["Hello,",{"var":"custom"}],"example":["Henry"]},"document":{"example":"https://example.com/file/123.pdf"},"image":{"example":"https://example.com/file/123.png"},"video":{"example":"https://example.com/file/123.mp4"}},"lang":"en","category":"test_0","code":"namespace#name_0#ru","name":"name_0","namespace":"namespace","footer":"footer_0","verificationStatus":"REJECTED","template":["Text_0",{"var":"custom"}],"buttons":[{"type":"PHONE_NUMBER","text":"your-phone-button-text","phoneNumber":"+79895553535"},{"type":"QUICK_REPLY","text":"Yes"},{"type":"URL","url":"https://example.com/file/{{1}}","text":"button","example":["https://www.website.com/dynamic-url-example"]}],"channel":{"type":"fbmessenger","name":"JABAAAAAAAAAA","id":1,"externalId":1,"allowedSendByPhone":false,"active":true},"id":1,"externalId":10,"active":true}]`
|
||||
}
|
||||
|
11
types.go
11
types.go
@ -1477,15 +1477,16 @@ type MGChannelTemplate struct {
|
||||
Header *Header `json:"header"`
|
||||
Lang string `json:"lang"`
|
||||
Category string `json:"category"`
|
||||
Code string `json:"code"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Footer string `json:"footer,omitempty"`
|
||||
VerificationStatus string `json:"verificationStatus"`
|
||||
BodyTemplate TemplateItemList `json:"template,omitempty"`
|
||||
VerificationStatus string `json:"verificationStatus,omitempty"`
|
||||
BodyTemplate TemplateItemList `json:"template"`
|
||||
BodyTemplateExample []string `json:"templateExample"`
|
||||
Buttons []Button `json:"buttons,omitempty"`
|
||||
Channel MGChannel `json:"channel"`
|
||||
ID int `json:"id"`
|
||||
ID int `json:"id,omitempty"`
|
||||
ExternalID int `json:"externalId,omitempty"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user