Back to all functions

Intercom: Create Ticket

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 By
Voiceflow Community
Community
download-icon
INPUT VARIABLES
{
contactEmail
}
The email of the contact in Intercom
{
ticketTypeId
}
The ID of the ticket type in Intercom
{
ticketTitle
}
The ticket title
{
intercomApiToken
}
The secret token from the Intercom Developer Hub
{
ticketDescription
}
The ticket description
{
intercomApiVersion
}
The version of the API endpoint (current: 2.11)
share-icon
OUTPUT VARIABLES
{
ticketCreatedText
}
A message with the ticket ID
{
ticketId
}
The record ID of the new ticket
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
success
}
Success
{
error
}
Error
{
}
{
}
{
}
{
}

Function Walkthrough

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

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.