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