Making customize voice call using twilio api in grails or java application

Posted By : Shakil Pathan | 29-Jun-2015

Hi,

In this blog, I am going to explain you about making customize voice calls via twilio api in your java/grails application.
Twilio is a third party paid service for making sms and voice calls from your application. I have explained how to send sms from your application in one of my previous blog. Now, I am going to explain you about how to make customize voice calls from your java application and the difficulties that I have faced in my project.

For using twilio we have to just include the client api(jar) which you can download from twilio website.

Blow is the code that I have used:

 import com.twilio.sdk.TwilioRestClient
import com.twilio.sdk.resource.factory.CallFactory
import com.twilio.sdk.resource.instance.Call

String serverURL = url		// Your server url
int callLoop = 3		// Number of repeat the message
String callVoice = 'man'	// Call voice 'man' or 'woman'
String verificationCode = '1234'	// Code in voice
String language = 'en'		// Voice language
String mobileMessage = URLEncoder.encode("Your verification code is " + verificationCode.toString().toList() + ". Please verify your number.")
String completeURL = serverURL + "/twilio?lang="+ language + "&voice=" + callVoice + "&loop=" + callLoop + "&message=" + mobileMessage
String fromNumber = '(000) 000-0000'	// Your twilio number
String ACCOUNTSID = accountId		// Your twilio account id
String AUTHTOKEN = authToken		// Your twilio auth token
TwilioRestClient client = new TwilioRestClient(ACCOUNTSID, AUTHTOKEN)
CallFactory callFactory = client.getAccount().getCallFactory();
Map param = new HashMap();
param.put("From", fromNumber);
param.put("To", toNumber);
param.put("Url", completeURL);
try {
	Call calls = callFactory.create(param);
	println("call sid " + calls.getSid())
}
catch (TwilioRestException e) {
	println("problem in message sending is " + e.message)
}
 

In the above code I am using "completeURL" for passing to the twilio, which is used for fetching the customized xml by the twilio. Which is also important and written as:

 {
	String xml = ''+params.message+''
	render(text: xml, contentType: "text/xml", encoding: "UTF-8")
}
 

This action is used for building the customized xml from your server url, which you have provided to the twilio.

Hope it helps!

Thanks

 

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..