Wednesday, October 8, 2014

Sending SMS via Twilio with TDI


I recently had a conversation with Chris.E on the topic of using services provided by Twilio.com to send SMS/MMS messages from IBM TDI.  In the future, I might look into reading messages from the service.

Here's a summary on implementing the send call within TDI.

First, follow the Twilio Java information on where to obtain the pre-built jar for 'twilio-java-sdk-*.*.*-jar-with-dependencies.jar'.  Once downloaded, place the jar in the <TDI711_Install_Directory>\jvm\jre\lib\ext directory.  Placing the jar in the default <TDI711\jars\3rdparty\others directory will cause the following invocation exception when the Assemblyline runs.

java.lang.reflect.InvocationTargetException
...
...
Caused by: java.lang.VerifyError: org.apache.http.params.BasicHttpParams

Second, with the jar added to the <TDI711_Install_Directory>\jvm\jre\lib\ext directory, start the TDI Config Editor.  From here, you can instantiate the TwilioRestClient and start sending SMS/MMS messages from TDI.


Here's an example of a simple implementation

Create custom functions:



/**
 * Functions created from examples provided at www.Twilio.com
 * https://www.twilio.com/docs/quickstart/java/sms/sending-via-rest
 * http://twilio.github.io/twilio-java/
 */

function createTwilioSession(ACCOUNT_SID, AUTH_TOKEN ){
 twclient = new com.twilio.sdk.TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
 return twclient
}

function sendSMS(client, to, from, body){
 messageFactory = client.getAccount().getMessageFactory();
 params = new java.util.ArrayList();
 params.add(new org.apache.http.message.BasicNameValuePair("To", to));
 params.add(new org.apache.http.message.BasicNameValuePair("From", from));
 params.add(new org.apache.http.message.BasicNameValuePair("Body", body));  
 message = messageFactory.create(params);
 return message.getSid()
}

function sendMMS(client, to, from, body, image){
 messageFactory = client.getAccount().getMessageFactory();
 params = new java.util.ArrayList();
 params.add(new org.apache.http.message.BasicNameValuePair("To", to));
 params.add(new org.apache.http.message.BasicNameValuePair("From", from));
 params.add(new org.apache.http.message.BasicNameValuePair("Body", body));
 params.add(new org.apache.http.message.BasicNameValuePair("MediaUrl", image));  
 message = messageFactory.create(params);
 return message.getSid()
}



Instantiate the connection on the start of the AL:



Iterate data in the AL Flow:



Call custom function:



Result: