diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 627d0981..1cbd9950 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -20,6 +20,7 @@ const listTaskNames = require('../utils/summarize-tasks'); const HttpRequestor = require('../utils/http-requestor'); const WsRequestor = require('../utils/ws-requestor'); const ActionHookDelayProcessor = require('../utils/action-hook-delay'); +const {parseUri} = require('drachtio-srf'); const { JAMBONES_INJECT_CONTENT, JAMBONES_EAGERLY_PRE_CACHE_AUDIO, @@ -339,6 +340,18 @@ class CallSession extends Emitter { this.application.fallback_speech_recognizer_language = language; } + /** + * global referHook + */ + + set referHook(hook) { + this._referHook = hook; + } + + get referHook() { + return this._referHook; + } + /** * Vad */ @@ -2234,17 +2247,59 @@ Duration=${duration} ` } /** - * Handle incoming REFER if we are in a dial task + * Handle incoming REFER * @param {*} req * @param {*} res */ _onRefer(req, res) { const task = this.currentTask; const sd = task.sd; - if (task && TaskName.Dial === task.name && sd) { + if (task && TaskName.Dial === task.name && sd && task.referHook) { task.handleRefer(this, req, res); } else { + this._handleRefer(req, res); + } + } + + async _handleRefer(req, res) { + if (this._referHook) { + try { + const to = parseUri(req.getParsedHeader('Refer-To').uri); + const by = parseUri(req.getParsedHeader('Referred-By').uri); + const b3 = this.b3; + const httpHeaders = b3 && {b3}; + const json = await this.requestor.request('verb:hook', this._referHook, { + ...(this.callInfo.toJSON()), + refer_details: { + sip_refer_to: req.get('Refer-To'), + sip_referred_by: req.get('Referred-By'), + sip_user_agent: req.get('User-Agent'), + refer_to_user: to.scheme === 'tel' ? to.number : to.user, + referred_by_user: by.scheme === 'tel' ? by.number : by.user, + referring_call_sid: this.callSid, + referred_call_sid: null, + } + }, httpHeaders); + + if (json && Array.isArray(json)) { + const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata)); + if (tasks && tasks.length > 0) { + this.logger.info('CallSession:handleRefer received REFER, get new tasks'); + this.replaceApplication(tasks); + if (this.wakeupResolver) { + this.wakeupResolver({reason: 'CallSession: referHook new taks'}); + this.wakeupResolver = null; + } + } + } + res.send(202); + this.logger.info('CallSession:handleRefer - sent 202 Accepted'); + } catch (err) { + this.logger.error({err}, 'CallSession:handleRefer - error while asking referHook'); + res.send(err.statusCode || 501); + } + } else { res.send(501); } } diff --git a/lib/session/rest-call-session.js b/lib/session/rest-call-session.js index bf8513b9..6606b1cd 100644 --- a/lib/session/rest-call-session.js +++ b/lib/session/rest-call-session.js @@ -1,10 +1,6 @@ const CallSession = require('./call-session'); const {CallStatus} = require('../utils/constants'); const moment = require('moment'); -const {parseUri} = require('drachtio-srf'); -const { normalizeJambones } = require('@jambonz/verb-specifications'); -const makeTask = require('../tasks/make_task'); - /** * @classdesc Subclass of CallSession. This represents a CallSession that is * created for an outbound call that is initiated via the REST API. @@ -49,59 +45,6 @@ class RestCallSession extends CallSession { dlg.on('modify', this._onReinvite.bind(this)); this.wrapDialog(dlg); } - - /** - * global referHook - */ - - set referHook(hook) { - this._referHook = hook; - } - - /** - * This is invoked when the called party sends REFER to Jambonz. - */ - async _onRefer(req, res) { - if (this._referHook) { - try { - const to = parseUri(req.getParsedHeader('Refer-To').uri); - const by = parseUri(req.getParsedHeader('Referred-By').uri); - const b3 = this.b3; - const httpHeaders = b3 && {b3}; - const json = await this.requestor.request('verb:hook', this._referHook, { - ...(this.callInfo.toJSON()), - refer_details: { - sip_refer_to: req.get('Refer-To'), - sip_referred_by: req.get('Referred-By'), - sip_user_agent: req.get('User-Agent'), - refer_to_user: to.scheme === 'tel' ? to.number : to.user, - referred_by_user: by.scheme === 'tel' ? by.number : by.user, - referring_call_sid: this.callSid, - referred_call_sid: null, - } - }, httpHeaders); - - if (json && Array.isArray(json)) { - const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata)); - if (tasks && tasks.length > 0) { - this.logger.info('RestCallSession:handleRefer received REFER, get new tasks'); - this.replaceApplication(tasks); - if (this.wakeupResolver) { - this.wakeupResolver({reason: 'RestCallSession: referHook new taks'}); - this.wakeupResolver = null; - } - } - } - res.send(202); - this.logger.info('RestCallSession:handleRefer - sent 202 Accepted'); - } catch (err) { - this.logger.error({err}, 'RestCallSession:handleRefer - error while asking referHook'); - res.send(err.statusCode || 501); - } - } else { - res.send(501); - } - } /** * This is invoked when the called party hangs up, in order to calculate the call duration. */ diff --git a/lib/tasks/config.js b/lib/tasks/config.js index 10bbe916..8f163062 100644 --- a/lib/tasks/config.js +++ b/lib/tasks/config.js @@ -74,6 +74,7 @@ class TaskConfig extends Task { get hasDub() { return Object.keys(this.dub).length; } get hasVad() { return Object.keys(this.vad).length; } get hasFillerNoise() { return Object.keys(this.fillerNoise).length; } + get hasReferHook() { return Object.keys(this.data).includes('referHook'); } get summary() { const phrase = []; @@ -104,6 +105,7 @@ class TaskConfig extends Task { if (this.notifyEvents) phrase.push(`event notification ${this.notifyEvents ? 'on' : 'off'}`); if (this.onHoldMusic) phrase.push(`onHoldMusic: ${this.onHoldMusic}`); if ('boostAudioSignal' in this.data) phrase.push(`setGain ${this.data.boostAudioSignal}`); + if (this.hasReferHook) phrase.push('set referHook'); return `${this.name}{${phrase.join(',')}}`; } @@ -299,6 +301,10 @@ class TaskConfig extends Task { mode: (this.vad.mode !== undefined && this.vad.mode !== null) ? this.vad.mode : 2 }; } + + if (this.hasReferHook) { + cs.referHook = this.data.referHook; + } } async kill(cs) {