mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
wip
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
const assert = require('assert');
|
||||||
const Task = require('../task');
|
const Task = require('../task');
|
||||||
const {TaskName, TaskPreconditions} = require('../../utils/constants');
|
const {TaskName, TaskPreconditions} = require('../../utils/constants');
|
||||||
const Intent = require('./intent');
|
const Intent = require('./intent');
|
||||||
@@ -10,22 +11,27 @@ class Dialogflow extends Task {
|
|||||||
super(logger, opts);
|
super(logger, opts);
|
||||||
this.preconditions = TaskPreconditions.Endpoint;
|
this.preconditions = TaskPreconditions.Endpoint;
|
||||||
this.credentials = this.data.credentials;
|
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.model = this.data.model || 'es';
|
||||||
this.cmd = this.model === 'cx' ? 'dialogflow_cx_start' : 'dialogflow_start';
|
this.cmd = this.model === 'cx' ? 'dialogflow_cx_start' : 'dialogflow_start';
|
||||||
this.cmdStop = this.model === 'cx' ? 'dialogflow_cx_stop' : 'dialogflow_stop';
|
this.cmdStop = this.model === 'cx' ? 'dialogflow_cx_stop' : 'dialogflow_stop';
|
||||||
|
|
||||||
/* set project id with environment and region (optionally) */
|
assert(this.agent || !this.isCX, 'agent is required for dialogflow cx');
|
||||||
if (this.data.environment && this.data.region) {
|
assert(this.credentials, 'dialogflow credentials are required');
|
||||||
this.project = `${this.data.project}:${this.data.environment}:${this.data.region}`;
|
|
||||||
}
|
if (!this.isCX) {
|
||||||
else if (this.data.environment) {
|
/* ES: set project id with environment and region (optionally) */
|
||||||
this.project = `${this.data.project}:${this.data.environment}`;
|
if (this.data.environment && this.data.region) {
|
||||||
}
|
this.project = `${this.data.project}:${this.data.environment}:${this.data.region}`;
|
||||||
else if (this.data.region) {
|
}
|
||||||
this.project = `${this.data.project}::${this.data.region}`;
|
else if (this.data.environment) {
|
||||||
}
|
this.project = `${this.data.project}:${this.data.environment}`;
|
||||||
else {
|
}
|
||||||
this.project = this.data.project;
|
else if (this.data.region) {
|
||||||
|
this.project = `${this.data.project}::${this.data.region}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lang = this.data.lang || 'en-US';
|
this.lang = this.data.lang || 'en-US';
|
||||||
@@ -74,28 +80,14 @@ class Dialogflow extends Task {
|
|||||||
|
|
||||||
get name() { return TaskName.Dialogflow; }
|
get name() { return TaskName.Dialogflow; }
|
||||||
|
|
||||||
|
get isCX() { return this.model === 'cx'; }
|
||||||
|
|
||||||
async exec(cs, {ep}) {
|
async exec(cs, {ep}) {
|
||||||
await super.exec(cs);
|
await super.exec(cs);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.init(cs, ep);
|
await this.init(cs, ep);
|
||||||
|
await this.startBot();
|
||||||
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();
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({err}, 'Dialogflow:exec error');
|
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,
|
* 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.
|
* we may get an empty intent, signified by the lack of a 'response_id' attribute.
|
||||||
|
|||||||
Reference in New Issue
Block a user