transport.js
3.22 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
const axios = require('axios');
const moment = require('moment');
const uniqid = require('uniqid');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const messagingClient = require('komodo-center-messaging-client-lib');
const modems = require('./modems');
const messageSplitter = require('./message-splitter');
const modemLocks = require('./modem-locks');
const removeSuffix = require('./remove-suffix');
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
async function sendToModem(partner, msg, modem, parentXid, part) {
const xid = parentXid || uniqid();
const [msgHead, msgTail] = messageSplitter(msg.trim(), Number(config.max_length) || 140);
if (!msgHead) {
return;
}
logger.info('TRANSPORT: Sending message to EVO-CP', {
xid,
partner,
msg: msgHead,
msgLength: msgHead.length,
tailLength: msgTail.length,
part,
modem: modem.name,
});
await modemLocks.lock(modem.name);
try {
await axios.get(modem.url || config.sender.url, {
params: {
to: partner,
password: modem.password || config.sender.password,
ts: moment().format('YYYY-MM-DD HH:mm:ss'),
text: msgHead,
modem: modem.name,
username: modem.username || config.sender.username,
},
});
} catch (e) {
logger.warn('TRANSPORT 32E1A559: Exception on sending message to EVO-CP', {
xid,
partner,
msgHead,
modemName: modem.name,
modemUrl: modem.url,
httpStatus: e.response && e.response.status,
eCode: e.code,
eMessage: e.message,
responseBody: e.response && e.response.data,
});
}
messagingClient.onIncomingMessage({
me: modem.imsi || modem.name,
partner,
partner_raw: partner,
msg: msgHead,
origin_label: modem.imsi || modem.name,
origin_transport: 'SMS',
origin_partner: partner,
do_not_forward_to_core: true,
is_outgoing: true,
});
await sleep(config.sleep_before_unlock_modem_ms || 1200);
modemLocks.unlock(modem.name);
if (msgTail) {
await sendToModem(partner, msgTail, modem, part + 1);
}
}
exports.send = async (partner, msg) => {
if (typeof partner !== 'string' || !partner.trim()) return;
if (typeof msg !== 'string' || !msg.trim()) return;
if (
(!config.send_on_process_msg && (msg.trim().search(/PROSES$/) >= 0))
) return;
const xid = uniqid();
logger.info('TRANSPORT: Got CORE message to forward to EVO-CP', {
xid,
partner,
msg,
});
const modem = config.ignore_prefix ? modems.randomModem() : modems.randomModemByPrefix(partner);
if (!modem) {
logger.warn('TRANSPORT: Not sending message to EVO-CP because of no available modem', {
xid,
partner,
msg,
});
return;
}
const to = removeSuffix(partner, config.number_suffix || '');
await sendToModem(to, msg.trim(), modem, xid, 1);
};