Skip to content

Commit 5b1d4d7

Browse files
committed
More cleanup
1 parent a96e2ef commit 5b1d4d7

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

services/ivr/twiml/service.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func twCalculateSignature(url string, form url.Values, authToken string) ([]byte
452452

453453
// TWIML building utilities
454454

455-
func ResponseForSprint(cfg *runtime.Config, number urns.URN, resumeURL string, es []flows.Event, indent bool) (string, error) {
455+
func ResponseForSprint(cfg *runtime.Config, urn urns.URN, resumeURL string, es []flows.Event, indent bool) (string, error) {
456456
r := &Response{}
457457
commands := make([]interface{}, 0)
458458
hasWait := false
@@ -461,14 +461,16 @@ func ResponseForSprint(cfg *runtime.Config, number urns.URN, resumeURL string, e
461461
switch event := e.(type) {
462462
case *events.IVRCreatedEvent:
463463
if len(event.Msg.Attachments()) == 0 {
464-
country := envs.DeriveCountryFromTel(number.Path())
465-
locale := envs.NewLocale(event.Msg.TextLanguage, country)
466-
languageCode := locale.ToBCP47()
464+
urnCountry := envs.DeriveCountryFromTel(urn.Path())
465+
msgLocale := envs.NewLocale(event.Msg.TextLanguage, urnCountry)
467466

468-
if _, valid := supportedSayLanguages[languageCode]; !valid {
469-
languageCode = ""
467+
// only send locale if it's a supported say language for Twilio
468+
msgLocaleCode := msgLocale.ToBCP47()
469+
if _, valid := supportedSayLanguages[msgLocaleCode]; !valid {
470+
msgLocaleCode = ""
470471
}
471-
commands = append(commands, Say{Text: event.Msg.Text(), Language: languageCode})
472+
473+
commands = append(commands, &Say{Text: event.Msg.Text(), Language: msgLocaleCode})
472474
} else {
473475
for _, a := range event.Msg.Attachments() {
474476
a = models.NormalizeAttachment(cfg, a)

services/ivr/twiml/service_test.go

+31-28
Original file line numberDiff line numberDiff line change
@@ -39,68 +39,71 @@ func TestResponseForSprint(t *testing.T) {
3939
expected string
4040
}{
4141
{
42-
[]flows.Event{
43-
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "", "")),
44-
},
45-
`<Response><Say>hello world</Say><Hangup></Hangup></Response>`,
46-
},
47-
{
48-
[]flows.Event{
49-
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "eng", "")),
42+
// ivr msg, no text language specified
43+
events: []flows.Event{
44+
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "Hi there", "", "")),
5045
},
51-
`<Response><Say language="en-US">hello world</Say><Hangup></Hangup></Response>`,
46+
expected: `<Response><Say>Hi there</Say><Hangup></Hangup></Response>`,
5247
},
5348
{
54-
[]flows.Event{
55-
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "ben", "")),
49+
// ivr msg, supported text language specified
50+
events: []flows.Event{
51+
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "Hi there", "eng", "")),
5652
},
57-
`<Response><Say>hello world</Say><Hangup></Hangup></Response>`,
53+
expected: `<Response><Say language="en-US">Hi there</Say><Hangup></Hangup></Response>`,
5854
},
5955
{
60-
[]flows.Event{
61-
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "eng", "/recordings/foo.wav")),
56+
// ivr msg, unsupported text language specified
57+
events: []flows.Event{
58+
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "Amakuru", "kin", "")),
6259
},
63-
`<Response><Play>https://mailroom.io/recordings/foo.wav</Play><Hangup></Hangup></Response>`,
60+
expected: `<Response><Say>Amakuru</Say><Hangup></Hangup></Response>`,
6461
},
6562
{
66-
[]flows.Event{
67-
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "", "https://temba.io/recordings/foo.wav")),
63+
// ivr msg with audio attachment, text language ignored
64+
events: []flows.Event{
65+
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "Hi there", "eng", "/recordings/foo.wav")),
6866
},
69-
`<Response><Play>https://temba.io/recordings/foo.wav</Play><Hangup></Hangup></Response>`,
67+
expected: `<Response><Play>https://mailroom.io/recordings/foo.wav</Play><Hangup></Hangup></Response>`,
7068
},
7169
{
72-
[]flows.Event{
70+
// 2 ivr msgs
71+
events: []flows.Event{
7372
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "hello world", "", "")),
7473
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "goodbye", "", "")),
7574
},
76-
`<Response><Say>hello world</Say><Say>goodbye</Say><Hangup></Hangup></Response>`,
75+
expected: `<Response><Say>hello world</Say><Say>goodbye</Say><Hangup></Hangup></Response>`,
7776
},
7877
{
79-
[]flows.Event{
78+
// ivr msg followed by wait for digits
79+
events: []flows.Event{
8080
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "enter a number", "", "")),
8181
events.NewMsgWait(nil, nil, hints.NewFixedDigitsHint(1)),
8282
},
83-
`<Response><Gather numDigits="1" timeout="30" action="http://temba.io/resume?session=1&amp;wait_type=gather"><Say>enter a number</Say></Gather><Redirect>http://temba.io/resume?session=1&amp;wait_type=gather&amp;timeout=true</Redirect></Response>`,
83+
expected: `<Response><Gather numDigits="1" timeout="30" action="http://temba.io/resume?session=1&amp;wait_type=gather"><Say>enter a number</Say></Gather><Redirect>http://temba.io/resume?session=1&amp;wait_type=gather&amp;timeout=true</Redirect></Response>`,
8484
},
8585
{
86-
[]flows.Event{
86+
// ivr msg followed by wait for terminated digits
87+
events: []flows.Event{
8788
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "enter a number, then press #", "", "")),
8889
events.NewMsgWait(nil, nil, hints.NewTerminatedDigitsHint("#")),
8990
},
90-
`<Response><Gather finishOnKey="#" timeout="30" action="http://temba.io/resume?session=1&amp;wait_type=gather"><Say>enter a number, then press #</Say></Gather><Redirect>http://temba.io/resume?session=1&amp;wait_type=gather&amp;timeout=true</Redirect></Response>`,
91+
expected: `<Response><Gather finishOnKey="#" timeout="30" action="http://temba.io/resume?session=1&amp;wait_type=gather"><Say>enter a number, then press #</Say></Gather><Redirect>http://temba.io/resume?session=1&amp;wait_type=gather&amp;timeout=true</Redirect></Response>`,
9192
},
9293
{
93-
[]flows.Event{
94+
// ivr msg followed by wait for recording
95+
events: []flows.Event{
9496
events.NewIVRCreated(flows.NewIVRMsgOut(urn, channelRef, "say something", "", "")),
9597
events.NewMsgWait(nil, nil, hints.NewAudioHint()),
9698
},
97-
`<Response><Say>say something</Say><Record action="http://temba.io/resume?session=1&amp;wait_type=record" maxLength="600"></Record><Redirect>http://temba.io/resume?session=1&amp;wait_type=record&amp;empty=true</Redirect></Response>`,
99+
expected: `<Response><Say>say something</Say><Record action="http://temba.io/resume?session=1&amp;wait_type=record" maxLength="600"></Record><Redirect>http://temba.io/resume?session=1&amp;wait_type=record&amp;empty=true</Redirect></Response>`,
98100
},
99101
{
100-
[]flows.Event{
102+
// dial wait
103+
events: []flows.Event{
101104
events.NewDialWait(urns.URN(`tel:+1234567890`), 60, 7200, &expiresOn),
102105
},
103-
`<Response><Dial action="http://temba.io/resume?session=1&amp;wait_type=dial" timeout="60" timeLimit="7200">+1234567890</Dial></Response>`,
106+
expected: `<Response><Dial action="http://temba.io/resume?session=1&amp;wait_type=dial" timeout="60" timeLimit="7200">+1234567890</Dial></Response>`,
104107
},
105108
}
106109

0 commit comments

Comments
 (0)