Run
An action can do one of the following things:
- Execute a function on the server (most common)
- If block is followed by a streamable variable, stream the variable on the client. Otherwise, execute a function on the server.
- Execute a function on the client
- Display a custom embed bubble
Server function
The most common action is to execute a function on the server. This is done by simply declaring that function in the action block.
Example:
As you can see the function takes credentials
, options
, variables
and logs
as arguments.
The credentials
are the credentials that the user has entered in the credentials block.
The options
are the options that the user has entered in the options block.
The variables
object contains helper to save and get variables if necessary.
The logs
allows you to log anything during the function execution. These logs will be displayed as toast in the preview mode or in the Results tab on production.
Server function + stream
If your block can stream a message (like OpenAI), you can implement a stream object to handle it.
The getStreamVariableId
function defines which variable should be streamed.
The run
works the same as the server function. It needs to return Promise<ReadableStream<any> | undefined>
.
Client function
If you want to execute a function on the client instead of the server, you can use the client
object.
This makes your block only compatible with the Web runtime. It won’t work on WhatsApp for example, the block will simply be skipped.
Example:
The web function needs to return an object with args
and content
.
args
is an object with arguments that are passed to the content
context. Note that the arguments can’t be undefined
. If you want to pass an not defined argument, you need to pass null
instead.
content
is the code that will be executed on the client. It can call the arguments passed in args
.
Display embed bubble
If you want to display a custom embed bubble, you can use the displayEmbedBubble
object. See Cal.com
block
as an example.
Example:
The displayEmbedBubble
accepts a parseInitFunction
function. This function needs to return the same object as the web
function. The function content can use the typebotElement
variable to get the DOM element where the block is rendered.
Optionally you can also define a waitForEvent
object. This object accepts a getSaveVariableId
function that returns the variable id where the event data should be saved. It also accepts a parseFunction
function that returns the same object as the web
function. The function content can use the continueFlow
function to continue the flow with the event data.