|
package twiml |
|
|
|
import ( |
|
"encoding/xml" |
|
"time" |
|
) |
|
|
|
|
|
type Hangup struct{} |
|
|
|
|
|
|
|
|
|
|
|
type Pause struct { |
|
|
|
|
|
|
|
Dur time.Duration `xml:"-"` |
|
} |
|
|
|
func (p *Pause) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
|
type PauseRaw Pause |
|
var pp struct { |
|
PauseRaw |
|
Length NullInt `xml:"length,attr"` |
|
} |
|
if err := d.DecodeElement(&pp, &start); err != nil { |
|
return err |
|
} |
|
*p = Pause(pp.PauseRaw) |
|
if pp.Length.Valid { |
|
p.Dur = time.Duration(pp.Length.Value) * time.Second |
|
} else { |
|
p.Dur = time.Second |
|
} |
|
return nil |
|
} |
|
|
|
func (p Pause) MarshalXML(e *xml.Encoder, start xml.StartElement) error { |
|
type PauseRaw Pause |
|
var pp struct { |
|
PauseRaw |
|
Length *int `xml:"length,attr,omitempty"` |
|
} |
|
sec := int(p.Dur / time.Second) |
|
pp.Length = &sec |
|
pp.PauseRaw = PauseRaw(p) |
|
return e.EncodeElement(pp, start) |
|
} |
|
|
|
|
|
type Redirect struct { |
|
|
|
|
|
|
|
Method string `xml:"method,attr,omitempty"` |
|
|
|
|
|
URL string `xml:",chardata"` |
|
} |
|
|
|
func (r *Redirect) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
|
type RedirectRaw Redirect |
|
var rr struct { |
|
RedirectRaw |
|
} |
|
if err := d.DecodeElement(&rr, &start); err != nil { |
|
return err |
|
} |
|
*r = Redirect(rr.RedirectRaw) |
|
if r.Method == "" { |
|
r.Method = "POST" |
|
} |
|
return nil |
|
} |
|
|
|
|
|
type Reject struct { |
|
|
|
|
|
|
|
Reason string `xml:"reason,attr,omitempty"` |
|
} |
|
|
|
type RejectReason int |
|
|
|
const ( |
|
RejectReasonRejected RejectReason = iota |
|
RejectReasonBusy |
|
) |
|
|
|
func (r *Reject) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
|
if err := d.DecodeElement(&r, &start); err != nil { |
|
return err |
|
} |
|
if r.Reason == "" { |
|
r.Reason = "rejected" |
|
} |
|
|
|
return nil |
|
} |
|
|
|
|
|
type Say struct { |
|
Content string `xml:",innerxml"` |
|
Language string `xml:"language,attr,omitempty"` |
|
Voice string `xml:"voice,attr,omitempty"` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LoopCount int `xml:"-"` |
|
} |
|
|
|
var ( |
|
_ xml.Unmarshaler = (*Say)(nil) |
|
_ xml.Marshaler = Say{} |
|
) |
|
|
|
func (s *Say) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
|
type SayRaw Say |
|
var ss struct { |
|
SayRaw |
|
Loop NullInt `xml:"loop,attr"` |
|
} |
|
if err := d.DecodeElement(&ss, &start); err != nil { |
|
return err |
|
} |
|
*s = Say(ss.SayRaw) |
|
if ss.Loop.Valid { |
|
s.LoopCount = ss.Loop.Value |
|
if s.LoopCount == 0 { |
|
s.LoopCount = 1000 |
|
} |
|
} |
|
return nil |
|
} |
|
|
|
func (s Say) MarshalXML(e *xml.Encoder, start xml.StartElement) error { |
|
type SayRaw Say |
|
var ss struct { |
|
SayRaw |
|
Content string `xml:",innerxml"` |
|
Loop *int `xml:"loop,attr"` |
|
} |
|
ss.Content = s.Content |
|
if s.LoopCount != 0 { |
|
ss.Loop = &s.LoopCount |
|
} |
|
ss.SayRaw = SayRaw(s) |
|
return e.EncodeElement(ss, start) |
|
} |
|
|