gotwilio

package module
v0.0.0-...-ab71fd1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2017 License: BSD-2-Clause Imports: 13 Imported by: 0

README

Overview

This is the start of a library for Twilio. Gotwilio supports making voice calls and sending text messages.

License

Gotwilio is licensed under a BSD license.

Installation

To install gotwilio, simply run go get github.com/sfreiberg/gotwilio.

SMS Example

package main

import (
	"github.com/sfreiberg/gotwilio"
)

func main() {
	accountSid := "ABC123..........ABC123"
	authToken := "ABC123..........ABC123"
	twilio := gotwilio.NewTwilioClient(accountSid, authToken)

	from := "+15555555555"
	to := "+15555555555"
	message := "Welcome to gotwilio!"
	twilio.SendSMS(from, to, message, "", "")
}

MMS Example

package main

import (
	"github.com/sfreiberg/gotwilio"
)

func main() {
	accountSid := "ABC123..........ABC123"
	authToken := "ABC123..........ABC123"
	twilio := gotwilio.NewTwilioClient(accountSid, authToken)

	from := "+15555555555"
	to := "+15555555555"
	message := "Welcome to gotwilio!"
	twilio.SendMMS(from, to, message, "http://host/myimage.gif", "", "")
}

Voice Example

package main

import (
	"github.com/sfreiberg/gotwilio"
)

func main() {
	accountSid := "ABC123..........ABC123"
	authToken := "ABC123..........ABC123"
	twilio := gotwilio.NewTwilioClient(accountSid, authToken)

	from := "+15555555555"
	to := "+15555555555"
	callbackParams := gotwilio.NewCallbackParameters("http://example.com")
	twilio.CallWithUrlCallbacks(from, to, callbackParams)
}

Documentation

Overview

Package gotwilio is a library for interacting with http://www.twilio.com/ API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackParameters

type CallbackParameters struct {
	Url                  string   // Required
	Method               string   // Optional
	FallbackUrl          string   // Optional
	FallbackMethod       string   // Optional
	StatusCallback       string   // Optional
	StatusCallbackMethod string   // Optional
	StatusCallbackEvent  []string // Optional
	SendDigits           string   // Optional
	Timeout              int      // Optional
	Record               bool     // Optional
	RecordingChannels    string   // Optional
	MachineDetection     string   // Optional
}

These are the paramters to use when you want Twilio to use callback urls. See http://www.twilio.com/docs/api/rest/making-calls for more info.

func NewCallbackParameters

func NewCallbackParameters(url string) *CallbackParameters

Returns a CallbackParameters type with the specified url and CallbackParameters.Timeout set to 60.

type Exception

type Exception struct {
	Status   int    `json:"status"`    // HTTP specific error code
	Message  string `json:"message"`   // HTTP error message
	Code     int    `json:"code"`      // Twilio specific error code
	MoreInfo string `json:"more_info"` // Additional info from Twilio
}

Exception is a representation of a twilio exception.

type SmsResponse

type SmsResponse struct {
	Sid         string   `json:"sid"`
	DateCreated string   `json:"date_created"`
	DateUpdate  string   `json:"date_updated"`
	DateSent    string   `json:"date_sent"`
	AccountSid  string   `json:"account_sid"`
	To          string   `json:"to"`
	From        string   `json:"from"`
	MediaUrl    string   `json:"media_url"`
	Body        string   `json:"body"`
	Status      string   `json:"status"`
	Direction   string   `json:"direction"`
	ApiVersion  string   `json:"api_version"`
	Price       *float32 `json:"price,omitempty"`
	Url         string   `json:"uri"`
}

SmsResponse is returned after a text/sms message is posted to Twilio

func (*SmsResponse) DateCreatedAsTime

func (sms *SmsResponse) DateCreatedAsTime() (time.Time, error)

Returns SmsResponse.DateCreated as a time.Time object instead of a string.

func (*SmsResponse) DateSentAsTime

func (sms *SmsResponse) DateSentAsTime() (time.Time, error)

Returns SmsResponse.DateSent as a time.Time object instead of a string.

func (*SmsResponse) DateUpdateAsTime

func (sms *SmsResponse) DateUpdateAsTime() (time.Time, error)

Returns SmsResponse.DateUpdate as a time.Time object instead of a string.

type Twilio

type Twilio struct {
	AccountSid string
	AuthToken  string
	BaseUrl    string
	HTTPClient *http.Client
}

Twilio stores basic information important for connecting to the twilio.com REST api such as AccountSid and AuthToken.

func NewTwilioClient

