Attention: Deprecation notice for Bintray, JCenter, GoCenter and ChartCenter. Learn More
To use GoCenter:
export GOPROXY=https://gocenter.io
github.com/Januzellij/gotwilio
Twilio library for Go (golang).
August 30th 2019
Last Modified
7
Stars
BSD-2-Clause
License
7
Downloads
ReadMe
Mod File
GoDocs
New
Security
Dependencies (0)
Used By (0)
Metrics
Versions
Overview
This is an unofficial Go API library for Twilio. Gotwilio supports making voice calls, sending text messages, validating requests, creating TWiML responses, and various REST resources.
License
Gotwilio is licensed under a BSD license.
Installation
To install gotwilio, run go get github.com/Januzellij/gotwilio
.
Getting Started
Just create a Twilio client with either NewTwilioClient(accountSid, authToken)
or NewTwilioClientFromEnv()
, and store the accountSid and authToken in TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
environment variables, respectively. For security purposes, please use NewTwilioClientFromEnv()
in any open source code.
Docs
All documentation can be found here
SMS Example
package main
import (
"log"
"github.com/Januzellij/gotwilio"
)
func main() {
accountSid := "ABC123..........ABC123"
authToken := "ABC123..........ABC123"
twilio := gotwilio.NewTwilioClient(accountSid, authToken)
from := "+15555555555"
to := "+15555555555"
message := "Welcome to gotwilio!"
_, exception, err := twilio.SendSMS(from, to, message, "", "")
if exception != nil {
log.Fatal(*exception)
}
if err != nil {
log.Fatal(err)
}
}
Voice Example
package main
import "github.com/Januzellij/gotwilio"
func main() {
accountSid := "ABC123..........ABC123"
authToken := "ABC123..........ABC123"
twilio := gotwilio.NewTwilioClient(accountSid, authToken)
from := "+15555555555"
to := "+15555555555"
callbackParams := gotwilio.NewCallbackParameters("http://example.com")
_, exception, err := twilio.CallWithUrlCallbacks(from, to, callbackParams)
if exception != nil {
log.Fatal(*exception)
}
if err != nil {
log.Fatal(err)
}
}
Validate Example
package main
import (
"log"
"net/http"
"github.com/Januzellij/gotwilio"
)
func root(w http.ResponseWriter, r *http.Request) {
twilio, err := NewTwilioClientFromEnv()
if err != nil {
// one or more environment variables are missing
log.Fatal(err)
}
url := "http://example.com/"
isValid, err := gotwilio.Validate(r, url, twilio.authToken)
if err != nil {
// the request was malformed
}
if isValid {
// proceed, the request is from Twilio
}
}
func main() {
http.HandleFunc("/", root)
http.ListenAndServe(":8080", nil)
}
Twiml Response Example
package main
import (
"log"
"os"
"github.com/Januzellij/gotwilio"
)
func main() {
newSay := gotwilio.Say{Text: "test", Voice: "alice"}
newPause := gotwilio.Pause{Length: "2"}
resp := gotwilio.NewTwimlResponse(newSay, newPause)
err := resp.SendTwimlResponse(os.Stdout) // when using Twiml in a real web app, this would actually be written to a http.ResponseWriter.
if err != nil {
// something other than TWiML verbs was given to the TWiML response
log.Fatal(err)
}
}
Usage Record Example
package main
import (
"fmt"
"log"
"github.com/Januzellij/gotwilio"
)
func main() {
twilio, err := gotwilio.NewTwilioClientFromEnv()
if err != nil {
log.Fatal(err)
}
filter := &gotwilio.UsageFilter{StartDate: "2012-6-4", EndDate: "2014-1-1"}
records, exception, err := twilio.UsageRecordsDaily(filter)
if exception != nil {
log.Fatal(*exception)
}
if err != nil {
log.Fatal(err)
}
for _, record := range records.UsageRecords {
fmt.Printf("Category: %s, Usage: %d \n", record.Category, record.Usage)
}
}