index.js 3.01 KB
var request = require('request');
var http = require('http');
var url = require('url');
var fs = require('fs');
var ini = require('ini');
var strftime = require('strftime');
var xmpp = require('simple-xmpp');
var winston = require('winston');

var warming_up = true;

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({
      timestamp: function() {
        return strftime('%F %T', new Date());
      }
    }),
    new (winston.transports.DailyRotateFile)({
      filename: 'log',
      timestamp: function() {
        return strftime('%F %T', new Date());
      }
    })
  ]
});

var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));

function createHttpListener() {

    var httpServer = http.createServer(function(request,response){

    var qs = url.parse(request.url, true).query;
        logger.info('Incoming message from SMSIN server', {qs: qs});
        response.end('OK');

        sendMessage(qs.PhoneNumber, qs.text);
    });

    httpServer.listen(config.globals.listen_port, function(){
        logger.info("HTTP server listening on " + config.globals.listen_port);
    })
}

function sendMessage(destination, message) {
    logger.info('Sending xmpp message', {destination: destination, message: message});
    xmpp.send(destination, message);
}

function reportToSMSIN(from, message) {
    var opts = {
        url: config.globals.smsin,
        qs: {
            PhoneNumber: from,
            Text: message,
            Res_Port: config.globals.listen_port,
            SMSCID: config.globals.smscid
        }
    }

    request(opts, function(err, response, body) {
        if (err) {
            logger.warn('Error reporting to SMSIN: ' + err);
            return;
        }
    });
}

xmpp.on('online', function(data) {
    logger.info('XMPP online', {data: data});
    logger.info('Connected with JID: ' + data.jid.user);

    setTimeout(function() {
        warming_up = false;
        logger.info('BOT selesai warming up, pesan-pesan selanjutnya akan diproses');
    }, config.globals.warming_up);
});

xmpp.on('chat', function(from, message) {
    logger.info('Incoming message via XMPP ', {from: from, message: message});

    if (warming_up) {
        logger.info('BOT masih dalam tahap warming up, abaikan pesan');
        var response_message = 'Pesan diabaikan. Silahkan diulang kembali: ' + message;
        sendMessage(from, response_message);
        return;
    }

    var response_message = 'Pesan anda telah diterima dan akan segera diproses: ' + message;
    sendMessage(from, response_message);

    reportToSMSIN(from, message);
});

xmpp.on('error', function(err) {
    logger.warn('XMPP error, terminating in 3 secs', {err: err});
    setTimeout(process.exit, 3000, 1);
});

xmpp.on('subscribe', function(from) {
    logger.warn('XMPP subscribe request from ' + from);
    xmpp.acceptSubscription(from);
});

xmpp.connect({
        jid: config.globals.jid,
        password: config.globals.password,
});

// check for incoming subscription requests
xmpp.getRoster();

createHttpListener();