Hello. I have a problem creating the first freshdesk application. Specifically, server.js and iparams.json collaboration:
iparams.json:
{
“domain”: {
“display_name”: “Freshdesk Domain”,
“description”: “Please enter your Freshdesk domain”,
“type”: “text”,
“required”: true
}
}
Server.js:
const axios = require(‘axios’);
const { Base64 } = require(‘js-base64’);
const axiosInstance = axios.create({
headers: {
‘Content-Type’: ‘application/json’
}
});
axiosInstance.interceptors.request.use(request => {
const apiKey = ‘XXXXXXXXXXXXXX’;
if (!request.headers[‘Authorization’]) {
const encodedApiKey = Base64.encode(apiKey + ‘:’);
request.headers[‘Authorization’] = Basic ${encodedApiKey}
;
}
return request;
}, error => {
// Handle request error here
return Promise.reject(error);
});
exports = {
events: [
{
event: ‘onTicketCreate’,
callback: ‘onTicketCreateHandler’
}
],
onTicketCreateHandler: async function(args) {
const client = args.client;
const iparams = await client.iparams.get();
const freshdeskDomain = iparams.domain;
const ticketId = args.data.ticket.id;
const ticketDescription = “Hello world”;
const noteData = {
body: ticketDescription,
private: false
};
axiosInstance.post(`${freshdeskDomain}/api/v2/tickets/${ticketId}/notes`, noteData)
.then() // Handle success
.catch(() => {
// Handle error
});
}
};
The server.js file is in the server folder
The iparams.json file is in the config folder
The structure is how it was generated by the default call to fdk create
The original code where I did not use iparams.json and called the URL and API code directly in server.js created the note, but as soon as I move the domain call to iparams.json the code does not work.
Does anyone have a sample code that reads the ticket and inserts a note and at the same time can load the URL and API code from iparams.json?
Thank you for your help.
Pavel