Creating custom actions

Create Rasa 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.

Since Botfront uses Rasa, custom actions in Botfront 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 slot
  • tracker.latest_message: retrieve the latest message sent by a user. The latest_message contains the whole payload, including the intent and entities.
  • dispatcher.utter_message(template="utter_whatever"): utter a bot response

Using Custom actions

With Botfront Open Source, creating custom actions is super easy. As explained below, you can spin up an action server in no time with the CLI and your code will be hot reloading so you can see the impact of your changes live.

When initializing a project with botfront init an actions folder is created and the action server is automatically launched with botfront up. You can use botfront watch to rebuild and restart your actions server as above.

Deployment

You can build your action server Docker image with the Dockerfile.production in the actions folder of your project.