72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
/*
|
|
Proxy Script for CoPilot integration with IVAS
|
|
*/
|
|
console.log({ script: "copilot", body: req.body });
|
|
|
|
let copilot_settings = copilot_settings;
|
|
|
|
if (req.headers.authorization) {
|
|
try {
|
|
console.log("decoding authorization ...");
|
|
let bearer = req.headers.authorization.replace("Bearer ", "");
|
|
|
|
req.body.event.metadata = {
|
|
...req.body.event.metadata,
|
|
...{
|
|
bearer: jwt.decode(bearer),
|
|
},
|
|
};
|
|
|
|
console.log("applying copilot overrides ...");
|
|
const found = copilot_overrides.find(
|
|
(element) =>
|
|
element.v_username === req.body.event.metadata.bearer.v_username
|
|
);
|
|
if (found) {
|
|
console.log("found copilot overrides for user: " + found.v_username);
|
|
copilot_settings = {
|
|
...copilot_settings,
|
|
...found.copilot_settings,
|
|
};
|
|
req.params = { ...req.params, ...found.req.params };
|
|
req.body.event.params = {
|
|
...req.body.event.params,
|
|
...found.event.params,
|
|
};
|
|
} else {
|
|
console.log(
|
|
"no copilot overrides found for user: " +
|
|
req.body.event.metadata.bearer.v_username
|
|
);
|
|
}
|
|
} catch (e) {
|
|
console.log("Error decoding authorization:");
|
|
console.log(e);
|
|
}
|
|
}
|
|
|
|
req.body.event.metadata = _.merge(req.body.event.metadata, {
|
|
channel: copilot_settings.channel,
|
|
});
|
|
|
|
const messengerUrl = `${copilot_settings.nlu.apiBaseURL}ProxyScript/run/${req.params.workspaceId}/${req.params.branch}/${copilot_settings.messengerRouteName}`;
|
|
(async () => {
|
|
try {
|
|
const { data: recognizedData } = await axios({
|
|
url: messengerUrl,
|
|
method: "post",
|
|
data: req.body,
|
|
// headers: {
|
|
// Authorization: `Bearer ${copilot_settings.ops.token}`
|
|
// }
|
|
});
|
|
res.send(recognizedData);
|
|
} catch (error) {
|
|
console.log(error.message);
|
|
res.send({
|
|
answers: ["Something went wrong. Please try again."],
|
|
outputs: {},
|
|
});
|
|
}
|
|
})();
|