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