Webhook Sender

HEROWebhookSender subscribes to a named event a HERO object and posts every incoming metadata + payload as JSON to an arbitrary incoming-webhook endpoint. Since the request body is fully defined by a Jinja2 template, it works with any webhook-compatible platform (Slack, Mattermost, Discord, MS Teams, …).

Payload templates

The body_template is rendered against two variables:

payload

The event data.

metadata

The per-event metadata merged over default_metadata (per-event values take precedence on key collision).

The rendered string must be valid JSON — it is validated with json.loads before being sent. Note that Jinja2 does not escape interpolated values, so apply the tojson filter wherever a variable takes the position of a JSON value, otherwise payloads containing quotes or special characters will break the document:

{"text": {{ payload.message | tojson }}, "channel": "{{ channel }}"}

Usage

class herostools.actor.webhook.HEROWebhookSender(webhook_url: str, body_template: str, request_timeout: float = 10, *args, **kwargs)[source]

HERODataArchiver that renders metadata and payload into a JSON template and posts them to an arbitrary incoming-webhook endpoint.

Works with any webhook-compatible platform (Slack, Mattermost, Discord, MS Teams, …) since the payload format is fully defined by the template.

Parameters:
  • object_selector – Zenoh object selector for the devices to subscribe to.

  • event_name – Name of the event.

  • webhook_url – Full URL of the incoming webhook endpoint.

  • default_metadata – Default metadata merged with every incoming payload’s metadata.

  • body_template – Template for POST message body as JSON string rendered with Jinja2, using the payload and metadata as template variables (e.g. '{"text": {{ payload.value | tojson }}}'). Rendering payload only works, if it is json serializable.

  • max_retries – Number of times to retry a failed delivery before dropping the item.

  • request_timeout – Timeout in seconds for the POST request to the webhook endpoint.

BOSS configuration example

{
  "_id": "mattermost-notifier",
  "classname": "herostools.actor.webhook.HEROWebhookSender",
  "arguments": {
    "object_selector": "my-device",
    "event_name": "alarm",
    "webhook_url": "https://mattermost.example.com/hooks/abc123",
    "default_metadata": {
      "channel": "town-square"
    },
    "body_template": "{\"text\": {{ payload.message | tojson }}, \"channel\": {{ channel | tojson }}}"
  }
}

Error handling

Failures are handled exactly like in the HERODataArchiver: any failed delivery (including payloads whose template does not render to valid JSON) is logged with a warning and re-queued up to max_retries times. Once the limit is exceeded, the item is dropped with an error log.