Code Editor
Learn how to steer the conversation by writing JavaScript code with the Code Action.
Last updated
Learn how to steer the conversation by writing JavaScript code with the Code Action.
Last updated
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.
To get started with the Code Action, create a new 'Action Dialogstate' and select the Code plugin as an action.
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.
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.
If you don't call the send()
function anywhere in your Code Action, users will not see any of your results after the code has executed.
Another way to accomplish the same result:
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!
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.
Route the conversation to the given dialogstate ID.
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.
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.
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.
With bot messages we can also add buttons, but with code there is more variety than the bot message.
Here, the 'invoices' are the different button options displayed. With the .addButtonGroup
you can add these buttons or create URL buttons.
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.
The addMediaMessage
method supports sending images, audio & video files through code actions.
The following functionality exists inside the Code action's scope:
Lodash is a JavaScript utility library. You can find more info here.
Fetch allows you to perform API calls. We use node-fetch as the default fetch library.
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.
We recommend using the delay block to create a delay between bot dialogs. You can learn more about how that works here: