From cda60479423826ffff689fb544682956b8e4923a Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 20 Aug 2024 09:56:41 -0400 Subject: [PATCH] wip --- lib/tasks/dialogflow/index.js | 41 ++++++++++++++++++++++++----------- package-lock.json | 14 ++++++------ package.json | 2 +- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/tasks/dialogflow/index.js b/lib/tasks/dialogflow/index.js index 8240a069..db77087e 100644 --- a/lib/tasks/dialogflow/index.js +++ b/lib/tasks/dialogflow/index.js @@ -10,6 +10,9 @@ class Dialogflow extends Task { super(logger, opts); this.preconditions = TaskPreconditions.Endpoint; this.credentials = this.data.credentials; + this.model = this.data.model || 'es'; + this.cmd = this.model === 'cx' ? 'dialogflow_cx_start' : 'dialogflow_start'; + this.cmdStop = this.model === 'cx' ? 'dialogflow_cx_stop' : 'dialogflow_stop'; /* set project id with environment and region (optionally) */ if (this.data.environment && this.data.region) { @@ -77,18 +80,18 @@ class Dialogflow extends Task { try { await this.init(cs, ep); - this.logger.debug(`starting dialogflow bot ${this.project}`); + this.logger.debug(`starting dialogflow ${this.model} bot ${this.project}`); // kick it off const baseArgs = `${this.ep.uuid} ${this.project} ${this.lang} ${this.welcomeEvent}`; if (this.welcomeEventParams) { - this.ep.api('dialogflow_start', `${baseArgs} '${JSON.stringify(this.welcomeEventParams)}'`); + this.ep.api(this.cmd, `${baseArgs} '${JSON.stringify(this.welcomeEventParams)}'`); } else if (this.welcomeEvent.length) { - this.ep.api('dialogflow_start', baseArgs); + this.ep.api(this.cmd, baseArgs); } else { - this.ep.api('dialogflow_start', `${this.ep.uuid} ${this.project} ${this.lang}`); + this.ep.api(this.cmd, `${this.ep.uuid} ${this.project} ${this.lang}`); } this.logger.debug(`started dialogflow bot ${this.project}`); @@ -108,6 +111,12 @@ class Dialogflow extends Task { this.ep.removeCustomEventListener('dialogflow::end_of_utterance'); this.ep.removeCustomEventListener('dialogflow::error'); + this.ep.removeCustomEventListener('dialogflow_cx::intent'); + this.ep.removeCustomEventListener('dialogflow_cx::transcription'); + this.ep.removeCustomEventListener('dialogflow_cx::audio_provided'); + this.ep.removeCustomEventListener('dialogflow_cx::end_of_utterance'); + this.ep.removeCustomEventListener('dialogflow_cx::error'); + this._clearNoinputTimer(); if (!this.reportedFinalAction) this.performAction({dialogflowResult: 'caller hungup'}) @@ -141,6 +150,12 @@ class Dialogflow extends Task { this.ep.addCustomEventListener('dialogflow::end_of_utterance', this._onEndOfUtterance.bind(this, ep, cs)); this.ep.addCustomEventListener('dialogflow::error', this._onError.bind(this, ep, cs)); + this.ep.addCustomEventListener('dialogflow_cx::intent', this._onIntent.bind(this, ep, cs)); + this.ep.addCustomEventListener('dialogflow_cx::transcription', this._onTranscription.bind(this, ep, cs)); + this.ep.addCustomEventListener('dialogflow_cx::audio_provided', this._onAudioProvided.bind(this, ep, cs)); + this.ep.addCustomEventListener('dialogflow_cx::end_of_utterance', this._onEndOfUtterance.bind(this, ep, cs)); + this.ep.addCustomEventListener('dialogflow_cx::error', this._onError.bind(this, ep, cs)); + const obj = typeof this.credentials === 'string' ? JSON.parse(this.credentials) : this.credentials; const creds = JSON.stringify(obj); await this.ep.set('GOOGLE_APPLICATION_CREDENTIALS', creds); @@ -171,20 +186,20 @@ class Dialogflow extends Task { if (this.noinput && this.greetingPlayed) { this.logger.info('no input timer fired, reprompting..'); this.noinput = false; - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang} ${this.noInputEvent}`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang} ${this.noInputEvent}`); } else if (this.dtmfEntry && this.greetingPlayed) { this.logger.info('dtmf detected, reprompting..'); - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang} none \'${this.dtmfEntry}\'`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang} none \'${this.dtmfEntry}\'`); this.dtmfEntry = null; } else if (this.greetingPlayed) { this.logger.info('starting another intent'); - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang}`); } else { this.logger.info('got empty intent'); - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang}`); } return; } @@ -231,7 +246,7 @@ class Dialogflow extends Task { // start a new intent, (we want to continue to listen during the audio playback) // _unless_ we are transferring or ending the session if (!this.hangupAfterPlayDone) { - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang}`); } try { @@ -389,7 +404,7 @@ class Dialogflow extends Task { // start a new intent, (we want to continue to listen during the audio playback) // _unless_ we are transferring or ending the session if (/*this.greetingPlayed &&*/ !this.hangupAfterPlayDone) { - ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); + ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang}`); } this.playInProgress = true; @@ -417,7 +432,7 @@ class Dialogflow extends Task { /* if (!this.inbound && !this.greetingPlayed) { this.logger.info('finished greeting on outbound call, starting new intent'); - this.ep.api('dialogflow_start', `${ep.uuid} ${this.project} ${this.lang}`); + this.ep.api(this.cmd, `${ep.uuid} ${this.project} ${this.lang}`); } */ this.greetingPlayed = true; @@ -454,7 +469,7 @@ class Dialogflow extends Task { } // kill the current dialogflow, which will result in us getting an immediate intent - ep.api('dialogflow_stop', `${ep.uuid}`) + ep.api(this.cmdStop, `${ep.uuid}`) .catch((err) => this.logger.info(`dialogflow_stop failed: ${err.message}`)); } @@ -472,7 +487,7 @@ class Dialogflow extends Task { } // kill the current dialogflow, which will result in us getting an immediate intent - ep.api('dialogflow_stop', `${ep.uuid}`) + ep.api(this.cmdStop, `${ep.uuid}`) .catch((err) => this.logger.info(`dialogflow_stop failed: ${err.message}`)); } diff --git a/package-lock.json b/package-lock.json index 6a663072..65a66479 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@jambonz/speech-utils": "^0.1.15", "@jambonz/stats-collector": "^0.1.10", "@jambonz/time-series": "^0.2.9", - "@jambonz/verb-specifications": "^0.0.76", + "@jambonz/verb-specifications": "^0.0.77", "@opentelemetry/api": "^1.8.0", "@opentelemetry/exporter-jaeger": "^1.23.0", "@opentelemetry/exporter-trace-otlp-http": "^0.50.0", @@ -1575,9 +1575,9 @@ } }, "node_modules/@jambonz/verb-specifications": { - "version": "0.0.76", - "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.76.tgz", - "integrity": "sha512-7s61qAsG07xLLaEAHW236rSYzEoh9Qg0aRWHPbTfxCsuTKDNeq+5EwGAShDU5R5ZpjgweZJLhArQm8Ym+4xJ2A==", + "version": "0.0.77", + "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.77.tgz", + "integrity": "sha512-reGTuBmxiiCo4eCWAtHOBT+Dm6OxZ8d2Yb9J/3scQ92oT+UHFO3U3gutEUH0qjdZAn3ogCkSmWABWn6LEqhqaA==", "dependencies": { "debug": "^4.3.4", "pino": "^8.8.0" @@ -10539,9 +10539,9 @@ } }, "@jambonz/verb-specifications": { - "version": "0.0.76", - "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.76.tgz", - "integrity": "sha512-7s61qAsG07xLLaEAHW236rSYzEoh9Qg0aRWHPbTfxCsuTKDNeq+5EwGAShDU5R5ZpjgweZJLhArQm8Ym+4xJ2A==", + "version": "0.0.77", + "resolved": "https://registry.npmjs.org/@jambonz/verb-specifications/-/verb-specifications-0.0.77.tgz", + "integrity": "sha512-reGTuBmxiiCo4eCWAtHOBT+Dm6OxZ8d2Yb9J/3scQ92oT+UHFO3U3gutEUH0qjdZAn3ogCkSmWABWn6LEqhqaA==", "requires": { "debug": "^4.3.4", "pino": "^8.8.0" diff --git a/package.json b/package.json index 286bdb4f..3537d2db 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@jambonz/speech-utils": "^0.1.15", "@jambonz/stats-collector": "^0.1.10", "@jambonz/time-series": "^0.2.9", - "@jambonz/verb-specifications": "^0.0.76", + "@jambonz/verb-specifications": "^0.0.77", "@opentelemetry/api": "^1.8.0", "@opentelemetry/exporter-jaeger": "^1.23.0", "@opentelemetry/exporter-trace-otlp-http": "^0.50.0",