File size: 4,927 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
package twiml
import (
"encoding/xml"
"fmt"
"strconv"
"time"
)
// Gather is a TwiML verb that gathers pressed digits from the caller.
type Gather struct {
XMLName xml.Name `xml:"Gather"`
// Action defaults to the current request URL and can be relative or absolute.
Action string `xml:"action,attr,omitempty"`
// FinishOnKey defaults to "#".
FinishOnKey string `xml:"finishOnKey,attr,omitempty"`
Hints string `xml:"hints,attr,omitempty"`
// Input defaults to dtmf.
Input string `xml:"input,attr,omitempty"`
// Language defaults to en-US.
Language string `xml:"language,attr,omitempty"`
// Method defaults to POST.
Method string `xml:"method,attr,omitempty"`
// NumDigitsCount is the normalized version of `numDigits`. A zero value indicates no limit and is the default.
NumDigitsCount int `xml:"-"`
// PartialResultCallback defaults to the current request URL and can be relative or absolute.
//
// https://www.twilio.com/docs/voice/twiml/gather#partialresultcallback
PartialResultCallback string `xml:"partialResultCallback,attr,omitempty"`
PartialResultCallbackMethod string `xml:"partialResultCallbackMethod,attr,omitempty"`
// DisableProfanityFilter disables the profanity filter.
DisableProfanityFilter bool `xml:"-"`
// TimeoutDur defaults to 5 seconds.
TimeoutDur time.Duration `xml:"-"`
// SpeechTimeoutDur is the speechTimeout attribute.
//
// It should be ignored if SpeechTimeoutAuto is true. Defaults to TimeoutDur.
SpeechTimeoutDur time.Duration `xml:"-"`
// SpeechTimeoutAuto will be true if the `speechTimeout` value is set to true.
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)
}
|