api-client-go/template.go

123 lines
2.9 KiB
Go
Raw Normal View History

2023-05-17 17:17:34 +03:00
package retailcrm
import (
"encoding/json"
"errors"
"fmt"
)
const (
// TemplateItemTypeText is a type for text chunk in template.
TemplateItemTypeText uint8 = iota
// TemplateItemTypeVar is a type for variable in template.
TemplateItemTypeVar
QuickReply ButtonType = "QUICK_REPLY"
PhoneNumber ButtonType = "PHONE_NUMBER"
URL ButtonType = "URL"
)
const (
// TemplateVarCustom is a custom variable type.
TemplateVarCustom = "custom"
// TemplateVarName is a name variable type.
TemplateVarName = "name"
// TemplateVarFirstName is a first name variable type.
TemplateVarFirstName = "first_name"
// TemplateVarLastName is a last name variable type.
TemplateVarLastName = "last_name"
)
// templateVarAssoc for checking variable validity, only for internal use.
var templateVarAssoc = map[string]interface{}{
TemplateVarCustom: nil,
TemplateVarName: nil,
TemplateVarFirstName: nil,
TemplateVarLastName: nil,
}
type Text struct {
2023-05-22 09:00:51 +03:00
Parts []TextTemplateItem `json:"parts"`
Example []string `json:"example,omitempty"`
2023-05-17 17:17:34 +03:00
}
type Media struct {
Example string `json:"example,omitempty"`
}
type Header struct {
Text *Text `json:"text,omitempty"`
Document *Media `json:"document,omitempty"`
Image *Media `json:"image,omitempty"`
Video *Media `json:"video,omitempty"`
}
2023-05-22 09:00:51 +03:00
type TemplateItemList []TextTemplateItem
2023-05-17 17:17:34 +03:00
2023-05-22 09:00:51 +03:00
// TextTemplateItem is a part of template.
type TextTemplateItem struct {
2023-05-17 17:17:34 +03:00
Text string
VarType string
Type uint8
}
2023-05-22 09:00:51 +03:00
// MarshalJSON controls how TextTemplateItem will be marshaled into JSON.
func (t TextTemplateItem) MarshalJSON() ([]byte, error) {
2023-05-17 17:17:34 +03:00
switch t.Type {
case TemplateItemTypeText:
return json.Marshal(t.Text)
case TemplateItemTypeVar:
return json.Marshal(map[string]interface{}{
"var": t.VarType,
})
}
2023-05-22 09:00:51 +03:00
return nil, errors.New("unknown TextTemplateItem type")
2023-05-17 17:17:34 +03:00
}
2023-05-22 09:00:51 +03:00
// UnmarshalJSON will correctly unmarshal TextTemplateItem.
func (t *TextTemplateItem) UnmarshalJSON(b []byte) error {
2023-05-17 17:17:34 +03:00
var obj interface{}
err := json.Unmarshal(b, &obj)
if err != nil {
return err
}
switch bodyPart := obj.(type) {
case string:
t.Type = TemplateItemTypeText
t.Text = bodyPart
case map[string]interface{}:
// {} case
if len(bodyPart) == 0 {
2023-05-22 09:00:51 +03:00
t.Type = TemplateItemTypeText
t.Text = "{}"
2023-05-17 17:17:34 +03:00
return nil
}
if varTypeCurr, ok := bodyPart["var"].(string); ok {
if _, ok := templateVarAssoc[varTypeCurr]; !ok {
return fmt.Errorf("invalid placeholder var '%s'", varTypeCurr)
}
t.Type = TemplateItemTypeVar
t.VarType = varTypeCurr
} else {
2023-05-22 09:00:51 +03:00
return errors.New("invalid TextTemplateItem")
2023-05-17 17:17:34 +03:00
}
default:
2023-05-22 09:00:51 +03:00
return errors.New("invalid TextTemplateItem")
2023-05-17 17:17:34 +03:00
}
return nil
}
type ButtonType string
type Button struct {
Type ButtonType `json:"type"`
URL string `json:"url,omitempty"`
Text string `json:"text,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
Example []string `json:"example,omitempty"`
}