twilio

package
v0.30.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CallStatusUnknown    = CallStatus("")
	CallStatusInitiated  = CallStatus("initiated")
	CallStatusQueued     = CallStatus("queued")
	CallStatusRinging    = CallStatus("ringing")
	CallStatusInProgress = CallStatus("in-progress")
	CallStatusCompleted  = CallStatus("completed")
	CallStatusBusy       = CallStatus("busy")
	CallStatusFailed     = CallStatus("failed")
	CallStatusNoAnswer   = CallStatus("no-answer")
	CallStatusCanceled   = CallStatus("canceled")
)

Defined status values for voice calls.

View Source
const (
	MessageStatusUnknown     = MessageStatus("")
	MessageStatusAccepted    = MessageStatus("accepted")
	MessageStatusQueued      = MessageStatus("queued")
	MessageStatusSending     = MessageStatus("sending")
	MessageStatusSent        = MessageStatus("sent")
	MessageStatusReceiving   = MessageStatus("receiving")
	MessageStatusReceived    = MessageStatus("received")
	MessageStatusDelivered   = MessageStatus("delivered")
	MessageStatusUndelivered = MessageStatus("undelivered")
	MessageStatusFailed      = MessageStatus("failed")
)

Defined status values for messages.

View Source
const (
	MessageErrorCodeQueueOverflow       = MessageErrorCode(30001)
	MessageErrorCodeAccountSuspended    = MessageErrorCode(30002)
	MessageErrorCodeHandsetUnreachable  = MessageErrorCode(30003)
	MessageErrorCodeMessageBlocked      = MessageErrorCode(30004)
	MessageErrorCodeHandsetUnknown      = MessageErrorCode(30005)
	MessageErrorCodeLandlineUnreachable = MessageErrorCode(30006)
	MessageErrorCodeCarrierViolation    = MessageErrorCode(30007)
	MessageErrorCodeUnknown             = MessageErrorCode(30008)
	MessageErrorCodeMissingSegment      = MessageErrorCode(30009)
	MessageErrorCodeExceedsMaxPrice     = MessageErrorCode(30010)
)

Defined error codes for messages.

View Source
const (
	CallTypeAlert       = CallType("alert")
	CallTypeAlertStatus = CallType("alert-status")
	CallTypeTest        = CallType("test")
	CallTypeVerify      = CallType("verify")
	CallTypeStop        = CallType("stop")
)

Supported call types.

View Source
const DefaultLookupURL = "https://lookups.twilio.com"

DefaultLookupURL is the value that will be used for lookup calls if Config.BaseURL is empty.

View Source
const DefaultTwilioAPIURL = "https://api.twilio.com/2010-04-01"

DefaultTwilioAPIURL is the value that will be used for API calls if Config.BaseURL is empty.

Variables

View Source
var ErrCarrierStale = errors.New("carrier data is stale")

ErrCarrierStale is returned if the available carrier data is over a year old.

View Source
var ErrCarrierUnavailable = errors.New("carrier data is unavailable")

ErrCarrierUnavailable is returned if the carrier data is missing and fetch is disabled.

Functions

func Signature

func Signature(authToken, url string, fields url.Values) []byte

Signature will calculate the raw signature for a request from Twilio. https://www.twilio.com/docs/api/security#validating-requests

func WrapHeaderHack

func WrapHeaderHack(h http.Handler) http.Handler

WrapHeaderHack wraps an http.Handler so that a 204 is returned if the body is empty.

A Go 1.10 change removed the implicit header for responses with no content. Unfortunately Twilio logs empty responses (with no `Content-Type`) as 502s.

func WrapValidation

func WrapValidation(h http.Handler, c Config) http.Handler

WrapValidation will wrap an http.Handler to do X-Twilio-Signature checking.

Types

type Call

type Call struct {
	SID            string
	To             string
	From           string
	Status         CallStatus
	SequenceNumber *int
	Direction      string
	CallDuration   time.Duration
	ErrorMessage   *string
	ErrorCode      *CallErrorCode
}

