Skip to main content
Version: v5.0

Custom scripts

Overview#

A trigger may have a script associated with it which allows for extra control over how the Notification Service works. If the trigger has an associated script then when event matching succeeds for the trigger, the script is executed and passed the same information as was used in event matching.

The script can be used to either selectively continue or skip processing the notification, or to change the information that is made available to templates.

Scripts processing multiple events#

For performance reasons, the script may be executed with multiple similar events in parallel. The script may either return the passed events unchanged to continue processing, remove some or all events to skip further processing of those events, or alter and return the events to continue processing and change the data available in templates.

Script Manager#

The script must be a standard Script Manager script with the following properties:

  • Must have a function with a single parameter: events, which is an array of matched notification event objects.
  • Each event object contains the following parameters:
    • request (Object): The request payload. (For an EVENT trigger, this is the body of the event).
    • device (Object): The device object (Fields: email, id, owner, etc.).
    • sender (Object): The sender object (Fields: id, email, displayName, etc.).
    • group (Object, optional): The group object if present.
  • The function should return an array of event objects. To alter the body or subject of a template, modify the relevant entry in the array. To skip a notification, remove the relevant entry from the array.
  • The function may be called with multiple notification events. Note that only the returned entries will be sent, and the others will be skipped.

Example Script#

function processNotifications(events) {  // Example: skip notifications for a certain group, and add a note to the body  return events.filter(r => r.group == null || r.group.name !== "Admins")             .map(r => {                 r.request.additionalParameter = "Some value";                 return r;             });}

Notes on scripts#

When working with scripts, consider the following:

  • The event.request object can be modified and those changes will be available during template processing.
  • No other properties of the event objects should be modified.
  • Removing an entry from the returned array will skip sending that notification.
  • All parameters listed above are present in each request object.