UNPKG

@onereach/step-voice

Version:
96 lines (95 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const lodash_1 = tslib_1.__importDefault(require("lodash")); const voice_1 = tslib_1.__importDefault(require("./voice")); class Transfer extends voice_1.default { async runStep() { const call = await this.fetchData(); this.triggers.local(`in/voice/${call.id}`, async (event) => { switch (event.params.type) { case 'bridge': { await this.transcript(call, { action: 'Transfer Answered', actionFromBot: true, reportingSettingsKey: 'transcriptAnswered' }); // TODO // this.set(`__transferAnswered_${this.currentStepId}`, true) // this.set('__sessionEndedBy', 'Transfer') // Maximum transfer duration this.triggers.timeout(60 * 60 * 1000, () => { }); return this.exitFlow(); } case 'bridge/ended': { const { error, originateDisposition } = event.params; const isSuccess = error === 'NORMAL_CLEARING' && (lodash_1.default.isUndefined(originateDisposition) || originateDisposition === 'SUCCESS'); if (isSuccess) { await this.transcript(call, { action: 'Transfer Ended - Answered', actionFromBot: true, reportingSettingsKey: 'transcriptEndedAnswered' }); return this.exitStep('success'); } else { await this.transcript(call, { action: 'Transfer Ended - Not Answered', actionFromBot: true, reportingSettingsKey: 'transcriptEndedNotAnswered' }); return this.exitStep('no answer'); } } case 'hangup': { await this.handleHangup(call); return await this.waitConvEnd(); } case 'error': return this.throwError(event.params.error); } return this.exitFlow(); }); this.triggers.otherwise(async () => { const { phoneNumber, sessionTimeout, destination, sipHeaders = [], sipHost, sipUser, sipPassword, refer, gatewaySettigsMode, from, muteRecording } = this.data; const destinationIsSip = (/^sip:/i).test(destination); const destinationIsUser = (/^user:/i).test(destination); const callerID = phoneNumber; const timeout = Number(sessionTimeout); const inheritGatewaySetting = gatewaySettigsMode === 'inherit'; const headers = Object.fromEntries(sipHeaders.map(({ name, value }) => [name, value])); const command = { name: destinationIsSip ? (refer ? 'refer' : 'bridge_sip') : (destinationIsUser ? 'bridge_user' : 'bridge'), params: { botNumber: inheritGatewaySetting ? from : call.botNumber, transferFrom: callerID, transferTo: destination, headers, gateway: sipHost ? { host: sipHost, user: sipUser, username: sipUser, password: sipPassword } : undefined, timeout } }; const muteAfterTransfer = !!muteRecording; await this.pauseRecording(call, command, { muteStep: muteAfterTransfer, muteBot: muteAfterTransfer, muteUser: muteAfterTransfer }); await this.transcript(call, { action: 'Transfer Start', actionFromBot: true, reportingSettingsKey: 'transcript' }); if (destinationIsSip && refer) { // this.set('__sessionEndedBy', 'Transfer') return this.exitStep('success'); } return this.exitFlow(); }); } } exports.default = Transfer;