UNPKG

@onereach/step-voice

Version:
140 lines (139 loc) 6.44 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, sipProfile, refer, withReplaces, replacesConversation, replacesConversationThread, gatewaySettingsMode, 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 = gatewaySettingsMode === 'inherit'; const headers = Object.fromEntries(sipHeaders.map(({ name, value }) => [name, value])); let transferFrom = callerID; let transferTo = destination; let replacesDialog; if (refer && withReplaces && replacesConversation) { const replacesCall = replacesConversationThread ? await this.process.getSafeThread(replacesConversationThread).get(replacesConversation) : await this.getConversationByName(replacesConversation); // For attended transfer: transferFrom becomes current call's ID, // and replacesDialog carries the dialog identifiers for Replaces header transferFrom = call.id; replacesDialog = { callId: replacesCall['toCallId'], toTag: replacesCall['toTag'], fromTag: replacesCall['fromTag'], uri: replacesCall['toUri'], }; } // GET SIP PROFILE SETTINGS let gateway; const profile = sipProfile !== "default" /* SIP_PROFILE.DEFAULT */ ? sipProfile : undefined; switch (gatewaySettingsMode) { case "custom" /* GATEWAY_SETTINGS_MODE.CUSTOM */: gateway = { host: sipHost, user: sipUser, username: sipUser, password: sipPassword, profile }; break; case "profile" /* GATEWAY_SETTINGS_MODE.PROFILE */: gateway = gateway = profile != null ? { profile } : undefined; break; } let commandName = 'bridge'; if (destinationIsSip) { commandName = refer ? 'refer' : 'bridge_sip'; } else if (destinationIsUser) { commandName = 'bridge_user'; } const botNumber = inheritGatewaySetting ? from : call.botNumber; const maxLoops = this.session.data?.loopPrevention?.enabled ? this.session.data.loopPrevention.maxLoops : undefined; const command = { name: commandName, params: { botNumber, transferFrom, transferTo, headers, gateway, timeout, maxLoops, ...(replacesDialog && { replacesDialog }), } }; 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;