func NewTwilioClient(accountSid, authToken string) *Twilio

Create a new Twilio struct.

func NewTwilioClientCustomHTTP

func NewTwilioClientCustomHTTP(accountSid, authToken string, HTTPClient *http.Client) *Twilio

Create a new Twilio client, optionally using a custom http.Client

func (*Twilio) CallWithApplicationCallbacks

func (twilio *Twilio) CallWithApplicationCallbacks(from, to, applicationSid string) (*VoiceResponse, *Exception, error)

Place a voice call with an ApplicationSid specified.

func (*Twilio) CallWithUrlCallbacks

func (twilio *Twilio) CallWithUrlCallbacks(from, to string, callbackParameters *CallbackParameters) (*VoiceResponse, *Exception, error)

Place a voice call with a list of callbacks specified.

func (*Twilio) CheckRequestSignature

func (twilio *Twilio) CheckRequestSignature(r *http.Request, baseURL string) (bool, error)

CheckRequestSignature checks that the X-Twilio-Signature header on a request matches the expected signature defined by the GenerateSignature function.

The baseUrl parameter will be prepended to the request URL. It is useful for specifying the protocol and host parts of the server URL hosting your endpoint.

Passing a non-POST request or a request without the X-Twilio-Signature header is an error.

func (*Twilio) GenerateSignature

func (twilio *Twilio) GenerateSignature(url string, form url.Values) ([]byte, error)

GenerateSignature computes the Twilio signature for verifying the authenticity of a request. It is based on the specification at: https://www.twilio.com/docs/security#validating-requests

func (*Twilio) GetSMS

func (twilio *Twilio) GetSMS(sid string) (smsResponse *SmsResponse, exception *Exception, err error)

GetSMS uses Twilio to get information about a text message. See https://www.twilio.com/docs/api/rest/sms for more information.

func (*Twilio) SendMMS

func (twilio *Twilio) SendMMS(from, to, body, mediaUrl, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error)

SendMultimediaMessage uses Twilio to send a multimedia message.

func (*Twilio) SendSMS

func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error)

SendTextMessage uses Twilio to send a text message. See http://www.twilio.com/docs/api/rest/sending-sms for more information.

func (*Twilio) SendSMSWithCopilot

func (twilio *Twilio) SendSMSWithCopilot(messagingServiceSid, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error)

SendSMSWithCopilot uses Twilio Copilot to send a text message. See https://www.twilio.com/docs/api/rest/sending-messages-copilot

type VoiceResponse

type VoiceResponse struct {
	Sid            string   `json:"sid"`
	DateCreated    string   `json:"date_created"`
	DateUpdated    string   `json:"date_updated"`
	ParentCallSid  string   `json:"parent_call_sid"`
	AccountSid     string   `json:"account_sid"`
	To             string   `json:"to"`
	ToFormatted    string   `json:"to_formatted"`
	From           string   `json:"from"`
	FromFormatted  string   `json:"from_formatted"`
	PhoneNumberSid string   `json:"phone_number_sid"`
	Status         string   `json:"status"`
	StartTime      string   `json:"start_time"`
	EndTime        string   `json:"end_time"`
	Duration       int      `json:"duration"`
	Price          *float32 `json:"price,omitempty"`
	Direction      string   `json:"direction"`
	AnsweredBy     string   `json:"answered_by"`
	ApiVersion     string   `json:"api_version"`
	Annotation     string   `json:"annotation"`
	ForwardedFrom  string   `json:"forwarded_from"`
	GroupSid       string   `json:"group_sid"`
	CallerName     string   `json:"caller_name"`
	Uri            string   `json:"uri"`
}

VoiceResponse contains the details about successful voice calls.

func (*VoiceResponse) DateCreatedAsTime

func (vr *VoiceResponse) DateCreatedAsTime() (time.Time, error)

Returns VoiceResponse.DateCreated as a time.Time object instead of a string.

func (*VoiceResponse) DateUpdatedAsTime

func (vr *VoiceResponse) DateUpdatedAsTime() (time.Time, error)

Returns VoiceResponse.DateUpdated as a time.Time object instead of a string.

func (*VoiceResponse) EndTimeAsTime

func (vr *VoiceResponse) EndTimeAsTime() (time.Time, error)

Returns VoiceResponse.EndTime as a time.Time object instead of a string.

func (*VoiceResponse) StartTimeAsTime

func (vr *VoiceResponse) StartTimeAsTime() (time.Time, error)

Returns VoiceResponse.StartTime as a time.Time object instead of a string.

Jump to

Keyboard shortcuts

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