Skip to content

Commit d4d683f

Browse files
committed
Re use the same MsgTemplating struct and GetTemplating method
1 parent 29098b1 commit d4d683f

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

Diff for: handlers/meta/whatsapp/templates.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ type MsgTemplating struct {
1414
Name string `json:"name" validate:"required"`
1515
UUID string `json:"uuid" validate:"required"`
1616
} `json:"template" validate:"required,dive"`
17-
Namespace string `json:"namespace"`
18-
Components []struct {
17+
Namespace string `json:"namespace"`
18+
ExternalID_ string `json:"external_id"`
19+
Components []struct {
1920
Type string `json:"type"`
2021
Name string `json:"name"`
2122
Params []struct {
2223
Type string `json:"type"`
24+
Name string `json:"name"`
2325
Value string `json:"value"`
2426
} `json:"params"`
2527
} `json:"components"`

Diff for: handlers/whatsapp_legacy/handler.go

+2-40
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gomodule/redigo/redis"
1717
"github.com/nyaruka/courier"
1818
"github.com/nyaruka/courier/handlers"
19+
"github.com/nyaruka/courier/handlers/meta/whatsapp"
1920
"github.com/nyaruka/courier/utils"
2021
"github.com/nyaruka/gocommon/httpx"
2122
"github.com/nyaruka/gocommon/i18n"
@@ -697,7 +698,7 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
697698

698699
} else {
699700
// do we have a template?
700-
templating, err := h.getTemplating(msg)
701+
templating, err := whatsapp.GetTemplating(msg)
701702
if err != nil {
702703
return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
703704
}
@@ -1080,45 +1081,6 @@ func (h *handler) checkWhatsAppContact(channel courier.Channel, baseURL string,
10801081
}
10811082
}
10821083

1083-
func (h *handler) getTemplating(msg courier.MsgOut) (*MsgTemplating, error) {
1084-
if len(msg.Metadata()) == 0 {
1085-
return nil, nil
1086-
}
1087-
1088-
metadata := &struct {
1089-
Templating *MsgTemplating `json:"templating"`
1090-
}{}
1091-
if err := json.Unmarshal(msg.Metadata(), metadata); err != nil {
1092-
return nil, err
1093-
}
1094-
1095-
if metadata.Templating == nil {
1096-
return nil, nil
1097-
}
1098-
1099-
if err := utils.Validate(metadata.Templating); err != nil {
1100-
return nil, errors.Wrapf(err, "invalid templating definition")
1101-
}
1102-
1103-
return metadata.Templating, nil
1104-
}
1105-
1106-
type MsgTemplating struct {
1107-
Template struct {
1108-
Name string `json:"name" validate:"required"`
1109-
UUID string `json:"uuid" validate:"required"`
1110-
} `json:"template" validate:"required,dive"`
1111-
Namespace string `json:"namespace"`
1112-
Components []struct {
1113-
Type string `json:"type"`
1114-
Name string `json:"name"`
1115-
Params []struct {
1116-
Type string `json:"type"`
1117-
Value string `json:"value"`
1118-
} `json:"params"`
1119-
} `json:"components"`
1120-
}
1121-
11221084
func getSupportedLanguage(lc i18n.Locale) string {
11231085
// look for exact match
11241086
if lang := supportedLanguages[lc]; lang != "" {

0 commit comments

Comments
 (0)