@cognigy/rest-api-client
Version:
Cognigy REST-Client
170 lines • 6.72 kB
JavaScript
import { __awaiter } from "tslib";
/* Custom modules */
import { createNodeDescriptor } from "../../createNodeDescriptor";
import { BaseContext } from "../../../helper/BaseContext";
import { getXAppsOverlaySettingsFields, getXAppsOverlaySettingsSection, } from "./utils/getXAppsOverlaySettings";
/**
* Node name: 'setAdaptiveCardAppState'
*
* Purpose:
* Set the state of an AdaptiveCard App
*/
export const SET_ADAPTIVE_CARD_APP_STATE = createNodeDescriptor({
type: "setAdaptiveCardAppState",
defaultLabel: "xApp: Show Adaptive Card",
summary: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__SUMMARY",
appearance: {
showIcon: false,
},
fields: [
{
key: "card",
type: "json",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__CARD__LABEL",
description: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__CARD__DESCRIPTION",
defaultValue: {},
},
{
key: "primaryColor",
type: "cognigyText",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__PRIMARY_COLOR__LABEL",
description: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__PRIMARY_COLOR__DESCRIPTION",
defaultValue: "",
},
{
key: "primaryContrastColor",
type: "cognigyText",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__PRIMARY_CONTRAST_COLOR__LABEL",
description: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__PRIMARY_CONTRAST_COLOR__DESCRIPTION",
defaultValue: "",
},
{
key: "waitForInput",
type: "toggle",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__WAIT_FOR_INPUT__LABEL",
defaultValue: false,
},
{
key: "storeResultInContext",
type: "toggle",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__STORE_RESULT_IN_CONTEXT__LABEL",
condition: {
key: "waitForInput",
value: true,
},
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__CONTEXT_KEY__LABEL",
defaultValue: "result",
condition: {
and: [
{
key: "waitForInput",
value: true,
},
{
key: "storeResultInContext",
value: true,
},
],
},
},
...getXAppsOverlaySettingsFields(),
],
sections: [
{
key: "overrideSection",
defaultCollapsed: true,
fields: ["primaryColor", "primaryContrastColor"],
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__OVERRIDE_SECTIONS__LABEL",
},
{
key: "waitingBehavior",
defaultCollapsed: true,
fields: ["waitForInput", "storeResultInContext", "contextKey"],
label: "UI__NODE_EDITOR__SET_ADAPTIVE_CARD_APP_STATE__WAITING_BEHAVIOR__LABEL",
},
getXAppsOverlaySettingsSection(),
],
form: [
{ type: "field", key: "card" },
{ type: "section", key: "overrideSection" },
{ type: "section", key: "waitingBehavior" },
{ type: "section", key: getXAppsOverlaySettingsSection().key },
],
tags: ["basic", "apps", "xApps"],
function: ({ cognigy, config, nodeId }) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const { api, input, context } = cognigy;
const { card, primaryColor, primaryContrastColor, waitForInput, storeResultInContext, contextKey, } = config;
const overlaySettings = {
autoOpen: config.autoOpen,
closeOnSubmit: config.closeOnSubmit,
feedbackMessage: config.feedbackMessage,
screenTitle: config.screenTitle,
sendEventOnCloseIconClick: config.sendEventOnCloseIconClick,
showCloseIcon: config.showCloseIcon,
};
const systemContextWaitingKey = `adaptiveCardAPPWaiting-${nodeId}`;
const appTemplateURL = `${input.apps.baseUrl}/apps/adaptivecards/`;
// only set the app state once
if (!api.getSystemContext(systemContextWaitingKey)) {
api.setAppState(appTemplateURL, {
card,
primaryColor: primaryColor || undefined,
primaryContrastColor: primaryContrastColor || undefined
}, {
webchat3: {
overlaySettingsMetaData: {
overlaySettings,
endpointType: input.endpointType,
url: input.apps.url,
URLToken: input.URLToken
}
}
});
api.logDebugMessage(`UI__DEBUG_MODE__SET_ADAPTIVE_CARD_APP_STATE__MESSAGE`);
}
if (api.getSystemContext(systemContextWaitingKey) &&
contextKey &&
((_b = (_a = input.data) === null || _a === void 0 ? void 0 : _a._cognigy) === null || _b === void 0 ? void 0 : _b._app)) {
api.deleteSystemContext(systemContextWaitingKey);
setxAppResult({
input,
storeResultInContext,
contextKey,
context,
});
return;
}
if (waitForInput) {
api.setSystemContext(systemContextWaitingKey, true);
api.setNextNode(nodeId);
api.stopExecution();
}
}),
});
/**
* Sets the result of an xApp in the context
*/
export const setxAppResult = (params) => {
var _a, _b, _c;
const { input, storeResultInContext, contextKey, context } = params;
const result = (_c = (_b = (_a = input === null || input === void 0 ? void 0 : input.data) === null || _a === void 0 ? void 0 : _a._cognigy) === null || _b === void 0 ? void 0 : _b._app) === null || _c === void 0 ? void 0 : _c.payload;
if (storeResultInContext) {
// check whether contextKey or inputKey is a deep query and set
// storage location accordingly
let location = context;
let target = contextKey;
// deep query
if (target.indexOf(".") > -1) {
const splits = target.split(".");
location = BaseContext.findLocationPathStateless(splits, true, location);
target = splits[splits.length - 1];
}
location[target] = result;
}
};
//# sourceMappingURL=setAdaptiveCardAppState.js.map