diff --git a/lib/session/rest-call-session.js b/lib/session/rest-call-session.js index b4628819..35a3382f 100644 --- a/lib/session/rest-call-session.js +++ b/lib/session/rest-call-session.js @@ -1,6 +1,9 @@ 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 @@ -42,9 +45,62 @@ class RestCallSession extends CallSession { setDialog(dlg) { this.dlg = dlg; dlg.on('destroy', this._callerHungup.bind(this)); + dlg.on('refer', this._onRefer.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/rest_dial.js b/lib/tasks/rest_dial.js index 163970c4..23178e52 100644 --- a/lib/tasks/rest_dial.js +++ b/lib/tasks/rest_dial.js @@ -17,6 +17,7 @@ class TaskRestDial extends Task { this.call_hook = this.data.call_hook; this.timeout = this.data.timeout || 60; this.sipRequestWithinDialogHook = this.data.sipRequestWithinDialogHook; + this.referHook = this.data.referHook; this.on('connect', this._onConnect.bind(this)); this.on('callStatus', this._onCallStatus.bind(this)); @@ -64,6 +65,7 @@ class TaskRestDial extends Task { this.canCancel = false; const cs = this.callSession; cs.setDialog(dlg); + cs.referHook = this.referHook; this.logger.debug('TaskRestDial:_onConnect - call connected'); if (this.sipRequestWithinDialogHook) this._initSipRequestWithinDialogHandler(cs, dlg); try { diff --git a/package-lock.json b/package-lock.json index 3f591a46..ebd8d8c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@jambonz/speech-utils": "^0.1.11", "@jambonz/stats-collector": "^0.1.10", "@jambonz/time-series": "^0.2.8", - "@jambonz/verb-specifications": "^0.0.72", + "@jambonz/verb-specifications": "^0.0.74", "@opentelemetry/api": "^1.8.0", "@opentelemetry/exporter-jaeger": "^1.23.0", "@opentelemetry/exporter-trace-otlp-http": "^0.50.0", @@ -2360,9 +2360,9 @@ } }, "node_modules/@jambonz/verb-specifications": { - "version": "0.0.72", - "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.72.tgz", - "integrity": "sha512-sjA+/LQP2p1zE02UByy9OaAaSxbfQNxQ6D0pwYoMG42U8n+8Det+GFM/9+oFVnbNjUH9bvgT8vrR57U0lU4Cpw==", + "version": "0.0.74", + "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.74.tgz", + "integrity": "sha512-CiO5w6wB8pILmtFkSUQ0MpKFf9kARYpkeaV1+ogOm8YPuOS5FbGTBtbNNginwdZZcX05UBwnNviPMwVqjxlIZA==", "dependencies": { "debug": "^4.3.4", "pino": "^8.8.0" @@ -11994,9 +11994,9 @@ } }, "@jambonz/verb-specifications": { - "version": "0.0.72", - "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.72.tgz", - "integrity": "sha512-sjA+/LQP2p1zE02UByy9OaAaSxbfQNxQ6D0pwYoMG42U8n+8Det+GFM/9+oFVnbNjUH9bvgT8vrR57U0lU4Cpw==", + "version": "0.0.74", + "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.74.tgz", + "integrity": "sha512-CiO5w6wB8pILmtFkSUQ0MpKFf9kARYpkeaV1+ogOm8YPuOS5FbGTBtbNNginwdZZcX05UBwnNviPMwVqjxlIZA==", "requires": { "debug": "^4.3.4", "pino": "^8.8.0" diff --git a/package.json b/package.json index 40021b96..6324f6a7 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@jambonz/speech-utils": "^0.1.11", "@jambonz/stats-collector": "^0.1.10", "@jambonz/time-series": "^0.2.8", - "@jambonz/verb-specifications": "^0.0.72", + "@jambonz/verb-specifications": "^0.0.74", "@opentelemetry/api": "^1.8.0", "@opentelemetry/exporter-jaeger": "^1.23.0", "@opentelemetry/exporter-trace-otlp-http": "^0.50.0",