@onereach/step-voice
Version:
Onereach.ai Voice Steps
191 lines (190 loc) • 8.19 kB
JavaScript
"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"));
const exitKeypadData = ({ mfVersion, digits, event }) => mfVersion === '1'
? digits
: {
digits,
digitsSplit: event.params.digitsSplit,
language: event.params.language,
};
const validateInput = ({ input = '', type, regex, expectedLength }) => {
switch (type) {
case 'regEx': {
const regExInput = /^\/(.+)\/(\w*)$/.exec(regex);
if (regExInput == null)
throw new Error(`invalid regular expression: ${regex}`);
const regexp = new RegExp(regExInput[1], regExInput[2]);
return regexp.test(input);
}
case 'expectedNumber':
return input.length === expectedLength;
default:
return true;
}
};
class KeypadInput extends voice_1.default {
async runStep() {
const call = await this.fetchData();
const { interTimeout, asr, tts, noReplyDelay, textType, keypadBargeIn, endInput, terminationKey, endUserInputValidationOther, expectedNumberOfDigits, regExToValidate, mfVersion, sensitiveData, recognitionModel, usePromptsForUnrecognized } = this.data;
const interDigitTimeout = Number(interTimeout) * 1000;
const ttsSettings = tts.getSettings(call.tts);
const asrSettings = asr.getSettings(call.asr);
const grammar = {
id: this.currentStepId,
asr: {
...asrSettings,
config: {
version: 'v1beta',
recognitionModel
}
}
};
const command = {
name: 'request-digits',
params: {
sections: [],
interDigitTimeout,
grammar
}
};
switch (endUserInputValidationOther) {
case 'expectedNumber':
command.params.expectedNumberOfDigits = Number(expectedNumberOfDigits);
break;
case 'regEx':
break;
case 'notValidate':
break;
}
switch (endInput) {
case 'terminatingKey':
command.params.terminator = terminationKey;
break;
case 'waitTimeout':
break;
}
const repromptsList = this.buildReprompts({ prompts: this.data.prompts, allowKeypadBargeIn: keypadBargeIn });
const speechSections = this.buildSections({ sections: this.data.audio, textType, ttsSettings, allowKeypadBargeIn: keypadBargeIn });
this.triggers.local(`in/voice/${call.id}`, async (event) => {
const reportingSettingsKey = this.rptsStarted ? 'transcriptRepromptResponse' : 'transcriptResponse';
await this.handleInterruption({
call,
event,
speechSections,
repromptsList,
reportingSettingsKey: this.rptsStarted ? 'transcriptReprompt' : 'transcriptPrompt'
});
switch (event.params.type) {
case 'digits': {
const digits = event.params.digits;
if (validateInput({
input: digits,
type: endUserInputValidationOther,
regex: regExToValidate,
expectedLength: command.params.expectedNumberOfDigits
})) {
await this.transcript(call, {
keyPress: digits,
message: digits,
action: 'Call DTMF',
reportingSettingsKey,
actionFromBot: false
});
await this.resumeRecording(call, sensitiveData);
return this.exitStep('reply', exitKeypadData({ mfVersion, digits, event }));
}
else {
if ((lodash_1.default.isUndefined(usePromptsForUnrecognized) || usePromptsForUnrecognized) && this.rptsHasMore({ repromptsList })) {
await this.transcript(call, {
message: 'Unrecognized',
keyPress: digits,
action: 'Call DTMF',
reportingSettingsKey,
actionFromBot: false
});
await this.rptsSend(call, {
command,
repromptsList,
noReplyDelay,
speechSections,
textType,
ttsSettings,
sensitiveData
});
return this.exitFlow();
}
await this.transcript(call, {
message: 'Not Recognized',
keyPress: digits,
action: 'Call DTMF',
reportingSettingsKey,
actionFromBot: false
});
await this.resumeRecording(call, sensitiveData);
return this.exitStep('unrecognized', exitKeypadData({ mfVersion, digits, event }));
}
}
case 'timeout': {
if (this.rptsHasMore({ repromptsList })) {
await this.transcript(call, {
message: 'No Reply',
action: 'Call DTMF',
reportingSettingsKey,
actionFromBot: false
});
await this.rptsSend(call, {
command,
repromptsList,
noReplyDelay,
speechSections,
textType,
ttsSettings,
sensitiveData
});
return this.exitFlow();
}
await this.transcript(call, {
message: 'Not Replied',
action: 'Call DTMF',
reportingSettingsKey,
actionFromBot: false
});
await this.resumeRecording(call, sensitiveData);
return this.exitStep('no reply', {});
}
case 'hangup': {
await this.handleHangup(call);
return await this.waitConvEnd();
}
case 'cancel': {
return this.handleCancel();
}
case 'error':
return this.throwError(event.params.error);
default:
return this.exitFlow();
}
});
this.triggers.otherwise(async () => {
const eventId = await this.transcript(call, {
sections: speechSections,
reprompt: {
maxAttempts: repromptsList.length,
attempt: 0
},
reportingSettingsKey: 'transcriptPrompt',
action: 'Call Prompt',
actionFromBot: true
});
command.params.reporterTranscriptEventId = eventId;
command.params.firstDigitTimeout = this.rptsTimeout({ noReplyDelay, repromptsList, initial: true });
command.params.sections = speechSections;
await this.pauseRecording(call, command, sensitiveData);
return this.exitFlow();
});
}
}
exports.default = KeypadInput;