Integrating Twilio text messaging service with Grails

Grails

While working on a first big project of mine written in Grails I needed functionality of sending a text message to the user in order to verify their identity. The choice was either go with Google Voice API, use email-to-text-message service of a particular wireless provider or look for something else.

Google Voice’s API is great, but because Google Voice itself is free you can’t really rely on it for web applications. After reaching a limit of 250 texts a day it will stop letting you send more. An alternative to it would be email to text solution – something like your-number@vtext.com for instance for sending texts to Verizon Wireless customers. However, you want to make your customer think less and therefore an extra question as to what is your wireless provider is not a great option.

After some research, I’ve found numerous sites offering API for sending texts and later on decided to go with Twilio for my needs. A piece below describes the process of creating Grails service for sending text messages via Twilio.

There is a code for the Twilio plugin by Ryan Norris out there, which I could not make work for me. This gave me an idea about extracting piece of code and creating Grails Service out of it to make an API call to Twilio for sending a message. I called mine SmsService:

Here is the code for it:

import groovyx.net.http.*
import org.codehaus.groovy.grails.commons.GrailsApplication

class SmsService {

def grailsApplication
def roomrentHttpEndpointBean = new HTTPBuilder("https://api.twilio.com/2010-04-01/")

static transactional = true

def send(String destinationPhoneNumber, String message) {
roomrentHttpEndpointBean.auth.basic(
grailsApplication.config.twilio?.account.sid,
grailsApplication.config.twilio?.account.auth_token)

def result = roomrentHttpEndpointBean.request(Method.POST) { req ->
		requestContentType = ContentType.URLENC
		uri.path = "Accounts/${grailsApplication.config.twilio?.account.sid}/SMS/Messages.json"
		if(grailsApplication.config.twilio?.sms?.enable_status_callback) {
			body = [ To: destinationPhoneNumber,
			From: grailsApplication.config.twilio?.phones.main,
			Body: message,
			StatusCallback: "${grailsApplication.config.grails.serverURL}/sms/callback" ]
		} else {
			body = [ To: destinationPhoneNumber, From: grailsApplication.config.twilio?.phones.main, Body: message ]
		}
		response.success = { resp, data ->
			return [status: data.status, sid: data.sid]
		}
		response.failure = { resp, data ->
			return [status: data.status, code: data.message]
		}
	}
}
}

Now you will need to add configuration piece to Config.groovy:

twilio {
	account {
		sid = 'YOUR_TWILIO_SID'
		auth_token = 'YOUR_TWILIO_AUTH_TOKEN'
	}

	sms {
		enable_status_callback = false  // set this to false if you DON'T want twilio to send ACK requests to your application
	}

	phones {
		main = '+1YOUR_TWILIO_NUMBER'
	}

}

Finally in your application controller define smsService first :

def smsService

And now call it and handle the Twilio response

def result = smsService.send(toPhoneNumber,yourTextMessage)
if (result.status != "queued" ) {
// use result.code to output details of the error in your application
}

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.