When a Rasa assistant calls a custom action, it sends a request to the action server. Rasa only knows about whatever events and responses come back in the request response; it's up to the action server to call the correct code based on the action name that Rasa provides.
To better understand what happens when Rasa calls a custom action, consider the following example:
You have deployed a weather bot to both Facebook and Slack. The user
can ask for the weather with the intent
ask_weather. There is a slot
which will be filled if the user has specified a location.
action_tell_weather will use an API to get the weather forecast ,
using a default location if the user doesn't specify one.
The action will set the
temperature to the maximum temperature of the weather forecast.
The message returned will differ according to the channel they are using.
Custom Action Input
Your action server receives the following payload from the Rasa server:
next_action field tells your action server what action to run.
Your actions don't have to be implemented as classes, but they do have
to be callable by name.
In the example case, your action server should run the action
sender_id tells you the unique ID of the
user having the conversation. Its format varies according to the input channel.
What it tells you about the user also depends on the input channel and how
the user is identified by the channel.
In the example case, the
sender_id is not used for anything.
tracker contains information about the conversation, including a history of events
and a record of all slots:
sender_id: The same
sender_idas is available in the top level of the payload
slots: Each slot in your bot's domain and its value at the current time
latest_message: The attributes of the latest message
latest_event_time: The timestamp at which the last event was added to the tracker
followup_action: The action called was a forced follow up action
paused: Whether the conversation is currently paused
events: A list of all previous events
latest_input_channel: The input channel from which the last user message was received
active_form: The name of the currently active form, if any
latest_action_name: The name of the last action the bot executed
In the example case, your custom action uses the value of the
location slot (if it is set)
to get the weather forecast. It also checks the
and formats the message payload so that it will display correctly in Facebook Messenger.
domain is a json representation of your
It is unlikely that a custom action
will refer to its contents, as they are static and do not indicate the state
of the conversation.
This is the version of the Rasa server. A custom action is also unlikely to refer to this, although you might use it in a verification step if your action server is only compatible with certain Rasa versions.
Custom Action Output
The Rasa server expects a dictionary of
responses as a response
to a custom action call.
Events are how your action server can influence the conversation.
In the example case, your custom action should store the maximum temperature
temperature slot, so it needs to return a
slot event. To set the
slot and do nothing else, your response payload would look like this:
Note that events will be applied to the tracker in the order you list them; with
events, the order won't matter, but with other event types it can.
In the example case, you want to send the user a message with the weather forecast. To send a regular text message, the response payload would look like this:
However, you want to make use of your channels' specific capabilities. Since
latest_input_channel was Facebook, you add a response with
a custom payload that will be rendered as a media message according to Facebook's API spec.
Your response payload then looks like this:
When this response is sent back to
the Rasa server, Rasa will apply the
slot event and two responses to the tracker,
and return both messages to the user.
Special Action Types
There are special action types that are automatically triggered under certain circumstances, namely default actions and slot validation actions. These special action types have predefined naming conventions that must be followed to maintain the automatic triggering behavior.
You can customize a default action by implementing a custom action with exactly the same name. Please see the docs on default actions for the expected behavior of each action.
Slot validation actions are run on every user turn, depending on whether a form is active or not.
A slot validation action that should run when a form is not active must be called
A slot validation action that should run when a form is active must be called
These actions are expected to return
SlotSet events only and to behave like the Rasa SDK
FormValidationAction class respectively.