Commit 6222f4f937b2cad7b014602737871f1735a6fd3e
1 parent
c8ddf66255
Exists in
master
terminate on error
Showing 1 changed file with 12 additions and 13 deletions Side-by-side Diff
index.js
... | ... | @@ -37,7 +37,7 @@ var logger = new (winston.Logger)({ |
37 | 37 | |
38 | 38 | function xmppConnect() { |
39 | 39 | logger.info('Connecting to XMPP server'); |
40 | - | |
40 | + | |
41 | 41 | xmpp.connect({ |
42 | 42 | jid: config.globals.jid, |
43 | 43 | password: config.globals.password, |
... | ... | @@ -50,24 +50,23 @@ function xmppConnect() { |
50 | 50 | function onHttpIncomingMessage(request, response) { |
51 | 51 | var qs = url.parse(request.url, true).query; |
52 | 52 | logger.verbose("onHttpIncomingMessage()", {qs: qs}); |
53 | - | |
53 | + | |
54 | 54 | //logger.info('Sending xmpp message from ' + config.globals.jid + ' to ' + qs.to + ': ' + qs.msg); |
55 | 55 | sendMessage(qs.to, qs.msg); |
56 | 56 | response.end('OK'); |
57 | 57 | } |
58 | 58 | |
59 | - | |
60 | 59 | function createHttpListener() { |
61 | 60 | |
62 | 61 | var httpServer = http.createServer(function(request,response){ |
63 | - | |
62 | + | |
64 | 63 | var qs = url.parse(request.url, true).query; |
65 | 64 | logger.verbose('Incoming message from EVO', {qs: qs}); |
66 | 65 | response.end('OK'); |
67 | - | |
66 | + | |
68 | 67 | sendMessage(qs.to, qs.msg); |
69 | 68 | }); |
70 | - | |
69 | + | |
71 | 70 | httpServer.listen(config.globals.listen_port, function(){ |
72 | 71 | logger.info("HTTP server listening on " + config.globals.listen_port); |
73 | 72 | }) |
... | ... | @@ -88,7 +87,7 @@ function reportToEvo(from, message) { |
88 | 87 | ts: strftime('%F %T'), |
89 | 88 | } |
90 | 89 | }; |
91 | - | |
90 | + | |
92 | 91 | logger.verbose("Forwarding message to evo", {request_opts: opts}); |
93 | 92 | request(opts, function(err, response, body) { |
94 | 93 | if (err) { |
... | ... | @@ -97,7 +96,6 @@ function reportToEvo(from, message) { |
97 | 96 | return; |
98 | 97 | } |
99 | 98 | }); |
100 | - | |
101 | 99 | } |
102 | 100 | |
103 | 101 | xmpp.on('close', function() { |
... | ... | @@ -108,7 +106,7 @@ xmpp.on('close', function() { |
108 | 106 | xmpp.on('online', function(data) { |
109 | 107 | logger.info('XMPP online', {data: data}); |
110 | 108 | logger.info('Connected with JID: ' + data.jid.user); |
111 | - | |
109 | + | |
112 | 110 | setTimeout(function() { |
113 | 111 | warming_up = false; |
114 | 112 | logger.info('BOT selesai warming up, pesan-pesan selanjutnya akan diproses'); |
... | ... | @@ -117,22 +115,23 @@ xmpp.on('online', function(data) { |
117 | 115 | |
118 | 116 | xmpp.on('chat', function(from, message) { |
119 | 117 | logger.info('Incoming message via XMPP ', {to: config.globals.jid, from: from, message: message}); |
120 | - | |
118 | + | |
121 | 119 | if (warming_up) { |
122 | 120 | logger.info('BOT masih dalam tahap warming up, abaikan pesan'); |
123 | 121 | var response_message = 'Pesan diabaikan. Silahkan diulang kembali: ' + message; |
124 | 122 | sendMessage(from, response_message); |
125 | 123 | return; |
126 | 124 | } |
127 | - | |
125 | + | |
128 | 126 | var response_message = 'Pesan anda telah diterima dan akan segera diproses: ' + message; |
129 | 127 | sendMessage(from, response_message); |
130 | - | |
128 | + | |
131 | 129 | reportToEvo(from, message); |
132 | 130 | }); |
133 | 131 | |
134 | 132 | xmpp.on('error', function(err) { |
135 | - logger.warn('XMPP error', {err: err}); | |
133 | + logger.warn('XMPP error, terminating in 3 secs', {err: err}); | |
134 | + setTimeout(process.exit, 3000, 1); | |
136 | 135 | }); |
137 | 136 | |
138 | 137 | xmpp.on('subscribe', function(from) { |