Creating Custom Actions
Custom actions are used for all actions that cannot be handled in the visual story editor and require custom code. You can use them for API calls, calculations, etc.
Example actions are shipped with Clai that demonstrate how to write actions to issue xog and rest api calls against a Clarity instance, process the results and format the data responses finally returning the response to the webchat window.
Since Clai uses Rasa, custom actions in Clai and Rasa are the same.
A custom action is a Python class:
class FetchStatus(Action):
def name(self):
return 'action_fetch_status'
def run(self, dispatcher, tracker, domain):
url = "https://some.api.com/user/xxx/status"
status = requests.get(url).json
return [SlotSet("status", status)]
The run
method performs an arbitrary action and returns an array of Events, generally SlotSet
events.
The action above assigns the fetched status to the slot status. The slot can then be used in the conversation.
The action can be invoked from the conversation builder:
In the run
method you can access the conversation state and utter responses. Those are the most commonly used methods, see the Rasa SDK documentation for more.
tracker.get_slot(slot_name)
: get the value of a slottracker.latest_message
: retrieve the latest message sent by a user. Thelatest_message
contains the whole payload, including the intent and entities.dispatcher.utter_message(template="utter_whatever")
: utter a bot response