From 6abb33e39cacc42d658b089116d7970e302382d2 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Tue, 30 Jul 2019 16:56:40 +0700 Subject: [PATCH] Incoming SMS from with + --- lib/report-sender.js | 11 +++++++++++ lib/sms.js | 3 ++- test/sms.js | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/report-sender.js b/lib/report-sender.js index 1f604a7..c79f9ff 100644 --- a/lib/report-sender.js +++ b/lib/report-sender.js @@ -14,12 +14,23 @@ function incomingSMS(message) { throw e; } + if (!message.metadata.from) { + const e = new Error('Missing metadata.from on incoming sms'); + e.smsObj = message; + throw e; + } + if (!message.message) { const e = new Error('Missing message on incoming sms'); e.smsObj = message; throw e; } + if (message.metadata.from.indexOf('+') !== 0) { + logger.verbose('Do not send report on incoming message when sender does not start with "+".', { from: message.metadata.from, msg: message.message }); + return; + } + const requestOptions = { url: config.report_url.incoming_sms, qs: { diff --git a/lib/sms.js b/lib/sms.js index 1d0fa93..c64396d 100644 --- a/lib/sms.js +++ b/lib/sms.js @@ -22,7 +22,8 @@ function extract(raw) { [result.metadata.raw] = lines; result.metadata.status = typeof metadata[0] === 'string' ? metadata[0].replace(/^.+: /, '') : ''; - result.metadata.from = typeof metadata[1] === 'string' ? metadata[1].replace(/^\+/, '') : ''; + // result.metadata.from = typeof metadata[1] === 'string' ? metadata[1].replace(/^\+/, '') : ''; + result.metadata.from = metadata[1]; const tsFromRaw = `${metadata[3]} ${(metadata[4] || '').replace(/\+\d+/, '')}`; result.metadata.ts = moment(tsFromRaw, 'YY/MM/DD HH:mm:ss').format('YYYY-MM-DD HH:mm:ss'); diff --git a/test/sms.js b/test/sms.js index 28c238a..0adcaa0 100644 --- a/test/sms.js +++ b/test/sms.js @@ -17,7 +17,7 @@ describe('#sms', () => { it('should return correct metadata', () => { messageParsed.metadata.status.should.equal('REC UNREAD'); - messageParsed.metadata.from.should.equal('6282210008543'); + messageParsed.metadata.from.should.equal('+6282210008543'); messageParsed.metadata.ts.should.equal('2019-07-26 13:21:16'); }); -- 1.9.0