Rule Machine API
It is possible for other apps to use Rules, Triggers, Triggered Rules, and Actions defined in Rule Machine. This is very similar, and uses the same mechanism, as actions in Rule Machine that affect other rules. It is also possible to use Rules, Triggers, Triggered Rules, and Actions from an HTTP request to an endpoint.
Using Rule Machine from other apps
First, import the RM helper class into your app:
The List of Rules
Rule Machine maintains a list of available Rules, Triggers, Triggered Rules, and Actions. This list can be obtained with this method call:
def rules = RMUtils.getRuleList()
The resulting list, in this example in the variable "rules", can be used as an input to an "enum", as the options.
It is possible to set the Private Boolean, evaluate a rule, run the rule actions, or stop running rule actions (either delayed or repeating). This is accomplished by sending an action request to Rule Machine. These will each take this form:
def RMUtils.sendAction(rules, action, appLabel)
Set Private Boolean True:
Set Private Boolean False:
Run Rule Actions:
Stop Rule Actions:
In each case above, a list of rules selected by input is sent. The rule options come from the variable to which they were input as described above, in the options section of the input..
The "appLabel" parameter is passed and will appear in the log entry that the rule makes when it performs the action commanded. Typically, simply pass
app.label, for the name of the app that is causing the action. This has no other function than logging.
def rules = RMUtils.getRuleList() input "theseRules", "enum", title: "Select which rules to stop", options: rules RMUtils.sendAction(theseRules, "stopRuleAct", app.label)
Using Rule Machine from HTTP requests
It is also possible to cause Rule Machine to perform these same actions from an HTTP request. To do this one would create a Trigger or a Triggered Rule with either a Local End Point or Cloud End Point. The endpoint URL given by Rule Machine has this form:
To run rule actions this URL must be modified to include the list of rules and the action. The modification takes this form:
Where action is the action from the list above and
rule1&rule2&rule3 are the appIds of the rules to run separated by ampersands.
This parameter is inserted in the endpoint URL just before the ? that precedes the access_token, like this:
This example would do the same thing as the code example above, where 943&956&10217 are the appIds that were selected by consequence of the input for theseRules, and stopRuleAct is the action to perform.
The appIds are the values selected by the input described above, for example
theseRules. The appIds can also be found for a rule by opening the rule and observing its appId in its url, like this:
The appId for that rule is 10249.
Get Rule List
To get the list of rules as is returned from
getRuleList() use this insert for the URL:
for full URL like this:
This returns a JSON object with appId and rule name pairs. The other requests return a JSON object with a human readable description of what was done.
Note: Rule Machine actions Stop Rule Actions, Pause Rule and Resume Rule only work for Rule 2.5 or later.