rest dial dtmf

This commit is contained in:
surajshivakumar
2024-08-10 15:22:38 -04:00
parent d75ae26db0
commit 14d1c0d260
+32 -6
View File
@@ -9,26 +9,32 @@ const DtmfCollector = require('../utils/dtmf-collector');
*/
function parseDtmfOptions(logger, dtmfCapture) {
console.log('DTMF Capture Settings:', dtmfCapture); // Log the initial DTMF capture settings
let parentDtmfCollector, childDtmfCollector;
const parentKeys = [], childKeys = [];
if (Array.isArray(dtmfCapture)) {
Array.prototype.push.apply(parentKeys, dtmfCapture);
Array.prototype.push.apply(childKeys, dtmfCapture);
}
else if (dtmfCapture.childCall || dtmfCapture.parentCall) {
} else if (dtmfCapture.childCall || dtmfCapture.parentCall) {
if (dtmfCapture.childCall && Array.isArray(dtmfCapture.childCall)) {
Array.prototype.push.apply(childKeys, dtmfCapture.childCall);
}
if (dtmfCapture.parentCall && Array.isArray(dtmfCapture.parentCall)) {
Array.prototype.push.apply(childKeys, dtmfCapture.parentCall);
Array.prototype.push.apply(parentKeys, dtmfCapture.parentCall);
}
}
console.log('Parent DTMF Keys:', parentKeys); // Log the keys for the parent call
console.log('Child DTMF Keys:', childKeys); // Log the keys for the child call
if (childKeys.length) {
childDtmfCollector = new DtmfCollector({logger, patterns: childKeys});
console.log('Child DtmfCollector created:', childDtmfCollector); // Log when child DTMF collector is created
}
if (parentKeys.length) {
parentDtmfCollector = new DtmfCollector({logger, patterns: parentKeys});
console.log('Parent DtmfCollector created:', parentDtmfCollector); // Log when parent DTMF collector is created
}
return {childDtmfCollector, parentDtmfCollector};
@@ -47,7 +53,7 @@ class TaskRestDial extends Task {
this.timeout = this.data.timeout || 60;
this.sipRequestWithinDialogHook = this.data.sipRequestWithinDialogHook;
this.referHook = this.data.referHook;
console.log(this.dtmfHook);
if (this.dtmfHook) {
const {parentDtmfCollector, childDtmfCollector} = parseDtmfOptions(logger, this.data.dtmfCapture || {});
if (parentDtmfCollector) {
@@ -72,6 +78,7 @@ class TaskRestDial extends Task {
* INVITE has just been sent at this point
*/
async exec(cs) {
console.log('Executing TaskRestDial'); // Log when exec is called
await super.exec(cs);
this.cs = cs;
this.canCancel = true;
@@ -84,8 +91,10 @@ class TaskRestDial extends Task {
this._setCallTimer();
await this.awaitTaskDone();
console.log('TaskRestDial execution completed'); // Log when exec is finished
}
turnOffAmd() {
if (this.callSession.ep && this.callSession.ep.amd) this.stopAmd(this.callSession.ep, this);
}
@@ -105,6 +114,7 @@ class TaskRestDial extends Task {
}
async _onConnect(dlg) {
console.log('Call connected, setting up dialog and DTMF detection');
this.canCancel = false;
const cs = this.callSession;
cs.setDialog(dlg);
@@ -113,6 +123,7 @@ class TaskRestDial extends Task {
// Attach DTMF detection
if (this.parentDtmfCollector || this.childDtmfCollector) {
console.log('Setting up DTMF detection'); // Log when DTMF detection is being set up
this._installDtmfDetection(cs, dlg);
}
@@ -196,35 +207,50 @@ class TaskRestDial extends Task {
}
_installDtmfDetection(cs, dlg) {
console.log('Installing DTMF detection'); // Log when installing DTMF detection
dlg.on('info', this._onInfo.bind(this, cs, dlg));
}
_onInfo(cs, dlg, req, res) {
console.log('INFO message received:', req.body); // Log the incoming INFO message
res.send(200);
if (req.get('Content-Type') !== 'application/dtmf-relay') {
console.log('INFO message is not DTMF relay'); // Log if the INFO message is not a DTMF relay
return;
}
const dtmfDetector = dlg === cs.dlg ? this.parentDtmfCollector : this.childDtmfCollector;
if (!dtmfDetector) return;
if (!dtmfDetector) {
console.log('No DTMF detector available'); // Log if no DTMF detector is available
return;
}
const arr = /Signal=([0-9#*])/.exec(req.body);
if (!arr) return;
if (!arr) {
console.log('No DTMF signal found in INFO message'); // Log if no DTMF signal is found
return;
}
const key = arr[1];
console.log('DTMF Signal received:', key); // Log the detected DTMF signal
const match = dtmfDetector.keyPress(key);
if (match) {
console.log('DTMF match found:', match); // Log if a DTMF match is found
const b3 = this.getTracingPropagation();
const httpHeaders = b3 && {b3};
this.logger.info({callSid: cs.callSid}, `RestDial:_onInfo triggered dtmf match: ${match}`);
cs.requestor.request('verb:hook', this.dtmfHook, {dtmf: match, ...cs.callInfo.toJSON()}, httpHeaders)
.catch((err) => this.logger.info(err, 'RestDial:_onDtmf - error'));
} else {
console.log('No DTMF match found for key:', key); // Log if no match is found for the DTMF signal
}
}
_removeDtmfDetection(dlg) {
console.log('Removing DTMF detection'); // Log when removing DTMF detection
dlg && dlg.removeAllListeners('info');
}