twilio-go

module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2021 License: MIT

README

twilio-go

Build Status Learn OSS Contribution in TwilioQuest

Documentation

The documentation for the Twilio API can be found here.

Supported Go Versions

This library supports the following Go implementations:

  • 1.14
  • 1.15
  • 1.16

Installation

To use twilio-go in your project initialize go modules then run:

go get github.com/twilio/twilio-go@latest

Getting Started

Getting started with the Twilio API couldn't be easier. Create a Client and you're ready to go.

API Credentials

The Twilio Client needs your Twilio credentials. You should pass these directly to the constructor (see the code below).

package main
import "github.com/twilio/twilio-go/twilio"

func main(){
    accountSID := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
    client := twilio.NewClient(accountSID, authToken)
}

We suggest storing your credentials as environment variables and then use it in your code. Why? You'll never have to worry about committing your credentials and accidentally posting them somewhere public.

package main
import (
	"github.com/twilio/twilio-go/twilio"
	"os"
)

func main(){
    accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
    authToken := os.Getenv("TWILIO_AUTH_TOKEN")
    client := twilio.NewClient(accountSid, authToken)
}
Buy a phone number
package main
import (
	"fmt"
	twilio "github.com/twilio/twilio-go/twilio"
	openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
	"os"
)

func main() {
	accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
	authToken := os.Getenv("TWILIO_AUTH_TOKEN")
	phoneNumber := "AVAILABLE_TWILIO_PHONE_NUMBER"

	client := twilio.NewClient(accountSid, authToken)

	params := &openapi.CreateIncomingPhoneNumberParams{}
	params.PhoneNumber = &phoneNumber

	resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		fmt.Println("Phone Number Status: " + *resp.Status)
	}
}
Send a text message
package main
import (
	"fmt"
	twilio "github.com/twilio/twilio-go/twilio"
	openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
	"os"
)
func main() {
	accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
	authToken := os.Getenv("TWILIO_AUTH_TOKEN")
	from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
	to := os.Getenv("TWILIO_TO_PHONE_NUMBER")

	client := twilio.NewClient(accountSid, authToken)

	text := "Hello there"

	params := &openapi.CreateMessageParams{}
	params.To = &to
	params.From = &from
	params.Body = &text


	resp, err := client.ApiV2010.CreateMessage(accountSid, params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		fmt.Println( "Message Status: " + *resp.Status)
		fmt.Println( "Message Sid: " + *resp.Sid)
    }
}
Make a call
package main

import (
	"fmt"
	twilio "github.com/twilio/twilio-go/twilio"
	openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
	"os"
)

func main() {
	accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
	authToken := os.Getenv("TWILIO_AUTH_TOKEN")
	from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
	to := os.Getenv("TWILIO_TO_PHONE_NUMBER")

	client := twilio.NewClient(accountSid, authToken)

	callurl := "http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient"

	params := &openapi.CreateCallParams{}
	params.To = &to
	params.From = &from
	params.Url = &callurl

	resp, err := client.ApiV2010.CreateCall(accountSid, params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		fmt.Println("Call Status: " + *resp.Status)
		fmt.Println("Call Sid: " + *resp.Sid)
		fmt.Println("Call Direction: " + *resp.Direction)
	}
}
Handling Exceptions
package main
import (
	"fmt"
	twilio "github.com/twilio/twilio-go/twilio"
	"github.com/twilio/twilio-go/framework/error"
	openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
	"os"
)

func main() {
    accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
    authToken := os.Getenv("TWILIO_AUTH_TOKEN")
    phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")

    client := twilio.NewClient(accountSid, authToken)

    params := &openapi.CreateIncomingPhoneNumberParams{}
    params.PhoneNumber = &phoneNumber

    resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
    if err != nil {
        twilioError := err.(*error.TwilioRestError)
        fmt.Println(twilioError.Error())
    }
}

For more descriptive exception types, please see the Twilio documentation.

Building

To build twilio-go run:

go build ./...
Testing

To execute the test suite run:

go test ./...
Getting help

If you need help installing or using the library, please check the Twilio Support Help Center first, and file a support ticket if you don't find an answer to your question.

All the code here was generated by twilio-oai-generator by leveraging openapi-generator and twilio-oai. If you find an issue with the generation or the openapi specs, please go ahead and open an issue or a PR against the relevant repositories.

Directories

Path Synopsis
Package client provides internal utilities for the twilio-go client library.
Package client provides internal utilities for the twilio-go client library.
framework
error
Package error provides the interface for Twilio specfic errors.
Package error provides the interface for Twilio specfic errors.
Package twilio provides bindings for Twilio's REST APIs.
Package twilio provides bindings for Twilio's REST APIs.

Jump to

Keyboard shortcuts

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