The Get Started tutorial used a third party site named Twilio (www.twilio.com) to call your phone when an alert was generated on the platform.
In this tutorial, we will explain how it was done and how you can use Twilio for your applications.
What is Twilio?
Twilio provides a web-service API for businesses to build scalable, reliable communication apps. With Twilio's features, you can make voice calls, SMS, and other features described on their site.
Twilio provides documentation and sample codes to assist you in building your cloud application. The flow works like this: an application calls Twilio's API with a phone number to call. When someone answers the call, Twilio POSTs a call (REST style) to a URL you specify. This call returns the message to speak to the person.
Expression Rule and Groovy Scripts
We incorporated Twilio into our platform by using our custom objects (Groovy Scripting) functionality.
Two scripts are needed because one calls Twilio and the other handles the call back from Twilio.
Expression Rule: NotifyOnAlarm
Let's take a closer look at the Rule and what it does.
ExecuteCustomObject("TwilioCallMe", "555-867-5309")
The ExecuteCustomObject will execute the Groovy Script named "TwilioCallMe" and then pass in the telephone number you entered to the Groovy Script's phoneNumber parameter.
Now that you understand how the Expression Rule calls a script, let's look at the two Groovy scripts.
Groovy Scripts
The groovy script named CallMe is executed by the Expression Rule above and connects to the Twilio Server passing in a list of parameters.
String apiID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
String apiPass = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
The "Url" POST parameter is the URL that Twilio will fetch when the call is answered. The URL we configured tells Twilio to call an Axeda REST Web Service (called Scripto) which will call a Groovy Script called "TwilioVoiceResponse". This URL needs to authenticate, so you also need to put your username and password into the URL string.
Script 1: TwilioCallMe
// Add explicit phoneNumber parameter to script import org.apache.commons.httpclient.Credentials import org.apache.commons.httpclient.HostConfiguration import org.apache.commons.httpclient.HttpClient import org.apache.commons.httpclient.UsernamePasswordCredentials import org.apache.commons.httpclient.auth.AuthScope import org.apache.commons.httpclient.methods.GetMethod import org.apache.commons.httpclient.methods.PostMethod import org.apache.commons.httpclient.NameValuePair import com.axeda.drm.sdk.data.Alarm String twilioHost = 'api.twilio.com' String apiID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' String apiPass = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' HostConfiguration hc = new HostConfiguration() hc.setHost(twilioHost, 443, "https") def url = "/2008-08-01/Accounts/$apiID/Calls" def client = new HttpClient() Credentials defaultcreds = new UsernamePasswordCredentials(apiID, apiPass) client.getState().setCredentials(null, null, defaultcreds) PostMethod post = new PostMethod(url); post.addParameter 'Caller', 'XXXXXXXXXX' post.addParameter 'Called', parameters.phoneNumber post.addParameter 'Url', "http://dev6.axeda.com/services/v1/rest/Scripto/execute /TwilioVoiceResponse?username=UUUUU&password=PPPPP" client.executeMethod(hc, post); post.releaseConnection();
Script 2: TwilioVoiceResponse
The second Groovy Script "TwilioVoiceResponse" creates the message that you hear in the phone call. It uses the Platform SDK to find new alarms and puts the alarm name and asset into the message. Because this script is called through a web service, the output must be returned with a content types and XML string. The message is formatted in TwiML.
import com.axeda.drm.sdk.Context import com.axeda.drm.sdk.data.AlarmFinder import groovy.xml.MarkupBuilder import com.axeda.drm.sdk.data.Alarm import com.axeda.drm.sdk.data.AlarmState Context ctx = Context.create() AlarmFinder af = new AlarmFinder(ctx) af.setState(AlarmState.STARTED) def writer = new StringWriter() def xml = new MarkupBuilder(writer) List alarmList = af.findAll() def serial = "" def alarmname = "" def description = "" if (alarmList.size() > 0) { Alarm alarm = alarmList[0] serial = alarm.device.serialNumber alarmname = alarm.name if (alarm.description!=null) description = alarm.description } def response = """ <Response> <Say voice='man'>This is an automated voice message from the Axeda Platform.</Say> <Say voice='woman'>An alarm has occurred for asset $serial. </Say> <Say voice='woman'>$alarmname</Say> </Response> """ return ['Content-Type': 'text/xml', 'Content': response.toString()]
As you can see, we have been able to incorporate cloud based services using the Platform's rules and scriping.
For more information about Expression Rules and Groovy scripting, take a look at the following links:
- Building a "HelloWorld" WebService with Scripto shows how a script is called from a URL
- Creating Expression Rules
- Groovy Scripting
- Groovy code Examples

