core.js 3.53 KB
"use strict";

const strftime = require("strftime");
const config = require("./config");
const logger = require("./logger");

const bot = require("./xmpp");
const archive = require("./message-archive");
const forwarder = require("./evo-forwarder");

var isReady = false;

function generateGenericRefId(command) {
    return strftime("%Y%m%d") + '_' + command.code + '_' + command.destination;
}

function isTopupMessage(command) {
    if (!command.code) {
        return false;
    }

    if (command.code.search(/\d/) >= 0) {
        return true;
    }
    else {
        return false;
    }
}

function messageParser(partner, msg) {
    const command = {
        user_id: partner,
        password: null,
        code: null,
        destination: null,
        ref_id: null,
        raw: msg,
        isTopupMessage: null
    }

    const token = msg.split('.');

    command.code = token[0].toUpperCase();
    command.isTopupMessage = isTopupMessage(command);

    if (command.isTopupMessage) {
        logger.verbose("Assumpting message is topup request");
        command.destination = token[1];
        command.password = token[2];

        command.ref_id = generateGenericRefId(command);
        if (token[3]) {
            command.ref_id = command.ref_id + token[3];
        }
    }

    logger.verbose("Message parsed", {cmd: command});

    return command;
}

function sendImmediateReply(partner, msg) {
    const replyMessage = "Pesan anda telah diterima dan akan segera diproses: " + msg;
    bot.sendMessage(partner, replyMessage);
}

function sendNotReadyMessage(partner, msg) {
    if (!config.not_ready_message_prefix) {
        return;
    }
    const replyMessage = config.not_ready_message_prefix.trim() + ' ' + msg;
    bot.sendMessage(partner, replyMessage);
}

function onOnline() {
    const wait_secs = config.wait_after_online_secs || 60;

    logger.info('Bot is online, waiting for ' + wait_secs + " seconds before ready to processing message");
    setTimeout(
        function() {
            logger.info('Bot is ready to process message');
            isReady = true;
        },

        wait_secs * 1000
    )
}

function onMessage(partner, msg) {
    if (!msg) {
        return;
    }

    if (!isReady) {
        logger.info('Bot is not ready yet to process message');
        sendNotReadyMessage(partner, msg);
        return;
    }

    if (!archive.isReady()) {
        logger.info('Archive DB is not ready yet to process message');
        sendNotReadyMessage(partner, msg);
        return;
    }


    msg = msg.trim();

    logger.verbose('Going to process message', {partner: partner, msg: msg});
    sendImmediateReply(partner, msg);

    const command = messageParser(partner, msg);

    if (command.isTopupMessage) {
        onTopupMessage(command);
    } else {
        forwarder.toEvoClassic(command);
    }
}

function onTopupMessage(command) {
    archive.find(command, function(err, data) {
        if (err) {
            logger.warn('Fail on duplicate check', {err: err, command: command});
            return;
        }

        if (data) {
            logger.verbose('Duplicate trx, forwarding to status check', {command: command})

            if (config.check_status_on_dupe) {
                forwarder.toEvoH2HCheckStatus(command);
            }
            else {
                forwarder.toEvoH2HRequest(command);
            }

        }
        else {
            archive.insert(command, function(err) {
                forwarder.toEvoH2HRequest(command);
            });
        }

    });
}

exports.onMessage = onMessage;
exports.onOnline = onOnline;