Skip to content

Commit 14c3781

Browse files
committed
Move HSM definitions to hsm.go
1 parent 91312a9 commit 14c3781

File tree

4 files changed

+142
-59
lines changed

4 files changed

+142
-59
lines changed

conversation/api.go

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,15 @@ const (
160160
// per request.
161161
type MessageContent struct {
162162
Audio *Audio `json:"audio,omitempty"`
163-
HSM *HSM `json:"hsm,omitempty"`
164163
File *File `json:"file,omitempty"`
165164
Image *Image `json:"image,omitempty"`
166165
Location *Location `json:"location,omitempty"`
167166
Video *Video `json:"video,omitempty"`
168167
Text string `json:"text,omitempty"`
168+
169+
// HSM is a highly structured message for WhatsApp. Its definition lives in
170+
// hsm.go.
171+
HSM *HSM `json:"hsm,omitempty"`
169172
}
170173

171174
type Media struct {
@@ -177,60 +180,6 @@ type File Media
177180
type Image Media
178181
type Video Media
179182

180-
// HSM is a pre-approved, reusable message template required when messaging
181-
// over WhatsApp. It allows you to just send the required parameter values
182-
// instead of the full message. It also allows for localization of the message
183-
// and decreases the possibility of being blocked on the first contact as the
184-
// message is pre-approved by WhatsApp.
185-
type HSM struct {
186-
Namespace string `json:"namespace"`
187-
TemplateName string `json:"templateName"`
188-
Language *HSMLanguage `json:"language"`
189-
LocalizableParameters []*HSMLocalizableParameter `json:"params"`
190-
}
191-
192-
// HSMLanguage is used to set the message's locale.
193-
type HSMLanguage struct {
194-
Policy HSMLanguagePolicy `json:"policy"`
195-
196-
// Code can be both language and language_locale formats (e.g. en and
197-
// en_US).
198-
Code string `json:"code"`
199-
}
200-
201-
// HSMLanguagePolicy sets how the provided language is enforced.
202-
type HSMLanguagePolicy string
203-
204-
const (
205-
// HSMLanguagePolicyFallback will deliver the message template in the
206-
// user's device language. If the settings can't be found on the user's
207-
// device the fallback language is used.
208-
HSMLanguagePolicyFallback HSMLanguagePolicy = "fallback"
209-
210-
// HSMLanguagePolicyDeterministic will deliver the message template
211-
// exactly in the language and locale asked for.
212-
HSMLanguagePolicyDeterministic HSMLanguagePolicy = "deterministic"
213-
)
214-
215-
// HSMLocalizableParameter are used to replace the placeholders in the message
216-
// template. They will be localized by WhatsApp. Default values are used when
217-
// localization fails. Default is required. Additionally, currency OR DateTime
218-
// may be present in a request.
219-
type HSMLocalizableParameter struct {
220-
Default string `json:"default"`
221-
Currency *HSMLocalizableParameterCurrency `json:"currency,omitempty"`
222-
DateTime *time.Time `json:"dateTime,omitempty"`
223-
}
224-
225-
type HSMLocalizableParameterCurrency struct {
226-
// Code is the currency code in ISO 4217 format.
227-
Code string `json:"currencyCode"`
228-
229-
// Amount is the total amount, including cents, multiplied by 1000. E.g.
230-
// 12.34 become 12340.
231-
Amount int64 `json:"amount"`
232-
}
233-
234183
type Location struct {
235184
Latitude float32 `json:"latitude"`
236185
Longitude float32 `json:"longitude"`

conversation/doc.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44
// contains the structs returned by the API, and the other files provide the
55
// functionality needed to send requests, as well as any structs required for
66
// that - e.g. request data or pagination options.
7-
//
8-
// @todo Write docs, mostly on HSM.
9-
// @todo Consider moving struct defs from api.go to separate files - or
10-
// reorder them in a more logical way.
117
package conversation

conversation/hsm.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package conversation
2+
3+
import "time"
4+
5+
// HSM is a pre-approved, reusable message template required when messaging
6+
// over WhatsApp. It allows you to just send the required parameter values
7+
// instead of the full message. It also allows for localization of the message
8+
// and decreases the possibility of being blocked on the first contact as the
9+
// message is pre-approved by WhatsApp.
10+
type HSM struct {
11+
Namespace string `json:"namespace"`
12+
TemplateName string `json:"templateName"`
13+
Language *HSMLanguage `json:"language"`
14+
LocalizableParameters []*HSMLocalizableParameter `json:"params"`
15+
}
16+
17+
// HSMLanguage is used to set the message's locale.
18+
type HSMLanguage struct {
19+
Policy HSMLanguagePolicy `json:"policy"`
20+
21+
// Code can be both language and language_locale formats (e.g. en and
22+
// en_US).
23+
Code string `json:"code"`
24+
}
25+
26+
// HSMLanguagePolicy sets how the provided language is enforced.
27+
type HSMLanguagePolicy string
28+
29+
const (
30+
// HSMLanguagePolicyFallback will deliver the message template in the
31+
// user's device language. If the settings can't be found on the user's
32+
// device the fallback language is used.
33+
HSMLanguagePolicyFallback HSMLanguagePolicy = "fallback"
34+
35+
// HSMLanguagePolicyDeterministic will deliver the message template
36+
// exactly in the language and locale asked for.
37+
HSMLanguagePolicyDeterministic HSMLanguagePolicy = "deterministic"
38+
)
39+
40+
// HSMLocalizableParameter are used to replace the placeholders in the message
41+
// template. They will be localized by WhatsApp. Default values are used when
42+
// localization fails. Default is required. Additionally, currency OR DateTime
43+
// may be present in a request.
44+
type HSMLocalizableParameter struct {
45+
Default string `json:"default"`
46+
Currency *HSMLocalizableParameterCurrency `json:"currency,omitempty"`
47+
DateTime *time.Time `json:"dateTime,omitempty"`
48+
}
49+
50+
type HSMLocalizableParameterCurrency struct {
51+
// Code is the currency code in ISO 4217 format.
52+
Code string `json:"currencyCode"`
53+
54+
// Amount is the total amount, including cents, multiplied by 1000. E.g.
55+
// 12.34 become 12340.
56+
Amount int64 `json:"amount"`
57+
}
58+
59+
// DefaultLocalizableHSMParameter gets a simple parameter with a default value
60+
// that will do a simple string replacement.
61+
func DefaultLocalizableHSMParameter(d string) *HSMLocalizableParameter {
62+
return &HSMLocalizableParameter{
63+
Default: d,
64+
}
65+
}
66+
67+
// CurrencyLocalizableHSMParameter gets a parameter that localizes a currency.
68+
// Code is the currency code in ISO 4217 format and amount is the total amount,
69+
// including cents, multiplied by 1000. E.g. 12.34 becomes 12340.
70+
func CurrencyLocalizableHSMParameter(d string, code string, amount int64) *HSMLocalizableParameter {
71+
return &HSMLocalizableParameter{
72+
Default: d,
73+
Currency: &HSMLocalizableParameterCurrency{
74+
Code: code,
75+
Amount: amount,
76+
},
77+
}
78+
}
79+
80+
// DateTimeLocalizableHSMParameter gets a parameter that localizes a DateTime.
81+
func DateTimeLocalizableHSMParameter(d string, dateTime time.Time) *HSMLocalizableParameter {
82+
return &HSMLocalizableParameter{
83+
Default: d,
84+
DateTime: &dateTime,
85+
}
86+
}

conversation/hsm_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package conversation
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestLocalizableParameter(t *testing.T) {
10+
now := time.Now()
11+
12+
tt := []struct {
13+
name string
14+
got *HSMLocalizableParameter
15+
expect *HSMLocalizableParameter
16+
}{
17+
{
18+
name: "default",
19+
got: DefaultLocalizableHSMParameter("foo"),
20+
expect: &HSMLocalizableParameter{
21+
Default: "foo",
22+
},
23+
},
24+
{
25+
name: "currency",
26+
got: CurrencyLocalizableHSMParameter("EUR 12.34", "EUR", int64(12340)),
27+
expect: &HSMLocalizableParameter{
28+
Default: "EUR 12.34",
29+
Currency: &HSMLocalizableParameterCurrency{
30+
Code: "EUR",
31+
Amount: int64(12340),
32+
},
33+
},
34+
},
35+
{
36+
name: "date time",
37+
got: DateTimeLocalizableHSMParameter("baz", now),
38+
expect: &HSMLocalizableParameter{
39+
Default: "baz",
40+
DateTime: &now,
41+
},
42+
},
43+
}
44+
45+
for _, tc := range tt {
46+
t.Run(tc.name, func(t *testing.T) {
47+
if !reflect.DeepEqual(tc.got, tc.expect) {
48+
t.Fatalf("got %v, expected %v", tc.got, tc.expect)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)