diff --git a/lib/tasks/rest_dial.js b/lib/tasks/rest_dial.js index 23178e52..9017328a 100644 --- a/lib/tasks/rest_dial.js +++ b/lib/tasks/rest_dial.js @@ -2,10 +2,38 @@ const Task = require('./task'); const {TaskName} = require('../utils/constants'); const makeTask = require('./make_task'); const { normalizeJambones } = require('@jambonz/verb-specifications'); - +const DtmfCollector = require('../utils/dtmf-collector'); /** * Manages an outdial made via REST API */ + +function parseDtmfOptions(logger, dtmfCapture) { + 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) { + 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); + } + } + if (childKeys.length) { + childDtmfCollector = new DtmfCollector({logger, patterns: childKeys}); + } + if (parentKeys.length) { + parentDtmfCollector = new DtmfCollector({logger, patterns: parentKeys}); + } + + return {childDtmfCollector, parentDtmfCollector}; +} + + class TaskRestDial extends Task { constructor(logger, opts) { super(logger, opts); @@ -15,10 +43,23 @@ class TaskRestDial extends Task { this.fromHost = this.data.fromHost; this.to = this.data.to; this.call_hook = this.data.call_hook; + this.dtmfHook = this.data.dtmfHook; this.timeout = this.data.timeout || 60; this.sipRequestWithinDialogHook = this.data.sipRequestWithinDialogHook; this.referHook = this.data.referHook; + + + if (this.dtmfHook) { + const {parentDtmfCollector, childDtmfCollector} = parseDtmfOptions(logger, this.data.dtmfCapture || {}); + if (parentDtmfCollector) { + this.parentDtmfCollector = parentDtmfCollector; + } + if (childDtmfCollector) { + this.childDtmfCollector = childDtmfCollector; + } + } + this.on('connect', this._onConnect.bind(this)); this.on('callStatus', this._onCallStatus.bind(this)); }