Code Editor

Learn how to steer the conversation by writing JavaScript code with the Code Action.

The code action allows developers to quickly build custom logic on top of their bot by writing their own Javascript code blocks. Typically, the code editor is used to perform requests to external systems, or to do operations with variables.

Getting Started

To get started with the Code Action, create a new 'Action Dialogstate' and select the Code plugin as an action.

Arguments in the Code Editor

You can pass arguments to your Code actions by assigning them keys. Your keys will be made available to the args variable inside the Code Editor.

ChatlayerResponseBuilder function

The ChatlayerResponseBuilder function returns a helper instance that allows you to steer your conversation by sending messages as a bot, navigating to bot dialogs, or even creating session data.

To start manipulating conversation data in your code action, simply call the function ChatlayerResponseBuilder() which will return a ChatlayerResponseBuilder instance.

The ChatlayerResponseBuilder has a fluent interface, this means that every function you call will return the same instance. This makes it easy to chain multiple function calls when, for example, you want to show a message and manipulate session data at the same time.

Whenever you want to publish your changes to the conversation, you must call the send() function.

Another way to accomplish the same result:

Set variables or send messages

The ChatlayerResponseBuilder has the ability to set variables or adding messages to the chatbot. Both are shown in the example below:

addSessionVariable(namespace: string, data: any): Insert a variable on a certain namespace within the session.

addMessage(message: string): Adds a text message to be sent by the bot.

To enhance your variables even more, you can store multiple variables about the user in an object.

If you would like to use this information in a bot message, simply type {user.lastName} and the information is visible in the chatbot!

Next or previous dialogs

Based on code, variables or other input, you can steer the conversation to other dialogstates. With the code below you can go to a next dialogstate.

setNextDialogState(dialogstateId: string)

Route the conversation to the given dialogstate ID.

Bot Message functionalities

In the code editor, some 'bot message' functionalities are also available, such as quick replies or buttons. For more in depth functionality, these same options can be created using code.

addCarousel

In the Code Editor it is also possible to add a carousel, just like in Bot messages. This can be of added value when dynamic content needs to be shown or to combine a Bot message and Action dialog in one.

addQuickReplies

Just like the example above, Quick Replies can also be created in the code editor. By copying the code below you can add as many Quick reply buttons as needed.

addButtonGroup

With bot messages we can also add buttons, but with code there is more variety than the bot message.

These buttons are created with the code below

Here, the 'invoices' are the different button options displayed. With the .addButtonGroup you can add these buttons or create URL buttons.

Add HTML or Iframe

HTML can de added in the chatbot to show more diverse output to the user.

Iframes can be used to embed other pages in the chatbot. A perfect example of why you would need an iframe is embedding Youtube videos.

Add media

The addMediaMessage method supports sending images, audio & video files through code actions.

Utility library and API calls

The following functionality exists inside the Code action's scope:

lodash (_)

Lodash is a JavaScript utility library. You can find more info here.

fetch

Fetch allows you to perform API calls. We use node-fetch as the default fetch library.

Creating a slight delay between bot dialogs

We recommend using the delay block to create a delay between bot dialogs. You can learn more about how that works here: Delay

To create a delay in between the integer seconds, like 1500 miliseconds or 1,5 seconds, add a Code widget to your action instead of the Delay pictured above and paste the following code:

You can adjust the length of the delay by replacing the 1500 with 2500 (2,5 seconds) etc.

Last updated

Was this helpful?