updates for lex v2

This commit is contained in:
Dave Horton
2020-10-26 09:59:10 -04:00
parent 6b63009707
commit b47e490424
4 changed files with 112 additions and 93 deletions

View File

@@ -7,10 +7,16 @@ class Lex extends Task {
super(logger, opts);
this.preconditions = TaskPreconditions.Endpoint;
this.credentials = this.data.credentials;
this.bot = this.data.bot;
this.alias = this.data.alias;
if (this.data.credentials) {
this.awsAccessKeyId = this.data.credentials.accessKey;
this.awsSecretAccessKey = this.data.credentials.secretAccessKey;
}
this.bot = this.data.botId;
this.alias = this.data.botAlias;
this.region = this.data.region;
this.locale = this.data.locale || 'en_US';
this.intent = this.data.intent;
this.welcomeMessage = this.data.welcomeMessage;
this.bargein = this.data.bargein || false;
this.passDtmf = this.data.passDtmf || false;
if (this.data.noInputTimeout) this.noInputTimeout = this.data.noInputTimeout * 1000;
@@ -22,12 +28,8 @@ class Lex extends Task {
this.botName = `${this.bot}:${this.alias}:${this.region}`;
if (this.data.eventHook) this.eventHook = this.data.eventHook;
if (this.eventHook && Array.isArray(this.data.events)) {
this.events = this.data.events;
}
else if (this.eventHook) {
// send all events by default - except interim transcripts
this.events = [
this.events = this.eventHook ?
[
'intent',
'transcription',
'dtmf',
@@ -35,11 +37,7 @@ class Lex extends Task {
'stop-play',
'play-interrupted',
'response-text'
];
}
else {
this.events = [];
}
] : [];
if (this.data.actionHook) this.actionHook = this.data.actionHook;
}
@@ -51,10 +49,10 @@ class Lex extends Task {
try {
await this.init(cs, ep);
this.logger.debug(`starting lex bot ${this.botName}`);
this.logger.debug(`starting lex bot ${this.botName} with locale ${this.locale}`);
// kick it off
this.ep.api('aws_lex_start', `${this.ep.uuid} ${this.bot} ${this.alias} ${this.region}`)
this.ep.api('aws_lex_start', `${this.ep.uuid} ${this.bot} ${this.alias} ${this.region} ${this.locale}`)
.catch((err) => {
this.logger.error({err}, `Error starting lex bot ${this.botName}`);
this.notifyTaskDone();
@@ -102,12 +100,25 @@ class Lex extends Task {
this.ep.addCustomEventListener('lex::error', this._onError.bind(this, ep, cs));
this.ep.on('dtmf', this._onDtmf.bind(this, ep, cs));
const channelVars = {};
if (this.bargein) {
await this.ep.set('x-amz-lex:barge-in-enabled', 1);
Object.assign(channelVars, {'x-amz-lex:barge-in-enabled': 1});
}
if (this.noInputTimeout) {
await this.ep.set('x-amz-lex:start-silence-threshold-ms', this.noInputTimeout);
Object.assign(channelVars, {'x-amz-lex:audio:start-timeout-ms': this.noInputTimeout});
}
if (this.awsAccessKeyId && this.awsSecretAccessKey) {
Object.assign(channelVars, {
AWS_ACCESS_KEY_ID: this.awsAccessKeyId,
AWS_SECRET_ACCESS_KEY: this.awsSecretAccessKey
});
}
if (this.vendor) Object.assign(channelVars, {LEX_USE_TTS: 1});
if (this.intent && this.intent.length) Object.assign(channelVars, {LEX_WELCOME_INTENT: this.intent});
if (this.welcomeMessage && this.welcomeMessage.length) {
Object.assign(channelVars, {LEX_WELCOME_MESSAGE: this.welcomeMessage});
}
if (Object.keys(channelVars).length) await this.ep.set(channelVars);
} catch (err) {
this.logger.error({err}, 'Error setting listeners');
@@ -149,37 +160,42 @@ class Lex extends Task {
*/
async _onTextResponse(ep, cs, evt) {
this.logger.debug({evt}, `got text response for ${this.botName}`);
const messages = evt.messages;
if (this.events.includes('response-text')) {
this._performHook(cs, this.eventHook, {event: 'response-text', data: evt});
}
if (this.vendor && ['PlainText', 'SSML'].includes(evt.type) && evt.msg) {
const {srf} = cs;
const {synthAudio} = srf.locals.dbHelpers;
if (this.vendor && Array.isArray(messages) && messages.length) {
const msg = messages[0].msg;
const type = messages[0].type;
if (['PlainText', 'SSML'].includes(type) && msg) {
const {srf} = cs;
const {synthAudio} = srf.locals.dbHelpers;
try {
this.logger.debug(`tts with ${this.vendor} ${this.voice}`);
const fp = await synthAudio({
text: evt.msg,
vendor: this.vendor,
language: this.language,
voice: this.voice,
salt: cs.callSid
});
if (fp) cs.trackTmpFile(fp);
if (this.events.includes('start-play')) {
this._performHook(cs, this.eventHook, {event: 'start-play', data: {path: fp}});
}
await ep.play(fp);
if (this.events.includes('stop-play')) {
this._performHook(cs, this.eventHook, {event: 'stop-play', data: {path: fp}});
}
this.logger.debug(`finished tts, sending play_done ${this.vendor} ${this.voice}`);
this.ep.api('aws_lex_play_done', this.ep.uuid)
.catch((err) => {
this.logger.error({err}, `Error sending play_done ${this.botName}`);
try {
this.logger.debug(`tts with ${this.vendor} ${this.voice}`);
const fp = await synthAudio({
text: msg,
vendor: this.vendor,
language: this.language,
voice: this.voice,
salt: cs.callSid
});
} catch (err) {
this.logger.error({err}, 'Lex:_onTextResponse - error playing tts');
if (fp) cs.trackTmpFile(fp);
if (this.events.includes('start-play')) {
this._performHook(cs, this.eventHook, {event: 'start-play', data: {path: fp}});
}
await ep.play(fp);
if (this.events.includes('stop-play')) {
this._performHook(cs, this.eventHook, {event: 'stop-play', data: {path: fp}});
}
this.logger.debug(`finished tts, sending play_done ${this.vendor} ${this.voice}`);
this.ep.api('aws_lex_play_done', this.ep.uuid)
.catch((err) => {
this.logger.error({err}, `Error sending play_done ${this.botName}`);
});
} catch (err) {
this.logger.error({err}, 'Lex:_onTextResponse - error playing tts');
}
}
}
}

View File

@@ -152,14 +152,17 @@
},
"lex": {
"properties": {
"bot": "string",
"alias": "string",
"botId": "string",
"botAlias": "string",
"credentials": "object",
"region": "string",
"locale": "string",
"intent": "string",
"welcomeMessage": "string",
"bargein": "boolean",
"passDtmf": "boolean",
"actionHook": "object|string",
"eventHook": "object|string",
"events": "[string]",
"prompt": {
"type": "string",
"enum": ["lex", "tts"]
@@ -168,8 +171,8 @@
"tts": "#synthesizer"
},
"required": [
"bot",
"alias",
"botId",
"botAlias",
"region",
"prompt"
]