Skip to content

Instantly share code, notes, and snippets.

@WhiteyDude
Forked from davehorton/sample.js
Last active Jun 9, 2019
Embed
What would you like to do?
const calls = require('./calls');
srf.invite(async(req, res) => {
const ms = req.app.locals.ms;
const to_number = calls.convertToE164(req.calledNumber);
let ep, dlg;
try {
const {endpoint, dialog} = await ms.connectCaller(req, res);
ep = endpoint;
dlg = dialog;
const number = await calls.check_is_valid(to_number);
if (!number) {
console.log(`[${req.callid}] ${to_number} does not match`)
await calls.playback_failure(endpoint);
dialog.destroy();
endpoint.destroy();
}
else {
setHandlers(req, res, dialog, endpoint);
// Valid number, continuing execution
endpoint.play(['ivr/8000/ivr-please_state_your_name_and_reason_for_calling.wav']);
}
} catch (err) {
console.error(err, 'Error connecting call to media server');
res.send(503);
if (dlg) dlg.destroy();
if (ep) ep.destroy();
}
});
function setHandlers(req, res, dialog, endpoint) {
dialog.on('destroy', () => {
console.log(`Received BYE, deleting ${req.callid}`)
endpoint.destroy();
// calls.delete_call(req.call_id);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment