more dialogflow changes

This commit is contained in:
Dave Horton
2020-07-08 16:07:49 -04:00
parent a4bcfca9e6
commit 6e78b46674
2 changed files with 29 additions and 26 deletions

View File

@@ -3,6 +3,7 @@ const {TaskName, TaskPreconditions} = require('../../utils/constants');
const Intent = require('./intent'); const Intent = require('./intent');
const DigitBuffer = require('./digit-buffer'); const DigitBuffer = require('./digit-buffer');
const Transcription = require('./transcription'); const Transcription = require('./transcription');
const normalizeJambones = require('../../utils/normalize-jambones');
class Dialogflow extends Task { class Dialogflow extends Task {
constructor(logger, opts) { constructor(logger, opts) {
@@ -23,14 +24,13 @@ class Dialogflow extends Task {
this.events = this.data.events; this.events = this.data.events;
} }
else if (this.eventHook) { else if (this.eventHook) {
// send all events by default // send all events by default - except interim transcripts
this.events = [ this.events = [
'intent', 'intent',
'transcription', 'transcription',
'dtmf', 'dtmf',
'end-utterance',
'start-play', 'start-play',
'end-play', 'stop-play',
'no-input' 'no-input'
]; ];
} }
@@ -142,7 +142,7 @@ class Dialogflow extends Task {
} }
if (this.events.includes('intent')) { 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 // clear the no-input timer and the digit buffer
@@ -158,6 +158,7 @@ class Dialogflow extends Task {
setTimeout(() => { setTimeout(() => {
if (this.waitingForPlayStart) { if (this.waitingForPlayStart) {
this.logger.info('hanging up since intent was marked end interaction'); this.logger.info('hanging up since intent was marked end interaction');
this.performAction({dialogflowResult: 'completed'});
this.notifyTaskDone(); this.notifyTaskDone();
} }
}, 1000); }, 1000);
@@ -185,10 +186,10 @@ class Dialogflow extends Task {
const transcription = new Transcription(this.logger, evt); const transcription = new Transcription(this.logger, evt);
if (this.events.includes('transcription') && transcription.isFinal) { 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) { 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 // if a final transcription, start a typing sound
@@ -203,8 +204,8 @@ class Dialogflow extends Task {
* @param {*} evt - event data * @param {*} evt - event data
*/ */
_onEndOfUtterance(cs, evt) { _onEndOfUtterance(cs, evt) {
if (this.events.includes('end-of-utterance')) { if (this.events.includes('end-utterance')) {
this.performHook(this.eventHook, {event: 'end-of-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. * Dialogflow has returned an error of some kind.
* @param {*} evt - event data * @param {*} evt - event data
*/ */
_onError(evt) { _onError(ep, cs, evt) {
this.logger.error(`got error: ${JSON.stringify(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}`); this.logger.info(`starting to play ${evt.path}`);
if (this.events.includes('start-play')) { 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); await ep.play(evt.path);
if (this.events.includes('end-play')) { if (this.events.includes('stop-play')) {
this.performHook(this.eventHook, {event: 'stop-play', data: {path: evt.path}}); this._performHook(cs, this.eventHook, {event: 'stop-play', data: {path: evt.path}});
} }
this.logger.info(`finished ${evt.path}`); this.logger.info(`finished ${evt.path}`);
@@ -261,6 +262,7 @@ class Dialogflow extends Task {
if (this.hangupAfterPlayDone) { if (this.hangupAfterPlayDone) {
this.logger.info('hanging up since intent was marked end interaction and we completed final prompt'); this.logger.info('hanging up since intent was marked end interaction and we completed final prompt');
this.performAction({dialogflowResult: 'completed'});
this.notifyTaskDone(); this.notifyTaskDone();
} }
else { else {
@@ -276,7 +278,7 @@ class Dialogflow extends Task {
_onDtmf(ep, cs, evt) { _onDtmf(ep, cs, evt) {
if (this.digitBuffer) this.digitBuffer.process(evt.dtmf); if (this.digitBuffer) this.digitBuffer.process(evt.dtmf);
if (this.events.includes('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; this.noinput = true;
if (this.events.includes('no-input')) { 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 // 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); 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; module.exports = Dialogflow;

View File

@@ -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) { async transferCallToFeatureServer(cs, sipAddress, opts) {
const uuid = uuidv4(); const uuid = uuidv4();
const {addKey} = cs.srf.locals.dbHelpers; const {addKey} = cs.srf.locals.dbHelpers;