Custom Actions

In this lesson we will show you how to make a basic custom action in Rasa. These custom actions are going to be important for us later on when we will be creating our own forms.



In this video we're demonstrating how to write a custom action in Rasa. In the video we show a custom action that will fetch the current time. Here's the code from the actions/ file.

import datetime as dt
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
class ActionHelloWorld(Action):
def name(self) -> Text:
return "action_show_time"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
return []

Note that it is very important to get the name right, you can see it highlighted in the codeblock. If this does not correspond with the domain.yml file then the action won't be found.

If you want to run these custom actions you'll need to make sure that your endpoints.yml file is properly configured.

url: "http://localhost:5055/webhook"

Assuming that you've also gotten your nlu/stories/domain file you're now all set to interact with your custom action. Just remember to run the action server first.

rasa run actions

With the action server running you should be able to interact with your custom action from the rasa shell.

rasa shell



Try to answer the following questions to test your knowledge.

  1. When you just wrote a custom action in your actions/ file, what are the next steps that you must do before you can use it in rasa shell?
  2. What does the tracker allow you to do in the run method of a custom action?
  3. What does the dispatcher allow you to do in the run method of a custom action?

2016-2024 © Rasa.