fixes for listen and transcribe

This commit is contained in:
Dave Horton
2020-01-25 16:39:37 -05:00
parent 0df1e44f15
commit 03e8727c8b
3 changed files with 16 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
const Task = require('./task');
const {TaskName, TaskPreconditions, ListenEvents} = require('../utils/constants');
const makeTask = require('./make_task');
const moment = require('moment');
class TaskListen extends Task {
constructor(logger, opts) {
@@ -8,12 +9,13 @@ class TaskListen extends Task {
this.preconditions = TaskPreconditions.Endpoint;
[
'action', 'url', 'finishOnKey', 'maxLength', 'metadata', 'mixType', 'passDtmf', 'playBeep',
'action', 'method', 'url', 'finishOnKey', 'maxLength', 'metadata', 'mixType', 'passDtmf', 'playBeep',
'sampleRate', 'timeout', 'transcribe'
].forEach((k) => this[k] = this.data[k]);
this.mixType = this.mixType || 'mono';
this.sampleRate = this.sampleRate || 8000;
this.method = this.method || 'POST';
this.earlyMedia = this.data.earlyMedia === true;
this.results = {};
@@ -35,6 +37,7 @@ class TaskListen extends Task {
}
await this._startListening(ep);
await this.awaitTaskDone();
if (this.action) await this.performAction(this.method, null, this.results);
} catch (err) {
this.logger.info(err, `TaskListen:exec - error ${this.url}`);
}
@@ -45,11 +48,15 @@ class TaskListen extends Task {
async kill() {
super.kill();
this._clearTimer();
if (this.transcribeTask) await this.transcribeTask.kill();
if (this.ep.connected) {
await this.ep.forkAudioStop()
.catch((err) => this.logger.info(err, 'TaskListen:kill'));
}
if (this.recordStartTime) {
const duration = moment().diff(this.recordStartTime, 'seconds');
this.results.dialCallDuration = duration;
}
if (this.transcribeTask) await this.transcribeTask.kill();
this.notifyTaskDone();
}
@@ -66,11 +73,12 @@ class TaskListen extends Task {
sampling: this.sampleRate,
metadata: this.metadata
});
if (this.timeout) {
this.recordStartTime = moment();
if (this.maxLength) {
this._timer = setTimeout(() => {
this.logger.debug(`TaskListen:_startListening terminating task due to timeout of ${this.timeout} reached`);
this.logger.debug(`TaskListen terminating task due to timeout of ${this.timeout}s reached`);
this.kill();
}, this.timeout * 1000);
}, this.maxLength * 1000);
}
}
@@ -95,6 +103,7 @@ class TaskListen extends Task {
_onDtmf(evt) {
if (evt.dtmf === this.finishOnKey) {
this.logger.info(`TaskListen:_onDtmf terminating task due to dtmf ${evt.dtmf}`);
this.results.digits = evt.dtmf;
this.kill();
}
}

View File

@@ -78,7 +78,7 @@ class TaskTranscribe extends Task {
_onTranscription(ep, evt) {
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
this.notifyHook(this.transcriptionCallback, 'POST', {speech: evt});
this.notifyHook(this.transcriptionCallback, 'POST', null, {speech: evt});
if (this.killed) {
this.logger.debug('TaskTranscribe:_onTranscription exiting after receiving final transcription');
this._clearTimer();

View File

@@ -8,7 +8,7 @@ function hooks(logger, callAttributes) {
if (auth && typeof auth === 'object' && Object.keys(auth) === 2) basicauth = auth;
if ('GET' === method.toUpperCase()) qs = params;
else body = params;
const obj = {url, method, auth: basicauth, json: expectResponse || body, qs, body};
const obj = {url, method, auth: basicauth, json: expectResponse || !!body, qs, body};
logger.debug({opts: obj}, 'actionHook');
return new Promise((resolve, reject) => {
request(obj, (err, response, body) => {