Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

After we install a ruleset containing this action definition and rule in our pico, we can use the Sky Event Console to publish an event to the pico. If you're using valid values for the account SID, authentication token, and from phone number, the to phone number should receive an SMS with the message you give. 

The use user-defined action hides some of the details of the Twilio API and presents a simple action that any rule can use to send an SMS. 

...

The previous section is not as useful as it could be because it requires that the account SID and authorization token be . If we put passed in each time. This is where KRL's parameterized modules come in handy. 

Modules are parameterized using a configure pragma in the meta block. The variables declared in the configure pragma take on the values that are set when the module is used. The configuration allows for default values. 

The following is a complete module for the send_sms() action using configuration. 

Code Block
languagejs
themeConfluence
linenumberstrue
ruleset io.picolabs.twilio_v2 {
  meta {


    configure using account_sid = ""
                    auth_token = ""
    
    provides 
        send_sms
  }

  global {
    
    send_sms = defaction(to, from, message) {
       base_url = "https://" + account_sid + ":" + auth_token} +
                      "@api.twilio.com/2010-04-01/Accounts/" + account_sid + "/"
       http:post(base_url + "Messages.json")
            with body = {
                "From":from,
                "To":to,
                "Body":message
            }
    }
  }
}

The configure pragma's defaults are the empty string since there's no workable default for the Twilio keys. You can see that those variables are referenced in the send_sms() action to create the based URL for the Twilio call. 

We use a ruleset and then use that ruleset as a module, parameterized module by declaring its use in the meta block of the ruleset that wants to use the module. The use pragma allows parameters to be supplied for configuration variables. The following shows a ruleset that uses the Twilio module. 

Code Block
languagejs
themeConfluence
ruleset io.picolabs.use_twilio_v2 {
  meta {
    key twilio {
          "account_sid": "<your SID goes here>",  
          "auth_token" : "<your auth token goes here>" 
    }
    use module io.picolabs.twilio_v2 alias twilio
        with account_sid = keys:twilio("account_sid")
             auth_token =  keys:twilio("auth_token")
  }

  rule test_send_sms {
    select when test new_message
    twilio:send_sms(event:attr("to"),
                    event:attr("from"),
                    event:attr("message")
                   )
  }
}



## outline

  1. Create a ruleset to serve as our module called twilio
  2. write an action that makes an HTTP POST to send an SMS with credentials hard coded
  3. write ruleset to test it
  4. pull credentials into keys pragma. Still not great. 
  5. Use module configuration to get keys out of module and into testing ruleset
  6. use key module to get keys out of testing ruleset. 

...