mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2026-02-09 15:10:14 +00:00
Compare commits
135 Commits
snyk-upgra
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4855fec4f5 | ||
|
|
a11822609f | ||
|
|
8dd9bfbb74 | ||
|
|
cee21c3dbc | ||
|
|
e2753ca8a3 | ||
|
|
df302320ca | ||
|
|
83a3bb61fe | ||
|
|
c36b55303a | ||
|
|
60d7f0f31c | ||
|
|
b089bd4663 | ||
|
|
5c8f91c1c1 | ||
|
|
50f926bce4 | ||
|
|
2a61d21bff | ||
|
|
418baa20df | ||
|
|
eb418a42e9 | ||
|
|
57ba79f908 | ||
|
|
3a791a67b5 | ||
|
|
91108fa3ef | ||
|
|
36c723d8f0 | ||
|
|
bc59fc80c9 | ||
|
|
6960466afc | ||
|
|
09c2608114 | ||
|
|
f1c17e537d | ||
|
|
22dad4eed6 | ||
|
|
017bc39103 | ||
|
|
8b982f20d6 | ||
|
|
b6d0d4cb0e | ||
|
|
4636b487b4 | ||
|
|
fce40a47ce | ||
|
|
1fd94dce94 | ||
|
|
efba631282 | ||
|
|
2fc3febcf6 | ||
|
|
cc67132dfa | ||
|
|
f8e88f085f | ||
|
|
1050eb47cd | ||
|
|
4e2feda7f3 | ||
|
|
1685262658 | ||
|
|
6fc9a3567e | ||
|
|
fb33574861 | ||
|
|
d2732c9be6 | ||
|
|
294d38dcd1 | ||
|
|
7bdac328bf | ||
|
|
2e0f4b94dc | ||
|
|
53e5360ab3 | ||
|
|
8163c33462 | ||
|
|
7e9b6498c5 | ||
|
|
a1c59a5f25 | ||
|
|
52a2ce8a86 | ||
|
|
cfe9397502 | ||
|
|
dda3335060 | ||
|
|
2738299524 | ||
|
|
e872d314b3 | ||
|
|
b1683bc294 | ||
|
|
78869e4bb8 | ||
|
|
ba994af012 | ||
|
|
e579514321 | ||
|
|
baed1b0eac | ||
|
|
b9dfecceff | ||
|
|
c7c99f45a4 | ||
|
|
67d18b26ff | ||
|
|
53cfbc1f56 | ||
|
|
da35449c16 | ||
|
|
83c015c839 | ||
|
|
f1f21fb23b | ||
|
|
c88ead7f71 | ||
|
|
b41c1ffb91 | ||
|
|
cc98f40d44 | ||
|
|
11035264ec | ||
|
|
5389083107 | ||
|
|
9657017669 | ||
|
|
b532a49e45 | ||
|
|
97d7a60994 | ||
|
|
2329f0cda0 | ||
|
|
6dbbbf8c9e | ||
|
|
4ea9707e4e | ||
|
|
e20c472bd2 | ||
|
|
f2421bb3dd | ||
|
|
18a141edc6 | ||
|
|
30a4e0e6a3 | ||
|
|
924627a50c | ||
|
|
d76a5e6efb | ||
|
|
cdfb1fff1e | ||
|
|
5c1501b6c7 | ||
|
|
161e67ece5 | ||
|
|
65b04d7cbf | ||
|
|
8a641f1d9a | ||
|
|
95188f59ec | ||
|
|
4e76077dc9 | ||
|
|
3c75d5a489 | ||
|
|
3164e1ea4e | ||
|
|
c9e3e97d53 | ||
|
|
ed157c6aee | ||
|
|
c395109966 | ||
|
|
7228594f79 | ||
|
|
28cde62d5d | ||
|
|
db6f56a068 | ||
|
|
9f757b439f | ||
|
|
35114b22d8 | ||
|
|
68b2ad526a | ||
|
|
b8ef1dba73 | ||
|
|
36683dc151 | ||
|
|
ce738a7852 | ||
|
|
77a696a0dc | ||
|
|
62ff44540d | ||
|
|
e5821cddf8 | ||
|
|
25567a7842 | ||
|
|
40bd3c9c88 | ||
|
|
2ce902c00d | ||
|
|
8c00c89882 | ||
|
|
dcd6ddcbca | ||
|
|
9a71350875 | ||
|
|
1bca165fc1 | ||
|
|
b94605127e | ||
|
|
27d6d32359 | ||
|
|
142f5d409f | ||
|
|
da4a7184a4 | ||
|
|
2c72bf50cd | ||
|
|
b27f349fc6 | ||
|
|
138aa5836a | ||
|
|
e1a023c21e | ||
|
|
8acb4d1a24 | ||
|
|
26d4bfb63b | ||
|
|
45dcab8517 | ||
|
|
27e3cba00b | ||
|
|
097f36cb00 | ||
|
|
752eed428f | ||
|
|
afb874aabc | ||
|
|
59227febf9 | ||
|
|
8593f12b51 | ||
|
|
3bf1984854 | ||
|
|
0e45e9b27c | ||
|
|
b0a8a6828d | ||
|
|
27d4ad5674 | ||
|
|
d38e77c06c | ||
|
|
c9e2a162c2 |
@@ -1,4 +1,4 @@
|
||||
FROM node:16
|
||||
FROM node:17.4-slim
|
||||
WORKDIR /opt/app/
|
||||
COPY package.json ./
|
||||
RUN npm install
|
||||
|
||||
12
app.js
12
app.js
@@ -7,7 +7,7 @@ assert.ok(process.env.DRACHTIO_PORT || process.env.DRACHTIO_HOST, 'missing DRACH
|
||||
assert.ok(process.env.DRACHTIO_SECRET, 'missing DRACHTIO_SECRET env var');
|
||||
assert.ok(process.env.JAMBONES_FREESWITCH, 'missing JAMBONES_FREESWITCH env var');
|
||||
assert.ok(process.env.JAMBONES_REDIS_HOST, 'missing JAMBONES_REDIS_HOST env var');
|
||||
assert.ok(process.env.JAMBONES_NETWORK_CIDR, 'missing JAMBONES_SUBNET env var');
|
||||
assert.ok(process.env.JAMBONES_NETWORK_CIDR || process.env.K8S, 'missing JAMBONES_SUBNET env var');
|
||||
|
||||
const Srf = require('drachtio-srf');
|
||||
const srf = new Srf();
|
||||
@@ -17,7 +17,7 @@ const opts = {
|
||||
level: process.env.JAMBONES_LOGLEVEL || 'info'
|
||||
};
|
||||
const logger = require('pino')(opts);
|
||||
const {LifeCycleEvents} = require('./lib/utils/constants');
|
||||
const {LifeCycleEvents, FS_UUID_SET_NAME} = require('./lib/utils/constants');
|
||||
const installSrfLocals = require('./lib/utils/install-srf-locals');
|
||||
installSrfLocals(srf, logger);
|
||||
|
||||
@@ -31,6 +31,7 @@ const {
|
||||
|
||||
// HTTP
|
||||
const express = require('express');
|
||||
const helmet = require('helmet');
|
||||
const app = express();
|
||||
Object.assign(app.locals, {
|
||||
logger,
|
||||
@@ -73,6 +74,8 @@ srf.invite((req, res) => {
|
||||
});
|
||||
|
||||
// HTTP
|
||||
app.use(helmet());
|
||||
app.use(helmet.hidePoweredBy());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use('/', httpRoutes);
|
||||
@@ -92,6 +95,10 @@ sessionTracker.on('idle', () => {
|
||||
}
|
||||
});
|
||||
|
||||
const getCount = () => sessionTracker.count;
|
||||
const healthCheck = require('@jambonz/http-health-check');
|
||||
healthCheck({app, logger, path: '/', fn: getCount});
|
||||
|
||||
setInterval(() => {
|
||||
srf.locals.stats.gauge('fs.sip.calls.count', sessionTracker.count);
|
||||
}, 5000);
|
||||
@@ -113,6 +120,7 @@ function handle(signal) {
|
||||
const setName = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:active-fs`;
|
||||
logger.info(`got signal ${signal}, removing ${srf.locals.localSipAddress} from set ${setName}`);
|
||||
removeFromSet(setName, srf.locals.localSipAddress);
|
||||
removeFromSet(FS_UUID_SET_NAME, srf.locals.fsUUID);
|
||||
srf.locals.disabled = true;
|
||||
}
|
||||
|
||||
|
||||
29
bin/k8s-pre-stop-hook.js
Executable file
29
bin/k8s-pre-stop-hook.js
Executable file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env node
|
||||
const bent = require('bent');
|
||||
const getJSON = bent('json');
|
||||
const PORT = process.env.HTTP_PORT || 3000;
|
||||
|
||||
const sleep = (ms) => {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
};
|
||||
|
||||
(async function() {
|
||||
|
||||
try {
|
||||
do {
|
||||
const obj = await getJSON(`http://127.0.0.1:${PORT}/`);
|
||||
const {calls} = obj;
|
||||
if (calls === 0) {
|
||||
console.log('no calls on the system, we can exit');
|
||||
process.exit(0);
|
||||
}
|
||||
else {
|
||||
console.log(`waiting for ${calls} to exit..`);
|
||||
}
|
||||
await sleep(10000);
|
||||
} while (1);
|
||||
} catch (err) {
|
||||
console.error(err, 'Error querying health endpoint');
|
||||
process.exit(-1);
|
||||
}
|
||||
})();
|
||||
@@ -35,6 +35,8 @@ router.post('/', async(req, res) => {
|
||||
|
||||
opts.headers = {
|
||||
...opts.headers,
|
||||
'X-Jambonz-Routing': target.type,
|
||||
'X-Jambonz-FS-UUID': srf.locals.fsUUID,
|
||||
'X-Call-Sid': callSid,
|
||||
'X-Account-Sid': req.body.account_sid
|
||||
};
|
||||
|
||||
@@ -9,8 +9,4 @@ api.use('/enqueue', require('./enqueue'));
|
||||
api.use('/messaging', require('./messaging')); // inbound SMS
|
||||
api.use('/createMessage', require('./create-message')); // outbound SMS (REST)
|
||||
|
||||
// health checks
|
||||
api.get('/', (req, res) => res.sendStatus(200));
|
||||
api.get('/health', (req, res) => res.sendStatus(200));
|
||||
|
||||
module.exports = api;
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
const express = require('express');
|
||||
const api = require('./api');
|
||||
const routes = express.Router();
|
||||
const sessionTracker = require('../session/session-tracker');
|
||||
|
||||
const readiness = (req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
const {count} = sessionTracker;
|
||||
const {srf} = require('../..');
|
||||
const {getFreeswitch} = srf.locals;
|
||||
if (getFreeswitch()) {
|
||||
return res.status(200).json({calls: count});
|
||||
}
|
||||
logger.info('responding to /health check with failure as freeswitch is not up');
|
||||
res.sendStatus(480);
|
||||
};
|
||||
|
||||
routes.use('/v1', api);
|
||||
|
||||
// health checks
|
||||
routes.get('/', (req, res) => {
|
||||
res.sendStatus(200);
|
||||
});
|
||||
|
||||
routes.get('/health', (req, res) => {
|
||||
res.sendStatus(200);
|
||||
});
|
||||
// health check
|
||||
routes.get('/health', readiness);
|
||||
|
||||
module.exports = routes;
|
||||
|
||||
@@ -8,7 +8,13 @@ const normalizeJambones = require('./utils/normalize-jambones');
|
||||
const dbUtils = require('./utils/db-utils');
|
||||
|
||||
module.exports = function(srf, logger) {
|
||||
const {lookupAppByPhoneNumber, lookupAppBySid, lookupAppByRealm, lookupAppByTeamsTenant} = srf.locals.dbHelpers;
|
||||
const {
|
||||
lookupAppByPhoneNumber,
|
||||
lookupAppByRegex,
|
||||
lookupAppBySid,
|
||||
lookupAppByRealm,
|
||||
lookupAppByTeamsTenant
|
||||
} = srf.locals.dbHelpers;
|
||||
const {lookupAccountDetails} = dbUtils(logger, srf);
|
||||
function initLocals(req, res, next) {
|
||||
const callSid = req.has('X-Retain-Call-Sid') ? req.get('X-Retain-Call-Sid') : uuidv4();
|
||||
@@ -112,7 +118,15 @@ module.exports = function(srf, logger) {
|
||||
logger.error(err, `Error retrieving transferred call app for ${arr[1]}`);
|
||||
}
|
||||
}
|
||||
else app = await lookupAppByPhoneNumber(req.locals.calledNumber);
|
||||
else {
|
||||
const voip_carrier_sid = req.get('X-Voip-Carrier-Sid');
|
||||
app = await lookupAppByPhoneNumber(req.locals.calledNumber, voip_carrier_sid);
|
||||
|
||||
if (!app) {
|
||||
/* lookup by call_routes.regex */
|
||||
app = await lookupAppByRegex(req.locals.calledNumber, account_sid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!app || !app.call_hook || !app.call_hook.url) {
|
||||
|
||||
@@ -264,6 +264,12 @@ class CallSession extends Emitter {
|
||||
region: credential.region
|
||||
};
|
||||
}
|
||||
else if ('wellsaid' === vendor) {
|
||||
return {
|
||||
speech_credential_sid: credential.speech_credential_sid,
|
||||
api_key: credential.api_key
|
||||
};
|
||||
}
|
||||
}
|
||||
else {
|
||||
writeAlerts({
|
||||
@@ -780,9 +786,19 @@ class CallSession extends Emitter {
|
||||
|
||||
async _onReinvite(req, res) {
|
||||
try {
|
||||
const newSdp = await this.ep.modify(req.body);
|
||||
res.send(200, {body: newSdp});
|
||||
this.logger.info({offer: req.body, answer: newSdp}, 'handling reINVITE');
|
||||
if (this.ep) {
|
||||
const newSdp = await this.ep.modify(req.body);
|
||||
res.send(200, {body: newSdp});
|
||||
this.logger.info({offer: req.body, answer: newSdp}, 'handling reINVITE');
|
||||
}
|
||||
else if (this.currentTask && this.currentTask.name === TaskName.Dial) {
|
||||
this.logger.info('handling reINVITE after media has been released');
|
||||
await this.currentTask.handleReinviteAfterMediaReleased(req, res);
|
||||
}
|
||||
else {
|
||||
this.logger.info('got reINVITE but no endpoint and media has not been released');
|
||||
res.send(488);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error(err, 'Error handling reinvite');
|
||||
}
|
||||
@@ -953,6 +969,13 @@ class CallSession extends Emitter {
|
||||
});
|
||||
}
|
||||
|
||||
async handleReinviteAfterMediaReleased(req, res) {
|
||||
assert(this.dlg && this.dlg.connected && !this.ep);
|
||||
const sdp = await this.dlg.modify(req.body);
|
||||
this.logger.info({sdp}, 'CallSession:handleReinviteAfterMediaReleased - reinvite to A leg returned sdp');
|
||||
res.send(200, {body: sdp});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called any time call status changes. This method both invokes the
|
||||
* call_status_hook callback as well as updates the realtime database
|
||||
|
||||
470
lib/tasks/cognigy/index.js
Normal file
470
lib/tasks/cognigy/index.js
Normal file
@@ -0,0 +1,470 @@
|
||||
const Task = require('../task');
|
||||
const {TaskName, TaskPreconditions} = require('../../utils/constants');
|
||||
const makeTask = require('../make_task');
|
||||
const { SocketClient } = require('@cognigy/socket-client');
|
||||
const SpeechConfig = require('./speech-config');
|
||||
const queue = require('queue');
|
||||
|
||||
const parseGallery = (obj = {}) => {
|
||||
const {_default} = obj;
|
||||
if (_default) {
|
||||
const {_gallery} = _default;
|
||||
if (_gallery) return _gallery.fallbackText;
|
||||
}
|
||||
};
|
||||
|
||||
const parseQuickReplies = (obj) => {
|
||||
const {_default} = obj;
|
||||
if (_default) {
|
||||
const {_quickReplies} = _default;
|
||||
if (_quickReplies) return _quickReplies.text || _quickReplies.fallbackText;
|
||||
}
|
||||
};
|
||||
|
||||
const parseBotText = (evt) => {
|
||||
const {text, data} = evt;
|
||||
if (text !== undefined) return String(text);
|
||||
|
||||
switch (data?.type) {
|
||||
case 'quickReplies':
|
||||
return parseQuickReplies(data?._cognigy);
|
||||
case 'gallery':
|
||||
return parseGallery(data?._cognigy);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
class Cognigy extends Task {
|
||||
constructor(logger, opts) {
|
||||
super(logger, opts);
|
||||
this.preconditions = TaskPreconditions.Endpoint;
|
||||
|
||||
this.url = this.data.url;
|
||||
this.token = this.data.token;
|
||||
this.prompt = this.data.prompt;
|
||||
this.eventHook = this.data?.eventHook;
|
||||
this.actionHook = this.data?.actionHook;
|
||||
this.data = this.data.data || {};
|
||||
this.prompts = [];
|
||||
this.retry = {};
|
||||
this.timeoutCount = 0;
|
||||
// create a task queue so we can execute our taskss subsequently
|
||||
// also executing tasks whenever they come in
|
||||
|
||||
this.taskQueue = queue({concurrency: 1, autostart: 1});
|
||||
this.changeConfigTasks = [];
|
||||
|
||||
// keep track of turns so we only do gather once per turn
|
||||
this.turn = 0;
|
||||
this.gatherTurn = 0;
|
||||
}
|
||||
|
||||
get name() { return TaskName.Cognigy; }
|
||||
|
||||
get hasReportedFinalAction() {
|
||||
return this.reportedFinalAction || this.isReplacingApplication;
|
||||
}
|
||||
|
||||
async _enqueueTask(task) {
|
||||
let resolver;
|
||||
let rejector;
|
||||
|
||||
const boundTask = task.bind(this);
|
||||
const taskPromise = new Promise(async(resolve, reject) => {
|
||||
resolver = resolve;
|
||||
rejector = reject;
|
||||
});
|
||||
taskPromise.resolve = resolver;
|
||||
this.taskQueue.push(async(cb) => {
|
||||
this.logger.debug('executing task from queue');
|
||||
try {
|
||||
|
||||
const result = await boundTask();
|
||||
// if this is a config task, remove it from the config task storage,
|
||||
// as we have now executed it
|
||||
if(task.isConfigTask){
|
||||
this.changeConfigTasks.shift();
|
||||
}
|
||||
resolver(result);
|
||||
cb(result);
|
||||
} catch (err) {
|
||||
this.logger.error({err}, 'could not execute task in task queue');
|
||||
rejector(err);
|
||||
cb(err);
|
||||
}
|
||||
this.logger.debug('say task executed from queue');
|
||||
});
|
||||
|
||||
// if this is a config task, lets also push the config
|
||||
if(task.isConfigTask){
|
||||
this.changeConfigTasks.push(task);
|
||||
}
|
||||
if (this.taskQueue.lastPromise) {
|
||||
// resolve the previous promise for cleanup
|
||||
this.taskQueue.lastPromise.resolve({});
|
||||
}
|
||||
this.taskQueue.lastPromise = taskPromise;
|
||||
return taskPromise;
|
||||
}
|
||||
|
||||
async exec(cs, ep) {
|
||||
await super.exec(cs);
|
||||
|
||||
const opts = {
|
||||
synthesizer: this.data.synthesizer || {
|
||||
vendor: 'default',
|
||||
language: 'default',
|
||||
voice: 'default'
|
||||
},
|
||||
recognizer: this.data.recognizer || {
|
||||
vendor: 'default',
|
||||
language: 'default'
|
||||
},
|
||||
bargein: this.data.bargein || {},
|
||||
bot: this.data.bot || {},
|
||||
user: this.data.user || {},
|
||||
dtmf: this.data.dtmf || {}
|
||||
};
|
||||
this.config = new SpeechConfig({logger: this.logger, ep, opts});
|
||||
this.ep = ep;
|
||||
try {
|
||||
|
||||
/* set event handlers and start transcribing */
|
||||
this.on('transcription', this._onTranscription.bind(this, cs, ep));
|
||||
this.on('dtmf-collected', this._onDtmf.bind(this, cs, ep));
|
||||
this.on('timeout', this._onTimeout.bind(this, cs, ep));
|
||||
this.on('error', this._onError.bind(this, cs, ep));
|
||||
|
||||
/* connect to the bot and send initial data */
|
||||
this.client = new SocketClient(
|
||||
this.url,
|
||||
this.token,
|
||||
{
|
||||
sessionId: cs.callSid,
|
||||
channel: 'jambonz',
|
||||
forceWebsockets: true,
|
||||
reconnection: true,
|
||||
settings: {
|
||||
enableTypingIndicator: false
|
||||
}
|
||||
}
|
||||
);
|
||||
this.client.on('output', this._onBotUtterance.bind(this, cs, ep));
|
||||
this.client.on('error', this._onBotError.bind(this, cs, ep));
|
||||
this.client.on('finalPing', this._onBotFinalPing.bind(this, cs, ep));
|
||||
await this.client.connect();
|
||||
// todo make welcome message configurable (enable or disable it when
|
||||
// we start a conversation (should be enabled by defaul))
|
||||
this.client.sendMessage('Welcome Message', {...this.data, ...cs.callInfo});
|
||||
|
||||
await this.awaitTaskDone();
|
||||
} catch (err) {
|
||||
this.logger.error({err}, 'Cognigy error');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async kill(cs) {
|
||||
super.kill(cs);
|
||||
this.logger.debug('Cognigy:kill');
|
||||
|
||||
|
||||
this.removeAllListeners();
|
||||
this.transcribeTask && this.transcribeTask.kill();
|
||||
|
||||
this.client.removeAllListeners();
|
||||
if (this.client && this.client.connected) this.client.disconnect();
|
||||
|
||||
try {
|
||||
// end the task queue AFTER we have removed all listeneres since now we cannot get new stuff inside the queue
|
||||
this.taskQueue.end();
|
||||
} catch (err) {
|
||||
this.logger.error({err}, 'could not end tasks queue!!');
|
||||
}
|
||||
|
||||
|
||||
if (!this.hasReportedFinalAction) {
|
||||
this.reportedFinalAction = true;
|
||||
this.performAction({cognigyResult: 'caller hungup'})
|
||||
.catch((err) => this.logger.info({err}, 'cognigy - error w/ action webook'));
|
||||
}
|
||||
|
||||
if (this.ep.connected) {
|
||||
await this.ep.api('uuid_break', this.ep.uuid).catch((err) => this.logger.info(err, 'Error killing audio'));
|
||||
}
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a promt which will be sent to the consumer. We will create a say task if bargein is disabled
|
||||
* for session and nextTurn, else create a gather task.
|
||||
*/
|
||||
_createPromtTask({text, url, turnConfig, listenAfterSpeech} = {}) {
|
||||
const bargeInOnNextTurn = turnConfig?.bargein?.enable?.length > 0;
|
||||
const bargeInSession = this.config.bargeInEnabled;
|
||||
if (bargeInOnNextTurn || bargeInSession) {
|
||||
return this._makeGatherTask({textPrompt: text, url, turnConfig, listenAfterSpeech});
|
||||
}
|
||||
return this._makeSayTask({text, turnConfig});
|
||||
}
|
||||
|
||||
_makeGatherTask({textPrompt, urlPrompt, turnConfig, listenAfterSpeech} = {}) {
|
||||
this.logger.debug({textPrompt, urlPrompt, turnConfig}, '_makeGatherTask');
|
||||
const config = this.config.makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig, listenAfterSpeech});
|
||||
const {retry, ...rest} = config;
|
||||
this.retry = retry;
|
||||
const gather = makeTask(this.logger, {gather: rest}, this);
|
||||
return gather;
|
||||
}
|
||||
|
||||
_makeSayTask({ text, turnConfig } = {}) {
|
||||
this.logger.debug({text, turnConfig}, '_makeSayTask');
|
||||
const config = this.config.makeSayTaskConfig({text, turnConfig });
|
||||
this.logger.debug({config}, 'created say task config');
|
||||
const say = makeTask(this.logger, { say: config }, this);
|
||||
return say;
|
||||
}
|
||||
|
||||
_makeReferTask(referTo) {
|
||||
return makeTask(this.logger, {'sip:refer': {
|
||||
referTo
|
||||
}}
|
||||
);
|
||||
}
|
||||
|
||||
_makeHangupTask(reason) {
|
||||
return makeTask(this.logger, {
|
||||
hangup: {
|
||||
headers: {
|
||||
'X-Reason': reason
|
||||
}
|
||||
}});
|
||||
}
|
||||
|
||||
_makePlayTask(url, loop) {
|
||||
return makeTask(this.logger, {
|
||||
play: {
|
||||
url,
|
||||
loop
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* if we need to interrupt the currently-running say task(s), call this */
|
||||
_killSayTasks(ep) {
|
||||
if (ep && ep.connected) {
|
||||
ep.api('uuid_break', this.ep.uuid)
|
||||
.catch((err) => this.logger.info({err}, 'Cognigy:_killSayTasks - error killing audio for current say task'));
|
||||
}
|
||||
}
|
||||
|
||||
async _onBotError(cs, ep, evt) {
|
||||
this.logger.info({evt}, 'Cognigy:_onBotError');
|
||||
this.performAction({cognigyResult: 'botError', message: evt.message });
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
|
||||
async _onBotFinalPing(cs, ep) {
|
||||
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
|
||||
try {
|
||||
// lets wait until we have finished processing the speech before
|
||||
// starting a gather...
|
||||
this.logger.debug('enqueued bot final ping gather');
|
||||
this._enqueueTask(async() => {
|
||||
this.logger.debug('executing bot final ping gather');
|
||||
try {
|
||||
const gatherTask = this._makeGatherTask();
|
||||
await gatherTask.exec(cs, ep, this);
|
||||
} catch (err) {
|
||||
this.logger.info({err}, 'Cognigy final ping gather task returned error');
|
||||
}
|
||||
this.logger.debug('executed bot final ping gather');
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
this.logger.info({err}, 'Cognigy gather task returned error');
|
||||
}
|
||||
}
|
||||
|
||||
async _onBotUtterance(cs, ep, evt) {
|
||||
this.logger.debug({evt}, 'Cognigy:_onBotUtterance');
|
||||
|
||||
if (this.eventHook) {
|
||||
this.performHook(cs, this.eventHook, {event: 'botMessage', message: evt})
|
||||
.then((redirected) => {
|
||||
if (redirected) {
|
||||
this.logger.info('Cognigy_onBotUtterance: event handler for bot message redirected us to new webhook');
|
||||
this.reportedFinalAction = true;
|
||||
this.performAction({cognigyResult: 'redirect'}, false);
|
||||
}
|
||||
return;
|
||||
})
|
||||
.catch(({err}) => {
|
||||
this.logger.info({err}, 'Cognigy_onBotUtterance: error sending event hook');
|
||||
});
|
||||
}
|
||||
|
||||
const text = parseBotText(evt);
|
||||
|
||||
// only add say task if its a normal cognigy node and not a "gather task"
|
||||
if (text && (evt?.data?.type !== 'promt')) {
|
||||
this.logger.info({text}, 'received text');
|
||||
this._enqueueTask(async() => {
|
||||
// todo inject the session config into the say task
|
||||
const promtTask = this._createPromtTask({ text, listenAfterSpeech: false });
|
||||
await promtTask.exec(cs, ep, this);
|
||||
this.logger.debug({text}, 'executed say task');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
switch (evt?.data?.type) {
|
||||
case 'hangup':
|
||||
this._enqueueTask(async() => {
|
||||
this.performAction({cognigyResult: 'hangup Succeeded'});
|
||||
this.reportedFinalAction = true;
|
||||
cs.replaceApplication([this._makeHangupTask(evt.data.reason)]);
|
||||
this.taskQueue.end();
|
||||
});
|
||||
return;
|
||||
case 'refer':
|
||||
this._enqueueTask(async() => {
|
||||
this.performAction({cognigyResult: 'refer succeeded'});
|
||||
this.reportedFinalAction = true;
|
||||
cs.replaceApplication([this._makeReferTask(evt.data.referTo)]);
|
||||
});
|
||||
return;
|
||||
case 'promt':
|
||||
this._enqueueTask(async() => {
|
||||
const sayTask = this._createPromtTask({
|
||||
text: evt.data.text,
|
||||
turnConfig: evt?.data?.config?.nextTurn,
|
||||
url: evt.data.url
|
||||
});
|
||||
try {
|
||||
await sayTask.exec(cs, ep, this);
|
||||
} catch (err) {
|
||||
this.logger.info({err}, 'Cognigy sayTask task returned error');
|
||||
}
|
||||
});
|
||||
return;
|
||||
case 'setSessionConfig':
|
||||
// change session params in the order they come in with the say tasks
|
||||
// so we are consistent with the flow logic executed within cognigy
|
||||
const updateConfigTask = () => {
|
||||
if (evt?.data?.config?.session) this.config.update(evt.data.config.session);
|
||||
};
|
||||
updateConfigTask.isConfigTask = true;
|
||||
this._enqueueTask(updateConfigTask);
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.info({err, evtData: evt.data}, 'encountered error parsing cognigy response data');
|
||||
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err});
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
|
||||
async _onTranscription(cs, ep, evt) {
|
||||
this.logger.debug({evt}, `Cognigy: got transcription for callSid ${cs.callSid}`);
|
||||
const utterance = evt.alternatives[0].transcript;
|
||||
|
||||
//if we have barge in enabled AND we enabled skipping until next question
|
||||
//then stop execution of currently queues bot output before sending the
|
||||
//response to waiting bot since otherwise we could stop upcoming bot output
|
||||
|
||||
if (this.config.bargeInEnabled && this.config.skipToBotOutputEnd !== false) {
|
||||
// clear task queue, resolve the last promise and cleanup;
|
||||
this._killSayTasks();
|
||||
this.taskQueue.lastPromise.resolve();
|
||||
this.taskQueue.end();
|
||||
while(this.changeConfigTasks.length > 0){
|
||||
// apply all the config tasks FIFO
|
||||
const changeConfigTask = this.changeConfigTasks.shift();
|
||||
changeConfigTask();
|
||||
}
|
||||
this.taskQueue.autostart = true;
|
||||
}
|
||||
|
||||
if (this.eventHook) {
|
||||
this.performHook(cs, this.eventHook, {event: 'userMessage', message: utterance})
|
||||
.then((redirected) => {
|
||||
if (redirected) {
|
||||
this.logger.info('Cognigy_onTranscription: event handler for user message redirected us to new webhook');
|
||||
this.reportedFinalAction = true;
|
||||
this.performAction({cognigyResult: 'redirect'}, false);
|
||||
if (this.transcribeTask) this.transcribeTask.kill(cs);
|
||||
}
|
||||
return;
|
||||
})
|
||||
.catch(({err}) => {
|
||||
this.logger.info({err}, 'Cognigy_onTranscription: error sending event hook');
|
||||
});
|
||||
}
|
||||
|
||||
/* send the user utterance to the bot */
|
||||
try {
|
||||
if (this.client && this.client.connected) {
|
||||
this.client.sendMessage(utterance);
|
||||
}
|
||||
else {
|
||||
// if the bot is not connected, should we maybe throw an error here?
|
||||
this.logger.info('Cognigy_onTranscription - not sending user utterance as bot is disconnected');
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error({err}, 'Cognigy_onTranscription: Error sending user utterance to Cognigy - ending task');
|
||||
this.performAction({cognigyResult: 'socketError'});
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
|
||||
_onDtmf(cs, ep, evt) {
|
||||
this.logger.info({evt}, 'got dtmf');
|
||||
|
||||
/* send dtmf to bot */
|
||||
try {
|
||||
if (this.client && this.client.connected) {
|
||||
this.client.sendMessage(String(evt.digits));
|
||||
}
|
||||
else {
|
||||
// if the bot is not connected, should we maybe throw an error here?
|
||||
this.logger.info('Cognigy_onTranscription - not sending user dtmf as bot is disconnected');
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error({err}, '_onDtmf: Error sending user dtmf to Cognigy - ending task');
|
||||
this.performAction({cognigyResult: 'socketError'});
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
_onError(cs, ep, err) {
|
||||
this.logger.info({err}, 'Cognigy: got error');
|
||||
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err});
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
|
||||
_onTimeout(cs, ep, evt) {
|
||||
const {noInputRetries, noInputSpeech, noInputUrl} = this.retry;
|
||||
this.logger.debug({evt, retry: this.retry}, 'Cognigy: got timeout');
|
||||
if (noInputRetries && this.timeoutCount++ < noInputRetries) {
|
||||
const gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl});
|
||||
gatherTask.exec(cs, ep, this)
|
||||
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
|
||||
}
|
||||
else {
|
||||
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'timeout'});
|
||||
this.reportedFinalAction = true;
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Cognigy;
|
||||
148
lib/tasks/cognigy/speech-config.js
Normal file
148
lib/tasks/cognigy/speech-config.js
Normal file
@@ -0,0 +1,148 @@
|
||||
const obj = require('drachtio-fsmrf/lib/utils');
|
||||
const Emitter = require('events');
|
||||
const { isArray } = require('lodash');
|
||||
const lodash = require('lodash');
|
||||
const hasKeys = (obj) => typeof obj === 'object' && Object.keys(obj) > 0;
|
||||
|
||||
const stripNulls = (obj) => {
|
||||
Object.keys(obj).forEach((k) => (obj[k] === null || typeof obj[k] === 'undefined') && delete obj[k]);
|
||||
return obj;
|
||||
};
|
||||
|
||||
class SpeechConfig extends Emitter {
|
||||
constructor({logger, ep, opts = {}}) {
|
||||
super();
|
||||
this.logger = logger;
|
||||
this.ep = ep;
|
||||
this.sessionConfig = opts.session || {};
|
||||
this.update(opts);
|
||||
}
|
||||
|
||||
_mergeConfig(changedConfig = {}) {
|
||||
const merged = lodash.mergeWith(
|
||||
this.sessionConfig,
|
||||
changedConfig,
|
||||
(objValue, sourceValue) => {
|
||||
if (Array.isArray(objValue)) {
|
||||
if (Array.isArray(sourceValue)) {
|
||||
return sourceValue;
|
||||
}
|
||||
return objValue;
|
||||
}
|
||||
}
|
||||
);
|
||||
this.logger.debug({merged, sessionConfig: this.sessionConfig, changedConfig}, 'merged config');
|
||||
// should we override hints with empty array or leave it as it is once saved?
|
||||
// merged.recognizer.hints = changedConfig.recognizer?.hints
|
||||
return merged;
|
||||
}
|
||||
|
||||
update(session) {
|
||||
// TODO validation of session params?
|
||||
if (session) {
|
||||
this.sessionConfig = this._mergeConfig(session);
|
||||
}
|
||||
this.logger.debug({sessionLevel: this.sessionConfig}, 'SpeechConfig updated');
|
||||
}
|
||||
|
||||
/**
|
||||
* check if we should skip all nodes until next bot input
|
||||
*/
|
||||
get skipUntilBotInput() {
|
||||
return !this.sessionConfig.bargein?.skipUntilBotInput;
|
||||
}
|
||||
/**
|
||||
* Check if barge is enabled on session level
|
||||
*/
|
||||
get bargeInEnabled() {
|
||||
return this.sessionConfig.bargein?.enable?.length > 0;
|
||||
}
|
||||
|
||||
makeSayTaskConfig({text, turnConfig = {}} = {}) {
|
||||
const synthesizer = lodash.merge({}, this.sessionConfig.synthesizer, turnConfig.synthesizer);
|
||||
return {
|
||||
text,
|
||||
synthesizer
|
||||
};
|
||||
}
|
||||
|
||||
makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig = {}, listenAfterSpeech} = {}) {
|
||||
// we merge from top to bottom deeply so we wil have
|
||||
// defaults from session config and then will override them via turn config
|
||||
const opts = this._mergeConfig(turnConfig);
|
||||
|
||||
this.logger.debug({
|
||||
opts,
|
||||
sessionConfig: this.sessionConfig,
|
||||
turnConfig
|
||||
}, 'Congigy SpeechConfig:_makeGatherTask current options');
|
||||
|
||||
/* input type: speech and/or dtmf entry */
|
||||
const input = [];
|
||||
if (opts.recognizer) input.push('speech');
|
||||
if (hasKeys(opts.dtmf)) input.push('digits');
|
||||
|
||||
if (opts.synthesizer) {
|
||||
// todo remove this once we add support for disabling tts cache
|
||||
delete opts.synthesizer.disableTtsCache;
|
||||
}
|
||||
|
||||
/* bargein settings */
|
||||
const bargein = opts.bargein || {};
|
||||
const speechBargein = Array.isArray(bargein.enable) && bargein.enable.includes('speech');
|
||||
const dtmfBargein = Array.isArray(bargein.enable) && bargein.enable.includes('dtmf');
|
||||
const minBargeinWordCount = speechBargein ? (bargein.minWordCount || 1) : 0;
|
||||
const {interDigitTimeout = 0, maxDigits, minDigits = 1, submitDigit} = (opts.dtmf || {});
|
||||
const {noInputTimeout, noInputRetries, noInputSpeech, noInputUrl} = (opts.user || {});
|
||||
|
||||
let sayConfig;
|
||||
let playConfig;
|
||||
|
||||
if (textPrompt) {
|
||||
sayConfig = {
|
||||
text: textPrompt,
|
||||
synthesizer: opts.synthesizer
|
||||
};
|
||||
}
|
||||
|
||||
// todo what is the logic here if we put both? play over say or say over play?
|
||||
if (urlPrompt) {
|
||||
playConfig = {
|
||||
url: urlPrompt
|
||||
};
|
||||
}
|
||||
|
||||
const config = {
|
||||
input,
|
||||
listenDuringPrompt: speechBargein,
|
||||
bargein: speechBargein,
|
||||
minBargeinWordCount,
|
||||
dtmfBargein,
|
||||
minDigits,
|
||||
maxDigits,
|
||||
interDigitTimeout,
|
||||
finishOnKey: submitDigit,
|
||||
recognizer: opts?.recognizer,
|
||||
timeout: noInputTimeout,
|
||||
retry : {
|
||||
noInputRetries,
|
||||
noInputSpeech,
|
||||
noInputUrl
|
||||
},
|
||||
listenAfterSpeech
|
||||
};
|
||||
|
||||
const final = stripNulls(config);
|
||||
|
||||
const finalConfig = final;
|
||||
if (sayConfig) {
|
||||
finalConfig.say = sayConfig;
|
||||
} else if (playConfig) {
|
||||
finalConfig.play = playConfig;
|
||||
}
|
||||
this.logger.info({finalConfig}, 'created gather config');
|
||||
return finalConfig;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SpeechConfig;
|
||||
@@ -161,6 +161,10 @@ class TaskDial extends Task {
|
||||
|
||||
async kill(cs, reason) {
|
||||
super.kill(cs);
|
||||
if (this.dialMusic && this.epOther) {
|
||||
this.epOther.api('uuid_break', this.epOther.uuid)
|
||||
.catch((err) => this.logger.info(err, 'Error killing dialMusic'));
|
||||
}
|
||||
this.killReason = reason || KillReason.Hangup;
|
||||
if (this.timerMaxCallDuration) {
|
||||
clearTimeout(this.timerMaxCallDuration);
|
||||
@@ -175,6 +179,7 @@ class TaskDial extends Task {
|
||||
this._killOutdials();
|
||||
if (this.sd) {
|
||||
this.sd.kill();
|
||||
this.sd.removeAllListeners();
|
||||
this.sd = null;
|
||||
}
|
||||
if (this.callSid) sessionTracker.remove(this.callSid);
|
||||
@@ -433,6 +438,13 @@ class TaskDial extends Task {
|
||||
this.kill(cs);
|
||||
}
|
||||
})
|
||||
.on('reinvite', (req, res) => {
|
||||
try {
|
||||
cs.handleReinviteAfterMediaReleased(req, res);
|
||||
} catch (err) {
|
||||
this.logger.error(err, 'Error in dial einvite from B leg');
|
||||
}
|
||||
})
|
||||
.once('adulting', () => {
|
||||
/* child call just adulted and got its own session */
|
||||
this.logger.info('Dial:on_adulting: detaching child call leg');
|
||||
@@ -543,8 +555,7 @@ class TaskDial extends Task {
|
||||
try {
|
||||
const aLegSdp = cs.ep.remote.sdp;
|
||||
const bLegSdp = sd.dlg.remote.sdp;
|
||||
this.logger.debug({aLegSdp, bLegSdp}, 'Dial:_releaseMedia - releasing media from freewitch');
|
||||
await Promise.all[sd.releaseMediaToSBC(aLegSdp), cs.releaseMediaToSBC(bLegSdp)];
|
||||
await Promise.all[sd.releaseMediaToSBC(aLegSdp, cs.ep.local.sdp), cs.releaseMediaToSBC(bLegSdp)];
|
||||
this.epOther = null;
|
||||
this.logger.info('Dial:_releaseMedia - successfully released media from freewitch');
|
||||
} catch (err) {
|
||||
@@ -559,6 +570,12 @@ class TaskDial extends Task {
|
||||
await Promise.all([sd.reAnchorMedia(), cs.reAnchorMedia()]);
|
||||
this.epOther = cs.ep;
|
||||
}
|
||||
|
||||
async handleReinviteAfterMediaReleased(req, res) {
|
||||
const sdp = await this.dlg.modify(req.body);
|
||||
this.logger.info({sdp}, 'Dial:handleReinviteAfterMediaReleased - sent reinvite to B leg');
|
||||
res.send(200, {body: sdp});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TaskDial;
|
||||
|
||||
@@ -452,8 +452,8 @@ class Dialogflow extends Task {
|
||||
this.noinputTimer = setTimeout(this._onNoInput.bind(this, ep, cs), this.noInputTimeout);
|
||||
}
|
||||
|
||||
async _performHook(cs, hook, results) {
|
||||
const json = await this.cs.requestor.request(hook, results);
|
||||
async _performHook(cs, hook, results = {}) {
|
||||
const json = await this.cs.requestor.request(hook, {...results, ...cs.callInfo.toJSON()});
|
||||
if (json && Array.isArray(json)) {
|
||||
const makeTask = require('../make_task');
|
||||
const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata));
|
||||
|
||||
@@ -16,13 +16,17 @@ class TaskGather extends Task {
|
||||
this.preconditions = TaskPreconditions.Endpoint;
|
||||
|
||||
[
|
||||
'finishOnKey', 'hints', 'input', 'numDigits',
|
||||
'partialResultHook',
|
||||
'finishOnKey', 'hints', 'input', 'numDigits', 'minDigits', 'maxDigits',
|
||||
'interDigitTimeout', 'submitDigit', 'partialResultHook', 'bargein', 'dtmfBargein',
|
||||
'retries', 'retryPromptTts', 'retryPromptUrl',
|
||||
'speechTimeout', 'timeout', 'say', 'play'
|
||||
].forEach((k) => this[k] = this.data[k]);
|
||||
this.listenDuringPrompt = this.data.listenDuringPrompt === false ? false : true;
|
||||
this.minBargeinWordCount = this.data.minBargeinWordCount || 1;
|
||||
|
||||
this.timeout = (this.timeout || 5) * 1000;
|
||||
this.interim = this.partialResultCallback;
|
||||
this.logger.debug({opts}, 'created gather task');
|
||||
this.timeout = (this.timeout || 15) * 1000;
|
||||
this.interim = this.partialResultCallback || this.bargein;
|
||||
if (this.data.recognizer) {
|
||||
const recognizer = this.data.recognizer;
|
||||
this.vendor = recognizer.vendor;
|
||||
@@ -40,6 +44,10 @@ class TaskGather extends Task {
|
||||
this.profanityOption = recognizer.profanityOption || 'raw';
|
||||
this.requestSnr = recognizer.requestSnr || false;
|
||||
this.initialSpeechTimeoutMs = recognizer.initialSpeechTimeoutMs || 0;
|
||||
|
||||
/* vad: if provided, we dont connect to recognizer until voice activity is detected */
|
||||
const {enable, voiceMs = 0, mode = -1} = recognizer.vad || {};
|
||||
this.vad = {enable, voiceMs, mode};
|
||||
}
|
||||
|
||||
this.digitBuffer = '';
|
||||
@@ -48,6 +56,12 @@ class TaskGather extends Task {
|
||||
if (this.say) this.sayTask = makeTask(this.logger, {say: this.say}, this);
|
||||
if (this.play) this.playTask = makeTask(this.logger, {play: this.play}, this);
|
||||
|
||||
if(this.sayTask || this.playTask){
|
||||
// this is specially for barge in where we want to make a bargebale promt
|
||||
// to a user without listening after the say task has finished
|
||||
this.listenAfterSpeech = typeof this.data.listenAfterSpeech === "boolean" ? this.data.listenAfterSpeech : true;
|
||||
}
|
||||
|
||||
this.parentTask = parentTask;
|
||||
}
|
||||
|
||||
@@ -80,33 +94,67 @@ class TaskGather extends Task {
|
||||
throw new Error(`no speech-to-text service credentials for ${this.vendor} have been configured`);
|
||||
}
|
||||
|
||||
const startListening = (cs, ep) => {
|
||||
this._startTimer();
|
||||
if (this.input.includes('speech') && !this.listenDuringPrompt) {
|
||||
this.logger.debug('listening after speech 1');
|
||||
this._initSpeech(cs, ep)
|
||||
.then(() => {
|
||||
this._startTranscribing(ep);
|
||||
return updateSpeechCredentialLastUsed(this.sttCredentials.speech_credential_sid);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
if (this.sayTask) {
|
||||
this.sayTask.exec(cs, ep); // kicked off, _not_ waiting for it to complete
|
||||
this.sayTask.on('playDone', (err) => {
|
||||
if (!this.killed) this._startTimer();
|
||||
this.logger.debug('Gather: kicking off say task');
|
||||
this.sayTask.exec(cs, ep);
|
||||
this.sayTask.on('playDone', async(err) => {
|
||||
if (err) return this.logger.error({err}, 'Gather:exec Error playing tts');
|
||||
this.logger.debug('Gather: say task completed');
|
||||
if (!this.killed) {
|
||||
if (this.listenAfterSpeech === true) {
|
||||
this.logger.debug('listening after speech 2');
|
||||
startListening(cs, ep);
|
||||
} else {
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (this.playTask) {
|
||||
this.playTask.exec(cs, ep); // kicked off, _not_ waiting for it to complete
|
||||
this.playTask.on('playDone', (err) => {
|
||||
if (!this.killed) this._startTimer();
|
||||
});
|
||||
this.playTask.on('playDone', async(err) => {
|
||||
if (err) return this.logger.error({err}, 'Gather:exec Error playing url');
|
||||
if (!this.killed) {
|
||||
if (this.listenAfterSpeech === true) {
|
||||
this.logger.debug('listening after speech 3');
|
||||
startListening(cs, ep);
|
||||
} else {
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
else this._startTimer();
|
||||
else startListening(cs, ep);
|
||||
|
||||
if (this.input.includes('speech')) {
|
||||
if (this.input.includes('speech') && this.listenDuringPrompt) {
|
||||
this.logger.debug('listening after speech 4');
|
||||
await this._initSpeech(cs, ep);
|
||||
this._startTranscribing(ep);
|
||||
updateSpeechCredentialLastUsed(this.sttCredentials.speech_credential_sid)
|
||||
.catch(() => {/*already logged error */});
|
||||
}
|
||||
|
||||
if (this.input.includes('digits')) {
|
||||
if (this.input.includes('digits') || this.dtmfBargein) {
|
||||
ep.on('dtmf', this._onDtmf.bind(this, cs, ep));
|
||||
}
|
||||
|
||||
await this.awaitTaskDone();
|
||||
this.logger.debug('Gather:exec task has completed');
|
||||
} catch (err) {
|
||||
this.logger.error(err, 'TaskGather:exec error');
|
||||
}
|
||||
@@ -118,6 +166,7 @@ class TaskGather extends Task {
|
||||
}
|
||||
|
||||
kill(cs) {
|
||||
this.logger.debug('Gather:kill');
|
||||
super.kill(cs);
|
||||
this._killAudio(cs);
|
||||
this.ep.removeAllListeners('dtmf');
|
||||
@@ -126,17 +175,37 @@ class TaskGather extends Task {
|
||||
|
||||
_onDtmf(cs, ep, evt) {
|
||||
this.logger.debug(evt, 'TaskGather:_onDtmf');
|
||||
if (evt.dtmf === this.finishOnKey) this._resolve('dtmf-terminator-key');
|
||||
clearTimeout(this.interDigitTimer);
|
||||
let resolved = false;
|
||||
if (this.dtmfBargein) this._killAudio(cs);
|
||||
if (evt.dtmf === this.finishOnKey) {
|
||||
resolved = true;
|
||||
this._resolve('dtmf-terminator-key');
|
||||
}
|
||||
else {
|
||||
this.digitBuffer += evt.dtmf;
|
||||
if (this.digitBuffer.length === this.numDigits) this._resolve('dtmf-num-digits');
|
||||
const len = this.digitBuffer.length;
|
||||
if (len === this.numDigits || len === this.maxDigits) {
|
||||
resolved = true;
|
||||
this._resolve('dtmf-num-digits');
|
||||
}
|
||||
}
|
||||
|
||||
if (!resolved && this.interDigitTimeout > 0 && this.digitBuffer.length >= this.minDigits) {
|
||||
/* start interDigitTimer */
|
||||
const ms = this.interDigitTimeout * 1000;
|
||||
this.logger.debug(`starting interdigit timer of ${ms}`);
|
||||
this.interDigitTimer = setTimeout(() => this._resolve('dtmf-interdigit-timeout'), ms);
|
||||
}
|
||||
this._killAudio(cs);
|
||||
}
|
||||
|
||||
async _initSpeech(cs, ep) {
|
||||
const opts = {};
|
||||
|
||||
if (this.vad.enable) {
|
||||
opts.START_RECOGNIZING_ON_VAD = 1;
|
||||
if (this.vad.voiceMs) opts.RECOGNIZER_VAD_VOICE_MS = this.vad.voiceMs;
|
||||
if (this.vad.mode >= 0 && this.vad.mode <= 3) opts.RECOGNIZER_VAD_MODE = this.vad.mode;
|
||||
}
|
||||
if ('google' === this.vendor) {
|
||||
if (this.sttCredentials) opts.GOOGLE_APPLICATION_CREDENTIALS = JSON.stringify(this.sttCredentials.credentials);
|
||||
Object.assign(opts, {
|
||||
@@ -197,7 +266,7 @@ class TaskGather extends Task {
|
||||
ep.startTranscription({
|
||||
vendor: this.vendor,
|
||||
locale: this.language,
|
||||
interim: this.partialResultCallback ? true : false,
|
||||
interim: this.interim,
|
||||
}).catch((err) => {
|
||||
const {writeAlerts, AlertType} = this.cs.srf.locals;
|
||||
this.logger.error(err, 'TaskGather:_startTranscribing error');
|
||||
@@ -237,25 +306,46 @@ class TaskGather extends Task {
|
||||
}
|
||||
|
||||
_onTranscription(cs, ep, evt) {
|
||||
this.logger.debug(evt, 'TaskGather:_onTranscription');
|
||||
if ('aws' === this.vendor && Array.isArray(evt) && evt.length > 0) evt = evt[0];
|
||||
if ('microsoft' === this.vendor) {
|
||||
const nbest = evt.NBest;
|
||||
const newEvent = {
|
||||
is_final: evt.RecognitionStatus === 'Success',
|
||||
alternatives: [
|
||||
{
|
||||
confidence: nbest[0].Confidence,
|
||||
transcript: nbest[0].Display
|
||||
}
|
||||
]
|
||||
};
|
||||
evt = newEvent;
|
||||
const final = evt.RecognitionStatus === 'Success';
|
||||
if (final) {
|
||||
const nbest = evt.NBest;
|
||||
evt = {
|
||||
is_final: true,
|
||||
alternatives: [
|
||||
{
|
||||
confidence: nbest[0].Confidence,
|
||||
transcript: nbest[0].Display
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else {
|
||||
evt = {
|
||||
is_final: false,
|
||||
alternatives: [
|
||||
{
|
||||
transcript: evt.Text
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
this.logger.debug(evt, 'TaskGather:_onTranscription');
|
||||
if (evt.is_final) this._resolve('speech', evt);
|
||||
else if (this.partialResultHook) {
|
||||
this.cs.requestor.request(this.partialResultHook, Object.assign({speech: evt}, this.cs.callInfo))
|
||||
.catch((err) => this.logger.info(err, 'GatherTask:_onTranscription error'));
|
||||
else {
|
||||
if (evt.stability > 0.70 &&
|
||||
this.bargein &&
|
||||
evt.alternatives[0].transcript.split(' ').length >= this.minBargeinWordCount) {
|
||||
this.logger.debug('Gather:_onTranscription - killing audio due to bargein');
|
||||
this._killAudio(cs);
|
||||
this._resolve('speech', evt);
|
||||
}
|
||||
if (this.partialResultHook) {
|
||||
this.cs.requestor.request(this.partialResultHook, Object.assign({speech: evt}, this.cs.callInfo))
|
||||
.catch((err) => this.logger.info(err, 'GatherTask:_onTranscription error'));
|
||||
}
|
||||
}
|
||||
}
|
||||
_onEndOfUtterance(cs, ep) {
|
||||
@@ -281,7 +371,8 @@ class TaskGather extends Task {
|
||||
|
||||
this._clearTimer();
|
||||
if (reason.startsWith('dtmf')) {
|
||||
await this.performAction({digits: this.digitBuffer, reason: 'dtmfDetected'});
|
||||
if (this.parentTask) this.parentTask.emit('dtmf-collected', {reason, digits: this.digitBuffer});
|
||||
else await this.performAction({digits: this.digitBuffer, reason: 'dtmfDetected'});
|
||||
}
|
||||
else if (reason.startsWith('speech')) {
|
||||
if (this.parentTask) this.parentTask.emit('transcription', evt);
|
||||
|
||||
@@ -20,6 +20,9 @@ function makeTask(logger, obj, parent) {
|
||||
case TaskName.SipRefer:
|
||||
const TaskSipRefer = require('./sip_refer');
|
||||
return new TaskSipRefer(logger, data, parent);
|
||||
case TaskName.Cognigy:
|
||||
const TaskCognigy = require('./cognigy');
|
||||
return new TaskCognigy(logger, data, parent);
|
||||
case TaskName.Conference:
|
||||
const TaskConference = require('./conference');
|
||||
return new TaskConference(logger, data, parent);
|
||||
|
||||
@@ -42,7 +42,7 @@ class TaskMessage extends Task {
|
||||
}
|
||||
if (gw) {
|
||||
this.logger.info({gw, accountSid}, 'Message:exec - using smpp to send message');
|
||||
url = getSmpp();
|
||||
url = process.env.K8S ? 'http://smpp' : getSmpp();
|
||||
relativeUrl = '/sms';
|
||||
payload = {
|
||||
...payload,
|
||||
|
||||
@@ -21,15 +21,20 @@ class TaskSay extends Task {
|
||||
const {updateSpeechCredentialLastUsed} = require('../utils/db-utils')(this.logger, srf);
|
||||
const {writeAlerts, AlertType, stats} = srf.locals;
|
||||
const {synthAudio} = srf.locals.dbHelpers;
|
||||
const hasVerbLevelTts = this.synthesizer.vendor && this.synthesizer.vendor !== 'default';
|
||||
const vendor = hasVerbLevelTts ? this.synthesizer.vendor : cs.speechSynthesisVendor ;
|
||||
const language = hasVerbLevelTts ? this.synthesizer.language : cs.speechSynthesisLanguage ;
|
||||
const voice = hasVerbLevelTts ? this.synthesizer.voice : cs.speechSynthesisVoice ;
|
||||
|
||||
const vendor = this.synthesizer.vendor && this.synthesizer.vendor !== 'default' ? this.synthesizer.vendor : cs.speechSynthesisVendor;
|
||||
const language = this.synthesizer.language && this.synthesizer.language !== 'default' ? this.synthesizer.language : cs.speechSynthesisLanguage ;
|
||||
const voice = this.synthesizer.voice && this.synthesizer.voice !== 'default' ? this.synthesizer.voice : cs.speechSynthesisVoice;
|
||||
const engine = this.synthesizer.engine || 'standard';
|
||||
const salt = cs.callSid;
|
||||
const credentials = cs.getSpeechCredentials(vendor, 'tts');
|
||||
|
||||
this.logger.info({language, voice}, `Task:say - using vendor: ${vendor}`);
|
||||
this.logger.info({language,
|
||||
voice,
|
||||
localSynthesizer: this.synthesizer,
|
||||
speechSynthesisVendor: cs.speechSynthesisVendor,
|
||||
speechSynthesisLanguage: cs.speechSynthesisLanguage,
|
||||
speechSynthesisVoice: cs.speechSynthesisVoice
|
||||
}, `Task:say - using vendor: ${vendor}`);
|
||||
this.ep = ep;
|
||||
try {
|
||||
if (!credentials) {
|
||||
@@ -48,6 +53,7 @@ class TaskSay extends Task {
|
||||
vendor,
|
||||
language,
|
||||
voice,
|
||||
engine,
|
||||
salt,
|
||||
credentials
|
||||
}).catch((err) => {
|
||||
@@ -78,7 +84,11 @@ class TaskSay extends Task {
|
||||
const {memberId, confName, confUuid} = cs;
|
||||
await this.playToConfMember(this.ep, memberId, confName, confUuid, filepath[segment]);
|
||||
}
|
||||
else await ep.play(filepath[segment]);
|
||||
else {
|
||||
this.logger.debug(`Say:exec sending command to play file ${filepath[segment]}`);
|
||||
await ep.play(filepath[segment]);
|
||||
this.logger.debug(`Say:exec completed play file ${filepath[segment]}`);
|
||||
}
|
||||
} while (!this.killed && ++segment < filepath.length);
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
@@ -21,6 +21,22 @@
|
||||
"referTo"
|
||||
]
|
||||
},
|
||||
"cognigy": {
|
||||
"properties": {
|
||||
"url": "string",
|
||||
"token": "string",
|
||||
"recognizer": "#recognizer",
|
||||
"tts": "#synthesizer",
|
||||
"prompt": "string",
|
||||
"actionHook": "object|string",
|
||||
"eventHook": "object|string",
|
||||
"data": "object"
|
||||
},
|
||||
"required": [
|
||||
"url",
|
||||
"token"
|
||||
]
|
||||
},
|
||||
"dequeue": {
|
||||
"properties": {
|
||||
"name": "string",
|
||||
@@ -84,7 +100,15 @@
|
||||
"numDigits": "number",
|
||||
"partialResultHook": "object|string",
|
||||
"speechTimeout": "number",
|
||||
"listenDuringPrompt": "boolean",
|
||||
"bargein": "boolean",
|
||||
"minBargeinWordCount": "number",
|
||||
"dtmfBargein": "boolean",
|
||||
"minDigits": "number",
|
||||
"maxDigits": "number",
|
||||
"interDigitTimeout": "number",
|
||||
"timeout": "number",
|
||||
"listenAfterSpeech": "boolean",
|
||||
"recognizer": "#recognizer",
|
||||
"play": "#play",
|
||||
"say": "#say"
|
||||
@@ -308,7 +332,6 @@
|
||||
"earlyMedia": "boolean"
|
||||
},
|
||||
"required": [
|
||||
"transcriptionHook",
|
||||
"recognizer"
|
||||
]
|
||||
},
|
||||
@@ -329,7 +352,8 @@
|
||||
"auth": "#auth",
|
||||
"vmail": "boolean",
|
||||
"tenant": "string",
|
||||
"trunk": "string"
|
||||
"trunk": "string",
|
||||
"overrideTo": "string"
|
||||
},
|
||||
"required": [
|
||||
"type"
|
||||
@@ -353,6 +377,10 @@
|
||||
},
|
||||
"language": "string",
|
||||
"voice": "string",
|
||||
"engine": {
|
||||
"type": "string",
|
||||
"enum": ["standard", "neural"]
|
||||
},
|
||||
"gender": {
|
||||
"type": "string",
|
||||
"enum": ["MALE", "FEMALE", "NEUTRAL"]
|
||||
|
||||
@@ -62,7 +62,9 @@ class Task extends Emitter {
|
||||
kill(cs) {
|
||||
if (this.cs && !this.cs.isConfirmCallSession) this.logger.debug(`${this.name} is being killed`);
|
||||
this._killInProgress = true;
|
||||
// no-op
|
||||
|
||||
/* remove reference to parent task or else entangled parent-child tasks will not be gc'ed */
|
||||
setImmediate(() => this.parentTask = null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ class TaskTranscribe extends Task {
|
||||
constructor(logger, opts, parentTask) {
|
||||
super(logger, opts);
|
||||
this.preconditions = TaskPreconditions.Endpoint;
|
||||
this.parentTask = parentTask;
|
||||
|
||||
this.transcriptionHook = this.data.transcriptionHook;
|
||||
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
|
||||
@@ -76,6 +77,7 @@ class TaskTranscribe extends Task {
|
||||
await this.awaitTaskDone();
|
||||
} catch (err) {
|
||||
this.logger.info(err, 'TaskTranscribe:exec - error');
|
||||
this.parentTask && this.parentTask.emit('error', err);
|
||||
}
|
||||
ep.removeCustomEventListener(GoogleTranscriptionEvents.Transcription);
|
||||
ep.removeCustomEventListener(GoogleTranscriptionEvents.NoAudioDetected);
|
||||
@@ -210,25 +212,36 @@ class TaskTranscribe extends Task {
|
||||
}
|
||||
|
||||
_onTranscription(cs, ep, evt) {
|
||||
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
|
||||
if ('aws' === this.vendor && Array.isArray(evt) && evt.length > 0) evt = evt[0];
|
||||
if ('microsoft' === this.vendor) {
|
||||
const nbest = evt.NBest;
|
||||
const alternatives = nbest.map((n) => {
|
||||
const alternatives = nbest ? nbest.map((n) => {
|
||||
return {
|
||||
confidence: n.Confidence,
|
||||
transcript: n.Display
|
||||
};
|
||||
});
|
||||
}) :
|
||||
[
|
||||
{
|
||||
transcript: evt.DisplayText
|
||||
}
|
||||
];
|
||||
|
||||
const newEvent = {
|
||||
is_final: evt.RecognitionStatus === 'Success',
|
||||
alternatives
|
||||
};
|
||||
evt = newEvent;
|
||||
}
|
||||
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
|
||||
|
||||
this.cs.requestor.request(this.transcriptionHook, Object.assign({speech: evt}, this.cs.callInfo))
|
||||
.catch((err) => this.logger.info(err, 'TranscribeTask:_onTranscription error'));
|
||||
if (this.transcriptionHook) {
|
||||
this.cs.requestor.request(this.transcriptionHook, Object.assign({speech: evt}, this.cs.callInfo))
|
||||
.catch((err) => this.logger.info(err, 'TranscribeTask:_onTranscription error'));
|
||||
}
|
||||
if (this.parentTask) {
|
||||
this.parentTask.emit('transcription', evt);
|
||||
}
|
||||
if (this.killed) {
|
||||
this.logger.debug('TaskTranscribe:_onTranscription exiting after receiving final transcription');
|
||||
this._clearTimer();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"TaskName": {
|
||||
"Cognigy": "cognigy",
|
||||
"Conference": "conference",
|
||||
"Dequeue": "dequeue",
|
||||
"Dial": "dial",
|
||||
@@ -105,5 +106,6 @@
|
||||
"Replaced": "replaced"
|
||||
},
|
||||
"MAX_SIMRINGS": 10,
|
||||
"BONG_TONE": "tone_stream://v=-7;%(100,0,941.0,1477.0);v=-7;>=2;+=.1;%(1400,0,350,440)"
|
||||
"BONG_TONE": "tone_stream://v=-7;%(100,0,941.0,1477.0);v=-7;>=2;+=.1;%(1400,0,350,440)",
|
||||
"FS_UUID_SET_NAME": "fsUUIDs"
|
||||
}
|
||||
|
||||
@@ -36,6 +36,10 @@ const speechMapper = (cred) => {
|
||||
obj.api_key = o.api_key;
|
||||
obj.region = o.region;
|
||||
}
|
||||
else if ('wellsaid' === obj.vendor) {
|
||||
const o = JSON.parse(decrypt(credential));
|
||||
obj.api_key = o.api_key;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
@@ -53,6 +57,7 @@ module.exports = (logger, srf) => {
|
||||
const haveGoogle = speech.find((s) => s.vendor === 'google');
|
||||
const haveAws = speech.find((s) => s.vendor === 'aws');
|
||||
const haveMicrosoft = speech.find((s) => s.vendor === 'microsoft');
|
||||
const haveWellsaid = speech.find((s) => s.vendor === 'wellsaid');
|
||||
if (!haveGoogle || !haveAws || !haveMicrosoft) {
|
||||
const [r3] = await pp.query(sqlSpeechCredentialsForSP, account_sid);
|
||||
if (r3.length) {
|
||||
@@ -68,6 +73,10 @@ module.exports = (logger, srf) => {
|
||||
const ms = r3.find((s) => s.vendor === 'microsoft');
|
||||
if (ms) speech.push(speechMapper(ms));
|
||||
}
|
||||
if (!haveWellsaid) {
|
||||
const wellsaid = r3.find((s) => s.vendor === 'wellsaid');
|
||||
if (wellsaid) speech.push(speechMapper(wellsaid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,6 +112,7 @@ function installSrfLocals(srf, logger) {
|
||||
const {
|
||||
pool,
|
||||
lookupAppByPhoneNumber,
|
||||
lookupAppByRegex,
|
||||
lookupAppBySid,
|
||||
lookupAppByRealm,
|
||||
lookupAppByTeamsTenant,
|
||||
@@ -166,6 +167,7 @@ function installSrfLocals(srf, logger) {
|
||||
client,
|
||||
pool,
|
||||
lookupAppByPhoneNumber,
|
||||
lookupAppByRegex,
|
||||
lookupAppBySid,
|
||||
lookupAppByRealm,
|
||||
lookupAppByTeamsTenant,
|
||||
|
||||
@@ -5,15 +5,10 @@ const {TaskPreconditions, CallDirection} = require('../utils/constants');
|
||||
const CallInfo = require('../session/call-info');
|
||||
const assert = require('assert');
|
||||
const ConfirmCallSession = require('../session/confirm-call-session');
|
||||
const selectSbc = require('./select-sbc');
|
||||
const Registrar = require('@jambonz/mw-registrar');
|
||||
const AdultingCallSession = require('../session/adulting-call-session');
|
||||
const registrar = new Registrar({
|
||||
host: process.env.JAMBONES_REDIS_HOST,
|
||||
port: process.env.JAMBONES_REDIS_PORT || 6379
|
||||
});
|
||||
const deepcopy = require('deepcopy');
|
||||
const moment = require('moment');
|
||||
const stripCodecs = require('./strip-ancillary-codecs');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
class SingleDialer extends Emitter {
|
||||
@@ -63,7 +58,12 @@ class SingleDialer extends Emitter {
|
||||
async exec(srf, ms, opts) {
|
||||
opts = opts || {};
|
||||
opts.headers = opts.headers || {};
|
||||
opts.headers = {...opts.headers, 'X-Call-Sid': this.callSid};
|
||||
opts.headers = {
|
||||
...opts.headers,
|
||||
'X-Jambonz-Routing': this.target.type,
|
||||
'X-Jambonz-FS-UUID': srf.locals.fsUUID,
|
||||
'X-Call-Sid': this.callSid
|
||||
};
|
||||
this.ms = ms;
|
||||
let uri, to;
|
||||
try {
|
||||
@@ -84,7 +84,6 @@ class SingleDialer extends Emitter {
|
||||
break;
|
||||
case 'user':
|
||||
assert(this.target.name);
|
||||
const aor = this.target.name;
|
||||
uri = `sip:${this.target.name}`;
|
||||
to = this.target.name;
|
||||
|
||||
@@ -93,16 +92,6 @@ class SingleDialer extends Emitter {
|
||||
'X-Override-To': this.target.overrideTo
|
||||
});
|
||||
}
|
||||
|
||||
// need to send to the SBC registered on
|
||||
const reg = await registrar.query(aor);
|
||||
if (reg) {
|
||||
const sbc = selectSbc(reg.sbcAddress);
|
||||
if (sbc) {
|
||||
this.logger.debug(`SingleDialer:exec retrieved registration details for ${aor}, using sbc at ${sbc}`);
|
||||
this.sbcAddress = sbc;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'sip':
|
||||
assert(this.target.sipUri);
|
||||
@@ -209,9 +198,15 @@ class SingleDialer extends Emitter {
|
||||
.on('refresh', () => this.logger.info('SingleDialer:exec - dialog refreshed by uas'))
|
||||
.on('modify', async(req, res) => {
|
||||
try {
|
||||
const newSdp = await this.ep.modify(req.body);
|
||||
res.send(200, {body: newSdp});
|
||||
this.logger.info({offer: req.body, answer: newSdp}, 'SingleDialer:exec: handling reINVITE');
|
||||
if (this.ep) {
|
||||
const newSdp = await this.ep.modify(req.body);
|
||||
res.send(200, {body: newSdp});
|
||||
this.logger.info({offer: req.body, answer: newSdp}, 'SingleDialer:exec: handling reINVITE');
|
||||
}
|
||||
else {
|
||||
this.logger.info('SingleDialer:exec: handling reINVITE with released media, emit event');
|
||||
this.emit('reinvite', req, res);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error(err, 'Error handling reinvite');
|
||||
}
|
||||
@@ -322,9 +317,10 @@ class SingleDialer extends Emitter {
|
||||
return cs;
|
||||
}
|
||||
|
||||
async releaseMediaToSBC(remoteSdp) {
|
||||
async releaseMediaToSBC(remoteSdp, localSdp) {
|
||||
assert(this.dlg && this.dlg.connected && this.ep && typeof remoteSdp === 'string');
|
||||
await this.dlg.modify(remoteSdp, {
|
||||
const sdp = stripCodecs(this.logger, remoteSdp, localSdp) || remoteSdp;
|
||||
await this.dlg.modify(sdp, {
|
||||
headers: {
|
||||
'X-Reason': 'release-media'
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
const assert = require('assert');
|
||||
const noopLogger = {info: () => {}, error: () => {}};
|
||||
const {LifeCycleEvents} = require('./constants');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const {LifeCycleEvents, FS_UUID_SET_NAME} = require('./constants');
|
||||
const Emitter = require('events');
|
||||
const debug = require('debug')('jambonz:feature-server');
|
||||
const noopLogger = {info: () => {}, error: () => {}};
|
||||
|
||||
module.exports = (logger) => {
|
||||
logger = logger || noopLogger;
|
||||
@@ -67,7 +68,6 @@ module.exports = (logger) => {
|
||||
})();
|
||||
}
|
||||
|
||||
// send OPTIONS pings to SBCs
|
||||
async function pingProxies(srf) {
|
||||
if (process.env.NODE_ENV === 'test') return;
|
||||
|
||||
@@ -90,29 +90,40 @@ module.exports = (logger) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (process.env.K8S) {
|
||||
setImmediate(() => {
|
||||
logger.info('disabling OPTIONS pings since we are running as a kubernetes service');
|
||||
const {srf} = require('../..');
|
||||
const {addToSet} = srf.locals.dbHelpers;
|
||||
const uuid = srf.locals.fsUUID = uuidv4();
|
||||
addToSet(FS_UUID_SET_NAME, uuid)
|
||||
.catch((err) => logger.info({err}, `Error adding ${uuid} to set ${FS_UUID_SET_NAME}`));
|
||||
});
|
||||
}
|
||||
else {
|
||||
// OPTIONS ping the SBCs from each feature server every 60 seconds
|
||||
setInterval(() => {
|
||||
const {srf} = require('../..');
|
||||
pingProxies(srf);
|
||||
}, process.env.OPTIONS_PING_INTERVAL || 30000);
|
||||
|
||||
// OPTIONS ping the SBCs from each feature server every 60 seconds
|
||||
setInterval(() => {
|
||||
const {srf} = require('../..');
|
||||
pingProxies(srf);
|
||||
}, process.env.OPTIONS_PING_INTERVAL || 30000);
|
||||
// initial ping once we are up
|
||||
setTimeout(async() => {
|
||||
|
||||
// initial ping once we are up
|
||||
setTimeout(async() => {
|
||||
const {srf} = require('../..');
|
||||
// if SBCs are auto-scaling, monitor them as they come and go
|
||||
const {srf} = require('../..');
|
||||
if (!process.env.JAMBONES_SBCS) {
|
||||
const {monitorSet} = srf.locals.dbHelpers;
|
||||
const setName = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:active-sip`;
|
||||
await monitorSet(setName, 10, (members) => {
|
||||
sbcs = members;
|
||||
logger.info(`sbc-pinger: SBC roster has changed, list of active SBCs is now ${sbcs}`);
|
||||
});
|
||||
}
|
||||
|
||||
// if SBCs are auto-scaling, monitor them as they come and go
|
||||
if (!process.env.JAMBONES_SBCS) {
|
||||
const {monitorSet} = srf.locals.dbHelpers;
|
||||
const setName = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:active-sip`;
|
||||
await monitorSet(setName, 10, (members) => {
|
||||
sbcs = members;
|
||||
logger.info(`sbc-pinger: SBC roster has changed, list of active SBCs is now ${sbcs}`);
|
||||
});
|
||||
}
|
||||
|
||||
pingProxies(srf);
|
||||
}, 1000);
|
||||
pingProxies(srf);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
return {
|
||||
lifecycleEmitter,
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
const CIDRMatcher = require('cidr-matcher');
|
||||
const matcher = new CIDRMatcher([process.env.JAMBONES_NETWORK_CIDR]);
|
||||
|
||||
module.exports = (sbcList) => {
|
||||
const obj = sbcList
|
||||
.split(',')
|
||||
.map((str) => {
|
||||
const arr = /^(.*)\/(.*):(\d+)$/.exec(str);
|
||||
return {protocol: arr[1], host: arr[2], port: arr[3]};
|
||||
})
|
||||
.find((obj) => 'udp' == obj.protocol && matcher.contains(obj.host));
|
||||
if (obj) return `${obj.host}:${obj.port}`;
|
||||
};
|
||||
30
lib/utils/strip-ancillary-codecs.js
Normal file
30
lib/utils/strip-ancillary-codecs.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const sdpTransform = require('sdp-transform');
|
||||
|
||||
const stripCodecs = (logger, remoteSdp, localSdp) => {
|
||||
try {
|
||||
const sdp = sdpTransform.parse(remoteSdp);
|
||||
const local = sdpTransform.parse(localSdp);
|
||||
const m = local.media
|
||||
.find((m) => 'audio' === m.type);
|
||||
const pt = m.rtp[0].payload;
|
||||
|
||||
/* manipulate on the audio section */
|
||||
const audio = sdp.media.find((m) => 'audio' === m.type);
|
||||
|
||||
/* discard all of the codecs except the first in our 200 OK, and telephony-events */
|
||||
const ptSaves = audio.rtp
|
||||
.filter((r) => r.codec === 'telephone-event' || r.payload === pt)
|
||||
.map((r) => r.payload);
|
||||
const rtp = audio.rtp.filter((r) => ptSaves.includes(r.payload));
|
||||
|
||||
/* reattach the new rtp sections and stripped payload list */
|
||||
audio.rtp = rtp;
|
||||
audio.payloads = rtp.map((r) => r.payload).join(' ');
|
||||
return sdpTransform.write(sdp);
|
||||
} catch (err) {
|
||||
logger.error({err, remoteSdp, localSdp}, 'strip-ancillary-codecs error');
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = stripCodecs;
|
||||
|
||||
1330
package-lock.json
generated
1330
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "jambonz-feature-server",
|
||||
"version": "v0.7.0",
|
||||
"version": "v0.7.2",
|
||||
"main": "app.js",
|
||||
"engines": {
|
||||
"node": ">= 10.16.0"
|
||||
@@ -23,26 +23,32 @@
|
||||
"start": "node app",
|
||||
"test": "NODE_ENV=test JAMBONES_HOSTING=1 DRACHTIO_HOST=127.0.0.1 DRACHTIO_PORT=9060 DRACHTIO_SECRET=cymru JAMBONES_MYSQL_HOST=127.0.0.1 JAMBONES_MYSQL_PORT=3360 JAMBONES_MYSQL_USER=jambones_test JAMBONES_MYSQL_PASSWORD=jambones_test JAMBONES_MYSQL_DATABASE=jambones_test JAMBONES_REDIS_HOST=127.0.0.1 JAMBONES_REDIS_PORT=16379 JAMBONES_LOGLEVEL=debug ENABLE_METRICS=0 HTTP_PORT=3000 JAMBONES_SBCS=172.38.0.10 JAMBONES_FREESWITCH=127.0.0.1:8022:ClueCon:docker-host JAMBONES_TIME_SERIES_HOST=127.0.0.1 JAMBONES_NETWORK_CIDR=172.38.0.0/16 node test/ ",
|
||||
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
|
||||
"jslint": "eslint app.js lib"
|
||||
"jslint": "eslint app.js lib",
|
||||
"jslint:fix": "eslint app.js lib --fix"
|
||||
},
|
||||
"dependencies": {
|
||||
"@jambonz/db-helpers": "^0.6.14",
|
||||
"@cognigy/socket-client": "^4.5.5",
|
||||
"@jambonz/db-helpers": "^0.6.16",
|
||||
"@jambonz/http-health-check": "^0.0.1",
|
||||
"@jambonz/mw-registrar": "^0.2.1",
|
||||
"@jambonz/realtimedb-helpers": "^0.4.10",
|
||||
"@jambonz/realtimedb-helpers": "^0.4.19",
|
||||
"@jambonz/stats-collector": "^0.1.6",
|
||||
"@jambonz/time-series": "^0.1.5",
|
||||
"aws-sdk": "^2.846.0",
|
||||
"@jambonz/time-series": "^0.1.6",
|
||||
"aws-sdk": "^2.1060.0",
|
||||
"bent": "^7.3.12",
|
||||
"cidr-matcher": "^2.1.1",
|
||||
"debug": "^4.3.1",
|
||||
"debug": "^4.3.2",
|
||||
"deepcopy": "^2.1.0",
|
||||
"drachtio-fsmrf": "^2.0.13",
|
||||
"drachtio-srf": "^4.4.55",
|
||||
"drachtio-srf": "^4.4.61",
|
||||
"express": "^4.17.1",
|
||||
"helmet": "^5.0.2",
|
||||
"ip": "^1.1.5",
|
||||
"lodash": "^4.17.21",
|
||||
"moment": "^2.29.1",
|
||||
"parse-url": "^5.0.2",
|
||||
"pino": "^6.11.2",
|
||||
"parse-url": "^5.0.7",
|
||||
"pino": "^6.13.2",
|
||||
"queue": "^6.0.2",
|
||||
"to-snake-case": "^1.0.0",
|
||||
"uuid": "^8.3.2",
|
||||
"verify-aws-sns-signature": "^0.0.6",
|
||||
|
||||
@@ -9,6 +9,7 @@ networks:
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:5.7
|
||||
platform: linux/x86_64
|
||||
ports:
|
||||
- "3360:3306"
|
||||
environment:
|
||||
@@ -123,7 +124,7 @@ services:
|
||||
ipv4_address: 172.38.0.63
|
||||
|
||||
influxdb:
|
||||
image: influxdb:1.8-alpine
|
||||
image: influxdb:1.8
|
||||
ports:
|
||||
- "8086:8086"
|
||||
networks:
|
||||
|
||||
Reference in New Issue
Block a user