evo-forwarder.js 3.65 KB
"use strict";

const request = require("request");
const strftime = require('strftime');
const xmlparser = require('xml2js').parseString;

const config = require("./config");
const logger = require("./logger");
const bot = require("./xmpp");
const archive = require("./message-archive");

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

function toEvo(opts, cb) {
    logger.verbose('Requesting to EVO', {opts});
    request(opts, function(err, response, body) {
        if (err) {
            logger.warn('Error forwarding to evo', {err: err});

            if (cb) { cb(err); }
            return;
        }

        if (response.statusCode != 200) {
            logger.warn('Error forwarding to evo', {http_status: response.statusCode, body: body});

            if (cb) { cb('ER_HTTP_STATUS_IS_NOT_200', response, body); }
            return;
        }

        if (config.debug_evo_direct_response) {
            logger.verbose('Got EVO direct response', {http_status: response.statusCode, body: body});
        }

        if (cb) { cb(null, response, body); }
    })
}

function toEvoClassic(command) {
    const opts = {
        url: config.evo_classic_url,
        qs: {
            msg: command.raw,
            msisdn: command.user_id,
            smsc: config.smsc,
            ts: strftime('%F %T'),
        }
    };

    toEvo(opts);
}

function toEvoH2H(command, url, cb) {
    const opts = {
        url: url,
        qs: {
            code: command.code,
            msisdn: command.destination,
            user_id: command.user_id,
            password: command.password,
            ref_id: command.ref_id,
            smsc: config.smsc
        }
    };

    toEvo(opts, function(err, response, body) {
        if (err || response.statusCode != 200) {
            if (cb) { cb(err || 'ER_HTTP_STATUS_IS_NOT_200'); }
            return;
        }

        xmlparser(body, function(xmlParserError, data) {
            if (xmlParserError) {
                logger.warn('XML Parser error', {err: xmlParserError, body: body});

                if (cb) { cb('ER_XML_PARSER_ERROR'); }
                return;
            }

            const request_status = getDirectAckFieldValue(data, "request_status");
            const info = getDirectAckFieldValue(data, "info");

            if (config.forward_direct_reply_on_error && request_status && info && (request_status != 'OK')) {
                logger.warn('Evo direct response request status is not "OK"', {direct_response: data})
                bot.sendMessage(command.user_id, request_status + ': ' + info)
            }

            if (cb) { cb(null, data); }
        })
    });
}

function getDirectAckFieldValue(response, field) {
    try {
        return response.direct_ack[field][0];
    }
    catch(e) {
        return;
    }
}

function toEvoH2HRequest(command) {
    toEvoH2H(command, config.evo_h2h_request_url, function(err, data) {
        if (err) {
            return;
        }

        if (getDirectAckFieldValue(data, "code") == '01') {
            toEvoH2HCheckStatus(command);
        }
    });
}

function toEvoH2HCheckStatus(command) {
    toEvoH2H(command, config.evo_h2h_status_url, function(err, data) {
        if (err) {
            return;
        }

        const info = getDirectAckFieldValue(data, "info");
        if (info) {
            bot.sendMessage(command.user_id, info);
        }

        if (getDirectAckFieldValue(data, "topup_status") == "R") {
            logger.verbose('Got R status, deleting trx from archive');
            archive.remove(command.user_id, command.ref_id);
        }
    });
}

exports.toEvoClassic = toEvoClassic;
exports.toEvoH2HRequest = toEvoH2HRequest;
exports.toEvoH2HCheckStatus = toEvoH2HCheckStatus;