|
package twiml |
|
|
|
import ( |
|
"encoding/xml" |
|
"fmt" |
|
"strconv" |
|
"time" |
|
) |
|
|
|
|
|
type Gather struct { |
|
XMLName xml.Name `xml:"Gather"` |
|
|
|
|
|
Action string `xml:"action,attr,omitempty"` |
|
|
|
|
|
FinishOnKey string `xml:"finishOnKey,attr,omitempty"` |
|
|
|
Hints string `xml:"hints,attr,omitempty"` |
|
|
|
|
|
Input string `xml:"input,attr,omitempty"` |
|
|
|
|
|
Language string `xml:"language,attr,omitempty"` |
|
|
|
|
|
Method string `xml:"method,attr,omitempty"` |
|
|
|
|
|
NumDigitsCount int `xml:"-"` |
|
|
|
|
|
|
|
|
|
PartialResultCallback string `xml:"partialResultCallback,attr,omitempty"` |
|
|
|
PartialResultCallbackMethod string `xml:"partialResultCallbackMethod,attr,omitempty"` |
|
|
|
|
|
DisableProfanityFilter bool `xml:"-"` |
|
|
|
|
|
TimeoutDur time.Duration `xml:"-"` |
|
|
|
|
|
|
|
|
|
SpeechTimeoutDur time.Duration `xml:"-"` |
|
|
|
|
|
SpeechTimeoutAuto bool `xml:"-"` |
|
|
|
SpeechModel string `xml:"speechModel,attr,omitempty"` |
|
|
|
Enhanced bool `xml:"enhanced,attr,omitempty"` |
|
|
|
ActionOnEmptyResult bool `xml:"actionOnEmptyResult,attr,omitempty"` |
|
|
|
Verbs []GatherVerb `xml:"-"` |
|
} |
|
|
|
func defStr(s *string, defaultValue string) { |
|
if *s == "" { |
|
*s = defaultValue |
|
} |
|
} |
|
|
|
func (g *Gather) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
|
type RawGather Gather |
|
var gg struct { |
|
RawGather |
|
NumDigits NullInt `xml:"numDigits,attr"` |
|
SpeechTimeout string `xml:"speechTimeout,attr"` |
|
Timeout NullInt `xml:"timeout,attr"` |
|
ProfanityFilter *bool `xml:"profanityFilter,attr"` |
|
|
|
Content []anyVerb `xml:",any"` |
|
} |
|
|
|
if err := d.DecodeElement(&gg, &start); err != nil { |
|
return err |
|
} |
|
*g = Gather(gg.RawGather) |
|
for _, v := range gg.Content { |
|
switch t := v.verb.(type) { |
|
case *Say: |
|
g.Verbs = append(g.Verbs, t) |
|
case *Pause: |
|
g.Verbs = append(g.Verbs, t) |
|
default: |
|
return fmt.Errorf("unexpected verb in Gather: %T", t) |
|
} |
|
} |
|
|
|
defStr(&g.FinishOnKey, "#") |
|
defStr(&g.Method, "POST") |
|
defStr(&g.Input, "dtmf") |
|
defStr(&g.Language, "en-US") |
|
defStr(&g.PartialResultCallbackMethod, "POST") |
|
defStr(&g.SpeechModel, "default") |
|
if gg.NumDigits.Valid { |
|
g.NumDigitsCount = gg.NumDigits.Value |
|
if g.NumDigitsCount < 1 { |
|
g.NumDigitsCount = 1 |
|
} |
|
} |
|
if gg.ProfanityFilter != nil { |
|
g.DisableProfanityFilter = !*gg.ProfanityFilter |
|
} |
|
if gg.Timeout.Valid { |
|
g.TimeoutDur = time.Duration(gg.Timeout.Value) * time.Second |
|
} else { |
|
g.TimeoutDur = 5 * time.Second |
|
} |
|
switch gg.SpeechTimeout { |
|
case "auto": |
|
g.SpeechTimeoutAuto = true |
|
case "": |
|
g.SpeechTimeoutDur = g.TimeoutDur |
|
default: |
|
v, err := strconv.Atoi(gg.SpeechTimeout) |
|
if err != nil { |
|
return err |
|
} |
|
g.SpeechTimeoutDur = time.Duration(v) * time.Second |
|
} |
|
|
|
return nil |
|
} |
|
|
|
func (g Gather) MarshalXML(e *xml.Encoder, start xml.StartElement) error { |
|
type RawGather Gather |
|
var gg struct { |
|
RawGather |
|
NumDigits int `xml:"numDigits,attr,omitempty"` |
|
SpeechTimeout string `xml:"speechTimeout,attr,omitempty"` |
|
Timeout int `xml:"timeout,attr,omitempty"` |
|
ProfanityFilter *bool `xml:"profanityFilter,attr,omitempty"` |
|
Verbs []any `xml:",any"` |
|
} |
|
gg.RawGather = RawGather(g) |
|
for _, v := range g.Verbs { |
|
switch t := v.(type) { |
|
case *Say: |
|
gg.Verbs = append(gg.Verbs, anyVerb{verb: t}) |
|
case *Pause: |
|
gg.Verbs = append(gg.Verbs, anyVerb{verb: t}) |
|
default: |
|
return fmt.Errorf("unexpected verb in Gather: %T", v) |
|
} |
|
} |
|
if g.NumDigitsCount > 1 { |
|
gg.NumDigits = g.NumDigitsCount |
|
} |
|
if g.SpeechTimeoutAuto { |
|
gg.SpeechTimeout = "auto" |
|
} else if g.SpeechTimeoutDur != gg.TimeoutDur { |
|
gg.SpeechTimeout = strconv.Itoa(int(g.SpeechTimeoutDur / time.Second)) |
|
} |
|
if gg.Input == "dtmf" { |
|
gg.Input = "" |
|
} |
|
if gg.FinishOnKey == "#" { |
|
gg.FinishOnKey = "" |
|
} |
|
if gg.Language == "en-US" { |
|
gg.Language = "" |
|
} |
|
if gg.PartialResultCallback == "" || gg.PartialResultCallbackMethod == "POST" { |
|
gg.PartialResultCallbackMethod = "" |
|
} |
|
if gg.Method == "POST" { |
|
gg.Method = "" |
|
} |
|
if g.DisableProfanityFilter { |
|
gg.ProfanityFilter = new(bool) |
|
*gg.ProfanityFilter = false |
|
} |
|
if gg.SpeechModel == "default" { |
|
gg.SpeechModel = "" |
|
} |
|
gg.Timeout = int(g.TimeoutDur / time.Second) |
|
return e.EncodeElement(gg, start) |
|
} |
|
|