From 6e78b466744971b8abb170301e0630ede6f521e9 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 8 Jul 2020 16:07:49 -0400 Subject: [PATCH] more dialogflow changes --- lib/tasks/dialogflow/index.js | 43 +++++++++++++++++++++++------------ lib/tasks/task.js | 12 ---------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/tasks/dialogflow/index.js b/lib/tasks/dialogflow/index.js index acb6c221..f795748a 100644 --- a/lib/tasks/dialogflow/index.js +++ b/lib/tasks/dialogflow/index.js @@ -3,6 +3,7 @@ const {TaskName, TaskPreconditions} = require('../../utils/constants'); const Intent = require('./intent'); const DigitBuffer = require('./digit-buffer'); const Transcription = require('./transcription'); +const normalizeJambones = require('../../utils/normalize-jambones'); class Dialogflow extends Task { constructor(logger, opts) { @@ -23,14 +24,13 @@ class Dialogflow extends Task { this.events = this.data.events; } else if (this.eventHook) { - // send all events by default + // send all events by default - except interim transcripts this.events = [ 'intent', 'transcription', 'dtmf', - 'end-utterance', 'start-play', - 'end-play', + 'stop-play', 'no-input' ]; } @@ -142,7 +142,7 @@ class Dialogflow extends Task { } if (this.events.includes('intent')) { - this.performHook(this.eventHook, {event: 'intent', data: evt}); + this._performHook(cs, this.eventHook, {event: 'intent', data: evt}); } // clear the no-input timer and the digit buffer @@ -158,6 +158,7 @@ class Dialogflow extends Task { setTimeout(() => { if (this.waitingForPlayStart) { this.logger.info('hanging up since intent was marked end interaction'); + this.performAction({dialogflowResult: 'completed'}); this.notifyTaskDone(); } }, 1000); @@ -185,10 +186,10 @@ class Dialogflow extends Task { const transcription = new Transcription(this.logger, evt); if (this.events.includes('transcription') && transcription.isFinal) { - this.performHook(this.eventHook, {event: 'transcription', data: evt}); + this._performHook(cs, this.eventHook, {event: 'transcription', data: evt}); } else if (this.events.includes('interim-transcription') && !transcription.isFinal) { - this.performHook(this.eventHook, {event: 'transcription', data: evt}); + this._performHook(cs, this.eventHook, {event: 'transcription', data: evt}); } // if a final transcription, start a typing sound @@ -203,8 +204,8 @@ class Dialogflow extends Task { * @param {*} evt - event data */ _onEndOfUtterance(cs, evt) { - if (this.events.includes('end-of-utterance')) { - this.performHook(this.eventHook, {event: 'end-of-utterance'}); + if (this.events.includes('end-utterance')) { + this._performHook(cs, this.eventHook, {event: 'end-utterance'}); } } @@ -212,7 +213,7 @@ class Dialogflow extends Task { * Dialogflow has returned an error of some kind. * @param {*} evt - event data */ - _onError(evt) { + _onError(ep, cs, evt) { this.logger.error(`got error: ${JSON.stringify(evt)}`); } @@ -240,11 +241,11 @@ class Dialogflow extends Task { this.logger.info(`starting to play ${evt.path}`); if (this.events.includes('start-play')) { - this.performHook(this.eventHook, {event: 'start-play', data: {path: evt.path}}); + this._performHook(cs, this.eventHook, {event: 'start-play', data: {path: evt.path}}); } await ep.play(evt.path); - if (this.events.includes('end-play')) { - this.performHook(this.eventHook, {event: 'stop-play', data: {path: evt.path}}); + if (this.events.includes('stop-play')) { + this._performHook(cs, this.eventHook, {event: 'stop-play', data: {path: evt.path}}); } this.logger.info(`finished ${evt.path}`); @@ -261,6 +262,7 @@ class Dialogflow extends Task { if (this.hangupAfterPlayDone) { this.logger.info('hanging up since intent was marked end interaction and we completed final prompt'); + this.performAction({dialogflowResult: 'completed'}); this.notifyTaskDone(); } else { @@ -276,7 +278,7 @@ class Dialogflow extends Task { _onDtmf(ep, cs, evt) { if (this.digitBuffer) this.digitBuffer.process(evt.dtmf); if (this.events.includes('dtmf')) { - this.performHook(this.eventHook, {event: 'dtmf', data: evt}); + this._performHook(cs, this.eventHook, {event: 'dtmf', data: evt}); } } @@ -304,7 +306,7 @@ class Dialogflow extends Task { this.noinput = true; if (this.events.includes('no-input')) { - this.performHook(this.eventHook, {event: 'no-input'}); + this._performHook(cs, this.eventHook, {event: 'no-input'}); } // kill the current dialogflow, which will result in us getting an immediate intent @@ -332,6 +334,19 @@ class Dialogflow extends Task { this.noinputTimer = setTimeout(this._onNoInput.bind(this, ep, cs), this.noInputTimeout); } + async _performHook(cs, hook, results) { + const json = await this.cs.requestor.request(hook, results); + if (json && Array.isArray(json)) { + 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); + } + } + } + } module.exports = Dialogflow; diff --git a/lib/tasks/task.js b/lib/tasks/task.js index ad8bec66..e8d6bff9 100644 --- a/lib/tasks/task.js +++ b/lib/tasks/task.js @@ -99,18 +99,6 @@ class Task extends Emitter { } } - async performHook(hook, results, expectResponse = true) { - const json = await this.cs.requestor.request(hook, results); - if (expectResponse && json && Array.isArray(json)) { - 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.callSession.replaceApplication(tasks); - } - } - } - async transferCallToFeatureServer(cs, sipAddress, opts) { const uuid = uuidv4(); const {addKey} = cs.srf.locals.dbHelpers;