Commit 29a360eb6635af3c10c8ae8e77e83f888b7b33ac
1 parent
f343d0ee05
Exists in
master
delay dan isOnline
Showing 2 changed files with 50 additions and 27 deletions Side-by-side Diff
config.ini
index.js
1 | +var ym = require('yahoomessenger'); | |
1 | 2 | var http = require('http'); |
2 | 3 | var url = require('url'); |
3 | 4 | var winston = require('winston'); |
... | ... | @@ -15,6 +16,7 @@ var config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); |
15 | 16 | var keepalive_interval = 60 * 1000; |
16 | 17 | var last_message_hash = ''; |
17 | 18 | var log_level = 'info'; |
19 | +var isOnline = false; | |
18 | 20 | |
19 | 21 | if (config.globals.log_level) { |
20 | 22 | log_level = config.globals.log_level; |
... | ... | @@ -38,21 +40,28 @@ var logger = new (winston.Logger)({ |
38 | 40 | ] |
39 | 41 | }); |
40 | 42 | |
41 | - | |
42 | -var ym = require('yahoomessenger'); | |
43 | -ym.newInstance(); | |
44 | - | |
45 | 43 | function onReady(){ |
46 | 44 | ym.login(config.globals.username, config.globals.password); |
47 | 45 | } |
48 | 46 | |
49 | 47 | function onLoginSuccessful(data) { |
50 | 48 | logger.info('Login successful as ' + data.firstname + ' ' + data.lastname + ' (' + data.user_id + ')', {data: data}); |
49 | + isOnline = true; | |
51 | 50 | } |
52 | 51 | |
53 | 52 | function sendMessage(destination, message) { |
54 | - logger.info('Sending YM message', {from: config.globals.username, to: destination, message: message}); | |
55 | - ym.sendPM(destination, message); | |
53 | + if (isOnline) { | |
54 | + logger.info('Sending YM message', {from: config.globals.username, to: destination, message: message}); | |
55 | + ym.sendPM(destination, message); | |
56 | + } | |
57 | + else { | |
58 | + logger.warn('YM is not online yet, retrying to send message in 2 secs', {to: destination, msg: message}); | |
59 | + setTimeout( | |
60 | + sendMessage, | |
61 | + 2000, | |
62 | + destination, message | |
63 | + ) | |
64 | + } | |
56 | 65 | } |
57 | 66 | |
58 | 67 | function sendIgnoreResponse(destination, message) { |
... | ... | @@ -62,21 +71,21 @@ function sendIgnoreResponse(destination, message) { |
62 | 71 | function onPm(data) { |
63 | 72 | logger.info('Incoming message via YM', {data: data}); |
64 | 73 | var message = striptags(data.message); |
65 | - | |
74 | + | |
66 | 75 | var message_hash = data.sender + ': ' + data.message; |
67 | 76 | if (message_hash == last_message_hash) { |
68 | 77 | logger.warn('Ignoring duplicate message', {data: data}); |
69 | 78 | return; |
70 | 79 | } |
71 | 80 | last_message_hash = message_hash; |
72 | - | |
81 | + | |
73 | 82 | var greeting_prefix = "Pesan anda telah diterima dan akan segera diproses:"; |
74 | 83 | if (config.globals.greeting_prefix) { |
75 | 84 | greeting_prefix = config.globals.greeting_prefix; |
76 | 85 | } |
77 | 86 | |
78 | 87 | ym.sendPM(data.sender, greeting_prefix + ' ' + message); |
79 | - | |
88 | + | |
80 | 89 | forwardMessageToEvo(data.sender, message, formatTimestamp(data.time)); |
81 | 90 | } |
82 | 91 | |
... | ... | @@ -94,12 +103,12 @@ function onBuddyAddRequest(data) { |
94 | 103 | function onHttpIncomingMessage(request, response) { |
95 | 104 | var qs = url.parse(request.url, true).query; |
96 | 105 | logger.verbose("onHttpIncomingMessage()", {qs: qs}); |
97 | - | |
106 | + | |
98 | 107 | // abaikan balikan ping |
99 | 108 | if (qs.to == config.globals.ping_ym_id) { |
100 | 109 | return; |
101 | 110 | } |
102 | - | |
111 | + | |
103 | 112 | var destination = qs.to.replace(config.globals.msisdn_suffix, ''); |
104 | 113 | logger.info('Sending YM message from ' + config.globals.username + ' to ' + destination + ': ' + qs.msg); |
105 | 114 | sendMessage(destination, qs.msg); |
... | ... | @@ -125,43 +134,43 @@ function forwardMessageToEvo(sender, message, ts) { |
125 | 134 | ts: ts |
126 | 135 | } |
127 | 136 | }; |
128 | - | |
137 | + | |
129 | 138 | logger.verbose("Forwarding message to evo", {request_opts: opts}); |
130 | 139 | request(opts, function(err, response, body) { |
131 | 140 | if (err) { |
132 | 141 | logger.warn('Error forwarding to evo: ' + err); |
133 | 142 | return; |
134 | 143 | } |
135 | - | |
144 | + | |
136 | 145 | if (response.statusCode != 200) { |
137 | 146 | logger.warn('HTTP Status from evo: ' + response.statusCode, {status: response.statusCode, body: body}); |
138 | 147 | return; |
139 | 148 | } |
140 | - | |
149 | + | |
141 | 150 | logger.verbose('Got response from evo', {body: body}); |
142 | - | |
151 | + | |
143 | 152 | xmlparser(body, function(xmlerr, parsedResponse) { |
144 | 153 | if (xmlerr) { |
145 | 154 | logger.verbose('Evo response not in xml format'); |
146 | 155 | return; |
147 | 156 | } |
148 | - | |
149 | - return; | |
150 | - | |
157 | + | |
158 | + return; | |
159 | + | |
151 | 160 | logger.info('Evo response in xml format', {response: parsedResponse}); |
152 | 161 | if (parsedResponse.response.text) { |
153 | 162 | sendMessage(sender, parsedResponse.response.text[0].trim()); |
154 | 163 | } |
155 | 164 | }); |
156 | - | |
157 | - | |
165 | + | |
166 | + | |
158 | 167 | }); |
159 | - | |
160 | - | |
168 | + | |
169 | + | |
161 | 170 | // kirim ping 1 detik setelah pesan agar segera diproses |
162 | 171 | if (config.globals.send_ping_to_evo == '1') { |
163 | 172 | setTimeout(function() { |
164 | - | |
173 | + | |
165 | 174 | var pingOpts = { |
166 | 175 | url: config.globals.evo_url, |
167 | 176 | qs: { |
... | ... | @@ -171,9 +180,9 @@ function forwardMessageToEvo(sender, message, ts) { |
171 | 180 | ts: strftime('%F %T') |
172 | 181 | } |
173 | 182 | }; |
174 | - | |
183 | + | |
175 | 184 | logger.verbose('Sending ping message', {opts: pingOpts}); |
176 | - | |
185 | + | |
177 | 186 | request(pingOpts, function(err, response, body) { |
178 | 187 | if (err) { |
179 | 188 | logger.warn('Error send PING to evo: ' + err); |
... | ... | @@ -186,7 +195,7 @@ function forwardMessageToEvo(sender, message, ts) { |
186 | 195 | |
187 | 196 | function createHttpServer() { |
188 | 197 | logger.verbose('createHttpServer()'); |
189 | - | |
198 | + | |
190 | 199 | var httpServer = http.createServer(onHttpIncomingMessage); |
191 | 200 | httpServer.listen(config.globals.listen_port, function(){ |
192 | 201 | logger.info("HTTP server listening on " + config.globals.listen_port); |
... | ... | @@ -199,7 +208,12 @@ ym.on('loginSuccessful', onLoginSuccessful); |
199 | 208 | ym.on('pm', onPm); |
200 | 209 | ym.on('buddyAddRequest', onBuddyAddRequest); |
201 | 210 | |
202 | -setInterval(function() { | |
211 | +setTimeout(function() { | |
212 | + ym.newInstance, | |
213 | + 3000 | |
214 | +}) | |
215 | + | |
216 | +setInterval(function() { | |
203 | 217 | logger.verbose('Sending keepalive packet'); |
204 | 218 | ym.keepAlive(); |
205 | 219 | }, keepalive_interval); |