Feature/siprec server (#140)

* initial support for siprec/agent assist

* call siprec middleware

* logger fix

* remove verbs that are not valid in a siprec call session
This commit is contained in:
Dave Horton
2022-08-05 10:29:13 +01:00
committed by GitHub
parent bc3552dda7
commit 91204955c9
5 changed files with 382 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
const { v4: uuidv4 } = require('uuid');
const {CallDirection} = require('./utils/constants');
const {CallDirection, TaskName} = require('./utils/constants');
const {parseSiprecPayload} = require('./utils/siprec-utils');
const CallInfo = require('./session/call-info');
const HttpRequestor = require('./utils/http-requestor');
const WsRequestor = require('./utils/ws-requestor');
@@ -73,6 +74,35 @@ module.exports = function(srf, logger) {
next();
}
const handleSipRec = async(req, res, next) => {
if (Array.isArray(req.payload) && req.payload.length > 1) {
const {callId, logger} = req.locals;
logger.debug({payload: req.payload}, 'handling siprec call');
try {
const sdp = req.payload
.find((p) => p.type === 'application/sdp')
.content;
const {sdp1, sdp2, ...metadata} = await parseSiprecPayload(req, logger);
req.locals.calledNumber = metadata.caller.number;
req.locals.callingNumber = metadata.callee.number;
req.locals = {
...req.locals,
siprec: {
metadata,
sdp1,
sdp2
}
};
logger.info({callId, metadata, sdp}, 'successfully parsed SIPREC payload');
} catch (err) {
logger.info({callId}, 'Error parsing multipart payload');
return res.send(503);
}
}
next();
};
/**
* retrieve account information for the incoming call
*/
@@ -101,7 +131,10 @@ module.exports = function(srf, logger) {
* Within the system, we deal with E.164 numbers _without_ the leading '+
*/
function normalizeNumbers(req, res, next) {
const logger = req.locals.logger;
const {logger, siprec} = req.locals;
if (siprec) return next();
Object.assign(req.locals, {
calledNumber: req.calledNumber,
callingNumber: req.callingNumber
@@ -225,7 +258,7 @@ module.exports = function(srf, logger) {
*/
async function invokeWebCallback(req, res, next) {
const logger = req.locals.logger;
const {rootSpan, application:app} = req.locals;
const {rootSpan, siprec, application:app} = req.locals;
let span;
try {
if (app.tasks) {
@@ -261,6 +294,19 @@ module.exports = function(srf, logger) {
});
span.end();
if (0 === app.tasks.length) throw new Error('no application provided');
if (siprec) {
/* only transcribe and/or listen allowed on an incoming siprec call */
const tasks = app.tasks.filter((t) => [TaskName.Config, TaskName.Listen, TaskName.Transcribe].includes(t.name));
if (0 === tasks.length) {
logger.info({tasks: app.tasks}, 'only config, transcribe and/or listen allowed on an incoming siprec call');
throw new Error('invalid verbs for incoming siprec call');
}
if (tasks.length < app.tasks.length) {
logger.info('removing verbs that are not allowed for incoming siprec call');
app.tasks = tasks;
}
}
next();
} catch (err) {
span?.setAttributes({webhookStatus: err.statusCode});
@@ -274,6 +320,7 @@ module.exports = function(srf, logger) {
return {
initLocals,
createRootSpan,
handleSipRec,
getAccountDetails,
normalizeNumbers,
retrieveApplication,