action file:
This files contains your custom actions which can be used to run
custom Python code.
See this guide on how to implement these action:
This is a simple example for a custom action which utters “Hello World!”
from typing import Any, Text, Dict, List,Union,Optional
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
class LeadFormFirstPart(FormAction):
"""Example of a custom form action"""
def name(self) -> Text:
"""Unique identifier of the form"""
return "lead_form_p1"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["requirement", "mockup"]
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
"""A dictionary to map required slots to
- an extracted entity
- intent: value pairs
- a whole message
or a list of them, where a first match will be picked"""
return {
"requirement": [
self.from_text(),
],
"mockup": [
self.from_text(),
],
}
def submit(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict]:
"""Define what the form has to do
after all required slots are filled"""
# utter submit template
dispatcher.utter_template("utter_urlAvailable", tracker)
return []
class LeadFormSecondPart(FormAction):
"""Example of a custom form action"""
def name(self) -> Text:
"""Unique identifier of the form"""
return "lead_form_p2"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["url"]
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
"""A dictionary to map required slots to
- an extracted entity
- intent: value pairs
- a whole message
or a list of them, where a first match will be picked"""
return {
"url": [
self.from_text(),
],
}
def submit(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict]:
"""Define what the form has to do
after all required slots are filled"""
return []
class LeadFormThirdPart(FormAction):
"""Example of a custom form action"""
def name(self) -> Text:
"""Unique identifier of the form"""
return "lead_form_p3"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["timeline", "budget", "name", "email", "phone"]
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
"""A dictionary to map required slots to
- an extracted entity
- intent: value pairs
- a whole message
or a list of them, where a first match will be picked"""
return {
"timeline": [
self.from_text(),
],
"budget": [
self.from_text(),
],
"name": [
self.from_text(),
],
"email": [
self.from_text(),
],
"phone": [
self.from_text(),
],
}
def submit(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict]:
"""Define what the form has to do
after all required slots are filled"""
# utter submit template
dispatcher.utter_template("utter_lead_q2", tracker)
return []