Hello. I am trying to create an app that automatically updates the summary of a ticket when a private note is added in certain format.
I do not know why, but it is crashing.
Find the code below:
exports = {
events: [
{ event: 'onConversationCreate', callback: 'onConversationCreate' }
],
// args is a JSON block containing the payload information.
// args['iparam'] will contain the installation parameter values.
onConversationCreate: function(args) {
//Only apply that for notes
console.log("New conversation. Source: "+ args['data']['conversation']['source']);
if (args['data']['conversation']['source']===2){
var data = args['data']['conversation']['body_text'];
if (data.includes("[Basic summary]:")){
console.log("Includes [Basic summary]: tag");
var TicketID = args['data']['conversation']['ticket_id'];
var i = data.indexOf("[Basic summary]:");
var j = data.indexOf("[Next Steps]:");
var payload = data.substring(i,j);
//var EncodedAuthentication= btoa(client.iparams.get(api_key)+":X");
var EncodedAuthentication= "ThatIsTheKeyEncoded";
//Get previous Summary
var options = {
'method': 'GET',
'url': 'https://COMPANY.freshdesk.com/api/v2/tickets/' + TicketID + '/summary',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Basic '+EncodedAuthentication
}
};
console.log(options);
console.log("PAPA1");
$request(options, function (error, response) {
console.log("PAPA2");
if (error) throw new Error(error);
console.log(response.body);
var getReply = JSON.parse(response.body);
console.log(getReply);
var summary = getReply['body_text'];
summary.concat("<br><br>");
summary.concat(payload);
//Update the Summary
var options = {
'method': 'PUT',
'url': 'https://COMPANY.freshdesk.com/api/v2/tickets/'+ TicketID +'/summary',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Basic '+EncodedAuthentication
},
body: JSON.stringify({
"body": summary
})
};
$request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
});
}
}
}
};
It never reaches the PAPA2 log, crashing before:
I have the same kind of GET function on another app and it is working.
I have tested to twist a little bit the request and removing the METHOD on the options, but switching the $request to: $request.get(…) No crash is noticed then but still not processing the next console.log, it simplies does not goes on on the script.
Does anybody knows what might be going on?
Thanks in advance.