twilio-go

module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 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).

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

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.

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

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 := 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 {
		fmt.Println(err.Error())
		err = nil
	} else {
		fmt.Println(resp)
	}
}
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(resp)
	}
}
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(resp)
	}
}
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())
	}
	fmt.Println(resp)

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.

Jump to

Keyboard shortcuts

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