response-server.js 1.1 KB
"use strict";

const http = require('http');
const url = require("url");

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

function createHttpServer() {
    const httpServer = http.createServer(function(request,response){
        const qs = url.parse(request.url, true).query;

        logger.verbose('Incoming message from EVO', {qs: qs});
        response.end('OK');

        const partner = qs.to || qs.user_id;
        const msg = qs.msg || qs.info;

        if (!partner || !msg) {
            logger.warn('Invalid partner or message from EVO', {qs: qs});
            return;
        }

        bot.sendMessage(partner, msg);

        if (qs.topup_status == 'R' && qs.ref_id) {
            logger.verbose('Got R status, deleting trx from archive');

            archive.remove(partner, qs.ref_id);
        }
    });

    httpServer.listen(config.response_server_listen_port, function(){
        logger.info("HTTP Response Server listening on port " + config.response_server_listen_port);
    })
}

createHttpServer();