zabbix_export:
  version: "6.0"
  media_types:
    - name: Freesbe
      type: WEBHOOK
      parameters:
        - name: freesbe_url
          value: "{ALERT.SENDTO}"
        - name: event_id
          value: "{EVENT.ID}"
        - name: event_source
          value: "{EVENT.SOURCE}"
        - name: event_value
          value: "{EVENT.VALUE}"
        - name: event_update_status
          value: "{EVENT.UPDATE.STATUS}"
        - name: event_update_action
          value: "{EVENT.UPDATE.ACTION}"
        - name: event_update_message
          value: "{EVENT.UPDATE.MESSAGE}"
        - name: event_update_user
          value: "{USER.FULLNAME}"
        - name: event_ack_status
          value: "{EVENT.ACK.STATUS}"
        - name: event_name
          value: "{EVENT.NAME}"
        - name: event_recovery_name
          value: "{EVENT.RECOVERY.NAME}"
        - name: event_severity
          value: "{EVENT.SEVERITY}"
        - name: event_nseverity
          value: "{EVENT.NSEVERITY}"
        - name: event_opdata
          value: "{EVENT.OPDATA}"
        - name: event_tags
          value: "{EVENT.TAGSJSON}"
        - name: event_duration
          value: "{EVENT.DURATION}"
        - name: host_name
          value: "{HOST.NAME}"
        - name: trigger_id
          value: "{TRIGGER.ID}"
        - name: zabbix_url
          value: "{$ZABBIX.URL}"
        - name: http_proxy
          value: ""
      script: |
        // Sends the event to Freesbe, which posts it as a card in a channel and keeps the card
        // current as the problem is acknowledged and resolved. https://freesbe.net/docs/zabbix/
        var params = JSON.parse(value);
        var url = params.freesbe_url;
        if (typeof url !== 'string' || url.indexOf('https://') !== 0 || url.indexOf('/hooks/monitoring/zabbix/') === -1) {
          throw 'Put the Freesbe URL from Workspace settings, Integrations, Monitoring in the user media "Send to" field.';
        }
        var proxy = params.http_proxy;
        delete params.freesbe_url;
        delete params.http_proxy;
        var request = new HttpRequest();
        if (typeof proxy === 'string' && proxy.trim() !== '') {
          request.setProxy(proxy.trim());
        }
        request.addHeader('Content-Type: application/json');
        request.addHeader('User-Agent: Zabbix-Freesbe/1.0');
        var response = request.post(url, JSON.stringify(params));
        var status = request.getStatus();
        Zabbix.log(4, '[ Freesbe webhook ] answered ' + status + ': ' + response);
        if (status === 404) {
          throw 'Freesbe doesn\'t know this URL. The connection may have been deleted or turned off.';
        }
        if (status < 200 || status >= 300) {
          throw 'Freesbe answered ' + status + ': ' + response;
        }
        return 'OK';
      timeout: 10s
      process_tags: "NO"
      show_event_menu: "NO"
      description: |
        Posts Zabbix problems into a Freesbe channel as cards that turn green when the problem
        is resolved.

        1. In Freesbe, open Workspace settings, then Integrations, then Monitoring, and connect
           Zabbix. Copy the URL it shows.
        2. Add this media type to a user (for example, a "Freesbe" user with read access to the
           hosts you want), and paste the URL in "Send to".
        3. In Alerts, then Actions, then Trigger actions, send to that user on Problem, Recovery
           and Update operations.

        Optional: set the global macro {$ZABBIX.URL} to your frontend's address so cards link to
        each problem.
      message_templates:
        - event_source: TRIGGERS
          operation_mode: PROBLEM
          subject: "Problem: {EVENT.NAME}"
          message: "Problem started at {EVENT.TIME} on {EVENT.DATE} on {HOST.NAME}: {EVENT.NAME} ({EVENT.SEVERITY})"
        - event_source: TRIGGERS
          operation_mode: RECOVERY
          subject: "Resolved in {EVENT.DURATION}: {EVENT.NAME}"
          message: "Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE} on {HOST.NAME}: {EVENT.NAME}"
        - event_source: TRIGGERS
          operation_mode: UPDATE
          subject: "Updated problem in {EVENT.AGE}: {EVENT.NAME}"
          message: "{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}. {EVENT.UPDATE.MESSAGE}"
