zabbix_export:
  version: '7.0'
  media_types:
    -
      name: Line
      type: WEBHOOK
      parameters:
        -
          name: alert_message
          value: '{ALERT.MESSAGE}'
        -
          name: alert_subject
          value: '{ALERT.SUBJECT}'
        -
          name: bot_token
          value: '<PLACE BOT TOKEN>'
        -
          name: event_id
          value: '{EVENT.ID}'
        -
          name: event_nseverity
          value: '{EVENT.NSEVERITY}'
        -
          name: event_source
          value: '{EVENT.SOURCE}'
        -
          name: event_update_status
          value: '{EVENT.UPDATE.STATUS}'
        -
          name: event_value
          value: '{EVENT.VALUE}'
        -
          name: send_to
          value: '{ALERT.SENDTO}'
        -
          name: trigger_description
          value: '{TRIGGER.DESCRIPTION}'
        -
          name: trigger_id
          value: '{TRIGGER.ID}'
        -
          name: zabbix_url
          value: '{$ZABBIX.URL}'
      script: |
        function sendMessage(to, messages, params) {
            var response,
                request = new HttpRequest();
        
            if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
                request.setProxy(params.HTTPProxy);
            }
        
            request.addHeader('Content-Type: application/json');
            request.addHeader('Authorization: Bearer ' + params.bot_token);
        
            response = request.post('https://api.line.me/v2/bot/message/push', JSON.stringify({
                "to": to,
                "messages": messages
            }));
        
            if (request.getStatus() !== 200) {
                throw response;
            }
        }
        
        function validateParams(params) {
            if (typeof params.bot_token !== 'string' || params.bot_token.trim() === '') {
                throw 'Field "bot_token" cannot be empty';
            }
        
            if ([0, 1, 2, 3].indexOf(parseInt(params.event_source)) === -1) {
                throw 'Incorrect "event_source" parameter given: "' + params.event_source + '".\nMust be 0-3.';
            }
        
            if (params.event_source !== '0') {
                params.event_nseverity = '0';
                params.event_severity = 'Not classified';
                params.event_update_status = '0';
            }
        
            if (params.event_source === '1' || params.event_source === '2') {
                params.event_value = '1';
            }
        
            if ([0, 1, 2, 3, 4, 5].indexOf(parseInt(params.event_nseverity)) === -1) {
                throw 'Incorrect "event_nseverity" parameter given: ' + params.event_nseverity + '\nMust be 0-5.';
            }
        
            if (params.event_update_status !== '0' && params.event_update_status !== '1') {
                throw 'Incorrect "event_update_status" parameter given: ' + params.event_update_status + '\nMust be 0 or 1.';
            }
        
            if (params.event_value !== '0' && params.event_value !== '1') {
                throw 'Incorrect "event_value" parameter given: ' + params.event_value + '\nMust be 0 or 1.';
            }
        
            if (isNaN(params.trigger_id) && params.event_source === '0') {
                throw 'field "trigger_id" is not a number';
            }
        
            if (typeof params.zabbix_url !== 'string' || params.zabbix_url.trim() === '') {
                throw 'Field "zabbix_url" cannot be empty';
            }
        
            if (!/^(http|https):\/\/.+/.test(params.zabbix_url)) {
                throw 'Field "zabbix_url" must contain a schema';
            }
        }
        
        function getZabbixProblemLink(params) {
            return params.zabbix_url + (params.zabbix_url.endsWith('/') ? '' : '/') + 'tr_events.php?triggerid=' + params.trigger_id + '&eventid=' + params.event_id;    
        }
        
        try {
            var params = JSON.parse(value);
        
            validateParams(params);
        
            severity_emoji = [
                "\u26AA",
                "\uD83D\uDD35",
                "\uD83D\uDFE4",
                "\uD83D\uDFE1",
                "\uD83D\uDFE0",
                "\uD83D\uDD34",
            ];
        
            // If its a trigger and a recovery operation and not an update operation.
            if (params.event_source === '0' && params.event_value === '0' && params.event_update_status === '0') {
                var line_message = [
                    {
                        "type": "text",
                        "text": '\u2705 ' + params.alert_subject + '\n\n' + params.alert_message + '\n' + params.trigger_description + '\n\n' + getZabbixProblemLink(params)
                    }
                ];
            }
            // If its a trigger and its a problem.
            else if (params.event_source === '0' && params.event_value === '1') {
                var line_message = [
                    {
                        "type": "text",
                        "text": severity_emoji[params.event_nseverity] + ' ' + params.alert_subject + '\n\n' + params.alert_message + '\n' + params.trigger_description + '\n\n' + getZabbixProblemLink(params)
                    }
                ];
            }
            // If its a trigger and its an update operation.
            else if (params.event_source === '0' && params.event_update_status === '1') {
                var line_message = [
                    {
                        "type": "text",
                        "text": '\u2733 ' + severity_emoji[params.event_nseverity] + ' ' + params.alert_subject + '\n\n' + params.alert_message + '\n' + params.trigger_description + '\n\n' + getZabbixProblemLink(params)
                    }
                ];
            }
            else {
                var line_message = [
                    {
                        "type": "text",
                        "text": params.alert_subject + '\n\n' + params.alert_message + '\n'
                    }
                ];
            }
        
            sendMessage(params.send_to, line_message, params);
        
            return 'OK';
        }
        catch (err) {
            Zabbix.log(4, '[ Line Webhook ] Line notification failed : ' + err);
            throw 'Line notification failed : ' + err;
        }
      description: |
        Please refer to setup guide here: https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/media/line
        
        Set bot_token parameter to your Line bot token.
        When assigning Line media to the Zabbix user - add the ID of the target recipient. Use a userId, groupId, or roomId value.
      message_templates:
        -
          event_source: TRIGGERS
          operation_mode: PROBLEM
          subject: 'Problem: {EVENT.NAME}'
          message: |
            Problem started at {EVENT.TIME} on {EVENT.DATE}
            Problem name: {EVENT.NAME}
            Host: {HOST.NAME}
            Severity: {EVENT.SEVERITY}
            Operational data: {EVENT.OPDATA}
            Original problem ID: {EVENT.ID}
        -
          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}
            Problem name: {EVENT.NAME}
            Problem duration: {EVENT.DURATION}
            Host: {HOST.NAME}
            Severity: {EVENT.SEVERITY}
            Original problem ID: {EVENT.ID}
        -
          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}
            
            Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
        -
          event_source: DISCOVERY
          operation_mode: PROBLEM
          subject: 'Discovery: {DISCOVERY.DEVICE.STATUS} {DISCOVERY.DEVICE.IPADDRESS}'
          message: |
            Discovery rule: {DISCOVERY.RULE.NAME}
            
            Device IP: {DISCOVERY.DEVICE.IPADDRESS}
            Device DNS: {DISCOVERY.DEVICE.DNS}
            Device status: {DISCOVERY.DEVICE.STATUS}
            Device uptime: {DISCOVERY.DEVICE.UPTIME}
            
            Device service name: {DISCOVERY.SERVICE.NAME}
            Device service port: {DISCOVERY.SERVICE.PORT}
            Device service status: {DISCOVERY.SERVICE.STATUS}
            Device service uptime: {DISCOVERY.SERVICE.UPTIME}
        -
          event_source: AUTOREGISTRATION
          operation_mode: PROBLEM
          subject: 'Autoregistration: {HOST.HOST}'
          message: |
            Host name: {HOST.HOST}
            Host IP: {HOST.IP}
            Agent port: {HOST.PORT}
