Intercom: Create Ticket

Template
Function
1
Template
Function
by
Voiceflow Community

This function creates a new ticket in Intercom and assign it to existing contact with provided email, or creates new contact if it doesn't exist. The function returns the newly created ticket ID.

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) {
// Validate input variables from args - find the "validateInputs" function below
const invalidFieldTrace = validateInputs(args)

if (invalidFieldTrace) return invalidFieldTrace;

// Extract input variables from args (destructuring)
const {
contactEmail,
ticketTitle,
ticketDescription,
ticketTypeId,
intercomApiToken,
intercomApiVersion,
} = args.inputVars;

/**
* Build the payload for creating a new ticket in Intercom
*
* Read more:
* - https://developers.intercom.com/docs/references/rest-api/api.intercom.io/Tickets/ticket/
* - https://developers.intercom.com/docs/references/rest-api/api.intercom.io/Tickets/createTicket/
*/
const payload = {
"ticket_type_id": ticketTypeId,
"contacts": [{
"email": contactEmail,
}],
"ticket_attributes": {
"_default_title_": ticketTitle,
"_default_description_": ticketDescription,
// ...add more attributes
}
};

/**
* Configure the fetch request
*
* Get your Intercom API Token from the Intercom Developer Hub
* - https://app.intercom.com/
*/
const config = {
method: 'POST',
headers: {
'Authorization': `Bearer ${intercomApiToken}`,
'Content-Type': 'application/json',
'Intercom-Version': intercomApiVersion,
},
body: JSON.stringify(payload)
};

// Define the URL for creating ticket
const url = `https://api.intercom.io/tickets`;

try {
// Make the API call
const response = await fetch(url, config);

// Check if the response status is OK
if (!response.ok) {
throw new Error(`Intercom API Call Error: HTTP status code ${response.status}`);
}

// Extract the JSON body from the response
const responseBody = response.json;

// Validate the responseBody structure as expected
if (!responseBody || typeof responseBody !== 'object') {
throw new Error("Intercom API Call Error: Invalid or missing response body");
}

// Extract data from the response
const ticketId = responseBody.ticket_id;

// Create the success return object with extracted data
return {
outputVars: {
ticketId,
ticketCreatedText: `Your ticket was created, with ID #${ticketId}`,
},
next: {
path: 'success'
},
trace: [
{
type: "debug",
payload: {
message: 'Intercom API call successful'
}
},
{
type: "debug",
payload: {
message: `Ticket created - ID: ${ticketId}`
}
},
]
};
} catch (error) {
return {
next: {
path: 'error'
},
trace: [{
type: "debug",
payload: {
message: `Intercom API call error: ${error.message}`
}
}]
};
}

/**
* Here we validate all the inputs of the Voiceflow function
*
* The function can be used in the first lines of the code
* because of a Javascript concept called hoisting
*
* Read more: https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
*/
function validateInputs(args) {
// Extract input variables from args (destructuring)
const {
contactEmail,
ticketTitle,
ticketDescription,
ticketTypeId,
intercomApiToken,
intercomApiVersion,
} = args.inputVars;

// Validate that contactEmail variable is provided
if (!contactEmail) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: contactEmail" } }]
};
}

// Validate that ticketTitle variable is provided
if (!ticketTitle) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: ticketTitle" } }]
};
}

// Validate that ticketDescription variable is provided
if (!ticketDescription) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: ticketDescription" } }]
};
}

// Validate that ticketTypeId variable is provided
if (!ticketTypeId) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: ticketTypeId" } }]
};
}

// Validate that intercomApiToken variable is provided
if (!intercomApiToken) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: intercomApiToken" } }]
};
}

// Validate that intercomApiVersion variable is provided
if (!intercomApiVersion) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: intercomApiVersion" } }]
};
}

return null
}
}
copy-icon

Explore More Templates

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

ghraphic
No items found.
No items found.