mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
wip: implemented listen, transcribe, play
This commit is contained in:
@@ -4,6 +4,7 @@ const request = require('request');
|
||||
//require('request-debug')(request);
|
||||
const uuidv4 = require('uuid/v4');
|
||||
const makeTask = require('./tasks/make_task');
|
||||
const normalizeJamones = require('./utils/normalize-jamones');
|
||||
const {CallStatus, CallDirection} = require('./utils/constants');
|
||||
|
||||
module.exports = function(srf, logger) {
|
||||
@@ -68,19 +69,21 @@ module.exports = function(srf, logger) {
|
||||
const call_sid = uuidv4();
|
||||
const method = (app.hook_http_method || 'POST').toUpperCase();
|
||||
const from = req.getParsedHeader('From');
|
||||
const qs = req.locals.callAttributes = {
|
||||
req.locals.callAttributes = {
|
||||
CallSid: call_sid,
|
||||
AccountSid: app.account_sid,
|
||||
From: req.callingNumber,
|
||||
To: req.calledNumber,
|
||||
CallStatus: CallStatus.Trying,
|
||||
SipStatus: 100,
|
||||
Direction: CallDirection.Inbound,
|
||||
CallerName: from.name || req.callingNumber,
|
||||
SipCallID: req.get('Call-ID'),
|
||||
SipCallID: req.get('Call-ID')
|
||||
};
|
||||
const qs = Object.assign({}, req.locals.callAttributes, {
|
||||
CallStatus: CallStatus.Trying,
|
||||
SipStatus: 100,
|
||||
RequestorIP: req.get('X-Forwarded-For'),
|
||||
RequestorName: req.get('X-Originating-Carrier')
|
||||
};
|
||||
});
|
||||
const opts = {
|
||||
url: app.call_hook,
|
||||
method,
|
||||
@@ -88,6 +91,7 @@ module.exports = function(srf, logger) {
|
||||
qs
|
||||
};
|
||||
if (app.hook_basic_auth_user && app.hook_basic_auth_password) {
|
||||
logger.debug(`using basic auth with ${app.hook_basic_auth_user}:${app.hook_basic_auth_password}`);
|
||||
Object.assign(opts, {auth: {user: app.hook_basic_auth_user, password: app.hook_basic_auth_password}});
|
||||
}
|
||||
if (method === 'POST') Object.assign(opts, {body: req.msg});
|
||||
@@ -95,26 +99,16 @@ module.exports = function(srf, logger) {
|
||||
request(opts, (err, response, body) => {
|
||||
if (err) {
|
||||
logger.error(err, `Error invoking callback ${app.call_hook}`);
|
||||
return res.send(603, 'Bad webhook');
|
||||
return res.send(500, 'Webhook Failure');
|
||||
}
|
||||
logger.debug(body, 'application payload');
|
||||
const taskData = Array.isArray(body) ? body : [body];
|
||||
app.tasks = [];
|
||||
for (const t in taskData) {
|
||||
try {
|
||||
const task = makeTask(logger, taskData[t]);
|
||||
app.tasks.push(task);
|
||||
} catch (err) {
|
||||
logger.error({err, data: taskData[t]}, `invalid web callback payload: ${err.message}`);
|
||||
res.send(500, 'Application Error', {
|
||||
headers: {
|
||||
'X-Reason': err.message
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
logger.debug(body, `application payload: ${body}`);
|
||||
try {
|
||||
app.tasks = normalizeJamones(logger, body).map((tdata) => makeTask(logger, tdata));
|
||||
next();
|
||||
} catch (err) {
|
||||
logger.error(err, 'Invalid Webhook Response');
|
||||
res.send(500);
|
||||
}
|
||||
if (!res.finalResponseSent) next();
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error(err, 'Error invoking web callback');
|
||||
|
||||
Reference in New Issue
Block a user