Blame view
lib/transport.js
4.3 KB
c0741a574
|
1 |
"use strict"; |
3f614b14a
|
2 |
const MAX_SMS_LENGTH = 160; |
f43dbc16a
|
3 |
const url = require('url'); |
c0741a574
|
4 5 |
const request = require('request'); const uuidv4 = require('uuid/v4'); |
4fbfae95f
|
6 |
const moment = require('moment'); |
c0741a574
|
7 8 9 |
const config = require('komodo-sdk/config'); const logger = require('komodo-sdk/logger'); |
ef1c8149d
|
10 |
const messagingService = require('komodo-center-messaging-client-lib'); |
98fa55b40
|
11 |
const common = require('./common'); |
f43dbc16a
|
12 |
const modems = require('./modems2'); |
a21e7a161
|
13 14 |
const modemChooser = require('./modem-chooser'); // const partnerLastSeen = require('./partner-last-seen'); |
4fbfae95f
|
15 |
const history = require('./history'); |
6cd19a9c9
|
16 |
const prefixes = require('./prefixes'); |
c0741a574
|
17 |
|
03f73469d
|
18 |
function _send(destinationNumber, msg, handlerIMSI) { |
f53fe083f
|
19 |
|
077f3b0aa
|
20 |
if (msg.length > 160) { |
03f73469d
|
21 |
logger.info('Splitting message'); |
3f614b14a
|
22 23 |
const newMsg = msg.slice(0, MAX_SMS_LENGTH); const remainingMsg = msg.slice(MAX_SMS_LENGTH); |
f53fe083f
|
24 |
|
03f73469d
|
25 |
_send(destinationNumber, newMsg, handlerIMSI); |
077f3b0aa
|
26 |
setTimeout(() => { |
03f73469d
|
27 28 |
_send(destinationNumber, remainingMsg, handlerIMSI); }, 1000); |
077f3b0aa
|
29 30 31 |
return; } |
c0741a574
|
32 |
|
03f73469d
|
33 |
const modem = modems.get('imsi', handlerIMSI); |
c0741a574
|
34 |
if (!modem) { |
03f73469d
|
35 |
logger.warn('Not knowing modem to use. Ignoring message', { destination_number: destinationNumber, msg: msg, modem_imsi: handlerIMSI }); |
c0741a574
|
36 37 |
return; } |
f43dbc16a
|
38 39 |
if (!modem.reportIp || !modem.reportPort || !modem.reportApikey) { logger.warn('Invalid modem configuration', { modem }); |
c0741a574
|
40 41 |
return; } |
077f3b0aa
|
42 |
const reqId = uuidv4(); |
4fbfae95f
|
43 44 45 |
history.push({ ts: moment().format('YYYY-MM-DD HH:mm:ss'), modem: { |
03f73469d
|
46 47 48 |
name: modem.name, imsi: modem.imsi, msisdn: modem.msisdn, |
4fbfae95f
|
49 50 51 52 53 |
}, direction: 'OUTGOING', partner: destinationNumber, message: msg, }); |
8fb5c9291
|
54 |
logger.verbose('TRANSPORT: saving outgoing message'); |
ef1c8149d
|
55 56 57 58 59 60 61 62 63 64 65 |
messagingService.onIncomingMessage({ me: modem.name, partner: destinationNumber, partner_raw: `+${destinationNumber}`.replace(/^\++/, '+'), msg: msg, origin_label: `IMSI_${modem.imsi || 'UNKNOWN'}`, origin_transport: 'SMS', origin_partner: destinationNumber, do_not_forward_to_core: true, is_outgoing: true, }); |
c0741a574
|
66 |
const requestOptions = { |
f43dbc16a
|
67 68 69 70 |
url: url.format({ protocol: 'http', hostname: modem.reportIp, port: modem.reportPort, |
70431f3a4
|
71 |
pathname: modem.reportPathSms || '/sms', |
f43dbc16a
|
72 |
}), |
c0741a574
|
73 74 75 76 |
qs: { msg: msg, number: destinationNumber, reqid: reqId, |
b6d23624a
|
77 |
apikey: modem.reportApikey, |
c0741a574
|
78 79 |
} } |
03f73469d
|
80 |
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 }); |
c0741a574
|
81 82 |
request(requestOptions, function(err, res, body) { if (err) { |
03f73469d
|
83 |
logger.warn('Error requesting to modem handler. ' + err.toString(), { req_id: reqId, modem_name: modem.name, modem_imsi: modem.imsi }); |
c0741a574
|
84 85 86 |
} else if (res.statusCode != 200) { |
03f73469d
|
87 |
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 }); |
c0741a574
|
88 89 |
} else { |
03f73469d
|
90 |
logger.verbose('Message sent to handler', { req_id: reqId, modem_name: modem.name, modem_imsi: modem.imsi, response_body: body }); |
c0741a574
|
91 92 |
} }) |
077f3b0aa
|
93 94 |
} |
62e23ff59
|
95 |
async function send(partner, msg) { |
077f3b0aa
|
96 97 98 99 100 101 102 103 104 |
if (!partner) return; if (typeof msg !== 'string') { logger.warn('Message to send is not a string, ignoring message'); return; } msg = msg.trim(); if (!msg) return; |
98fa55b40
|
105 |
const destinationNumber = common.removeSuffixFromNumber(partner, config.number_suffix); |
4acd86be1
|
106 |
const prefixName = await prefixes.lookup(destinationNumber); |
6cd19a9c9
|
107 |
logger.verbose('Destination number prefix lookup', {partner: destinationNumber, prefix: prefixName}); |
f43dbc16a
|
108 109 |
// logger.verbose('Choosing handler name', { partner, destinationNumber, msg, origin }); |
a21e7a161
|
110 111 |
// const handlerIMSI = await partnerLastSeen.get(destinationNumber) ; const handlerIMSI = await modemChooser.chooser(destinationNumber, config); |
077f3b0aa
|
112 |
|
03f73469d
|
113 |
if (!handlerIMSI) { |
f43dbc16a
|
114 115 116 |
logger.warn(`Unknown handler for sending message to partner`, { partner, destinationNumber }); return; } |
077f3b0aa
|
117 |
|
03f73469d
|
118 |
_send(destinationNumber, msg, handlerIMSI); |
c0741a574
|
119 120 121 |
} exports.send = send; |