diff --git a/lib/tasks/dialogflow/index.js b/lib/tasks/dialogflow/index.js index 30ce216d..75b70f6c 100644 --- a/lib/tasks/dialogflow/index.js +++ b/lib/tasks/dialogflow/index.js @@ -87,7 +87,7 @@ class Dialogflow extends Task { this.ep.removeCustomEventListener('dialogflow::end_of_utterance'); this.ep.removeCustomEventListener('dialogflow::error'); - this.performAction({dialogflowResult: 'caller hungup'}) + if (!this.reportedFinalAction) this.performAction({dialogflowResult: 'caller hungup'}) .catch((err) => this.logger.error({err}, 'dialogflow - error w/ action webook')); await this.ep.api('uuid_break', this.ep.uuid).catch((err) => this.logger.info(err, 'Error killing audio')); @@ -152,6 +152,7 @@ class Dialogflow extends Task { } else { this.logger.info('got empty intent'); + ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); } return; } @@ -211,10 +212,15 @@ class Dialogflow extends Task { this.logger.debug({obj}, 'Dialogflow:_onIntent - playing message via tts'); const fp = await synthAudio(obj); if (fp) cs.trackTmpFile(fp); + + if (this.playInProgress) { + await ep.api('uuid_break', ep.uuid).catch((err) => this.logger.info(err, 'Error killing audio')); + } this.playInProgress = true; this.curentAudioFile = fp; this.logger.debug(`starting to play tts ${fp}`); + if (this.events.includes('start-play')) { this._performHook(cs, this.eventHook, {event: 'start-play', data: {path: fp}}); } @@ -226,6 +232,11 @@ class Dialogflow extends Task { if (this.curentAudioFile === fp) { this.playInProgress = false; + if (this.queuedTasks) { + this.logger.debug('finished playing audio and we have queued tasks'); + this._redirect(cs, this.queuedTasks); + return; + } } this.greetingPlayed = true; @@ -320,10 +331,16 @@ class Dialogflow extends Task { if (this.events.includes('stop-play')) { this._performHook(cs, this.eventHook, {event: 'stop-play', data: {path: evt.path}}); } - this.logger.info(`finished ${evt.path}`); + this.logger.info(`finished ${evt.path}, queued tasks: ${this.queuedTasks.length}`); if (this.curentAudioFile === evt.path) { this.playInProgress = false; + if (this.queuedTasks) { + this.logger.debug('finished playing audio and we have queued tasks'); + this._redirect(cs, this.queuedTasks); + this.queuedTasks.length = 0; + return; + } } /* if (!this.inbound && !this.greetingPlayed) { @@ -413,13 +430,24 @@ class Dialogflow extends Task { const makeTask = require('../make_task'); const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata)); if (tasks && tasks.length > 0) { - this.logger.info({tasks: tasks}, `${this.name} replacing application with ${tasks.length} tasks`); - this.performAction({dialogflowResult: 'redirect'}, false); - cs.replaceApplication(tasks); + if (this.playInProgress) { + this.queuedTasks = tasks; + this.logger.info({tasks: tasks}, + `${this.name} replacing application with ${tasks.length} tasks after play completes`); + return; + } + this._redirect(cs, tasks); } } } + _redirect(cs, tasks) { + this.logger.info({tasks: tasks}, `${this.name} replacing application with ${tasks.length} tasks`); + this.performAction({dialogflowResult: 'redirect'}, false); + this.reportedFinalAction = true; + cs.replaceApplication(tasks); + } + } module.exports = Dialogflow;