index.js
3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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();