Member-only story
Send an SMS with Twilio and Golang
Modern internet applications use primarily standard communication protocols such as HTTP. However, the telecom space uses many protocols and makes it difficult to easily work with an external application. How does an application interact with SMS/voice messages to users on their personal phones?
Solution: Twilio
Twilio is a service that acts as a bridge between the internet and telecom systems. It creates an ordinary-looking phone number for an application. The application uses Twilio REST APIs for SMS and voice messages. The application need not bother about delivering the message to the end-user phone number. Twilio takes care of interacting with the telecom operator and requesting delivery of the message.
The developer needs to purchase a Twilio phone number. There is also a trial, a free number for development and testing. The application makes API calls using the Twilio number, the destination number, target message, and a couple of access tokens.
Golang App Server Example
The following Go code creates a web server that sends a text SMS message to a user’s phone.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var (
TwilioAccountSID = "XXX"
TwilioAuthToken = "YYY"
)
func reqToSendMessage(message string) (*twResponse, error) {
// Set initial variables
accountSid := TwilioAccountSID
authToken := TwilioAuthToken
urlStr :=…