twilio

package module
v0.0.0-...-4d43e6d Latest Latest
Warning

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

Go to latest
Published: May 30, 2017 License: MIT Imports: 6 Imported by: 0

README

Twilio

Twilio is a go (golang) package for the twilio www.twilio.com cloud telephone service API. The package supports initiating calls, sending SMS and generating TwiML.

Usage

Example Code

##TwiML

package main

import (
        "bitbucket.org/ckvist/twilio/twiml"
        "net/http"
)

func helloMonkey(w http.ResponseWriter, r *http.Request) {

        callers := map[string]string{"+15005550001": "Langur"}

        resp := twiml.NewResponse()

        r.ParseForm()
        from := r.Form.Get("From")
        caller, ok := callers[from]

        msg := "Hello monkey"
        if ok {
                msg = "Hello " + caller
        }

        resp.Action(twiml.Say{Text: msg},
                twiml.Play{Url: "http://demo.twilio.com/hellomonkey/monkey.mp3")
        resp.Send(w)
}

func main() {
	http.HandleFunc("/", helloMonkey)
	http.ListenAndServe(":8080", nil)
}

##REST

package main

import (
        "bitbucket.org/ckvist/twilio/twirest"
        "fmt"
)

func main() {
        // Test account Sid/Token
        accountSid := "ACdf045ee0ab0e2212ae091a3217660db6"
        authToken := "f74298ebab3a31e099f7161235764b0a"

        client := twirest.NewClient(accountSid, authToken)

        msg := twirest.SendMessage{
                Text: "Hello monkey",
                To:   "+15005550001",
                From: "+15005550005"}

        resp, err := client.Request(msg)
        if err != nil {
                fmt.Println(err)
                return
        }

        fmt.Println(resp.Message.Status)

Status

Not all functionality is supported nor tested. For example, I have not implemented Short Codes and Transcriptions at this time among a few others.

Other features are implemented but they are not fully tested.

License

Released under MIT license. See LICENSE file.

Documentation

Overview

Package twilio contains helpers for building Twilio applications.

Index

Examples

Constants

This section is empty.

Variables

Clock provides time for calculating expiry times in tokens.

Functions

func Generate

func Generate(c Capabilities, expires time.Duration) (string, error)

Generate creates a Capabilities Token given some configuration values. See https://www.twilio.com/docs/api/client/capability-tokens for details.

Example
package main

import (
	"fmt"
	"time"

	"github.com/jonboulle/clockwork"
	"github.com/tmc/twilio"
)

func init() {
	twilio.Clock = clockwork.NewFakeClockAt(time.Unix(1257894000, 0))
}

func main() {
	caps := twilio.Capabilities{
		AccountSid:          "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
		AuthToken:           "yyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
		AllowClientIncoming: "tommy",
		AllowClientOutgoing: "APFOOOOOOOo",
	}

	token, err := twilio.Generate(caps, time.Second)
	if err != nil {
		panic(err)
	}
	fmt.Println(token)
}
Output:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjEyNTc4OTQwMDEsImlzcyI6IkFDeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHgiLCJzY29wZSI6InNjb3BlOmNsaWVudDpvdXRnb2luZz9hcHBTaWQ9QVBGT09PT09PT29cdTAwMjZjbGllbnROYW1lPXRvbW15IHNjb3BlOmNsaWVudDppbmNvbWluZz9jbGllbnROYW1lPXRvbW15In0.n-GADTApMTBanP_o69br2djf8GSmycaL3FpLYHHcrTA

Types

type Capabilities

type Capabilities struct {
	AccountSid          string // AccountSid is the Twilio Account Sid
	AuthToken           string // AuthToken is the secret Twilio Account Auth Token
	AllowClientIncoming string // AllowClientIncoming specifies the possible client name that can be adopted
	AllowClientOutgoing string // AllowClientOutgoing specifies that this token can make outgoing calls using the given Application Sid
}

Capabilities describes the possible capabilities for a Twilio token.

Directories

Path Synopsis
Package twiml provides Twilio Markup Language support for building web services with instructions for twilio how to handle incoming call or message.
Package twiml provides Twilio Markup Language support for building web services with instructions for twilio how to handle incoming call or message.
Package twirest provides a interface to Twilio REST API allowing the user to query meta-data from their account and, to initiate calls and send SMS.
Package twirest provides a interface to Twilio REST API allowing the user to query meta-data from their account and, to initiate calls and send SMS.

Jump to

Keyboard shortcuts

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