Axeda Groovy Script: Send a SMS notification through Twilio

    This Groovy script is called from an ExpressionRule of type Alarm. For example, in an Expression rule

     

    IF: Alarm.severity > 500

    THEN: ExecuteCustomObject("SMSMe", "[numberToSMS]")

      

     

    calls the script "SMSMe" with the parameter phoneNumber.  The ExecuteCustomObject provides a way to call from this simple two-line business rule into a modern programming environment with access to the complete Platform SDK, as well as all the features of the Groovy language.  In Groovy, it's straightforward to use the built-in httpclient library to POST an HTTP request to Twilio to send an SMS to the specified cellphone number.

     

    Groovy Scripts

     

    The groovy script named SMSMe is executed by the Expression Rule above and connects to the Twilio Server passing in a list of parameters.

     

    When you register with Twilio, you will be given an ACCOUNT SID (apiID) and an AUTH TOKEN (apiPass). These two strings need to be in the Groovy Script below:

     

    String apiID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' 

    String apiPass = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

      


    Parameters

     

    Variable Name      Display Name
    phoneNumber      phoneNumber

     

    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
    //logger.info "Calling ${parameters.phoneNumber}"
    String twilioHost = 'api.twilio.com'
    String apiID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    String aipPass = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    
    HostConfiguration hc = new HostConfiguration()
    hc.setHost(twilioHost, 443, "https")
    def url = "/2008-08-01/Accounts/$apiID/SMS/Messages"
    
    def client = new HttpClient()
    Credentials defaultcreds = new UsernamePasswordCredentials(apiID, aipPass)
    client.getState().setCredentials(null, null, defaultcreds)
    
    PostMethod post = new PostMethod(url);
    post.addParameter 'IfMachine', 'Continue'
    post.addParameter 'Method', 'POST'
    post.addParameter 'From', '[YourNumber]'
    post.addParameter 'To', parameters.phoneNumber
    post.addParameter 'Body', 'This is an SMS from Axeda'
    
    client.executeMethod(hc, post);
    //logger.info message = "Status:" + post.getStatusText()
    //logger.info post.getResponseBodyAsString()
    
    post.releaseConnection();