twilio

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2013 License: MIT Imports: 9 Imported by: 12

README

Twilio

Build Status

Simple Twilio API wrapper in Go.

Usage

As usual you can go get the twilio package by issuing:

$ go get github.com/subosito/twilio

Then you can use it on your application:

package main

import (
	"fmt"
	"github.com/subosito/twilio"
)

func main() {
	// Common stuffs
	AccountSid := "ac650108548e09aC2eed18ddb850c20b9"
	AuthToken := "2ecaf74387cbb28456aad6fb57b5ad682"
	from := "+15005550006"
	to := "+62801234567"
	callbackUrl := "http://subosito.com/"

	// Initialize twilio client
	t := twilio.NewTwilio(AccountSid, AuthToken)

	// You can set custom Transport, eg: you're using `appengine/urlfetch` on Google's appengine
	// c := appengine.NewContext(r) // r is a *http.Request
	// t.Transport = &urlfetch.Transport{Context: c}

	// Send SMS
	params := twilio.SMSParams{StatusCallback: callbackUrl}
	s, err := t.SendSMS(from, to, "Hello Go!", params)

	// or, make a voice call
	// params := twilio.CallParams{Url: callbackUrl}
	// s, err := t.MakeCall(from, to, params)

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("%+v\n", s)
	return
}

Resources

Documentation: http://godoc.org/github.com/subosito/twilio

Documentation

Overview

Package twilio provides support for interacting with Twilio REST API

Example

This example shows the usage of twilio package. You can get your AccountSid and AuthToken on Account Dashboard page.

package main

import (
	"fmt"
	"github.com/subosito/twilio"
)

