mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-10-30 03:14:18 +00:00
1 parent
e09933900b
commit
24621f79dd
31 files changed
+15
-1150
No files matched your search
@@ -5,9 +5,7 @@ const retrieveSql = `SELECT * from applications app
|
||||
LEFT JOIN webhooks AS ch
|
||||
ON app.call_hook_sid = ch.webhook_sid
|
||||
LEFT JOIN webhooks AS sh
|
||||
ON app.call_status_hook_sid = sh.webhook_sid
|
||||
LEFT JOIN webhooks AS mh
|
||||
ON app.messaging_hook_sid = mh.webhook_sid`;
|
||||
ON app.call_status_hook_sid = sh.webhook_sid`;
|
||||
|
||||
function transmogrifyResults(results) {
|
||||
return results.map((row) => {
|
||||
@@ -20,13 +18,8 @@ function transmogrifyResults(results) {
|
||||
Object.assign(obj, {call_status_hook: row.sh});
|
||||
}
|
||||
else obj.call_status_hook = null;
|
||||
if (row.mh && Object.keys(row.mh).length && row.mh.url !== null) {
|
||||
Object.assign(obj, {messaging_hook: row.mh});
|
||||
}
|
||||
else obj.messaging_hook = null;
|
||||
delete obj.call_hook_sid;
|
||||
delete obj.call_status_hook_sid;
|
||||
delete obj.messaging_hook_sid;
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
@@ -215,10 +208,6 @@ Application.fields = [
|
||||
name: 'call_status_hook_sid',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'messaging_hook_sid',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'record_all_calls',
|
||||
type: 'number',
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
const Model = require('./model');
|
||||
const {promisePool} = require('../db');
|
||||
const retrieveSql = 'SELECT * from smpp_gateways WHERE voip_carrier_sid = ?';
|
||||
|
||||
class SmppGateway extends Model {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* list all sip gateways for a voip_carrier
|
||||
*/
|
||||
static async retrieveForVoipCarrier(voip_carrier_sid) {
|
||||
const [rows] = await promisePool.query(retrieveSql, voip_carrier_sid);
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
||||
SmppGateway.table = 'smpp_gateways';
|
||||
SmppGateway.fields = [
|
||||
{
|
||||
name: 'smpp_gateway_sid',
|
||||
type: 'string',
|
||||
primaryKey: true
|
||||
},
|
||||
{
|
||||
name: 'voip_carrier_sid',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'ipv4',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'port',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'netmask',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'inbound',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'outbound',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'is_primary',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'use_tls',
|
||||
type: 'number'
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = SmppGateway;
|
||||
@@ -1,55 +0,0 @@
|
||||
const Model = require('./model');
|
||||
const {getMysqlConnection} = require('../db');
|
||||
|
||||
class Smpp extends Model {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* list all SBCs either for a given service provider, or those not associated with a
|
||||
* service provider (i.e. community SBCs)
|
||||
*/
|
||||
static retrieveAll(service_provider_sid) {
|
||||
const sql = service_provider_sid ?
|
||||
'SELECT * from smpp_addresses WHERE service_provider_sid = ?' :
|
||||
'SELECT * from smpp_addresses WHERE service_provider_sid IS NULL';
|
||||
const args = service_provider_sid ? [service_provider_sid] : [];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getMysqlConnection((err, conn) => {
|
||||
if (err) return reject(err);
|
||||
conn.query(sql, args, (err, results) => {
|
||||
conn.release();
|
||||
if (err) return reject(err);
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Smpp.table = 'smpp_addresses';
|
||||
Smpp.fields = [
|
||||
{
|
||||
name: 'smpp_address_sid',
|
||||
type: 'string',
|
||||
primaryKey: true
|
||||
},
|
||||
{
|
||||
name: 'ipv4',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'port',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'service_provider_sid',
|
||||
type: 'string'
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = Smpp;
|
||||
@@ -148,30 +148,6 @@ VoipCarrier.fields = [
|
||||
name: 'is_active',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'smpp_system_id',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'smpp_password',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'smpp_inbound_system_id',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'smpp_inbound_password',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'smpp_enquire_link_interval',
|
||||
type: 'number'
|
||||
},
|
||||
{
|
||||
name: 'smpp_system_id',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'register_from_user',
|
||||
type: 'string'
|
||||
|
||||
@@ -5,9 +5,7 @@ const retrieveApplicationsSql = `SELECT * from applications app
|
||||
LEFT JOIN webhooks AS ch
|
||||
ON app.call_hook_sid = ch.webhook_sid
|
||||
LEFT JOIN webhooks AS sh
|
||||
ON app.call_status_hook_sid = sh.webhook_sid
|
||||
LEFT JOIN webhooks AS mh
|
||||
ON app.messaging_hook_sid = mh.webhook_sid
|
||||
ON app.call_status_hook_sid = sh.webhook_sid
|
||||
WHERE service_provider_sid = ?`;
|
||||
|
||||
const transmogrifyResults = (results) => {
|
||||
@@ -21,13 +19,8 @@ const transmogrifyResults = (results) => {
|
||||
Object.assign(obj, {call_status_hook: row.sh});
|
||||
}
|
||||
else obj.call_status_hook = null;
|
||||
if (row.mh && Object.keys(row.mh).length && row.mh.url !== null) {
|
||||
Object.assign(obj, {messaging_hook: row.mh});
|
||||
}
|
||||
else obj.messaging_hook = null;
|
||||
delete obj.call_hook_sid;
|
||||
delete obj.call_status_hook_sid;
|
||||
delete obj.messaging_hook_sid;
|
||||
return obj;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -8,7 +8,6 @@ const ApiKey = require('../../models/api-key');
|
||||
const ServiceProvider = require('../../models/service-provider');
|
||||
const {deleteDnsRecords} = require('../../utils/dns-utils');
|
||||
const {deleteCustomer} = require('../../utils/stripe-utils');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const snakeCase = require('../../utils/snake-case');
|
||||
const sysError = require('../error');
|
||||
const {promisePool} = require('../../db');
|
||||
@@ -472,31 +471,6 @@ async function validateCreateCall(logger, sid, req) {
|
||||
}
|
||||
}
|
||||
|
||||
async function validateCreateMessage(logger, sid, req) {
|
||||
const obj = req.body;
|
||||
logger.debug({payload: req.body}, 'validateCreateMessage');
|
||||
|
||||
if (req.user.account_sid !== sid) {
|
||||
throw new DbErrorBadRequest(`unauthorized createMessage request for account ${sid}`);
|
||||
}
|
||||
|
||||
if (!obj.from) throw new DbErrorBadRequest('missing from property');
|
||||
/*
|
||||
else {
|
||||
const regex = /^\+(\d+)$/;
|
||||
const arr = regex.exec(obj.from);
|
||||
const from = arr ? arr[1] : obj.from;
|
||||
const account = await lookupAccountByPhoneNumber(from);
|
||||
if (!account) throw new DbErrorBadRequest(`accountSid ${sid} does not own phone number ${from}`);
|
||||
}
|
||||
*/
|
||||
if (!obj.to) throw new DbErrorBadRequest('missing to property');
|
||||
|
||||
if (!obj.text && !obj.media) {
|
||||
throw new DbErrorBadRequest('either text or media required in outbound message');
|
||||
}
|
||||
}
|
||||
|
||||
async function validateAdd(req) {
|
||||
/* account-level token can not be used to add accounts */
|
||||
if (req.user.hasAccountAuth) {
|
||||
@@ -1168,45 +1142,6 @@ router.put('/:sid/Calls/:callSid', async(req, res) => {
|
||||
/**
|
||||
* create a new Message
|
||||
*/
|
||||
router.post('/:sid/Messages', async(req, res) => {
|
||||
const {retrieveSet, logger} = req.app.locals;
|
||||
|
||||
try {
|
||||
const account_sid = parseAccountSid(req);
|
||||
await validateRequest(req, account_sid);
|
||||
|
||||
const setName = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:fs-service-url`;
|
||||
const serviceUrl = await getFsUrl(logger, retrieveSet, setName);
|
||||
if (!serviceUrl) {
|
||||
return res.status(480).json({msg: 'no available feature servers at this time'});
|
||||
}
|
||||
await validateCreateMessage(logger, account_sid, req);
|
||||
|
||||
const payload = {
|
||||
message_sid: uuidv4(),
|
||||
account_sid,
|
||||
...req.body
|
||||
};
|
||||
logger.debug({payload}, `sending createMessage API request to to ${serviceUrl}`);
|
||||
updateLastUsed(logger, account_sid, req).catch(() => {});
|
||||
const response = await fetch(serviceUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
if (!response.ok) {
|
||||
logger.error(`Error sending createMessage POST to ${serviceUrl}`);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
const body = await response.json();
|
||||
return res.status(response.status).json(body);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* retrieve info for a group of queues under an account
|
||||
*/
|
||||
|
||||
@@ -2,7 +2,6 @@ const router = require('express').Router();
|
||||
const PredefinedCarrier = require('../../models/predefined-carrier');
|
||||
const VoipCarrier = require('../../models/voip-carrier');
|
||||
const SipGateway = require('../../models/sip-gateway');
|
||||
const SmppGateway = require('../../models/smpp-gateway');
|
||||
const {parseServiceProviderSid} = require('./utils');
|
||||
const short = require('short-uuid');
|
||||
const {promisePool} = require('../../db');
|
||||
@@ -12,9 +11,7 @@ const sysError = require('../error');
|
||||
const sqlSelectCarrierByNameForSP = `SELECT * FROM voip_carriers
|
||||
WHERE service_provider_sid = ?
|
||||
AND name = ?`;
|
||||
const sqlSelectTemplateSipGateways = `SELECT * FROM predefined_sip_gateways
|
||||
WHERE predefined_carrier_sid = ?`;
|
||||
const sqlSelectTemplateSmppGateways = `SELECT * FROM predefined_smpp_gateways
|
||||
const sqlSelectTemplateSipGateways = `SELECT * FROM predefined_sip_gateways
|
||||
WHERE predefined_carrier_sid = ?`;
|
||||
|
||||
|
||||
@@ -46,10 +43,6 @@ router.post('/:sid', async(req, res) => {
|
||||
const [r3] = await promisePool.query(sqlSelectTemplateSipGateways, template.predefined_carrier_sid);
|
||||
logger.debug({r3}, `retrieved template sip gateways for ${template.name}`);
|
||||
|
||||
/* retrieve all the smpp gateways */
|
||||
const [r4] = await promisePool.query(sqlSelectTemplateSmppGateways, template.predefined_carrier_sid);
|
||||
logger.debug({r4}, `retrieved template smpp gateways for ${template.name}`);
|
||||
|
||||
/* add a voip_carrier */
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const {requires_static_ip, predefined_carrier_sid, ...obj} = template;
|
||||
@@ -63,14 +56,6 @@ router.post('/:sid', async(req, res) => {
|
||||
await SipGateway.make({...obj, voip_carrier_sid: uuid});
|
||||
}
|
||||
|
||||
/* add all the smpp gateways */
|
||||
for (const gw of r4) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const {predefined_carrier_sid, predefined_smpp_gateway_sid, ...obj} = gw;
|
||||
logger.debug({obj}, 'adding smpp gateway');
|
||||
await SmppGateway.make({...obj, voip_carrier_sid: uuid});
|
||||
}
|
||||
|
||||
logger.debug({sid: uuid}, 'Successfully added carrier from predefined list');
|
||||
res.status(201).json({sid: uuid});
|
||||
} catch (err) {
|
||||
|
||||
@@ -150,7 +150,7 @@ router.post('/', async(req, res) => {
|
||||
|
||||
// create webhooks if provided
|
||||
const obj = Object.assign({}, req.body);
|
||||
for (const prop of ['call_hook', 'call_status_hook', 'messaging_hook']) {
|
||||
for (const prop of ['call_hook', 'call_status_hook']) {
|
||||
if (obj[prop]) {
|
||||
obj[`${prop}_sid`] = await Webhook.make(obj[prop]);
|
||||
delete obj[prop];
|
||||
@@ -248,8 +248,8 @@ router.delete('/:sid', async(req, res) => {
|
||||
await validateDelete(req, sid);
|
||||
|
||||
const [application] = await promisePool.query('SELECT * FROM applications WHERE application_sid = ?', sid);
|
||||
const {call_hook_sid, call_status_hook_sid, messaging_hook_sid} = application[0];
|
||||
logger.info({call_hook_sid, call_status_hook_sid, messaging_hook_sid, sid}, 'deleting application');
|
||||
const {call_hook_sid, call_status_hook_sid} = application[0];
|
||||
logger.info({call_hook_sid, call_status_hook_sid, sid}, 'deleting application');
|
||||
await promisePool.execute('DELETE from applications where application_sid = ?', [sid]);
|
||||
|
||||
if (call_hook_sid) {
|
||||
@@ -267,13 +267,6 @@ router.delete('/:sid', async(req, res) => {
|
||||
await promisePool.execute('DELETE from webhooks where webhook_sid = ?', [call_status_hook_sid]);
|
||||
}
|
||||
}
|
||||
if (messaging_hook_sid) {
|
||||
const sql = 'SELECT COUNT(*) as count FROM applications WHERE messaging_hook_sid = ?';
|
||||
const [r] = await promisePool.query(sql, messaging_hook_sid);
|
||||
if (r[0]?.count === 0) {
|
||||
await promisePool.execute('DELETE from webhooks where webhook_sid = ?', [messaging_hook_sid]);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(204).end();
|
||||
} catch (err) {
|
||||
@@ -292,7 +285,7 @@ router.put('/:sid', async(req, res) => {
|
||||
|
||||
// create webhooks if provided
|
||||
const obj = Object.assign({}, req.body);
|
||||
for (const prop of ['call_hook', 'call_status_hook', 'messaging_hook']) {
|
||||
for (const prop of ['call_hook', 'call_status_hook']) {
|
||||
if (prop in obj && Object.keys(obj[prop]).length) {
|
||||
if ('webhook_sid' in obj[prop]) {
|
||||
const sid = obj[prop]['webhook_sid'];
|
||||
|
||||
@@ -22,7 +22,6 @@ api.use('/ServiceProviders', require('./service-providers'));
|
||||
api.use('/VoipCarriers', require('./voip-carriers'));
|
||||
api.use('/Webhooks', require('./webhooks'));
|
||||
api.use('/SipGateways', require('./sip-gateways'));
|
||||
api.use('/SmppGateways', require('./smpp-gateways'));
|
||||
api.use('/PhoneNumbers', require('./phone-numbers'));
|
||||
api.use('/ApiKeys', require('./api-keys'));
|
||||
api.use('/Accounts', require('./accounts'));
|
||||
@@ -56,9 +55,4 @@ api.use('/Clients', require('./clients'));
|
||||
// Google Custom Voices
|
||||
api.use('/GoogleCustomVoices', require('./google-custom-voices'));
|
||||
|
||||
// messaging
|
||||
api.use('/Smpps', require('./smpps')); // our smpp server info
|
||||
api.use('/messaging', require('./sms-inbound')); // inbound SMS from carrier
|
||||
api.use('/outboundSMS', require('./sms-outbound')); // outbound SMS from feature server
|
||||
|
||||
module.exports = api;
|
||||
@@ -24,12 +24,6 @@ WHERE voip_carrier_sid IN (
|
||||
FROM voip_carriers
|
||||
WHERE service_provider_sid = ?
|
||||
)`;
|
||||
const sqlDeleteSmppGateways = `DELETE from smpp_gateways
|
||||
WHERE voip_carrier_sid IN (
|
||||
SELECT voip_carrier_sid
|
||||
FROM voip_carriers
|
||||
WHERE service_provider_sid = ?
|
||||
)`;
|
||||
|
||||
/* only admin users can add a service provider */
|
||||
function validateAdd(req) {
|
||||
@@ -95,7 +89,6 @@ async function noActiveAccountsOrUsers(req, sid) {
|
||||
/* ok we can delete -- no active accounts. remove carriers and speech credentials */
|
||||
await promisePool.execute('DELETE from speech_credentials WHERE service_provider_sid = ?', [sid]);
|
||||
await promisePool.query(sqlDeleteSipGateways, [sid]);
|
||||
await promisePool.query(sqlDeleteSmppGateways, [sid]);
|
||||
await promisePool.query('DELETE from voip_carriers WHERE service_provider_sid = ?', [sid]);
|
||||
await promisePool.query('DELETE from api_keys WHERE service_provider_sid = ?', [sid]);
|
||||
}
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
const router = require('express').Router();
|
||||
const SmppGateway = require('../../models/smpp-gateway');
|
||||
const {DbErrorBadRequest, DbErrorForbidden} = require('../../utils/errors');
|
||||
const decorate = require('./decorate');
|
||||
const sysError = require('../error');
|
||||
|
||||
const checkUserScope = async(req, voip_carrier_sid) => {
|
||||
const {lookupCarrierBySid} = req.app.locals;
|
||||
if (!voip_carrier_sid) {
|
||||
throw new DbErrorBadRequest('missing voip_carrier_sid');
|
||||
}
|
||||
|
||||
if (req.user.hasAdminAuth) return;
|
||||
|
||||
if (req.user.hasAccountAuth) {
|
||||
const carrier = await lookupCarrierBySid(voip_carrier_sid);
|
||||
if (!carrier) throw new DbErrorBadRequest('invalid voip_carrier_sid');
|
||||
|
||||
if ((!carrier.service_provider_sid || carrier.service_provider_sid === req.user.service_provider_sid) &&
|
||||
(!carrier.account_sid || carrier.account_sid === req.user.account_sid)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (req.user.hasServiceProviderAuth) {
|
||||
const carrier = await lookupCarrierBySid(voip_carrier_sid);
|
||||
if (!carrier) throw new DbErrorBadRequest('invalid voip_carrier_sid');
|
||||
if (carrier.service_provider_sid === req.user.service_provider_sid) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new DbErrorForbidden('insufficient privileges');
|
||||
};
|
||||
|
||||
const validate = async(req, sid) => {
|
||||
const {lookupSmppGatewayBySid} = req.app.locals;
|
||||
let voip_carrier_sid;
|
||||
|
||||
if (sid) {
|
||||
const gateway = await lookupSmppGatewayBySid(sid);
|
||||
if (!gateway) throw new DbErrorBadRequest('invalid smpp_gateway_sid');
|
||||
voip_carrier_sid = gateway.voip_carrier_sid;
|
||||
}
|
||||
else {
|
||||
voip_carrier_sid = req.body.voip_carrier_sid;
|
||||
if (!voip_carrier_sid) throw new DbErrorBadRequest('missing voip_carrier_sid');
|
||||
}
|
||||
|
||||
await checkUserScope(req, voip_carrier_sid);
|
||||
};
|
||||
|
||||
const preconditions = {
|
||||
'add': validate,
|
||||
'retrieve': validate,
|
||||
'update': validate,
|
||||
'delete': validate
|
||||
};
|
||||
|
||||
decorate(router, SmppGateway, ['add', 'retrieve', 'update', 'delete'], preconditions);
|
||||
|
||||
/* list */
|
||||
router.get('/', async(req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
const voip_carrier_sid = req.query.voip_carrier_sid;
|
||||
try {
|
||||
await checkUserScope(req, voip_carrier_sid);
|
||||
if (!voip_carrier_sid) {
|
||||
logger.info('GET /SmppGateways missing voip_carrier_sid param');
|
||||
return res.status(400).json({message: 'missing voip_carrier_sid query param'});
|
||||
}
|
||||
const results = await SmppGateway.retrieveForVoipCarrier(voip_carrier_sid);
|
||||
res.status(200).json(results);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,30 +0,0 @@
|
||||
const router = require('express').Router();
|
||||
const Smpp = require('../../models/smpp');
|
||||
const decorate = require('./decorate');
|
||||
const sysError = require('../error');
|
||||
//const {DbErrorBadRequest} = require('../../utils/errors');
|
||||
//const {promisePool} = require('../../db');
|
||||
|
||||
decorate(router, Smpp, ['add', 'delete']);
|
||||
|
||||
/* list */
|
||||
router.get('/', async(req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
try {
|
||||
const service_provider_sid = req.query.service_provider_sid;
|
||||
/*
|
||||
if (req.user.hasAccountAuth) {
|
||||
const [r] = await promisePool.query('SELECT * from accounts WHERE account_sid = ?', req.user.account_sid);
|
||||
if (0 === r.length) throw new Error('invalid account_sid');
|
||||
service_provider_sid = r[0].service_provider_sid;
|
||||
}
|
||||
if (!service_provider_sid) throw new DbErrorBadRequest('missing service_provider_sid in query');
|
||||
*/
|
||||
const results = await Smpp.retrieveAll(service_provider_sid);
|
||||
res.status(200).json(results);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,142 +0,0 @@
|
||||
const router = require('express').Router();
|
||||
const getProvider = require('../../utils/sms-provider');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const sysError = require('../error');
|
||||
let idx = 0;
|
||||
|
||||
const getFsUrl = async(logger, retrieveSet, setName, provider) => {
|
||||
if (process.env.K8S) return `http://${process.env.K8S_FEATURE_SERVER_SERVICE_NAME}:3000/v1/messaging/${provider}`;
|
||||
|
||||
try {
|
||||
const fs = await retrieveSet(setName);
|
||||
if (0 === fs.length) {
|
||||
logger.info('No available feature servers to handle createCall API request');
|
||||
return ;
|
||||
}
|
||||
const f = fs[idx++ % fs.length];
|
||||
logger.info({fs}, `feature servers available for createCall API request, selecting ${f}`);
|
||||
return `${f}/v1/messaging/${provider}`;
|
||||
} catch (err) {
|
||||
logger.error({err}, 'getFsUrl: error retreving feature servers from redis');
|
||||
}
|
||||
};
|
||||
|
||||
const doSendResponse = async(res, respondFn, body) => {
|
||||
if (typeof respondFn === 'number') res.sendStatus(respondFn);
|
||||
else if (typeof respondFn !== 'function') res.sendStatus(200);
|
||||
else {
|
||||
const payload = await respondFn(body);
|
||||
res.status(200).json(payload);
|
||||
}
|
||||
};
|
||||
|
||||
router.post('/:provider', async(req, res) => {
|
||||
const provider = req.params.provider;
|
||||
const {
|
||||
retrieveSet,
|
||||
lookupAppByPhoneNumber,
|
||||
logger
|
||||
} = req.app.locals;
|
||||
const setName = `${process.env.JAMBONES_CLUSTER_ID || 'default'}:fs-service-url`;
|
||||
logger.debug({path: req.path, body: req.body}, 'incomingSMS from carrier');
|
||||
|
||||
// search for provider module
|
||||
const arr = getProvider(logger, provider);
|
||||
if (!arr) {
|
||||
logger.info({body: req.body, params: req.params},
|
||||
`rejecting incomingSms request from unknown provider ${provider}`
|
||||
);
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
|
||||
const providerData = arr[1];
|
||||
if (!providerData || !providerData.module) {
|
||||
logger.info({body: req.body, params: req.params},
|
||||
`rejecting incomingSms request from badly configured provider ${provider}`
|
||||
);
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
|
||||
// load provider module
|
||||
let filterFn, respondFn;
|
||||
try {
|
||||
const {
|
||||
fromProviderFormat,
|
||||
formatProviderResponse
|
||||
} = require(providerData.module);
|
||||
// must at least provide a filter function
|
||||
if (!fromProviderFormat) {
|
||||
logger.info(
|
||||
`missing fromProviderFormat function in module ${providerData.module} for provider ${provider}`
|
||||
);
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
filterFn = fromProviderFormat;
|
||||
respondFn = formatProviderResponse;
|
||||
} catch (err) {
|
||||
logger.info(
|
||||
err,
|
||||
`failure loading module ${providerData.module} for provider ${provider}`
|
||||
);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
|
||||
try {
|
||||
const serviceUrl = await getFsUrl(logger, retrieveSet, setName, provider);
|
||||
if (!serviceUrl) res.json({msg: 'no available feature servers at this time'}).status(480);
|
||||
const messageSid = uuidv4();
|
||||
const payload = await Promise.resolve(filterFn({messageSid}, req.body));
|
||||
|
||||
/**
|
||||
* lookup the application associated with the number in the To field
|
||||
* since there could be multiple Tos, we have to search through (and cc also)
|
||||
*/
|
||||
let app;
|
||||
const to = Array.isArray(payload.to) ? payload.to : [payload.to];
|
||||
const cc = Array.isArray(payload.cc) ? payload.cc : (payload.cc ? [payload.cc] : []);
|
||||
const dids = to.concat(cc).filter((n) => n.length);
|
||||
for (let did of dids) {
|
||||
const regex = /^\+(\d+)$/;
|
||||
const arr = regex.exec(did);
|
||||
did = arr ? arr[1] : did;
|
||||
const obj = await lookupAppByPhoneNumber(did);
|
||||
logger.info({obj}, `lookup app for phone number ${did}`);
|
||||
if (obj) {
|
||||
logger.info({did, obj}, 'Found app for DID');
|
||||
app = obj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!app) {
|
||||
logger.info({payload}, 'No application found for incoming SMS');
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
if (!app.messaging_hook) {
|
||||
logger.info({payload}, `app "${app.name}" found for incoming SMS does not have an associated messaging hook`);
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
payload.app = app;
|
||||
|
||||
logger.debug({body: req.body, payload}, 'filtered incoming SMS');
|
||||
|
||||
logger.info({payload, url: serviceUrl}, `sending incomingSms API request to FS at ${serviceUrl}`);
|
||||
|
||||
const response = await fetch(serviceUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error({response}, `Error sending incomingSms POST to ${serviceUrl}`);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
const body = await response.json();
|
||||
logger.info({body}, 'sending response to provider for incomingSMS');
|
||||
return doSendResponse(res, respondFn, body);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,44 +0,0 @@
|
||||
const router = require('express').Router();
|
||||
const getProvider = require('../../utils/sms-provider');
|
||||
const sysError = require('../error');
|
||||
|
||||
router.post('/', async(req, res) => {
|
||||
const { logger } = req.app.locals;
|
||||
|
||||
try {
|
||||
// if provider specified use it, otherwise use first in list
|
||||
const arr = getProvider(logger, req.body.provider);
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error('outboundSMS - unable to locate sms provider to use to send message');
|
||||
}
|
||||
|
||||
const providerData = arr[1];
|
||||
if (!providerData || !providerData.module) {
|
||||
throw new Error(`rejecting outgoingSms request for unknown or badly configured provider ${req.body.provider}`);
|
||||
}
|
||||
|
||||
const provider = arr[0];
|
||||
const opts = providerData.options;
|
||||
if (!opts || !opts.url) {
|
||||
throw new Error(`rejecting outgoingSms request -- no HTTP url for ${req.body.provider}`);
|
||||
}
|
||||
|
||||
// load provider module
|
||||
const { sendSms } = require(providerData.module);
|
||||
if (!sendSms) {
|
||||
throw new Error(`missing sendSms function in module ${providerData.module} for provider ${provider}`);
|
||||
}
|
||||
|
||||
// send the SMS
|
||||
const payload = req.body;
|
||||
delete payload.provider;
|
||||
logger.debug({opts, payload}, `outboundSMS - sending to ${opts.url}`);
|
||||
const response = await sendSms(opts, payload);
|
||||
logger.info({response, payload: req.body}, `outboundSMS - sent to ${opts.url}`);
|
||||
res.status(200).json(response);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -191,9 +191,8 @@ const validateDelete = async(req, sid) => {
|
||||
const activeAccounts = await VoipCarrier.getForeignKeyReferences('phone_numbers.voip_carrier_sid', sid);
|
||||
if (activeAccounts > 0) throw new DbErrorUnprocessableRequest('cannot delete voip carrier with active phone numbers');
|
||||
|
||||
/* remove all the sip and smpp gateways from the carrier first */
|
||||
/* remove all the sip gateways from the carrier first */
|
||||
await promisePool.execute('DELETE FROM sip_gateways WHERE voip_carrier_sid = ?', [sid]);
|
||||
await promisePool.execute('DELETE FROM smpp_gateways WHERE voip_carrier_sid = ?', [sid]);
|
||||
};
|
||||
|
||||
const preconditions = {
|
||||
|
||||
@@ -32,8 +32,6 @@ tags:
|
||||
description: Voip Carriers operations
|
||||
- name: Sip Gateways
|
||||
description: Sip Gateways operations
|
||||
- name: Smpp Gateways
|
||||
description: Smpp Gateways operations
|
||||
- name: Webhooks
|
||||
description: Webhooks operations
|
||||
- name: Microsoft Teams Tenants
|
||||
@@ -221,46 +219,6 @@ paths:
|
||||
description: sbc address deleted
|
||||
404:
|
||||
description: sbc address not found
|
||||
/Smpps:
|
||||
get:
|
||||
summary: retrieve public IP addresses of the jambonz smpp servers
|
||||
operationId: listSmpps
|
||||
parameters:
|
||||
- in: query
|
||||
name: service_provider_sid
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
description: return only the smpp servers operated for the sole use of this service provider
|
||||
responses:
|
||||
200:
|
||||
description: list of smpp server addresses
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
properties:
|
||||
ipv4:
|
||||
type: string
|
||||
description: ip address of one of our Sbcs
|
||||
port:
|
||||
type: number
|
||||
use_tls:
|
||||
type: boolean
|
||||
is_primary:
|
||||
type: boolean
|
||||
required:
|
||||
- ipv4
|
||||
- port
|
||||
- use_tls
|
||||
- is_primary
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
/InviteCodes:
|
||||
post:
|
||||
summary: validate an invite code
|
||||
@@ -1437,156 +1395,6 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
/SmppGateways:
|
||||
post:
|
||||
tags:
|
||||
- Smpp Gateways
|
||||
summary: create smpp gateway
|
||||
operationId: createSmppGateway
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
voip_carrier_sid:
|
||||
type: string
|
||||
description: voip carrier that provides this gateway
|
||||
format: uuid
|
||||
ipv4:
|
||||
type: string
|
||||
port:
|
||||
type: number
|
||||
netmask:
|
||||
type: number
|
||||
inbound:
|
||||
type: boolean
|
||||
outbound:
|
||||
type: boolean
|
||||
is_primary:
|
||||
type: boolean
|
||||
use_tls:
|
||||
type: boolean
|
||||
required:
|
||||
- voip_carrier_sid
|
||||
- ipv4
|
||||
responses:
|
||||
201:
|
||||
description: smpp gateway successfully created
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/SuccessfulAdd'
|
||||
400:
|
||||
description: bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
422:
|
||||
description: unprocessable entity
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
get:
|
||||
tags:
|
||||
- Smpp Gateways
|
||||
summary: list smpp gateways
|
||||
operationId: listSmppGateways
|
||||
responses:
|
||||
200:
|
||||
description: list of smpp gateways
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/SmppGateway'
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
/SmppGateways/{SmppGatewaySid}:
|
||||
parameters:
|
||||
- name: SmppGatewaySid
|
||||
in: path
|
||||
required: true
|
||||
style: simple
|
||||
explode: false
|
||||
schema:
|
||||
type: string
|
||||
delete:
|
||||
tags:
|
||||
- Smpp Gateways
|
||||
summary: delete a smpp gateway
|
||||
operationId: deleteSmppGateway
|
||||
responses:
|
||||
204:
|
||||
description: smpp gateway successfully deleted
|
||||
404:
|
||||
description: smpp gateway not found
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
get:
|
||||
tags:
|
||||
- Smpp Gateways
|
||||
summary: retrieve smpp gateway
|
||||
operationId: getSmppGateway
|
||||
responses:
|
||||
200:
|
||||
description: smpp gateway found
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/SmppGateway'
|
||||
404:
|
||||
description: smpp gateway not found
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
put:
|
||||
tags:
|
||||
- Smpp Gateways
|
||||
summary: update smpp gateway
|
||||
operationId: updateSmppGateway
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/SmppGateway'
|
||||
responses:
|
||||
204:
|
||||
description: smpp gateway updated
|
||||
400:
|
||||
description: bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
404:
|
||||
description: smpp gateway not found
|
||||
500:
|
||||
description: system error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
/PhoneNumbers:
|
||||
post:
|
||||
tags:
|
||||
@@ -3738,9 +3546,6 @@ paths:
|
||||
call_status_hook:
|
||||
$ref: '#/components/schemas/Webhook'
|
||||
description: webhook to report call status events
|
||||
messaging_hook:
|
||||
$ref: '#/components/schemas/Webhook'
|
||||
description: application webhook to handle inbound SMS/MMS messages
|
||||
app_json:
|
||||
type: string
|
||||
description: Voice Application Json, call_hook will not be invoked if app_json is provided
|
||||
@@ -4217,56 +4022,6 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GeneralError'
|
||||
/Accounts/{AccountSid}/Messages:
|
||||
post:
|
||||
tags:
|
||||
- Accounts
|
||||
summary: create an outgoing SMS message
|
||||
operationId: createMessage
|
||||
parameters:
|
||||
- name: AccountSid
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Message'
|
||||
responses:
|
||||
201:
|
||||
description: call successfully created
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
required:
|
||||
- sid
|
||||
properties:
|
||||
sid:
|
||||
type: string
|
||||
format: uuid
|
||||
example: 2531329f-fb09-4ef7-887e-84e648214436
|
||||
providerResponse:
|
||||
type: string
|
||||
480:
|
||||
description: temporary failure
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
required:
|
||||
- sid
|
||||
properties:
|
||||
sid:
|
||||
type: string
|
||||
format: uuid
|
||||
example: 2531329f-fb09-4ef7-887e-84e648214436
|
||||
message:
|
||||
type: string
|
||||
smpp_err_code:
|
||||
type: string
|
||||
400:
|
||||
description: bad request
|
||||
/Accounts/{AccountSid}/Queues:
|
||||
parameters:
|
||||
- name: AccountSid
|
||||
@@ -5238,17 +4993,6 @@ components:
|
||||
type: string
|
||||
is_active:
|
||||
type: boolean
|
||||
smpp_system_id:
|
||||
type: string
|
||||
smpp_password:
|
||||
type: string
|
||||
smpp_inbound_system_id:
|
||||
type: string
|
||||
smpp_inbound_password:
|
||||
type: string
|
||||
smpp_enquire_link_interval:
|
||||
type: number
|
||||
format: integer
|
||||
required:
|
||||
- voip_carrier_sid
|
||||
- name
|
||||
@@ -5277,35 +5021,6 @@ components:
|
||||
- ipv4
|
||||
- port
|
||||
- netmask
|
||||
SmppGateway:
|
||||
type: object
|
||||
properties:
|
||||
smpp_gateway_sid:
|
||||
type: string
|
||||
format: uuid
|
||||
ipv4:
|
||||
type: string
|
||||
port:
|
||||
type: number
|
||||
netmask:
|
||||
type: number
|
||||
voip_carrier_sid:
|
||||
type: string
|
||||
format: uuid
|
||||
is_primary:
|
||||
type: boolean
|
||||
use_tls:
|
||||
type: boolean
|
||||
inbound:
|
||||
type: boolean
|
||||
outbound:
|
||||
type: boolean
|
||||
required:
|
||||
- smpp_gateway_sid
|
||||
- voip_carrier_sid
|
||||
- ipv4
|
||||
- port
|
||||
- netmask
|
||||
Account:
|
||||
type: object
|
||||
properties:
|
||||
@@ -5346,9 +5061,6 @@ components:
|
||||
call_status_hook:
|
||||
$ref: '#/components/schemas/Webhook'
|
||||
description: webhhok for reporting call status events
|
||||
messaging_hook:
|
||||
$ref: '#/components/schemas/Webhook'
|
||||
description: application webhook for inbound SMS/MMS
|
||||
speech_synthesis_vendor:
|
||||
type: string
|
||||
speech_synthesis_voice:
|
||||
@@ -5681,8 +5393,6 @@ components:
|
||||
type: string
|
||||
call_status_hook_sid:
|
||||
type: string
|
||||
messaging_hook_sid:
|
||||
type: string
|
||||
speech_synthesis_vendor:
|
||||
type: string
|
||||
speech_synthesis_language:
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
const providers = new Map();
|
||||
let init = false;
|
||||
|
||||
function initProviders(logger) {
|
||||
if (init) return;
|
||||
if (process.env.JAMBONES_MESSAGING) {
|
||||
try {
|
||||
const obj = JSON.parse(process.env.JAMBONES_MESSAGING);
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
logger.debug({config: value}, `Adding SMS provider ${key}`);
|
||||
providers.set(key, value);
|
||||
}
|
||||
logger.info(`Configured ${providers.size} SMS providers`);
|
||||
} catch (err) {
|
||||
logger.error(err, `expected JSON for JAMBONES_MESSAGING : ${process.env.JAMBONES_MESSAGING}`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.info('no JAMBONES_MESSAGING env var, messaging is disabled');
|
||||
}
|
||||
init = true;
|
||||
}
|
||||
|
||||
function getProvider(logger, partner) {
|
||||
initProviders(logger);
|
||||
if (typeof partner === 'string') {
|
||||
const config = providers.get(partner);
|
||||
const arr = [partner, config];
|
||||
logger.debug({arr}, 'getProvider by name');
|
||||
return arr;
|
||||
}
|
||||
else if (providers.size) {
|
||||
const arr = providers.entries().next().value;
|
||||
logger.debug({arr}, 'getProvider by first available');
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getProvider;
|
||||
|
||||
Reference in new issue
Block a user