diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 2420779c5..f1c0344eb 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -23,6 +23,7 @@ const { AWS_REGION, JAMBONES_USE_FREESWITCH_TIMER_FD } = require('../config'); +const bent = require('bent'); const BackgroundTaskManager = require('../utils/background-task-manager'); const BADPRECONDITIONS = 'preconditions not met'; const CALLER_CANCELLED_ERR_MSG = 'Response not sent due to unknown transaction'; @@ -977,6 +978,64 @@ class CallSession extends Emitter { } } + /** + * perform live call control - create rest:dial + * @param {obj} opts create call options + */ + async _lccCallDial(opts) { + try { + const restDialUrl = `${this.srf.locals.serviceUrl}/v1/createCall`; + await this.transformInputIfRequired(opts); + const resp = bent('POST', 'json', 201)(restDialUrl, opts); + this.logger.info(resp.body, 'successfully create outbound call'); + return resp.body; + } catch (err) { + if (err.json) { + err.body = await err.json(); + } + this.logger.error(err, 'failed to create outbound call from ' + this.callSid); + this._notifyTaskError(err.body); + } + } + + async transformInputIfRequired(opts) { + const { + lookupAppBySid + } = this.srf.locals.dbHelpers; + opts.account_sid = this.accountSid; + + if (opts.application_sid) { + this.logger.debug(`Callsession:_validateCreateCall retrieving application ${opts.application_sid}`); + const application = await lookupAppBySid(opts.application_sid); + Object.assign(opts, { + call_hook: application.call_hook, + app_json: application.app_json, + call_status_hook: application.call_status_hook, + speech_synthesis_vendor: application.speech_synthesis_vendor, + speech_synthesis_language: application.speech_synthesis_language, + speech_synthesis_voice: application.speech_synthesis_voice, + speech_recognizer_vendor: application.speech_recognizer_vendor, + speech_recognizer_language: application.speech_recognizer_language + }); + this.logger.debug({opts, application}, 'Callsession:_validateCreateCall augmented with application settings'); + } + + if (typeof opts.call_hook === 'string') { + const url = opts.call_hook; + opts.call_hook = { + url, + method: 'POST' + }; + } + if (typeof opts.call_status_hook === 'string') { + const url = opts.call_status_hook; + opts.call_status_hook = { + url, + method: 'POST' + }; + } + } + /** * perform live call control -- set a new call_hook * @param {object} opts @@ -1454,6 +1513,10 @@ Duration=${duration} ` this._lccCallStatus(data); break; + case 'dial': + this._lccCallDial(data); + break; + case 'mute:status': this._lccMuteStatus(call_sid, data); break;