mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2026-02-15 02:39:35 +00:00
Feat/dialogflow cx (#7)
* wip * wip * wip * wip * logging * wip * wip * wip * update docker env to latest freeswitch * wip * lint * wip * support dialogflowcx * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip --------- Co-authored-by: Dave Horton <daveh@beachdognet.com>
This commit is contained in:
@@ -3,20 +3,44 @@ class Intent {
|
||||
this.logger = logger;
|
||||
this.evt = evt;
|
||||
|
||||
this.logger.debug({evt}, 'intent');
|
||||
this.dtmfRequest = checkIntentForDtmfEntry(logger, evt);
|
||||
this.qr = this.isCX ? evt.detect_intent_response.query_result : evt.query_result;
|
||||
this.dtmfRequest = this._checkIntentForDtmfEntry();
|
||||
}
|
||||
|
||||
get response_id() {
|
||||
return this.isCX ? this.evt.detect_intent_response.response_id : this.evt.response_id;
|
||||
}
|
||||
|
||||
get isEmpty() {
|
||||
return this.evt.response_id.length === 0;
|
||||
return !(this.response_id?.length > 0);
|
||||
}
|
||||
|
||||
get fulfillmentText() {
|
||||
return this.evt.query_result.fulfillment_text;
|
||||
if (this.isCX) {
|
||||
if (this.qr && this.qr.response_messages) {
|
||||
for (const msg of this.qr.response_messages) {
|
||||
if (msg.text && msg.text.text && msg.text.text.length > 0) {
|
||||
return msg.text.text.join('\n');
|
||||
}
|
||||
if (msg.output_audio_text) {
|
||||
if (msg.output_audio_text.text) return msg.output_audio_text.text;
|
||||
if (msg.output_audio_text.ssml) return msg.output_audio_text.ssml;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return this.qr.fulfillment_text;
|
||||
}
|
||||
|
||||
get saysEndInteraction() {
|
||||
return this.evt.query_result.intent.end_interaction ;
|
||||
if (this.isCX) {
|
||||
if (!this.qr || !this.qr.response_messages) return false;
|
||||
const end_interaction = this.qr.response_messages
|
||||
.find((m) => typeof m === 'object' && 'end_interaction' in m)?.end_interaction;
|
||||
return end_interaction && Object.keys(end_interaction).length > 0;
|
||||
}
|
||||
return this.qr.intent.end_interaction;
|
||||
}
|
||||
|
||||
get saysCollectDtmf() {
|
||||
@@ -28,7 +52,23 @@ class Intent {
|
||||
}
|
||||
|
||||
get name() {
|
||||
if (!this.isEmpty) return this.evt.query_result.intent.display_name;
|
||||
if (!this.isEmpty) {
|
||||
if (this.isCX) {
|
||||
return this.qr.match?.intent?.display_name;
|
||||
}
|
||||
return this.qr.intent.display_name;
|
||||
}
|
||||
}
|
||||
|
||||
get isCX() {
|
||||
return typeof this.evt.detect_intent_response === 'object';
|
||||
}
|
||||
|
||||
get isNoInput() {
|
||||
if (this.isCX && this.qr && this.qr.match) {
|
||||
return this.qr.match.match_type === 'NO_INPUT';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -38,52 +78,48 @@ class Intent {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a returned intent for DTMF entry information (ES only).
|
||||
* CX does not use fulfillment_messages or output_contexts.
|
||||
*
|
||||
* allow-dtmf-x-y-z
|
||||
* x = min number of digits
|
||||
* y = optional, max number of digits
|
||||
* z = optional, terminating character
|
||||
*/
|
||||
_checkIntentForDtmfEntry() {
|
||||
if (this.isCX) return;
|
||||
|
||||
const qr = this.qr;
|
||||
if (!qr || !qr.fulfillment_messages || !qr.output_contexts) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check for custom payloads with a gather verb
|
||||
const custom = qr.fulfillment_messages.find((f) => f.payload && f.payload.verb === 'gather');
|
||||
if (custom) {
|
||||
this.logger.info({custom}, 'found dtmf custom payload');
|
||||
return {
|
||||
max: custom.payload.numDigits,
|
||||
term: custom.payload.finishOnKey,
|
||||
template: custom.payload.responseTemplate
|
||||
};
|
||||
}
|
||||
|
||||
// check for an output context with a specific naming convention
|
||||
const context = qr.output_contexts.find((oc) => oc.name.includes('/contexts/allow-dtmf-'));
|
||||
if (context) {
|
||||
const arr = /allow-dtmf-(\d+)(?:-(\d+))?(?:-(.*))?/.exec(context.name);
|
||||
if (arr) {
|
||||
this.logger.info('found dtmf output context');
|
||||
return {
|
||||
min: parseInt(arr[1]),
|
||||
max: arr.length > 2 ? parseInt(arr[2]) : null,
|
||||
term: arr.length > 3 ? arr[3] : null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Intent;
|
||||
|
||||
/**
|
||||
* Parse a returned intent for DTMF entry information
|
||||
* i.e.
|
||||
* allow-dtmf-x-y-z
|
||||
* x = min number of digits
|
||||
* y = optional, max number of digits
|
||||
* z = optional, terminating character
|
||||
* e.g.
|
||||
* allow-dtmf-5 : collect 5 digits
|
||||
* allow-dtmf-1-4 : collect between 1 to 4 (inclusive) digits
|
||||
* allow-dtmf-1-4-# : collect 1-4 digits, terminating if '#' is entered
|
||||
* @param {*} intent - dialogflow intent
|
||||
*/
|
||||
const checkIntentForDtmfEntry = (logger, intent) => {
|
||||
const qr = intent.query_result;
|
||||
if (!qr || !qr.fulfillment_messages || !qr.output_contexts) {
|
||||
logger.info({f: qr.fulfillment_messages, o: qr.output_contexts}, 'no dtmfs');
|
||||
return;
|
||||
}
|
||||
|
||||
// check for custom payloads with a gather verb
|
||||
const custom = qr.fulfillment_messages.find((f) => f.payload && f.payload.verb === 'gather');
|
||||
if (custom && custom.payload && custom.payload.verb === 'gather') {
|
||||
logger.info({custom}, 'found dtmf custom payload');
|
||||
return {
|
||||
max: custom.payload.numDigits,
|
||||
term: custom.payload.finishOnKey,
|
||||
template: custom.payload.responseTemplate
|
||||
};
|
||||
}
|
||||
|
||||
// check for an output context with a specific naming convention
|
||||
const context = qr.output_contexts.find((oc) => oc.name.includes('/contexts/allow-dtmf-'));
|
||||
if (context) {
|
||||
const arr = /allow-dtmf-(\d+)(?:-(\d+))?(?:-(.*))?/.exec(context.name);
|
||||
if (arr) {
|
||||
logger.info({custom}, 'found dtmf output context');
|
||||
return {
|
||||
min: parseInt(arr[1]),
|
||||
max: arr.length > 2 ? parseInt(arr[2]) : null,
|
||||
term: arr.length > 3 ? arr[3] : null
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user