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;
}