Using Glassix NPM Package on functions

Overview

The Glassix NPM package contains all the public Glassix API requests.

By importing the Glassix package to your function, you can easily use our REST API in your code.

Add the Glassix NPM Package

  1. Go to Settings.
    1. Click on the symbol in the agent dashboard.

  2. Click on DevelopersFunctions. Click here
  3. Click Add New Function.
  4. Fill Function Name and select Category, Type, and Timeout → Click Create.
  5. Connect the function to a chatbot flow or a Webhook.

📘

Info:

Learn more about connecting your functions to a chatbot here or to a Webhook here.

  1. Switch to your function's Settings tab.
  2. Under the Import npm modules into your application section:
    1. In the Module text box, type glassix.
    2. Click Add
  3. Click Save.
  4. Define your clientOptions object

📘

Tip:

We recommend using environment variables for all theclientOptions parameters.

Examples

Tag a conversation based on message specific keywords

This is an example of a function for adding a tag when a message with specific keywords is sent. The function will be triggered when a new message is sent or received.

In this example, the keywords are "buy" or "purchase," and the tag added is "Sales".

🚧

Note:

In order to add a tag using a function the tag must be defined in the department settings. Click here to add tags to your department.

Steps:

  1. Create a Function with Type Event.
  2. Import the Glassix NPM according to the instructions above.
  3. Click on Environment variables and create the following environment variables for your Access Token:
    1. API_KEY
    2. API_SECRET
    3. API_USER
    4. WORKSPACE

📘

Info:

To use an environment variable in the code of your function, follow this format: *process.env.*YOUR_KEY

  1. Link our newly created function to the NEW_MESSAGE event. Read more about function events here or click here to go to your department's function mappings.
  2. Copy the following code and paste it in your function, replacing the example code which was added automatically when your new function was created:

📘

Info:

All Glassix functions will be wrapped in the required handler function for you. Any code you paste into a function will be placed inside the handler function automatically.

Using CTRL+A in the Glassix function editor will only select the editable portion of the function's code.

try {
  //Create a glassix client
  const clientOptions = {
    workspace: process.env.WORKSPACE,
    apiKey: process.env.API_KEY,
    apiSecret: process.env.API_SECRET,
    userName: process.env.API_USER,
    domain: process.env.DOMAIN
  };
  //Create the relevant tags
  const tags= ['Sales'];

  //Initiate glassix client
  const client= new glassix(clientOptions);

  //Use event from events array
  const change= changes[0];
  const isCorrectEvent = change._event === 'NEW_MESSAGE';

  // Checking if the event is 'New message' event and if the message contains any 'buy' or 'purchase'
  if (isCorrectEvent)
  {
    if (change.transaction.text.includes('buy') || change.transaction.text.includes('purchase'))
    {
      const ticketId = change.ticketId;
      const result = await client.tickets.addTags(ticketId, tags);
    }
  }
} catch (error) {
  console.error(error);
}
  1. Let's test our function!

Set dynamic field values for new tickets based on tags

This is an example of a function for filling in ticket dynamic fields based on a ticket's tags. The function will be triggered when a new message is sent or received.

In this example, we are assuming the tags have been added to the incoming ticket automatically, either via channel tags, contact tags or events. If you want to set fields when a tag is added to an existing ticket, just map your function to the TICKET_TAGS_CHANGE event instead.

This example looks for the tag values "Test 1", "Test 2" and so on, and sets values in fields 3, 5, 6 and 7. For your function, you should choose from the tags you have available in your department (or create new ones), and choose the fields you want to set when the function runs. Click here to view and add tags to your department.

🚧

Note:

In order to set a dynamic field value using a function, the field should be enabled in the department settings. Click here to view and enable dynamic fields for your department.

Steps:

  1. Create a Function with Type Event.
  2. Import the Glassix NPM according to the instructions above.
  3. Click on Environment variables and create the following environment variables for your Access Token:
    1. API_KEY
    2. API_SECRET
    3. API_USER
    4. WORKSPACE
  4. Link our newly created function to the "New Message" event. Read more about function events here or click here to go to your department's function mappings.
  5. Copy the following code and paste it in your function, replacing the example code which was added automatically when your new function was created:
try {
  // Create a Glassix client
  const clientOptions = {
    workspace: process.env.WORKSPACE,
    apiKey: process.env.API_KEY,
    apiSecret: process.env.API_SECRET,
    userName: process.env.API_USER,
    domain: process.env.DOMAIN
  };

  // Initiate the Glassix client
  const client= new glassix(clientOptions);

  // Get the ticket details from the events array
  // https://docs.glassix.com/reference/events-overview#/
  const change = changes[0];
  const ticketTags = change.ticket.tags;

  // Create a new object to hold key/value pairs
  const tagsToSet = {};

  // Set corresponding field values for the tags we want
  for (let tag of ticketTags) {
    if (tag === "Test 1") {
      tagsToSet["field3"] = "Example value 1";
    }
    if (tag === "Test 2") {
      tagsToSet["field5"] = "Example value 2";
    }
    if (tag === "Test 3") {
      tagsToSet["field6"] = "Example value 3";
    }
    if (tag === "Test 4") {
      tagsToSet["field7"] = "Example value 4";
    }
  }

  // Update the ticket
  if (Object.keys(tagsToSet).length > 0) {
    let payload = {
      field3: tagsToSet["field3"],
      field5: tagsToSet["field5"],
      field6: tagsToSet["field6"],
      field7: tagsToSet["field7"]
    }
    const ticketId = change["ticketId"];
    const result = await client.tickets.setFields(ticketId, payload);
  }

} catch (error) {
  console.error(error);
}
  1. Let's test our function!