Commit 76a4bcfcce7f3195e230f4dc9344cb709e2ca43e
1 parent
1c0e584d58
Exists in
master
Adaptasi modem baru
Showing 7 changed files with 147 additions and 54 deletions Side-by-side Diff
lib/apiserver/index.js
... | ... | @@ -76,7 +76,9 @@ function onIncomingSms(req, res) { |
76 | 76 | }); |
77 | 77 | */ |
78 | 78 | |
79 | - logger.info('APISERVER: Incoming SMS', { modem: req.query.modem, from: req.query.number, from_with_suffix: numberWithSuffix, msg: req.query.msg }); | |
79 | + const doNotForwardToCore = (req.query.number.search(/(\+)*62/) !== 0) || (req.query.number.length <= 8); | |
80 | + logger.info('APISERVER: Incoming SMS', { modem: req.query.modem, from: req.query.number, from_with_suffix: numberWithSuffix, msg: req.query.msg, doNotForwardToCore }); | |
81 | + | |
80 | 82 | messagingService.onIncomingMessage({ |
81 | 83 | me: req.query.modem, |
82 | 84 | partner: numberWithSuffix, |
... | ... | @@ -85,7 +87,7 @@ function onIncomingSms(req, res) { |
85 | 87 | origin_label: req.query.modem_imsi || 'UNKNOWN', |
86 | 88 | origin_transport: 'SMS', |
87 | 89 | origin_partner: req.query.number, |
88 | - do_not_forward_to_core: (req.query.number.indexOf('+62') !== 0) || (req.query.number.length <= 8), | |
90 | + do_not_forward_to_core: doNotForwardToCore, | |
89 | 91 | }); |
90 | 92 | } |
91 | 93 |
lib/transport.js
1 | 1 | "use strict"; |
2 | 2 | |
3 | -const MAX_SMS_LENGTH = 160; | |
3 | +const MAX_SMS_LENGTH = 140; | |
4 | 4 | |
5 | 5 | const url = require('url'); |
6 | 6 | const request = require('request'); |
... | ... | @@ -18,14 +18,20 @@ const modemChooser = require('./modem-chooser'); |
18 | 18 | // const partnerLastSeen = require('./partner-last-seen'); |
19 | 19 | const history = require('./history'); |
20 | 20 | const prefixes = require('./prefixes'); |
21 | +const truncate = require('./truncate-paragraph'); | |
21 | 22 | |
22 | 23 | function _send(destinationNumber, msg, handlerIMSI) { |
23 | 24 | |
24 | - if (msg.length > 160) { | |
25 | + if (msg.length > MAX_SMS_LENGTH) { | |
25 | 26 | logger.info('Splitting message'); |
26 | 27 | |
28 | + /* | |
27 | 29 | const newMsg = msg.slice(0, MAX_SMS_LENGTH); |
28 | 30 | const remainingMsg = msg.slice(MAX_SMS_LENGTH); |
31 | + */ | |
32 | + | |
33 | + const [newMsg, remainingMsg] = truncate(msg, MAX_SMS_LENGTH); | |
34 | + logger.verbose('TRANSPORT: Truncate long message', {maxLength: MAX_SMS_LENGTH, original: msg, head: newMsg, tail: remainingMsg}); | |
29 | 35 | |
30 | 36 | _send(destinationNumber, newMsg, handlerIMSI); |
31 | 37 | setTimeout(() => { |
lib/truncate-paragraph/index.js
... | ... | @@ -0,0 +1,30 @@ |
1 | +const DEBUG = process.env['DEBUG']; | |
2 | + | |
3 | +const validator = require('./validator'); | |
4 | + | |
5 | +function reverseString(str) { | |
6 | + return str.split('').reverse().join(''); | |
7 | +} | |
8 | + | |
9 | +module.exports = (src, maxLength) => { | |
10 | + const head = src.slice(0, maxLength); | |
11 | + const tail = src.slice(maxLength); | |
12 | + | |
13 | + if (validator(head, tail)) { | |
14 | + if (DEBUG) console.log(`TRUNCATE-PARAGRAPH: straight result src='${src}' maxLength=${maxLength}`); | |
15 | + return [head.trim(), tail.trim()]; | |
16 | + } | |
17 | + | |
18 | + const reverseI = reverseString(head).search(/[\n., ;-]/); | |
19 | + if (reverseI === -1) { | |
20 | + // if (DEBUG) console.log(`TRUNCATE-PARAGRAPH: out of boundary src='${src}' maxLength=${maxLength}`); | |
21 | + return [head.trim(), tail.trim()]; | |
22 | + } | |
23 | + | |
24 | + const i = src.slice(0, maxLength - reverseI).trim().length; | |
25 | + // if (DEBUG) console.log(`TRUNCATE-PARAGRAPH: calculated src='${src}' maxLength=${maxLength} i=${i}`); | |
26 | + return [ | |
27 | + src.slice(0, i).trim(), | |
28 | + src.slice(i).trim(), | |
29 | + ] | |
30 | +} |
lib/truncate-paragraph/validator.js
... | ... | @@ -0,0 +1,11 @@ |
1 | +function isSeparator(char) { | |
2 | + return char.search(/[\n., ;]/) === 0; | |
3 | +} | |
4 | + | |
5 | +function isSeparatorHead(char) { | |
6 | + return char.search(/-/) === 0 || isSeparator(char) | |
7 | +} | |
8 | + | |
9 | +module.exports = (head, tail) => { | |
10 | + return isSeparatorHead(head[head.length - 1]) || isSeparator(tail[0]); | |
11 | +}; |
test/modems.js
... | ... | @@ -1,50 +0,0 @@ |
1 | -"use strict"; | |
2 | - | |
3 | -/* global describe it */ | |
4 | - | |
5 | -const should = require('should'); | |
6 | - | |
7 | -const modemSelect = require('../lib/modemSelect'); | |
8 | - | |
9 | -describe('#modemSelect', function() { | |
10 | - | |
11 | - describe('#getModemUrl', function() { | |
12 | - | |
13 | - const modemsConfig = { | |
14 | - SMS0: { | |
15 | - url: "http://localhost:8888/" | |
16 | - } | |
17 | - } | |
18 | - | |
19 | - it('should return correct url', function() { | |
20 | - modemSelect.getModemUrl('SMS0', modemsConfig).should.equal('http://localhost:8888/'); | |
21 | - }) | |
22 | - | |
23 | - it ('should handle missing modems', function() { | |
24 | - should.not.exists(modemSelect.getModemUrl('SMS0', null)); | |
25 | - should.not.exists(modemSelect.getModemUrl('SMS0', {})); | |
26 | - should.not.exists(modemSelect.getModemUrl('SMS1', modemsConfig)); | |
27 | - }) | |
28 | - }) | |
29 | - | |
30 | - describe('#removeSuffixFromNumber', function() { | |
31 | - const config = { | |
32 | - number_suffix: '@phonenumber' | |
33 | - } | |
34 | - | |
35 | - it('should return correct number', function() { | |
36 | - modemSelect.removeSuffixFromNumber('08181234@phonenumber', config).should.equal('08181234'); | |
37 | - }) | |
38 | - | |
39 | - it ('should return correct number without suffix in the number', function() { | |
40 | - modemSelect.removeSuffixFromNumber('08181234', config).should.equal('08181234'); | |
41 | - }) | |
42 | - | |
43 | - it ('should return correct number without suffix in config', function() { | |
44 | - modemSelect.removeSuffixFromNumber('08181234', null).should.equal('08181234'); | |
45 | - modemSelect.removeSuffixFromNumber('08181234', {}).should.equal('08181234'); | |
46 | - modemSelect.removeSuffixFromNumber('08181234@phonenumber', {}).should.equal('08181234'); | |
47 | - }) | |
48 | - }) | |
49 | - | |
50 | -}) | |
51 | 0 | \ No newline at end of file |
test/modems.js.old
... | ... | @@ -0,0 +1,50 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +/* global describe it */ | |
4 | + | |
5 | +const should = require('should'); | |
6 | + | |
7 | +const modemSelect = require('../lib/modemSelect'); | |
8 | + | |
9 | +describe('#modemSelect', function() { | |
10 | + | |
11 | + describe('#getModemUrl', function() { | |
12 | + | |
13 | + const modemsConfig = { | |
14 | + SMS0: { | |
15 | + url: "http://localhost:8888/" | |
16 | + } | |
17 | + } | |
18 | + | |
19 | + it('should return correct url', function() { | |
20 | + modemSelect.getModemUrl('SMS0', modemsConfig).should.equal('http://localhost:8888/'); | |
21 | + }) | |
22 | + | |
23 | + it ('should handle missing modems', function() { | |
24 | + should.not.exists(modemSelect.getModemUrl('SMS0', null)); | |
25 | + should.not.exists(modemSelect.getModemUrl('SMS0', {})); | |
26 | + should.not.exists(modemSelect.getModemUrl('SMS1', modemsConfig)); | |
27 | + }) | |
28 | + }) | |
29 | + | |
30 | + describe('#removeSuffixFromNumber', function() { | |
31 | + const config = { | |
32 | + number_suffix: '@phonenumber' | |
33 | + } | |
34 | + | |
35 | + it('should return correct number', function() { | |
36 | + modemSelect.removeSuffixFromNumber('08181234@phonenumber', config).should.equal('08181234'); | |
37 | + }) | |
38 | + | |
39 | + it ('should return correct number without suffix in the number', function() { | |
40 | + modemSelect.removeSuffixFromNumber('08181234', config).should.equal('08181234'); | |
41 | + }) | |
42 | + | |
43 | + it ('should return correct number without suffix in config', function() { | |
44 | + modemSelect.removeSuffixFromNumber('08181234', null).should.equal('08181234'); | |
45 | + modemSelect.removeSuffixFromNumber('08181234', {}).should.equal('08181234'); | |
46 | + modemSelect.removeSuffixFromNumber('08181234@phonenumber', {}).should.equal('08181234'); | |
47 | + }) | |
48 | + }) | |
49 | + | |
50 | +}) | |
0 | 51 | \ No newline at end of file |
test/truncate-paragraph.js
... | ... | @@ -0,0 +1,44 @@ |
1 | +/* global describe it */ | |
2 | + | |
3 | +require('should'); | |
4 | +const validator = require('../lib/truncate-paragraph/validator'); | |
5 | +const truncate = require('../lib/truncate-paragraph'); | |
6 | + | |
7 | + | |
8 | +describe('#truncate-paragraph', () => { | |
9 | + | |
10 | + describe('#validator', () => { | |
11 | + it('should return correctly', () => { | |
12 | + validator('ada', 'aja').should.equal(false); | |
13 | + validator('ada', ' aja').should.equal(true); | |
14 | + validator('ada-', 'aja').should.equal(true); | |
15 | + }); | |
16 | + }) | |
17 | + | |
18 | + it('should return correctly - 1', () => { | |
19 | + const [head, tail] = truncate('12345 12', 5); | |
20 | + | |
21 | + // console.log([head, tail]); | |
22 | + | |
23 | + head.should.be.ok(); | |
24 | + tail.should.be.ok(); | |
25 | + }); | |
26 | + | |
27 | + it('should return correctly - 2', () => { | |
28 | + truncate('12345 123', 5)[0].should.equal('12345'); | |
29 | + truncate('12345 123', 5)[1].should.equal('123'); | |
30 | + | |
31 | + truncate('12345 789', 7)[0].should.equal('12345', 'head 12345 789, 7'); | |
32 | + truncate('12345 789', 7)[0].length.should.be.belowOrEqual(7, 'head length 12345 789, 7'); | |
33 | + truncate('12345 789', 7)[1].should.equal('789', 'tail 12345 789, 7'); | |
34 | + | |
35 | + truncate('12345 789 12345678', 10)[0].should.equal('12345 789', 'head 12345 789 12345678, 10'); | |
36 | + truncate('12345 789 12345678', 10)[0].length.should.be.belowOrEqual(10, 'head length 12345 789 12345678, 10'); | |
37 | + truncate('12345 789 12345678', 10)[1].should.equal('12345678', 'tail 12345 789 12345678, 10'); | |
38 | + | |
39 | + truncate('12345-6789', 7)[0].should.equal('12345-'); | |
40 | + truncate('12345-6789', 7)[1].should.equal('6789'); | |
41 | + | |
42 | + // truncate('Downline anda:\n#28 ABC CELL. Saldo: Rp. 4.500.000\n#35 BAYARKILAT. Saldo: Rp. 1.926.500\n#47 COBA1907011835. Saldo: Rp. 0\n#58 COBADARIMESSAGING. Saldo: Rp. 0\n#59 COBADARIMESSAGING1. Saldo: Rp. 0\n#60 COBADARIMESSAGING2. Saldo: Rp. 0\n#61 COBADARIMESSAGING3. Saldo: Rp. 0\n#62 COBADARIMESSAGING4. Saldo: Rp. 0\n#63 COBADARIMESSAGING5. Saldo: Rp. 0\n#48 COBADEFAULTMARKUP. Saldo: Rp. 0\n#49 COBADEFMARKUP2. Saldo: Rp. 0\n#50 COBADEFMARKUP3. Saldo: Rp. 0\n#51 COBADEFMARKUP4. Saldo: Rp. 0\n#52 COBADEFMARKUP5. Saldo: Rp. 0\n#53 COBADEFMARKUP6. Saldo: Rp. 0\n#54 COBADEFMARKUP7. Saldo: Rp. 0\n#55 COBADEFMARKUP8. Saldo: Rp. 0\n#56 COBADEFMARKUP9. Saldo: Rp. 0\n#57 COBADEFMARKUP99. Saldo: Rp. 0\n#36 DEMO. Saldo: Rp. 0\n#38 DUMMY. Saldo: Rp. 4.994.500\n#46 DUMMY13410. Saldo: Rp. 0\n#45 EVO. Saldo: Rp. 170.000\n#40 GIRASTEKDEV. Saldo: Rp. 1.000.000\n#39 HIKE. Saldo: Rp. 8.100.025\n#37 HOKISTORE. Saldo: Rp. 1.542.000\n#41 JPUDEV. Saldo: Rp. 1.000.000\n#30 KISELDEV. Saldo: Rp. 4.789.500\n#44 MELON SANDBOX. Saldo: Rp. 0\n#34 PASARSELON. Saldo: Rp. 0\n#23 PM0. Saldo: Rp. 664.700\n#27 RELOAD97. Saldo: Rp. 136.683.515\n#21 STORE2. Saldo: Rp. 664.700\n#2 TEST. Saldo: Rp. 664.700\n#3 TEST1. Saldo: Rp. 664.700\n#17 TEST10. Saldo: Rp. 664.700\n#8 TEST4. Saldo: Rp. 664.700\n#10 TEST5. Saldo: Rp. 664.700\n#12 TEST6. Saldo: Rp. 80.664.700\n#13 TEST7. Saldo: Rp. 664.700\n#14 TEST8. Saldo: Rp. 664.700\n#19 TOKOBAGUS. Saldo: Rp. 664.700\n#20 TOKOCUMI. Saldo: Rp. 664.700\n#43 VERSA. Saldo: Rp. 16.780.480\n#42 VERSA DEV. Saldo: Rp. 445.000\n#22 ZSTORE1. Saldo: Rp. 664.700\n#66 coba0717. Saldo: Rp. 0\n#67 coba0826. Saldo: Rp. 0\n#85 coba08261000. Saldo: Rp. 0\n#75 coba08261305. Saldo: Rp. 0\n#76 coba08261401. Saldo: Rp. 0\n#78 coba08261448. Saldo: Rp. 0\n#79 coba08261449m25. Saldo: Rp. 0\n#87 coba08261518. Saldo: Rp. 0\n#88 coba08261519. Saldo: Rp. 0\n#89 coba08261520. Saldo: Rp. 0\n#90 coba08261924. Saldo: Rp. 0\n#92 coba08261925. Saldo: Rp. 0\n#95 coba08261933. Saldo: Rp. 0\n#96 coba08261937. Saldo: Rp. 0\n#97 coba08261939. Saldo: Rp. 0\n#98 coba08261944. Saldo: Rp. 0\n#99 coba08261945. Saldo: Rp. 0\n#100 coba08261947. Saldo: Rp. 0\n#101 coba08262003. Saldo: Rp. 0\n#102 coba082620031. Saldo: Rp. 0\n#80 coba0826cekdefm. Saldo: Rp. 0\n#77 coba0826markup25. Saldo: Rp. 0\n#82 coba0826ms. Saldo: Rp. 0\n#81 coba0826overm. Saldo: Rp. 0\n#86 coba0826tele. Saldo: Rp. 0\n#64 cumi678. Saldo: Rp. 518.215","head":"Downline anda:\n#28 ABC CELL. Saldo: Rp. 4.500.000\n#35 BAYARKILAT. Saldo: Rp. 1.926.500\n#47 COBA1907011835. Saldo', 140)[0].should.equal('ad') | |
43 | + }) | |
44 | +}); |