In this post, let us see how we can use the unirest
package to create a freshdesk ticket with attachments from our serverless app. We will be making use of
unirest
specifically because it offers a simple interface & can attach files located remotely (remote file stream). However, feel free to use a different package if it serves the purpose better. Ideally, these files would reside in a file storage service that our app can securely access.
Note that this snippet should not be consumed as such and should ideally be refactored depending on the use case. Developers should test the application for varying scenarios - extensions, file size, no of attachments, file storage service latency, and ensure that the approach works well.
Steps:
- Use
fdk create
to create a simple serverless boilerplate app. - Proceed to add the code to
server.js
and modifymanifest.json
to include the dependencies - Modify
url
andapiKey
accordingly. Ensure to convert them to iparams while using them productively
server.js
var unirest = require('unirest');
exports = {
onAppInstallHandler: function (args) {
/* Substitute with the right handler and event. This is just to demo */
logger('--Inside App Install Handler--');
logger(args);
attFile();
renderData();
}
};
function attFile(){
logger("--Let's attach a file --");
var url = "https://xxx.freshdesk.com/api/v2/tickets";
var apiKey = "yyy";
var headers = {
"Content-Type": "multipart/form-data",
"Authorization": "Basic " + apiKey
};
var fields = {
"description": "support ticket....",
"subject": "support ticket...",
"email": "example@example.com",
"status":"2", // certain fields are required during creation
"priority":"1"
};
// Lets use of unirest to attach files from our secure file store/s3/...
unirest.post(url)
.headers(headers)
.field(fields)
.attach('attachments[]', 'https://i.imgur.com/lcB62xb.jpg', {'filename': 'IcB62xb.png', 'contentType': 'image/png'})
.attach('attachments[]', 'https://gist.githubusercontent.com/hemchander23/6708ad5595dd56ef31087d47d4f96a44/raw/89ab0df9685cac94bbb88e1ebfd3655bcade6c9d/DarkS1E4_Script.txt#', {'filename': 'DarkS1E4_Script.txt', 'contentType': 'text/plain'})
.attach('attachments[]', 'https://www.gstatic.com/covid19/mobility/2020-05-25_US_Mobility_Report_en.pdf', {'filename': '2020-05-25_US_Mobility_Report_en.pdf', 'contentType': 'application/pdf'})
.end(function(response){
logger(response.body)
logger("Response Status : " + response.status)
if(response.status == 201){
logger("Location Header : "+ response.headers['location'])
}
else{
logger("X-Request-Id :" + response.headers['x-request-id']);
}
});
}
function logger(dat){
console.log(dat);
}
manifest.json
{
"platform-version": "2.3",
"product": {
"freshdesk": {
"events": {
"onAppInstall": {
"handler": "onAppInstallHandler"
},
}
},
"dependencies":{
"unirest":"0.6.0"
},
"engines": {
"node": "18.12.1",
"fdk": "9.0.0"
},
}
will create a ticket like so:
It is very important to note that the file storage service you use must be secure and generate short-lived (signed preferably) links that are only valid for the ticket creation session. Upon creating the ticket, you will notice that the attachments have a link of their own.