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.

Video


Code

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/actions.py 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]]:
dispatcher.utter_message(text=f"{dt.datetime.now()}")
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.

action_endpoint:
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

Links

Exercises

Try to answer the following questions to test your knowledge.

  1. When you just wrote a custom action in your actions/actions.py 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.