Skip to content

Function

Function resources allow you to write custom Javascript code that you can call from any chatbot to apply special processing on your variables.

These special functions can return either directly executable Prisme.ai workflows or raw data that would be displayed / processed later on.

1. Creation

When editing your function from your Prisme.ai dashboard, a wide code area will let you save your custom function.
The only parameter you receive is the following request object :

interface request {
  timestamp: string; // DateTime of the call
  sessionId: string; // Session identifier
  bot: {
    id: string; // Wizard identifier
  };
  user: { // Current user
    id: string; // User ID
    location: string; // Location of the user if authorized by the user
    platform: string; // User agent platform
    conversationSize: number; // Number of messages in the conversation
    conversationId: string; // ConversationId: string; // ConversationId identifier
  };
  userMessage: { // User message
    event: string; // If the user's message is an event, its name
    text: string; // If it's text, its content
    payload: {}; // If the message contains an arbitrary object, this one
  }
  originalRequest: {}; // Object of arbitrary parameters passed to the injection script
  lang: string; // User's browser language
  query: string; // Request sent by the user
  event: string; // Event sent by the user
  fulfillment: { // Responses generated by the workflow before arriving at the function
    stream: {
      type: string;
      resourceId: string;
    }[];
  intent: { // Intention corresponding to the user's request
    name: string;
    confidence: number; // Match index
    inputs: {}; // Value of intention parameters
  };
  contexts: { // Contexts
    lifespan: string; // Lifetime
    required: boolean; // If required
    name: string; // Name
    parameters: {}; // Associated parameters
    scope: string; // Perimeter
  }[];
  endConversation: boolean; // If the convesation is over
}

So, if the content of my function is :

return bot.id;

The function will return the id of the bot running the function.

It is possible to execute asynchronous code using the keywords async/await :

const res = await fetch("http://myservice.com");
const data = await res.json();
return data.value;

Some javascript libraries are available among which :

  • fetch : allows to make http requests
  • DOM: allows you to parse the DOM. Used with the DOM(document: string) function which returns a DOMElement.

To access other libraries, please contact us.

Function return

The function can either return raw data, simple text to display or a Prisme.ai workflow :

// Displays "Hello world"
return "Hello World";

// Returns raw data to display / process later on  
return [{"title": "Hello World"}];
// Return 1 text response followed by one button
return [
  {
    type: "text",
    text: "Hello World",
  },
  {
    type: "button",
    text: "Click me",
  },
];

More information on workflows here.

2. Calling your function

Either from DSUL or from the graph editor, you can call your recently created function with its only available action : function

If your function returns raw data instead of a standard Prisme.ai workflow, you first need to save it in a variable using output option, before reading it from next responses.