transport.js
4.6 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
const MAX_SMS_LENGTH = 140;
const url = require('url');
const request = require('request');
const uuidv4 = require('uuid/v4');
const moment = require('moment');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const messagingService = require('komodo-center-messaging-client-lib');
const common = require('./common');
const modems = require('./modems2');
const modemChooser = require('./modem-chooser');
// const partnerLastSeen = require('./partner-last-seen');
const history = require('./history');
const prefixes = require('./prefixes');
const truncate = require('./truncate-paragraph');
function _send(destinationNumber, msg, handlerIMSI) {
if (msg.length > MAX_SMS_LENGTH) {
logger.info('Splitting message');
/*
const newMsg = msg.slice(0, MAX_SMS_LENGTH);
const remainingMsg = msg.slice(MAX_SMS_LENGTH);
*/
const [newMsg, remainingMsg] = truncate(msg, MAX_SMS_LENGTH);
logger.verbose('TRANSPORT: Truncate long message', {maxLength: MAX_SMS_LENGTH, original: msg, head: newMsg, tail: remainingMsg});
_send(destinationNumber, newMsg, handlerIMSI);
setTimeout(() => {
_send(destinationNumber, remainingMsg, handlerIMSI);
}, 1000);
return;
}
const modem = modems.get('imsi', handlerIMSI);
if (!modem) {
logger.warn('Not knowing modem to use. Ignoring message', { destination_number: destinationNumber, msg: msg, modem_imsi: handlerIMSI });
return;
}
if (!modem.reportIp || !modem.reportPort || !modem.reportApikey) {
logger.warn('Invalid modem configuration', { modem });
return;
}
const reqId = uuidv4();
history.push({
ts: moment().format('YYYY-MM-DD HH:mm:ss'),
modem: {
name: modem.name,
imsi: modem.imsi,
msisdn: modem.msisdn,
},
direction: 'OUTGOING',
partner: destinationNumber,
message: msg,
});
logger.verbose('TRANSPORT: saving outgoing message');
messagingService.onIncomingMessage({
me: modem.name,
partner: destinationNumber,
partner_raw: `+${destinationNumber}`.replace(/^\++/, '+'),
msg: msg,
origin_label: modem.imsi || 'UNKNOWN',
origin_transport: 'SMS',
origin_partner: destinationNumber,
do_not_forward_to_core: true,
is_outgoing: true,
});
const requestOptions = {
url: url.format({
protocol: 'http',
hostname: modem.reportIp,
port: modem.reportPort,
pathname: modem.reportPathSms || '/sms',
}),
qs: {
msg: msg,
number: destinationNumber,
reqid: reqId,
apikey: modem.reportApikey,
}
}
logger.info('Sending message to modem handler', { req_id: reqId, destination_number: destinationNumber, msg: msg, msg_length: msg.length, modem_name: modem.name, modem_imsi: modem.imsi });
request(requestOptions, function(err, res, body) {
if (err) {
logger.warn('Error requesting to modem handler. ' + err.toString(), { req_id: reqId, modem_name: modem.name, modem_imsi: modem.imsi });
}
else if (res.statusCode != 200) {
logger.warn('Modem handler not responding with HTTP status code 200.', { http_status_code: res.statusCode, req_id: reqId, modem_name: modem.name, modem_imsi: modem.imsi });
}
else {
logger.verbose('Message sent to handler', { req_id: reqId, modem_name: modem.name, modem_imsi: modem.imsi, response_body: body });
}
})
}
async function send(partner, msg) {
if (!partner) return;
if (typeof msg !== 'string') {
logger.warn('Message to send is not a string, ignoring message', { msg_type: typeof msg, msg });
return;
}
msg = msg.trim();
if (!msg) return;
const destinationNumber = common.removeSuffixFromNumber(partner, config.number_suffix);
const prefixName = await prefixes.lookup(destinationNumber);
logger.verbose('Destination number prefix lookup', {partner: destinationNumber, prefix: prefixName});
// logger.verbose('Choosing handler name', { partner, destinationNumber, msg, origin });
// const handlerIMSI = await partnerLastSeen.get(destinationNumber) ;
const handlerIMSI = await modemChooser.chooser(destinationNumber, config);
if (!handlerIMSI) {
logger.warn(`Unknown handler for sending message to partner`, { partner, destinationNumber });
return;
}
_send(destinationNumber, msg, handlerIMSI);
}
exports.send = send;