transport.js 3.22 KB
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);
};