func main() {
	// Common stuffs
	AccountSid := "ac650108548e09aC2eed18ddb850c20b9"
	AuthToken := "2ecaf74387cbb28456aad6fb57b5ad682"
	from := "+15005550006"
	to := "+62801234567"
	callbackUrl := "http://subosito.com/"

	// Initialize twilio client
	t := twilio.NewTwilio(AccountSid, AuthToken)

	// You can set custom Transport, eg: when you're using `appengine/urlfetch` on Google's appengine.
	// c := appengine.NewContext(r) // r is a *http.Request
	// t.Transport = urlfetch.Transport{Context: c}

	// Send SMS
	params := twilio.SMSParams{StatusCallback: callbackUrl}
	s, err := t.SendSMS(from, to, "Hello Go!", params)

	// or, make a voice call
	// params := twilio.CallParams{Url: callbackUrl}
	// s, err := t.MakeCall(from, to, params)

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("%+v\n", s)
	return
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallParams

type CallParams struct {
	// required, choose one of these
	Url            string
	ApplicationSid string

	Method               string
	FallbackUrl          string
	FallbackMethod       string
	StatusCallback       string
	StatusCallbackMethod string
	SendDigits           string
	IfMachine            string // Continue or Hangup
	Timeout              int
	Record               bool
}

type CallResponse

type CallResponse struct {
	Sid             string    `json:"sid"`
	DateCreated     Timestamp `json:"date_created,omitempty"`
	DateUpdated     Timestamp `json:"date_updated,omitempty"`
	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       Timestamp `json:"start_time,omitempty"`
	EndTime         Timestamp `json:"end_time,omitempty"`
	Duration        string    `json:"duration,omitempty"`
	Price           Price     `json:"price,omitempty"`
	Direction       string    `json:"direction"`
	AnsweredBy      string    `json:"answered_by,omitempty"`
	ApiVersion      string    `json:"api_version"`
	ForwardedFrom   string    `json:"forwarded_from,omitempty"`
	CallerName      string    `json:"caller_name,omitempty"`
	Uri             string    `json:"uri"`
	SubresourceUris struct {
		Notifications string `json:"notifications"`
		Recordings    string `json:"recordings"`
	} `json:"subresource_uris"`
}

type CallSipParams

type CallSipParams struct {
	From            string
	SipAuthUsername string
	SipAuthPassword string
}

type Exception

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

Exception holds information about error response returned by Twilio API

func (*Exception) Error

func (e *Exception) Error() string

Exception implements Error interface

type Pagination

type Pagination struct {
	Start           int    `json:"start"`
	Total           int    `json:"total"`
	NumPages        int    `json:"num_pages"`
	Page            int    `json:"page"`
	PageSize        int    `json:"page_size"`
	End             int    `json:"end"`
	Uri             string `json:"uri"`
	FirstPageUri    string `json:"first_page_uri"`
	LastPageUri     string `json:"last_page_uri"`
	NextPageUri     string `json:"next_page_uri"`
	PreviousPageUri string `json:"previous_page_uri"`
}

type Parameter

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

type Price

type Price float32

func (*Price) UnmarshalJSON

func (p *Price) UnmarshalJSON(b []byte) error

type SMSListResponse

type SMSListResponse struct {
	Pagination
	SMSMessages []SMSResponse
}

type SMSParams

type SMSParams struct {
	StatusCallback string
	ApplicationSid string
}

type SMSResponse

type SMSResponse struct {
	AccountSid  string    `json:"account_sid"`
	ApiVersion  string    `json:"api_version"`
	Body        string    `json:"body"`
	DateCreated Timestamp `json:"date_created,omitempty"`
	DateSent    Timestamp `json:"date_sent,omitempty"`
	DateUpdated Timestamp `json:"date_updated,omitempty"`
	Direction   string    `json:"direction"`
	From        string    `json:"from"`
	Price       Price     `json:"price,omitempty"`
	Sid         string    `json:"sid"`
	Status      string    `json:"status"`
	To          string    `json:"to"`
	Uri         string    `json:"uri"`
}

type Timestamp

type Timestamp time.Time

func (*Timestamp) UnmarshalJSON

func (m *Timestamp) UnmarshalJSON(b []byte) error

type Twilio

type Twilio struct {
	AccountSid string
	AuthToken  string
	BaseUrl    string

	// Transport is the HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil
	Transport http.RoundTripper
}

func NewTwilio

func NewTwilio(accountSid, authToken string) *Twilio

func (*Twilio) GetSMS

func (t *Twilio) GetSMS(sid string) (s *SMSResponse, err error)

func (*Twilio) ListSMS

func (t *Twilio) ListSMS(filters map[string]string) (sl *SMSListResponse, err error)

Returns a list of SMS messages associates with your account. It's support list filters via `map[string]string`:

"To" : Only show SMS messages to this phone number
"From" : Only show SMS messages from this phone number
"DateSent" : Only show SMS messages sent on this date (in GMT format), given as `YYYY-MM-DD`.

func (*Twilio) MakeCall

func (t *Twilio) MakeCall(from, to string, p CallParams) (r *CallResponse, err error)

Make a voice call. You need to set one of `Url` or `ApplicationSid` parameter on `CallParams`

Example
package main

import (
	"fmt"
	"github.com/subosito/twilio"
)

func main() {
	// Common stuffs
	AccountSid := "ac650108548e09aC2eed18ddb850c20b9"
	AuthToken := "2ecaf74387cbb28456aad6fb57b5ad682"
	from := "+15005550006"
	to := "+62801234567"
	callbackUrl := "http://subosito.com/"

	// Initialize twilio client
	t := twilio.NewTwilio(AccountSid, AuthToken)

	// Voice call
	params := twilio.CallParams{Url: callbackUrl, Timeout: 90}
	s, err := t.MakeCall(from, to, params)

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("%+v\n", s)
	return
}
Output:

func (*Twilio) MakeSipCall

func (t *Twilio) MakeSipCall(to string, p CallSipParams) (r *CallResponse, err error)

func (*Twilio) SendSMS

func (t *Twilio) SendSMS(from, to, body string, p SMSParams) (s *SMSResponse, err error)

Send SMS with more verbose options. It's support optional parameters.

StatusCallback : A URL that Twilio will POST to when your message is processed.
ApplicationSid : Twilio will POST `SMSSid` as well as other statuses to the URL in the `SMSStatusCallback` property of this application

func (*Twilio) SimpleSendSMS

func (t *Twilio) SimpleSendSMS(from, to, body string) (*SMSResponse, error)

Simple version of Send SMS with no optional parameters support.

Jump to

Keyboard shortcuts

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