Go

Sending SMS messages using Twilio and Golang


Recently, I had the opportunity to work on a Golang project which needed to send SMS messages. For this, I used Twilio, a service that allows software developers to programmatically make and receive phone calls and send and receive text messages using its web service APIs. Twilio's services are accessed over HTTP and are billed based on usage.

Let's begin

In this tutorial I will explain how to send SMS messages using Twilio as a service and Golang as a programming language. First of all, we need a Twilio account.

Twilio Account

Go to Twilio's web page and create an account. You will have to verify your phone number in order to receive SMS messages.

Once you have created your account, Twilio will give you a programmable number from which you can send messages or make calls.

Using a Golang library for Twilio

Because we only need Twilio for sending SMS, this Twilio Go wrapper is more than perfect.

$ go get github.com/subosito/twilio

Start coding

Let's create a TwilioApp folder, inside of it we need to create our main.go file.

package main

We need to import the Go wrapper.

import (
    "github.com/subosito/twilio"
    "log"
)

Now, we need the Account SID number, the Auth token and the 'From number'.

Test credentials

Twilio provides us with a set of test credentials so we can exercise parts of the REST API without charging our account. What this will do is that it'll simulate sending the SMS message and will respond us back a mocked/faked response.

We can make Twilio send us an error report, so we can manage them. Here you can find more information about the failures, errors and success responses.

Here you can find your test credentials. If you want a 'success response', then you have to use this +15005550006 as a From number.

Real credentials

If what you want is to send a real SMS message to your phone, then you'll need your real credentials. The From number has to be the one that Twilio assigned to you, your programmable number. Remember that it has to come with the +N zone number and each message has a cost (US cents).

After you have chosen how to use Twilio, you can declare those credentials as variables.

var (
    AccountSid = "AC6322d5e60c2aadd30700b124b06e6dde"
    AuthToken  = "7411ae9453892d1a5ba10c1903376294"
    From       = "+15005550006"
    // To         = "" Your phone number, including the +N zone number.
)

Finally, we need to implement our main() function. Where we have to initialize the Twilio client, and send the SMS with some text body.

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

    // Send Message
    params := twilio.MessageParams{
        Body: "Hello Go!",
    }

    s, resp, err := c.Messages.Send(From, To, params)
    log.Println("Send:", s)
    log.Println("Response:", resp)
    log.Println("Err:", err)
}

We just run our application and see the output in our terminal/console.

$ go run main.go

The output should be something like this:

2014/11/24 17:38:59 Send: &{AC667075994c14053ed10300d94ff1fda7 2010-04-01 Hello Go! 1 0 2014-11-25 01:38:59 +0000 +0000 0001-01-01 00:00:00 +0000 UTC 2014-11-25 01:38:59 +0000 +0000 outbound-api +12057032243 0 SMa7c46f695c4c4f40a1beafc7beb50ebe queued +523141250934 /2010-04-01/Accounts/AC667075994c14053ed10300d94ff1fda7/Messages/SMa7c46f695c4c4f40a1beafc7beb50ebe.json}
2014/11/24 17:38:59 Response: &{0xc2082e8d80 {0 0 0 0 0 0     }}
2014/11/24 17:38:59 Err: <nil>

If you used your real credentials, you will get an SMS message like this:

Sum up

This was a basic tutorial to show you how to send SMS messages using Twilio and Golang. As you have seen, this is very easy to accomplish, but Twilio allows us to make calls and more things, so I encourage you to read the full Twilio REST API documentation to do more stuff.

Use the comments section if you have any questions, and let us know if you want us to explore more Golang integrations.

Thank you for reading!

Best Practices
8 Tips For Bootstrap
Agile
React JS: Communication between components
Javascript
De Código, Café y Cervezas 05 – ReactJS + AngularJS (Parte 3)