Professional Documents
Culture Documents
interface CPaaSBody {
AccountSid: string;
AnsweredBy: string;
ApiVersion: string;
CallDuration: string;
CallSid: string;
CallerName: string;
Direction: string;
From: string;
SipCallId: string;
StatusCallback: string;
To: string;
UrlBase: string;
Digits: string;
SpeechResult: string;
SpeechResultError: string;
Body: string;
SmsSid: string;
SessionId: string;
}
createRestTransformer({
/**
* This transformer is executed when receiving a message
* from the user, before executing the Flow.
*
* @param endpoint The configuration object for the used Endpoint.
* @param request The Express request object with a JSON parsed body.
* @param response The Express response object.
*
* @returns A valid userId, sessionId, as well as text and/or data,
* which has been extracted from the request body.
*/
handleInput: async ({ endpoint, request, response }) => {
/**
* Extract the userId, sessionId and text
* from the request. Example:
*
* const { userId, sessionId, text, data } = request.body;
*
* Note that the format of the request body will be different for
* every Endpoint, and the example above needs to be adjusted
* accordingly.
*/
const { headers, body } = request;
const { host } = headers;
if (DEBUG_MODE) {
console.log('body='.concat(JSON.stringify(body)));
}
const { AccountSid, ApiVersion, Digits, CallSid, From, SpeechResult,
SpeechResultError, To, UrlBase, Body, SessionId } = body;
if (CallSid && endpoint?.settings?.cpaasToken && !
validSignature(endpoint,request,UrlBase)) {
response.status(401).send('Unauthorized');
console.log('Unauthorized')
return null;
} else if (DEBUG_MODE) {
console.log('valid signature or turned off');
}
if (ApiVersion != DEFAULT_API_VERSION) {
throw Error('wrong CPaaS API version '.concat(ApiVersion));
}
if (SpeechResultError == 'redirect') {
return null;
}
const userId = From;
const sessionId = CallSid ? CallSid : (SessionId ? SessionId :
(userId+getTimestamp()));
let sessionStorage = await getSessionStorage(userId,sessionId);
if (!sessionStorage.urlbase) {
sessionStorage.urlbase = host;
}
sessionStorage.From = From;
sessionStorage.To = To;
sessionStorage.cpaasChannel = CallSid ? 'call' : 'sms';
sessionStorage.numberOfDigits = sessionStorage.numberOfDigits ?
sessionStorage.numberOfDigits : DEFAULT_NUM_DIGITS;
sessionStorage.language = sessionStorage.language ?
sessionStorage.language : DEFAULT_LANGUAGE;
sessionStorage.voice = sessionStorage.voice ? sessionStorage.voice :
DEFAULT_VOICE;
let data = {"accountSid":AccountSid, "apiVersion":ApiVersion, "call":
false,"sms":false, "phone":From};
const menu = sessionStorage['menu'] ? sessionStorage['menu'] : {};
let text = '';
switch (sessionStorage.cpaasChannel) {
case 'call':
text = Digits ? (menu[Digits] ? menu[Digits] :
Digits.replace(/\s+/g, '')) : SpeechResult;
data.call = true;
break;
case 'sms':
text = Body.match(/^\d$/) ? (menu[Body] ? menu[Body] :
Body) : Body;
data.sms = true;
break;
default:
text = 'default';
break;
}
if (DEBUG_MODE) {
console.log('input.text='.concat(text));
}
return {
userId,
sessionId,
text,
data
};
},
/**
* This transformer is executed on every output from the Flow.
*
* @param output The raw output from the Flow. It is possible to manipulate
* and return every distinct output before they get formatted in the
'handleExecutionFinished'
* transformer.
*
* @param endpoint The configuration object for the used Endpoint.
* @param userId The unique ID of the user.
* @param sessionId The unique ID for this session. Can be used together with
the userId
* to retrieve the sessionStorage object.
*
* @returns The output that will be formatted into the final response in the
'handleExecutionFinished' transformer.
*/
handleOutput: async ({ output, endpoint, userId, sessionId }) => {
const sessionStorage = await getSessionStorage(userId, sessionId);
if (DEBUG_MODE) {
console.log('output='.concat(JSON.stringify(output)));
}
if (output?.data?._cognigy?._spoken) {
output = getExtensionOutput(output, userId, sessionStorage);
} else {
output = getCognigyOutput(output, sessionStorage);
}
return output;
},
/**
* This transformer is executed when the Flow execution has finished.
* For REST based transformers, the final output will be sent to
* the user.
*
* @param processedOutput This is the final object that will be sent to the
user.
* It is therefore structured according to the Endpoint channel of the
transformer.
*
* @param outputs This is an array of all of the outputs that were output by
the Flow.
* These will be merged to create the 'processedOutput' object.
*
* @param userId The unique ID of the user.
* @param sessionId The unique ID for this session. Can be used together with
the userId
* to retrieve the sessionStorage object.
* @param endpoint The configuration object for the used Endpoint.
* @param response The express response object that can be used to send a
custom response back to the user.
*
* @returns An object that will be sent to the user, unchanged. It therefore
has to have the
* correct format according to the documentation of the specific Endpoint
channel.
*/
handleExecutionFinished: async ({ processedOutput, outputs, userId,
sessionId, endpoint, response }) => {
const sessionStorage = await getSessionStorage(userId, sessionId);
const url = HTTPS + sessionStorage.urlbase + '/' + endpoint.URLToken;
let cpaasResponse = 'default';
switch (sessionStorage.cpaasChannel) {
case 'call':
cpaasResponse = getCPaaSCallCmd(sessionStorage, url,
outputs);
break;
case 'sms':
cpaasResponse = getCPaaSSmsCmd(sessionStorage, url,
sessionId, outputs);
break;
default:
break;
}
response.send(cpaasResponse);
if (DEBUG_MODE) {
console.log('CPaaS cmd='.concat(cpaasResponse));
}
return processedOutput;
}
});