Here is my requests.json file
{
"getchat": {
"schema": {
"method": "POST",
"host": "api.openai.com",
"path": "/v1/chat/completions",
"headers": {
"Authorization": "Bearer <my open ai key in plain text>",
"Content-Type": "application/json"
}
}
},
"convos": {
"schema": {
"method": "GET",
"host": "karankalsi.freshdesk.com",
"path": "/api/v2/tickets/<%= context.ticket_id %>/conversations",
"headers": {
"Authorization": "Basic <%= encode(my freshdesk key in plain text) %>",
"Content-Type": "application/json"
}
}
}
}
and here is my app.js file
var client;
init();
async function init() {
client = await app.initialized();
client.events.on('app.activated', renderText);
}
function jsonEscape(str) {
return str.replace(/\n/g, " ").replace(/\\/g," ")
}
async function renderText() {
const textElement = document.getElementById('apptext');
const ticketData = await client.data.get('ticket');
const {
ticket: { description_text }
} = ticketData;
try {
const ticketData1 = await client.data.get('ticket');
const {
ticket: { id }
} = ticketData1;
textElement.innerHTML = id;
let resp2 = await client.request.invokeTemplate("convos", {
"context": {
"ticked_id": id
}
});
let convo = " ";
resp2.forEach(function(json) {
if(data.incoming == false){
convo += "Company: " +json.body_text + "\n\n";}
else{
convo += "Customer: " +json.body_text + "\n\n";
}
});
let resp = await client.request.invokeTemplate("getchat", {
"context": {},
"body": JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": [{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Provide Summary:\n"+ "Customer: " + description_text + "\n\n"+ convo}
]
})
});
var rep_json = JSON.parse(jsonEscape(resp.response));
var rep2 = rep_json.choices[0].message.content;
textElement.innerHTML = rep2;
}
catch(err){
textElement.innerHTML = "Error request fail: " + JSON.stringify(err);
}
}
and the output I get is Status 400 - Error While Substituting the templates