File size: 5,005 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
package twilio
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"github.com/target/goalert/config"
)
type twiMLResponse struct {
say []string
voiceName string
voiceLanguage string
gatherURL string
redirectURL string
redirectPauseSec int
hangup bool
hasOptions bool
expectResponse bool
sent bool
w http.ResponseWriter
}
func newTwiMLResponse(ctx context.Context, w http.ResponseWriter) *twiMLResponse {
cfg := config.FromContext(ctx)
return &twiMLResponse{
voiceName: cfg.Twilio.VoiceName,
voiceLanguage: cfg.Twilio.VoiceLanguage,
w: w,
}
}
func (t *twiMLResponse) Redirect(url string) {
t.redirectURL = url
t.sendResponse()
}
func (t *twiMLResponse) RedirectPauseSec(url string, seconds int) {
t.redirectURL = url
t.redirectPauseSec = seconds
t.sendResponse()
}
type menuOption int
const (
optionUnknown menuOption = iota
optionCancel
optionConfirmStop
optionAck
optionEscalate
optionClose
optionAckAll
optionCloseAll
optionStop
optionRepeat
)
func (t *twiMLResponse) AddOptions(options ...menuOption) {
t.hasOptions = true
for _, opt := range options {
switch opt {
case optionConfirmStop:
t.expectResponse = true
t.Sayf("To confirm unenrollment of this number, press %s.", digitConfirm)
case optionCancel:
t.expectResponse = true
t.Sayf("To go back to the previous menu, press %s.", digitGoBack)
case optionStop:
t.Sayf("To disable voice notifications to this number, press %s.", digitStop)
case optionRepeat:
t.Sayf("To repeat this message, press %s.", sayRepeat)
case optionAck:
t.expectResponse = true
t.Sayf("To acknowledge, press %s.", digitAck)
case optionEscalate:
t.expectResponse = true
t.Sayf("To escalate, press %s.", digitEscalate)
case optionClose:
t.expectResponse = true
t.Sayf("To close, press %s.", digitClose)
case optionAckAll:
t.expectResponse = true
t.Sayf("To acknowledge all, press %s.", digitAck)
case optionCloseAll:
t.expectResponse = true
t.Sayf("To close all, press %s.", digitClose)
default:
panic("Unknown option")
}
}
}
func (t *twiMLResponse) Gather(url string) {
t.gatherURL = url
if !t.expectResponse {
t.Say("If you are done, you may simply hang up.")
}
t.AddOptions(optionRepeat)
t.sendResponse()
}
func (t *twiMLResponse) SayUnknownDigit() *twiMLResponse {
t.Say("Sorry, I didn't understand that.")
return t
}
func (t *twiMLResponse) Say(text string) *twiMLResponse {
t.say = append(t.say, text)
return t
}
func (t *twiMLResponse) Sayf(format string, args ...interface{}) *twiMLResponse {
return t.Say(fmt.Sprintf(format, args...))
}
func (t *twiMLResponse) Hangup() {
t.hangup = true
t.Say("Goodbye.")
t.sendResponse()
}
type verbSay struct {
XMLName xml.Name `xml:"Say"`
Language string `xml:"language,attr,omitempty"`
Voice string `xml:"voice,attr,omitempty"`
Text string `xml:"-"`
Prosody *prosody `xml:"prosody"`
}
type prosody struct {
XMLName xml.Name `xml:"prosody"`
Text string `xml:",chardata"`
Rate string `xml:"rate,attr"`
}
type twimlResponse struct {
XMLName xml.Name `xml:"Response"`
Verbs []any `xml:",any"`
}
type verbPause struct {
XMLName xml.Name `xml:"Pause"`
LengthSec int `xml:"length,attr"`
}
type verbRedirect struct {
XMLName xml.Name `xml:"Redirect"`
URL string `xml:",chardata"`
}
type verbHangup struct {
XMLName xml.Name `xml:"Hangup"`
}
type verbGather struct {
XMLName xml.Name `xml:"Gather"`
NumDigits int `xml:"numDigits,attr"`
TimeoutSec int `xml:"timeout,attr"`
Action string `xml:"action,attr"`
Verbs []any `xml:",any"`
}
func (t *twiMLResponse) sendResponse() {
if t.sent {
panic("Response already sent")
}
t.sent = true
if t.hasOptions && t.gatherURL == "" {
// if we give the user options, we need to allow them to respond
panic("Options without gather")
}
var doc twimlResponse
for _, text := range t.say {
doc.Verbs = append(
doc.Verbs,
verbSay{
Language: t.voiceLanguage,
Voice: t.voiceName,
Prosody: &prosody{
Rate: "slow",
Text: text,
},
},
)
}
if t.redirectPauseSec > 0 {
doc.Verbs = append(doc.Verbs, verbPause{LengthSec: t.redirectPauseSec})
}
if t.redirectURL != "" {
doc.Verbs = append(doc.Verbs, verbRedirect{URL: t.redirectURL})
}
if t.gatherURL != "" {
doc.Verbs = []any{verbGather{
Action: t.gatherURL,
TimeoutSec: 10,
NumDigits: 1,
Verbs: doc.Verbs,
}}
}
if t.hangup {
doc.Verbs = append(doc.Verbs, verbHangup{})
}
var buf bytes.Buffer
_, _ = io.WriteString(&buf, xml.Header)
enc := xml.NewEncoder(&buf)
enc.Indent("", "\t")
err := enc.Encode(doc)
if err != nil {
http.Error(t.w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
panic(err)
}
t.w.Header().Set("Content-Type", "application/xml; charset=utf-8")
_, _ = io.WriteString(t.w, buf.String())
}
|