Call represents a Twilio voice call.

type CallErrorCode

type CallErrorCode int

CallErrorCode is an error code encountered when making a call.

type CallStatus

type CallStatus string

CallStatus indicates the state of a voice call.

https://www.twilio.com/docs/api/twiml/twilio_request#request-parameters-call-status

func (*CallStatus) Scan

func (s *CallStatus) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

type CallType

type CallType string

CallType indicates a supported Twilio voice call type.

type CarrierInfo added in v0.25.0

type CarrierInfo struct {
	Name              string `json:"name"`
	Type              string `json:"type"`
	MobileNetworkCode string `json:"mobile_network_code"`
	MobileCountryCode string `json:"mobile_country_code"`
}

CarrierInfo holds information about the carrier network for a particular number.

type Config

type Config struct {

	// BaseURL can be used to override the Twilio API and Lookup URL bases.
	BaseURL string

	// Client is an optional net/http client to use, if nil the global default is used.
	Client *http.Client

	// CMStore is used for storing and fetching metadata (like carrier information).
	CMStore *contactmethod.Store
}

Config contains the details needed to interact with Twilio for SMS

func (*Config) CarrierInfo added in v0.25.0

func (c *Config) CarrierInfo(ctx context.Context, number string, fetch bool) (*CarrierInfo, error)

CarrierInfo will return carrier information for the provided number. If fetch is true, it will fetch data from the Twilio API if it is not available from the DB.

func (Config) FetchCarrierInfo added in v0.25.0

func (c Config) FetchCarrierInfo(ctx context.Context, number string) (*CarrierInfo, error)

FetchCarrierInfo will lookup carrier information for the provided number using the Twilio API.

func (*Config) GetSMS

func (c *Config) GetSMS(ctx context.Context, sid string) (*Message, error)

GetSMS will return the current state of a Message from Twilio.

func (*Config) GetVoice

func (c *Config) GetVoice(ctx context.Context, sid string) (*Call, error)

GetVoice will return the current state of a voice call from Twilio.

func (*Config) SendSMS

func (c *Config) SendSMS(ctx context.Context, to, body string, o *SMSOptions) (*Message, error)

SendSMS will send an SMS using Twilio.

func (*Config) StartVoice

func (c *Config) StartVoice(ctx context.Context, to string, o *VoiceOptions) (*Call, error)

StartVoice will initiate a voice call to the given number.

type Exception

type Exception struct {
	Status   int
	Message  string
	Code     int
	MoreInfo string `json:"more_info"`
}

Exception contains information on a Twilio error.

func (Exception) Error

func (e Exception) Error() string

type KeyPressed

type KeyPressed string

KeyPressed specifies a key pressed from the voice menu options.

type Message

type Message struct {
	SID          string
	To           string
	From         string
	Status       MessageStatus
	ErrorCode    *MessageErrorCode
	ErrorMessage *string

	MessagingServiceSID string `json:"messaging_service_sid"`
}

Message represents a Twilio message.

type MessageErrorCode

type MessageErrorCode int

A MessageErrorCode is a defined error code for Twilio messages.

https://www.twilio.com/docs/api/messaging/message#delivery-related-errors

type MessageStatus

type MessageStatus string

MessageStatus indicates the state of a message.

https://www.twilio.com/docs/api/messaging/message#message-status-values

func (*MessageStatus) Scan

func (s *MessageStatus) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

type SMS

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

SMS implements a notification.Sender for Twilio SMS.

func NewSMS

func NewSMS(ctx context.Context, db *sql.DB, c *Config) (*SMS, error)

NewSMS performs operations like validating essential parameters, registering the Twilio client and db and adding routes for successful and unsuccessful message delivery to Twilio

func (*SMS) FriendlyValue added in v0.28.0

func (s *SMS) FriendlyValue(ctx context.Context, value string) (string, error)

FriendlyValue will return the international formatting of the phone number.

func (*SMS) Send

Send implements the notification.Sender interface.

func (*SMS) ServeMessage

func (s *SMS) ServeMessage(w http.ResponseWriter, req *http.Request)

