Skip to content

Commit

Permalink
create outbound dial from webhook ws (jambonz#581)
Browse files Browse the repository at this point in the history
* wip, create outbound dial from webhook ws

* wip, create outbound dial from webhook ws

* clean
  • Loading branch information
xquanluu authored and avoylenko committed May 31, 2024
1 parent f21ad54 commit 303ce07
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/session/call-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 303ce07

Please sign in to comment.