mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2026-02-13 01:39:26 +00:00
fixes for listen and transcribe
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const Task = require('./task');
|
const Task = require('./task');
|
||||||
const {TaskName, TaskPreconditions, ListenEvents} = require('../utils/constants');
|
const {TaskName, TaskPreconditions, ListenEvents} = require('../utils/constants');
|
||||||
const makeTask = require('./make_task');
|
const makeTask = require('./make_task');
|
||||||
|
const moment = require('moment');
|
||||||
|
|
||||||
class TaskListen extends Task {
|
class TaskListen extends Task {
|
||||||
constructor(logger, opts) {
|
constructor(logger, opts) {
|
||||||
@@ -8,12 +9,13 @@ class TaskListen extends Task {
|
|||||||
this.preconditions = TaskPreconditions.Endpoint;
|
this.preconditions = TaskPreconditions.Endpoint;
|
||||||
|
|
||||||
[
|
[
|
||||||
'action', 'url', 'finishOnKey', 'maxLength', 'metadata', 'mixType', 'passDtmf', 'playBeep',
|
'action', 'method', 'url', 'finishOnKey', 'maxLength', 'metadata', 'mixType', 'passDtmf', 'playBeep',
|
||||||
'sampleRate', 'timeout', 'transcribe'
|
'sampleRate', 'timeout', 'transcribe'
|
||||||
].forEach((k) => this[k] = this.data[k]);
|
].forEach((k) => this[k] = this.data[k]);
|
||||||
|
|
||||||
this.mixType = this.mixType || 'mono';
|
this.mixType = this.mixType || 'mono';
|
||||||
this.sampleRate = this.sampleRate || 8000;
|
this.sampleRate = this.sampleRate || 8000;
|
||||||
|
this.method = this.method || 'POST';
|
||||||
this.earlyMedia = this.data.earlyMedia === true;
|
this.earlyMedia = this.data.earlyMedia === true;
|
||||||
this.results = {};
|
this.results = {};
|
||||||
|
|
||||||
@@ -35,6 +37,7 @@ class TaskListen extends Task {
|
|||||||
}
|
}
|
||||||
await this._startListening(ep);
|
await this._startListening(ep);
|
||||||
await this.awaitTaskDone();
|
await this.awaitTaskDone();
|
||||||
|
if (this.action) await this.performAction(this.method, null, this.results);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.info(err, `TaskListen:exec - error ${this.url}`);
|
this.logger.info(err, `TaskListen:exec - error ${this.url}`);
|
||||||
}
|
}
|
||||||
@@ -45,11 +48,15 @@ class TaskListen extends Task {
|
|||||||
async kill() {
|
async kill() {
|
||||||
super.kill();
|
super.kill();
|
||||||
this._clearTimer();
|
this._clearTimer();
|
||||||
if (this.transcribeTask) await this.transcribeTask.kill();
|
|
||||||
if (this.ep.connected) {
|
if (this.ep.connected) {
|
||||||
await this.ep.forkAudioStop()
|
await this.ep.forkAudioStop()
|
||||||
.catch((err) => this.logger.info(err, 'TaskListen:kill'));
|
.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();
|
this.notifyTaskDone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +73,12 @@ class TaskListen extends Task {
|
|||||||
sampling: this.sampleRate,
|
sampling: this.sampleRate,
|
||||||
metadata: this.metadata
|
metadata: this.metadata
|
||||||
});
|
});
|
||||||
if (this.timeout) {
|
this.recordStartTime = moment();
|
||||||
|
if (this.maxLength) {
|
||||||
this._timer = setTimeout(() => {
|
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.kill();
|
||||||
}, this.timeout * 1000);
|
}, this.maxLength * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +103,7 @@ class TaskListen extends Task {
|
|||||||
_onDtmf(evt) {
|
_onDtmf(evt) {
|
||||||
if (evt.dtmf === this.finishOnKey) {
|
if (evt.dtmf === this.finishOnKey) {
|
||||||
this.logger.info(`TaskListen:_onDtmf terminating task due to dtmf ${evt.dtmf}`);
|
this.logger.info(`TaskListen:_onDtmf terminating task due to dtmf ${evt.dtmf}`);
|
||||||
|
this.results.digits = evt.dtmf;
|
||||||
this.kill();
|
this.kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ class TaskTranscribe extends Task {
|
|||||||
|
|
||||||
_onTranscription(ep, evt) {
|
_onTranscription(ep, evt) {
|
||||||
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
|
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
|
||||||
this.notifyHook(this.transcriptionCallback, 'POST', {speech: evt});
|
this.notifyHook(this.transcriptionCallback, 'POST', null, {speech: evt});
|
||||||
if (this.killed) {
|
if (this.killed) {
|
||||||
this.logger.debug('TaskTranscribe:_onTranscription exiting after receiving final transcription');
|
this.logger.debug('TaskTranscribe:_onTranscription exiting after receiving final transcription');
|
||||||
this._clearTimer();
|
this._clearTimer();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ function hooks(logger, callAttributes) {
|
|||||||
if (auth && typeof auth === 'object' && Object.keys(auth) === 2) basicauth = auth;
|
if (auth && typeof auth === 'object' && Object.keys(auth) === 2) basicauth = auth;
|
||||||
if ('GET' === method.toUpperCase()) qs = params;
|
if ('GET' === method.toUpperCase()) qs = params;
|
||||||
else body = 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');
|
logger.debug({opts: obj}, 'actionHook');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request(obj, (err, response, body) => {
|
request(obj, (err, response, body) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user