func (*SMS) ServeStatusCallback

func (s *SMS) ServeStatusCallback(w http.ResponseWriter, req *http.Request)

func (*SMS) SetReceiver added in v0.27.0

func (s *SMS) SetReceiver(r notification.Receiver)

SetReceiver sets the notification.Receiver for incoming messages and status updates.

func (*SMS) Status

func (s *SMS) Status(ctx context.Context, externalID string) (*notification.Status, error)

Status provides the current status of a message.

type SMSOptions

type SMSOptions struct {
	// ValidityPeriod controls how long a message will still be valid in Twilio's queue.
	ValidityPeriod time.Duration

	// CallbackParams will be added to callback URLs
	CallbackParams url.Values

	// FromNumber allows overriding the specified FromNumber instead of using the context config.
	FromNumber string
}

SMSOptions allows configuring outgoing SMS messages.

func (*SMSOptions) StatusCallbackURL

func (sms *SMSOptions) StatusCallbackURL(cfg config.Config) (string, error)

StatusCallbackURL will return the status callback url for the given configuration.

type Voice

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

Voice implements a notification.Sender for Twilio voice calls.

func NewVoice

func NewVoice(ctx context.Context, db *sql.DB, c *Config) (*Voice, error)

NewVoice will send out the initial Call to Twilio, specifying all details needed for Twilio to make the first call to the end user It performs operations like validating essential parameters, registering the Twilio client and db and adding routes for successful and unsuccessful call connections to Twilio

func (*Voice) FriendlyValue added in v0.28.0

func (v *Voice) FriendlyValue(ctx context.Context, value string) (string, error)

FriendlyValue will return the international formatting of the phone number.

func (*Voice) Send

Send implements the notification.Sender interface.

func (*Voice) ServeAlert

func (v *Voice) ServeAlert(w http.ResponseWriter, req *http.Request)

ServeAlert serves a call for an alert notification.

func (*Voice) ServeAlertStatus

func (v *Voice) ServeAlertStatus(w http.ResponseWriter, req *http.Request)

func (*Voice) ServeCall

func (v *Voice) ServeCall(w http.ResponseWriter, req *http.Request)

func (*Voice) ServeInbound added in v0.29.0

func (v *Voice) ServeInbound(w http.ResponseWriter, req *http.Request)

ServeInbound is the handler for inbound calls.

func (*Voice) ServeStatusCallback

func (v *Voice) ServeStatusCallback(w http.ResponseWriter, req *http.Request)

func (*Voice) ServeStop

func (v *Voice) ServeStop(w http.ResponseWriter, req *http.Request)

func (*Voice) ServeTest

func (v *Voice) ServeTest(w http.ResponseWriter, req *http.Request)

func (*Voice) ServeVerify

func (v *Voice) ServeVerify(w http.ResponseWriter, req *http.Request)

func (*Voice) SetReceiver added in v0.27.0

func (v *Voice) SetReceiver(r notification.Receiver)

SetReceiver sets the notification.Receiver for incoming calls and status updates.

func (*Voice) Status

func (v *Voice) Status(ctx context.Context, externalID string) (*notification.Status, error)

Status provides the current status of a message.

type VoiceOptions

type VoiceOptions struct {
	// ValidityPeriod controls how long a message will still be valid in Twilio's queue.
	ValidityPeriod time.Duration

	// The call type/message.
	CallType CallType

	// CallbackParams will be added to all callback URLs
	CallbackParams url.Values

	// Params will be added to the voice callback URL
	Params url.Values
}

VoiceOptions allows configuring outgoing voice calls.

func (*VoiceOptions) CallbackURL

func (voice *VoiceOptions) CallbackURL(cfg config.Config) (string, error)

CallbackURL will return the callback url for the given configuration.

func (*VoiceOptions) StatusCallbackURL

func (voice *VoiceOptions) StatusCallbackURL(cfg config.Config) (string, error)

StatusCallbackURL will return the status callback url for the given configuration.

Jump to

Keyboard shortcuts

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