add support for retry logic and dtmf

This commit is contained in:
Dave Horton
2022-01-26 14:01:38 -05:00
parent dcd6ddcbca
commit 8c00c89882
4 changed files with 102 additions and 26 deletions

View File

@@ -3,6 +3,7 @@ const {TaskName, TaskPreconditions} = require('../../utils/constants');
const makeTask = require('../make_task');
const { SocketClient } = require('@cognigy/socket-client');
const SpeechConfig = require('./speech-config');
const { IoTThingsGraph } = require('aws-sdk');
const parseGallery = (obj = {}) => {
const {_default} = obj;
@@ -46,6 +47,8 @@ class Cognigy extends Task {
this.actionHook = this.data?.actionHook;
this.data = this.data.data || {};
this.prompts = [];
this.retry = {};
this.timeoutCount = 0;
}
get name() { return TaskName.Cognigy; }
@@ -80,6 +83,7 @@ class Cognigy extends Task {
/* set event handlers and start transcribing */
this.on('transcription', this._onTranscription.bind(this, cs, ep));
this.on('dtmf-collected', this._onDtmf.bind(this, cs, ep));
this.on('timeout', this._onTimeout.bind(this, cs, ep));
this.on('error', this._onError.bind(this, cs, ep));
@@ -132,9 +136,11 @@ class Cognigy extends Task {
this.notifyTaskDone();
}
_makeGatherTask(prompt) {
const config = this.config.makeGatherTaskConfig(prompt);
const gather = makeTask(this.logger, {gather: config}, this);
_makeGatherTask({textPrompt, urlPrompt}) {
const config = this.config.makeGatherTaskConfig({textPrompt, urlPrompt});
const {retry, ...rest} = config;
this.retry = retry;
const gather = makeTask(this.logger, {gather: rest}, this);
return gather;
}
@@ -148,7 +154,6 @@ class Cognigy extends Task {
voice: 'default'
}
};
this.logger.debug({opts}, 'constructing a nested say object');
const say = makeTask(this.logger, {say: opts}, this);
return say;
}
@@ -165,7 +170,7 @@ class Cognigy extends Task {
if (this.prompts.length) {
const text = this.prompts.join('.');
if (text && !this.killed) {
this.gatherTask = this._makeGatherTask(text);
this.gatherTask = this._makeGatherTask({textPrompt: text});
this.gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
}
@@ -230,18 +235,45 @@ class Cognigy extends Task {
this.notifyTaskDone();
}
}
_onDtmf(cs, ep, evt) {
this.logger.info({evt}, 'got dtmf');
/* send dtmf to bot */
try {
if (this.client && this.client.connected) {
this.client.sendMessage(evt.digits);
}
else {
this.logger.info('Cognigy_onTranscription - not sending user dtmf as bot is disconnected');
}
} catch (err) {
this.logger.error({err}, '_onDtmf: Error sending user dtmf to Cognigy - ending task');
this.performAction({cognigyResult: 'socketError'});
this.reportedFinalAction = true;
this.notifyTaskDone();
}
}
_onError(cs, ep, err) {
this.logger.debug({err}, 'Cognigy: got error');
this.logger.info({err}, 'Cognigy: got error');
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err});
this.reportedFinalAction = true;
this.notifyTaskDone();
}
_onTimeout(cs, ep, evt) {
this.logger.debug({evt}, 'Cognigy: got timeout');
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'timeout'});
this.reportedFinalAction = true;
this.notifyTaskDone();
const {noInputRetries, noInputSpeech, noInputUrl} = this.retry;
this.logger.debug({evt, retry: this.retry}, 'Cognigy: got timeout');
if (noInputRetries && this.timeoutCount++ < noInputRetries) {
this.gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl});
this.gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
}
else {
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'timeout'});
this.reportedFinalAction = true;
this.notifyTaskDone();
}
}
}