Freshdesk: Create Ticket

Template
Function
1
Template
Function
by
Voiceflow Community

A function to create tickets in Freshdesk

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Voiceflow Community
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

Function Code Snippet


export default async function main(args) {
let { apiKey, domain, description, subject, email, priority, status } = args.inputVars; // Input variables

// Check if the user has inputted the required variables
if (!apiKey || !domain || !description || !subject || !email || !priority || !status) {
return {
// Returns the error path so we can continue the design
next: { path: 'error' },
// Renders a debug message in Voiceflow
trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }]
};
}
function base64Encode(str) {
const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let output = '';

for (let i = 0; i < str.length; i += 3) {
const byte1 = str.charCodeAt(i) & 0xFF;
const byte2 = i + 1 < str.length ? str.charCodeAt(i + 1) & 0xFF : 0;
const byte3 = i + 2 < str.length ? str.charCodeAt(i + 2) & 0xFF : 0;

const enc1 = byte1 >> 2;
const enc2 = ((byte1 & 0x3) << 4) | (byte2 >> 4);
const enc3 = ((byte2 & 0xF) << 2) | (byte3 >> 6);
const enc4 = byte3 & 0x3F;

if (isNaN(byte2)) {
output += charSet.charAt(enc1) + charSet.charAt(enc2) + '==';
} else if (isNaN(byte3)) {
output += charSet.charAt(enc1) + charSet.charAt(enc2) + charSet.charAt(enc3) + '=';
} else {
output += charSet.charAt(enc1) + charSet.charAt(enc2) + charSet.charAt(enc3) + charSet.charAt(enc4);
}
}

return output;
}

// Base URL for creating a ticket in Freshdesk
const url = `https://${domain}.freshdesk.com/api/v2/tickets`;
// const url = `https://en2paqy0d1v6u.x.pipedream.net/`;

// Create the data payload
const payload = {
description: description,
subject: subject,
email: email,
priority: parseInt(priority),
status: parseInt(status),
};

// Setup the request options, including headers
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Basic ${base64Encode(apiKey+':X')}`
},
body: JSON.stringify(payload)
};

// This is where we made the fetch request, we use try-catch for error handling
try {
// Make the fetch request
const response = await fetch(url, config);

// Parse the response
const responseBody = await response.json;
// const responseBody = await response.text;
// Check if the response status is OK (status in the range 200-299)
if (!response.ok) {
// If not OK, throw an error to be caught by the catch block
throw new Error(`HTTP error! status: ${response.status}, Message: ${JSON.stringify(responseBody)}`);
}

return {
// Map the success path so we can continue in our flow
next: { path: 'success' },
// Optionally, return the response body if needed
outputVars: {
id: responseBody.id
}
};
}
// Catches all the errors we threw and displays the debug message
catch (error) {
return {
// Maps the error path so we can continue in our design
next: { path: 'error' },
// Renders a debug message in Voiceflow with the error
trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }]
};
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.