altervoice-asterisk-callbot
Version:
Jovo plugin for Altervoice asterisk integration
200 lines • 10.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AltervoiceAsteriskCallbot = void 0;
const jovo_core_1 = require("../../jovo-core");
const DialogflowAgent_1 = require("../../jovo-platform-dialogflow/dist/src/DialogflowAgent");
const DialogflowResponse_1 = require("../../jovo-platform-dialogflow/dist/src/core/DialogflowResponse");
const _get = require("lodash.get");
const _set = require("lodash.set");
const _sample = require('lodash.sample');
const _unionWith = require("lodash.unionwith");
const altervoice_asterisk_callbot_user_1 = require("./altervoice-asterisk-callbot-user");
//const idPlatform = 'telephony';
const idPlatform = 'phone_gateway';
const idSource = 'phone_gateway';
__exportStar(require("../../jovo-platform-dialogflow"), exports);
class AltervoiceAsteriskCallbot {
constructor(config) {
this.config = {
enabled: true,
};
}
install(dialogFlow) {
dialogFlow.middleware('$output').use(this.output.bind(this));
dialogFlow.middleware('$type').use(this.type.bind(this));
DialogflowAgent_1.DialogflowAgent.prototype.isCallbot = function () {
return _get(this.$request, 'originalDetectIntentRequest.payload.source') === idSource;
};
DialogflowAgent_1.DialogflowAgent.prototype.addActions = function (action) {
action.platform = idPlatform;
if (action.telephonyMaxCall && action.telephonyMaxCall.prompt)
action.telephonyMaxCall.prompt = action.telephonyMaxCall.prompt.toString();
if (action.telephonyInact && action.telephonyInact.prompt)
action.telephonyInact.prompt = action.telephonyInact.prompt.toString();
if (!this.actions)
this.actions = [];
this.actions.push(action);
};
DialogflowAgent_1.DialogflowAgent.prototype.ask = function (speech, reprompt, ...reprompts) {
delete this.$output.tell;
if (Array.isArray(speech)) {
speech = _sample(speech);
}
if (Array.isArray(reprompt)) {
reprompt = _sample(reprompt);
}
if (!reprompt) {
reprompt = [];
}
this.$output.ask = {
speech: speech.toString(),
reprompt: [reprompt.toString()],
};
if (reprompts) {
this.$output.ask.reprompt = [reprompt.toString()];
reprompts.forEach((repr) => {
this.$output.ask.reprompt.push(repr.toString());
});
}
return this;
};
DialogflowAgent_1.DialogflowAgent.prototype.getDtmf = function () {
let reg = /\/telephony_dtmf$/g;
let outputContext = _get(this.$request, 'queryResult.outputContexts');
let data = outputContext ? outputContext.find((x) => reg.exec(x.name)) : {};
return data.parameters;
};
DialogflowAgent_1.DialogflowAgent.prototype.getResult = function () {
let reg = /\/telephony_result$/g;
let outputContext = _get(this.$request, 'queryResult.outputContexts');
let data = outputContext ? outputContext.find((x) => reg.exec(x.name)) : {};
return data.parameters;
};
DialogflowAgent_1.DialogflowAgent.prototype.addOutputContext = function (name, parameters, lifespanCount = 1) {
let output = _get(this.$output, "Dialogflow.OutputContexts") || [];
output.push({ name, parameters, lifespanCount });
_set(this.$output, 'Dialogflow.OutputContexts', output);
};
DialogflowAgent_1.DialogflowAgent.prototype.getOutputContext = function (name) {
return _get(this.$request, 'queryResult.outputContexts', []).find((context) => {
return context.name.indexOf(`/contexts/${name}`) > -1;
});
};
DialogflowAgent_1.DialogflowAgent.prototype.addSessionEntityTypes = function (sessionEntityTypes) {
if (!_get(this.$output, 'Dialogflow.SessionEntityTypes')) {
_set(this.$output, 'Dialogflow.SessionEntityTypes', []);
}
sessionEntityTypes.forEach((el) => {
// Place session id in front of session entity name to accomodate to proper format
const sessionId = this.$request.getSessionId();
const entityName = el.name;
el.name = `${sessionId}/entityTypes/${entityName}`;
// Set default override mode
if (!el.entityOverrideMode) {
el.entityOverrideMode = 'ENTITY_OVERRIDE_MODE_SUPPLEMENT';
}
});
// Merge existing entities with new ones provided as arguments with _.unionWith.
_set(this.$output, 'Dialogflow.SessionEntityTypes', _unionWith(_get(this.$output, 'Dialogflow.SessionEntityTypes'), sessionEntityTypes, (newEntry, original) => {
// If the new session entity does not yet exist by its name, just add it to the new array.
if (newEntry.name !== original.name) {
return false;
}
// If the session entity already exists by its name, check if its entity values already exist.
const entities = _unionWith(newEntry.entities, original.entities, (n, o) => {
// If the current value is not yet present, just add it with a new entry.
if (n.value !== o.value) {
return false;
}
// Else merge the respective synonyms and unify them.
o.synonyms = _unionWith(o.synonyms, n.synonyms);
return true;
});
// Replace old entries with new, merged ones.
original.entities = entities;
return true;
}));
return this;
};
DialogflowAgent_1.DialogflowAgent.prototype.addSessionEntityType = function (sessionEntityType) {
return this.addSessionEntityTypes([sessionEntityType]);
};
}
uninstall(app) { }
type(dialogflowAgent) {
if (dialogflowAgent.isCallbot()) {
dialogflowAgent.$user = new altervoice_asterisk_callbot_user_1.AltervoiceAsteriskCallbotUser(dialogflowAgent);
if (!dialogflowAgent.$response)
dialogflowAgent.$response = new DialogflowResponse_1.DialogflowResponse();
}
}
output(dialogflowAgent) {
if (dialogflowAgent.isCallbot()) {
const output = dialogflowAgent.$output;
if (!dialogflowAgent.$response) {
dialogflowAgent.$response = new DialogflowResponse_1.DialogflowResponse();
}
const response = dialogflowAgent.$response;
if (dialogflowAgent.actions === undefined)
dialogflowAgent.actions = [];
if (dialogflowAgent.$output.Dialogflow) {
response.sessionEntityTypes = _get(dialogflowAgent.$output.Dialogflow, 'SessionEntityTypes');
}
if (dialogflowAgent.$output.ask) {
// _set(response, 'fulfillmentText', dialogflowAgent.$output.ask.speech);
dialogflowAgent.actions.push({ simpleResponse: { ssml: dialogflowAgent.$output.ask.speech } });
if (dialogflowAgent.$output.ask.reprompt.length)
_set(response, 'payload.' + idPlatform + '.noInputPrompts', dialogflowAgent.$output.ask.reprompt.map((x) => { return { ssml: x }; }));
_set(response, 'payload.' + idPlatform + '.expectUserResponse', true);
response.end_interaction = false;
// delete dialogflowAgent.$output.ask;
}
else if (dialogflowAgent.$output.tell) {
// _set(response, 'fulfillmentText', dialogflowAgent.$output.tell.speech);
dialogflowAgent.actions.push({ simpleResponse: { ssml: dialogflowAgent.$output.tell.speech } });
_set(response, 'payload.' + idPlatform + '.expectUserResponse', false);
response.end_interaction = true;
// delete dialogflowAgent.$output.tell;
}
if (response.fulfillmentText && jovo_core_1.SpeechBuilder.isSSML(response.fulfillmentText)) {
const ssml = response.fulfillmentText;
if (!response.fulfillmentMessages) {
response.fulfillmentMessages = [];
}
response.fulfillmentMessages.push({
platform: idSource,
telephonySynthesizeSpeech: {
ssml,
},
});
response.fulfillmentText = jovo_core_1.SpeechBuilder.removeSSML(response.fulfillmentText);
}
else {
jovo_core_1.Log.debug('Response does not contain SSML');
}
if (dialogflowAgent.actions) {
_set(response, 'payload.' + idPlatform + '.richResponse.items', dialogflowAgent.actions);
dialogflowAgent.actions = [];
}
/*
let user = _get(dialogflowAgent.$request, 'originalDetectIntentRequest.payload.user');
console.log('user=',user);
_set(response, 'payload.'+idPlatform+'.user.',user);
*/
// _set(response, 'userStorage.user', user);
// console.log("response", Object.keys(response));
}
}
}
exports.AltervoiceAsteriskCallbot = AltervoiceAsteriskCallbot;
//# sourceMappingURL=index.js.map