Skip to content

Commit a864705

Browse files
author
Mark Lewin
authored
Merge pull request #2 from Nexmo/markl-remove-client-tests
Remove client tests
2 parents eb456a5 + a59af5f commit a864705

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Fill in the values in `.env` as appropriate.
3131
Run the following command in the application directory:
3232

3333
```sh
34-
node receive-concat-express.js
34+
npm start
3535
```
3636
The application should be running on the port you specified in `.env`.
3737

@@ -43,9 +43,9 @@ https://demo.ngrok.io/webhooks/inbound-sms
4343

4444
### Using the App
4545

46-
Send a long (> 160 standard GSM characters) text message to the number you specified in the `TO_NUMBER` setting in your `.env` file. Alternatively, you can use `send-sms-concat.js` for testing.
46+
Send a long (> 160 standard GSM characters) text message to the number you specified in the `TO_NUMBER` setting in your `.env` file.
4747

48-
The console output of `receive-concat-express.js` should show the invididual message parts and then the complete message once all the parts have been received and reassembled.
48+
The console output of `server.js` should show the invididual message parts and then the complete message once all the parts have been received and reassembled.
4949

5050
## License
5151

send-sms-concat.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

send.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

server.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
require('dotenv').config();
2+
const app = require('express')();
3+
const bodyParser = require('body-parser');
4+
5+
app.use(bodyParser.json());
6+
app.use(bodyParser.urlencoded({ extended: true }));
7+
8+
app
9+
.route('/webhooks/inbound-sms')
10+
.get(handleInboundSms)
11+
.post(handleInboundSms);
12+
13+
let concat_sms = []; // Array of message objects
14+
15+
function handleInboundSms(request, response) {
16+
const params = Object.assign(request.query, request.body);
17+
18+
if (params['concat'] === 'true') {
19+
/* This is a concatenated message. Add it to an array
20+
so that we can process it later. */
21+
concat_sms.push({
22+
ref: params['concat-ref'],
23+
part: params['concat-part'],
24+
from: params.msisdn,
25+
message: params.text
26+
});
27+
28+
/* Do we have all the message parts yet? They might
29+
not arrive consecutively. */
30+
let parts_for_ref = concat_sms.filter(function (part) {
31+
return part.ref == params['concat-ref'];
32+
});
33+
34+
// Is this the last message part for this reference?
35+
if (parts_for_ref.length == params['concat-total']) {
36+
console.dir(parts_for_ref);
37+
processConcatSms(parts_for_ref);
38+
}
39+
} else {
40+
// Not a concatenated message, so just display it
41+
displaySms(params.msisdn, params.text);
42+
}
43+
44+
// Send OK status
45+
response.status(204).send();
46+
}
47+
48+
function processConcatSms(all_parts) {
49+
50+
// Order all the message parts
51+
all_parts.sort(function (a, b) {
52+
if (Number(a.part) < Number(b.part)) {
53+
return -1;
54+
} else {
55+
return 1;
56+
}
57+
})
58+
59+
let concat_message = '';
60+
61+
// Reassemble the message from the parts
62+
for (i = 0; i < all_parts.length; i++) {
63+
concat_message += all_parts[i].message;
64+
}
65+
66+
displaySms(all_parts[0].from, concat_message);
67+
}
68+
69+
function displaySms(msisdn, text) {
70+
console.log('FROM: ' + msisdn);
71+
console.log('MESSAGE: ' + text);
72+
console.log('---');
73+
}
74+
75+
app.listen(process.env.PORT);

0 commit comments

Comments
 (0)