Skip to content

Commit

Permalink
Added conditional auto answer example
Browse files Browse the repository at this point in the history
  • Loading branch information
David A Bruun-Lie committed Oct 4, 2018
1 parent eaf3bf2 commit 2615d07
Show file tree
Hide file tree
Showing 2 changed files with 589 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Conditional Autoanswer with Prompt/Conditional Autoanswer.js
@@ -0,0 +1,49 @@
const xapi = require('xapi');

/*
This macro demonstrates how to have a conditional autoanswer on a system. The macro, upon incoming call, checks whether the remote site is listed in the list of numbers to be auto answered.
If it finds it, it prompts for a few seconds to allow for either an immediate answer OR to reject the
*/

const AUTOANSWER_NUMBERS = ['mynumber@example.com', 'myothernumber@example.com'];
const AUTOANSWER_DELAY_SECONDS = 10;
var autoanswerhandler;


function normaliseRemoteURI(number){
var regex = /^(sip:|h323:|spark:|h320:|webex:|locus:)/gi;
number = number.replace(regex, '');
// console.log('Normalised Remote URI to ' + number);
return number;
}

function answerCall(){
xapi.command('Call Accept').catch(
(error) =>{
console.error(error);
});
}

xapi.event.on('IncomingCallIndication', (event) => {
console.log(event);
if(AUTOANSWER_NUMBERS.includes(normaliseRemoteURI(event.RemoteURI))){
xapi.command("UserInterface Message Alert Display", {Title: 'Incoming Autoanswer-number', Text: 'This call will automatically be answered after ' + AUTOANSWER_DELAY_SECONDS + ' seconds', Duration: AUTOANSWER_DELAY_SECONDS});
autoanswerhandler = setTimeout(() => answerCall(), AUTOANSWER_DELAY_SECONDS * 1000);
}
});


xapi.status.on('Call Status', (status) => {
if(status === 'Connected'){
clearTimeout(autoanswerhandler);
// console.log('Call was accepted before auto-answer was performed');
xapi.command("UserInterface Message Alert Clear");
}
});

xapi.event.on('CallDisconnect', (event) => {
clearTimeout(autoanswerhandler);
// console.log('Call was disconnected before auto-answer was performed');
xapi.command("UserInterface Message Alert Clear");
});

0 comments on commit 2615d07

Please sign in to comment.