diff --git a/lib/tasks/dialogflow/index.js b/lib/tasks/dialogflow/index.js index db77087e..a765b7f5 100644 --- a/lib/tasks/dialogflow/index.js +++ b/lib/tasks/dialogflow/index.js @@ -1,3 +1,4 @@ +const assert = require('assert'); const Task = require('../task'); const {TaskName, TaskPreconditions} = require('../../utils/constants'); const Intent = require('./intent'); @@ -10,22 +11,27 @@ class Dialogflow extends Task { super(logger, opts); this.preconditions = TaskPreconditions.Endpoint; this.credentials = this.data.credentials; + this.project = this.data.project; + this.agent = this.data.agent; + this.region = this.data.region || 'default'; 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) { - this.project = `${this.data.project}:${this.data.environment}:${this.data.region}`; - } - else if (this.data.environment) { - this.project = `${this.data.project}:${this.data.environment}`; - } - else if (this.data.region) { - this.project = `${this.data.project}::${this.data.region}`; - } - else { - this.project = this.data.project; + assert(this.agent || !this.isCX, 'agent is required for dialogflow cx'); + assert(this.credentials, 'dialogflow credentials are required'); + + if (!this.isCX) { + /* ES: set project id with environment and region (optionally) */ + if (this.data.environment && this.data.region) { + this.project = `${this.data.project}:${this.data.environment}:${this.data.region}`; + } + else if (this.data.environment) { + this.project = `${this.data.project}:${this.data.environment}`; + } + else if (this.data.region) { + this.project = `${this.data.project}::${this.data.region}`; + } } this.lang = this.data.lang || 'en-US'; @@ -74,28 +80,14 @@ class Dialogflow extends Task { get name() { return TaskName.Dialogflow; } + get isCX() { return this.model === 'cx'; } + async exec(cs, {ep}) { await super.exec(cs); try { await this.init(cs, ep); - - 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(this.cmd, `${baseArgs} '${JSON.stringify(this.welcomeEventParams)}'`); - } - else if (this.welcomeEvent.length) { - this.ep.api(this.cmd, baseArgs); - } - else { - this.ep.api(this.cmd, `${this.ep.uuid} ${this.project} ${this.lang}`); - } - this.logger.debug(`started dialogflow bot ${this.project}`); - - await this.awaitTaskDone(); + await this.startBot(); } catch (err) { this.logger.error({err}, 'Dialogflow:exec error'); } @@ -166,6 +158,38 @@ class Dialogflow extends Task { } } + async startBot() { + await (this.isCX ? this.startBotCX() : this.startBotES()); + } + + async startBotES() { + const baseArgs = `${this.ep.uuid} ${this.project} ${this.lang} ${this.welcomeEvent}`; + if (this.welcomeEventParams) { + await this.ep.api(this.cmd, `${baseArgs} '${JSON.stringify(this.welcomeEventParams)}'`); + } + else if (this.welcomeEvent.length) { + await this.ep.api(this.cmd, baseArgs); + } + else { + await this.ep.api(this.cmd, `${this.ep.uuid} ${this.project} ${this.lang}`); + } + } + + async startBotCX() { + const baseArgs = [ + this.ep.uuid, + this.region, + this.project, + this.agent, + this.environment, + this.lang, + ]; + if (this.welcomeEvent) { + baseArgs.push(this.welcomeEvent); + } + await this.ep.api(this.cmd, `${baseArgs.join(' ')}`) + } + /** * An intent has been returned. Since we are using SINGLE_UTTERANCE on the dialogflow side, * we may get an empty intent, signified by the lack of a 'response_id' attribute.