twiml

package
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package twiml provides a type-safe way of building and parsing the TwiML markup language in Go.

TwiML is an XML-based language used by Twilio to control phone calls and SMS messages. The package provides Go structs that can be used to generate TwiML documents or parse incoming TwiML messages.

The `Response` struct is the root element of a TwiML document. It can contain a sequence of `Verb`s, such as `Say`, `Gather`, `Pause`, `Redirect`, or `Hangup`. Each `Verb` is represented by a Go struct that contains the corresponding TwiML attributes and child elements.

Example usage:

r := &twiml.Response{
    Verbs: []twiml.Verb{
        &twiml.Say{
            Content: "Hello, world!",
        },
        &twiml.Gather{
            Action: "/process_gather",
            NumDigitsCount: 1,
            Verbs: []twiml.GatherVerb{
                &twiml.Say{
                    Content: "Press 1 to confirm, or any other key to try again.",
                },
            },
        },
    },
}
b, _ := xml.MarshalIndent(r, "", "  ")
fmt.Println(string(b))

Output:

<Response>

<Say>Hello, world!</Say>
<Gather action="/process_gather">
  <Say>Press 1 to confirm, or any other key to try again.</Say>
</Gather>

</Response>

Incoming TwiML messages can be parsed using the `Interpreter` struct, which reads a sequence of bytes and returns the next `Verb` in the message until the end is reached. The `Interpreter` can be used to implement server-side TwiML applications that respond to user input.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gather

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:"-"`
}

Gather is a TwiML verb that gathers pressed digits from the caller.

Example
package main

import (
	"encoding/xml"
	"os"

	"github.com/target/goalert/devtools/mocktwilio/twiml"
)

func main() {
	var resp twiml.Response
	resp.Verbs = append(resp.Verbs, &twiml.Gather{
		Action:         "/gather",
		NumDigitsCount: 5,
		Verbs: []twiml.GatherVerb{
			&twiml.Say{
				Content: "Please enter your 5-digit zip code.",
			},
		},
	})

	_ = xml.NewEncoder(os.Stdout).Encode(resp)
}
Output:

<Response><Gather action="/gather" numDigits="5"><Say>Please enter your 5-digit zip code.</Say></Gather></Response>

func (Gather) MarshalXML

func (g Gather) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Gather) UnmarshalXML

func (g *Gather) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type GatherVerb

type GatherVerb interface {
	Verb
	// contains filtered or unexported methods
}

GatherVerb is any verb that can be nested inside a Gather.

type Hangup

type Hangup struct{}

Hangup is a verb that hangs up the call.

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator will iterate over the verbs in a TwiML document, in the order they should be processed.

A Say verb within a Gather verb will be processed before the Gather verb itself.

func NewIterator

func NewIterator() *Iterator

NewIterator returns a new Iterator.

func (*Iterator) Next

func (i *Iterator) Next() bool

Next advances the iterator to the next verb and returns true if there is another verb.

func (*Iterator) SetResponse

func (i *Iterator) SetResponse(data []byte) error

SetResponse sets the response to interpret, and resets the state.

This method will return an error if the response is not valid TwiML.

func (*Iterator) Verb

func (i *Iterator) Verb() Verb

Verb returns the current verb.

type NullInt

type NullInt struct {
	Valid bool
	Value int
}

NullInt is a nullable int that can be used to marshal/unmarshal XML attributes that are optional but have special meaning when they are present vs. when they are not.

func (*NullInt) MarshalXMLAttr

func (n *NullInt) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

func (*NullInt) UnmarshalXMLAttr

func (n *NullInt) UnmarshalXMLAttr(attr xml.Attr) error

type Pause

type Pause struct {
	// Dur derives from the `length` attribute and indicates the length of the pause.
	//
	// https://www.twilio.com/docs/voice/twiml/pause#attributes-length
	Dur time.Duration `xml:"-"`
}

The Pause verb waits silently for a specific number of seconds.

If Pause is the first verb in a TwiML document, Twilio will wait the specified number of seconds before picking up the call. https://www.twilio.com/docs/voice/twiml/pause

func (Pause) MarshalXML

func (p Pause) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Pause) UnmarshalXML

func (p *Pause) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Redirect

type Redirect struct {
	// Method defaults to POST.
	//
	// https://www.twilio.com/docs/voice/twiml/redirect#attributes-method
	Method string `xml:"method,attr,omitempty"`

	// URL is the URL to redirect to.
	URL string `xml:",chardata"`
}

The Redirect verb redirects the current call to a new TwiML document.

func (*Redirect) UnmarshalXML

func (r *Redirect) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Reject

type Reject struct {
	// Reason defaults to 'rejected'.
	//
	// https://www.twilio.com/docs/voice/twiml/reject#attributes-reason
	Reason string `xml:"reason,attr,omitempty"`
}

The Reject verb rejects an incoming call.

func (*Reject) UnmarshalXML

func (r *Reject) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type RejectReason

type RejectReason int
const (
	RejectReasonRejected RejectReason = iota
	RejectReasonBusy
)

type Response

type Response struct {
	Verbs []Verb `xml:"-"`
}

func (Response) MarshalXML

func (r Response) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Response) UnmarshalXML

func (r *Response) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Say

type Say struct {
	Content  string `xml:",innerxml"`
	Language string `xml:"language,attr,omitempty"`
	Voice    string `xml:"voice,attr,omitempty"`

	// LoopCount is derived from the `loop` attribute, taking into account the default value
	// and the special value of 0.
	//
	// If LoopCount is below zero, the message is not played.
	//
	// https://www.twilio.com/docs/voice/twiml/say#attributes-loop
	LoopCount int `xml:"-"`
}

Say is a TwiML verb that plays back a message to the caller.

func (Say) MarshalXML

func (s Say) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Say) UnmarshalXML

func (s *Say) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Verb

type Verb interface {
	// contains filtered or unexported methods
}

Verb is any verb that can be nested inside a Response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL