twilio

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2020 License: MIT Imports: 11 Imported by: 0

README

Twilio

Simple Twilio API wrapper in Go. Forked from subosito/twilio to allow better credential hygiene.

Usage

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

$ go get github.com/caseyh/twilio

Then you can use it on your application:

package main

import (
	"log"
	"net/url"

	"github.com/caseyh/twilio"
)

var (
	AccountSid = "AC5ef8732a3c49700934481addd5ce1659"
	AuthToken  = "2ecaf0108548e09a74387cbb28456aa2"
)

func main() {
	// Initialize twilio client
	c := twilio.NewClient(AccountSid, AuthToken, nil)

	// You can set custom Client, eg: you're using `appengine/urlfetch` on Google's appengine
	// a := appengine.NewContext(r) // r is a *http.Request
	// f := urlfetch.Client(a)
	// c := twilio.NewClient(AccountSid, AuthToken, f)

	// Send Message
	params := twilio.MessageParams{
		Body: "Hello Go!",
	}
	s, response, err := c.Messages.Send("+15005550006", "+62801234567", params)
	if err != nil {
		log.Fatal(s, response, err)
	}

	// You can also using lower level function: Create
	s, response, err = c.Messages.Create(url.Values{
		"From": {"+15005550006"},
		"To":   {"+62801234567"},
		"Body": {"Hello Go!"},
	})
	if err != nil {
		log.Fatal(s, response, err)
	}
}

Documentation

Overview

Package twilio provides support for interacting with Twilio REST API

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse added in v0.1.0

func CheckResponse(r *http.Response) error

Types

type Client added in v0.1.0

type Client struct {

	// User agent used when communicating with Twilio API
	UserAgent string

	// The Twilio API base URL
	BaseURL *url.URL

	// Credentials which is used for authentication during API request
	AccountSid string
	AuthSid    string
	AuthToken  string

	// Services used for communicating with different parts of the Twilio API
	Messages *MessageService
	// contains filtered or unexported fields
}

A client manages communication with Twilio API

func NewClient added in v0.1.0

func NewClient(accountSid, authToken string, httpClient *http.Client) *Client

NewClient returns a new Twilio API client. This will load default http.Client if httpClient is nil.

func (*Client) Do added in v0.1.0

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

func (*Client) EndPoint added in v0.1.0

func (c *Client) EndPoint(parts ...string) *url.URL

Constructing API endpoint. This will returns an *url.URL. Here's the example:

c := NewClient("1234567", "token", nil)
c.EndPoint("Messages", "abcdef") // "/2010-04-01/Accounts/1234567/Messages/abcdef.json"

func (*Client) NewRequest added in v0.1.0

func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Request, error)

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 Message added in v0.1.0

type Message struct {
	AccountSid  string    `json:"account_sid"`
	ApiVersion  string    `json:"api_version"`
	Body        string    `json:"body"`
	NumSegments int       `json:"num_segments,string"`
	NumMedia    int       `json:"num_media,string"`
	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"`
}

func (*Message) IsSent added in v0.1.0

func (m *Message) IsSent() bool

type MessageListParams added in v0.1.0

type MessageListParams struct {
	To       string
	From     string
	DateSent string
	PageSize int
}

type MessageParams added in v0.1.0

type MessageParams struct {
	// The text of the message you want to send, limited to 1600 characters.
	Body string

	// The URL of the media you wish to send out with the message. Currently support: gif, png, and jpeg.
	MediaUrl []string

	StatusCallback string
	ApplicationSid string
}

func (MessageParams) Validates added in v0.1.0

func (p MessageParams) Validates() error

type MessageService added in v0.1.0

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

func (*MessageService) Create added in v0.1.0

func (s *MessageService) Create(v url.Values) (*Message, *Response, error)

func (*MessageService) Get added in v0.1.0

func (s *MessageService) Get(sid string) (*Message, *Response, error)

func (*MessageService) List added in v0.1.0

func (s *MessageService) List(params MessageListParams) ([]Message, *Response, error)

func (*MessageService) Send added in v0.1.0

func (s *MessageService) Send(from, to string, params MessageParams) (*Message, *Response, error)

Send Message with options. It's support required and optional parameters.

One of these parameters is required:

Body     : The text of the message you want to send
MediaUrl : The URL of the media you wish to send out with the message. Currently support: gif, png, and jpeg.

Optional parameters:

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

func (*MessageService) SendSMS added in v0.1.0

func (s *MessageService) SendSMS(from, to, body string) (*Message, *Response, error)

Shortcut for sending SMS with no optional parameters support.

type Pagination

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

type Price

type Price float64

func (*Price) UnmarshalJSON

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

type Response added in v0.1.0

type Response struct {
	*http.Response
	Pagination
}

Wraps http.Response. So we can add more functionalities later.

func NewResponse added in v0.1.0

func NewResponse(r *http.Response) *Response

type Timestamp

type Timestamp struct {
	time.Time
}

func (Timestamp) Equal added in v0.1.0

func (t Timestamp) Equal(m Timestamp) bool

func (*Timestamp) IsZero added in v0.1.0

func (t *Timestamp) IsZero() bool

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(b []byte) (err error)

Jump to

Keyboard shortcuts

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