@@ -101,6 +101,67 @@ Details of the ``dispatcher.utter_message()`` method:
101
101
102
102
.. automethod :: rasa_sdk.executor.CollectingDispatcher.utter_message
103
103
104
+
105
+ .. _custom_session_start :
106
+
107
+ Customising the session start action
108
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109
+
110
+ The default behaviour of the session start action is to take all existing slots and to
111
+ carry them over into the next session. Let's say you do not want to carry over all
112
+ slots, but only a user's name and their phone number. To do that, you'd override the
113
+ ``action_session_start `` with a custom action that might look like this:
114
+
115
+ .. testcode ::
116
+
117
+ from typing import Text, List, Dict, Any
118
+
119
+ from rasa_sdk import Action, Tracker
120
+ from rasa_sdk.events import SlotSet, SessionStarted, ActionExecuted, EventType
121
+ from rasa_sdk.executor import CollectingDispatcher
122
+
123
+
124
+ class ActionSessionStart(Action):
125
+ def name(self) -> Text:
126
+ return "action_session_start"
127
+
128
+ @staticmethod
129
+ def fetch_slots(tracker: Tracker) -> List[EventType]:
130
+ """Collect slots that contain the user's name and phone number."""
131
+
132
+ slots = []
133
+
134
+ for key in ("name", "phone_number"):
135
+ value = tracker.get_slot(key)
136
+ if value is not None:
137
+ slots.append(SlotSet(key=key, value=value))
138
+
139
+ return slots
140
+
141
+ async def run(
142
+ self,
143
+ dispatcher: CollectingDispatcher,
144
+ tracker: Tracker,
145
+ domain: Dict[Text, Any],
146
+ ) -> List[EventType]:
147
+
148
+ # the session should begin with a `session_started ` event
149
+ events = [SessionStarted()]
150
+
151
+ # any slots that should be carried over should come after the
152
+ # `session_started ` event
153
+ events.extend(self.fetch_slots(tracker))
154
+
155
+ # an `action_listen ` should be added at the end as a user message follows
156
+ events.append(ActionExecuted("action_listen"))
157
+
158
+ return events
159
+
160
+ .. note ::
161
+
162
+ You need to explicitly add ``action_session_start `` to your domain to override this
163
+ custom action.
164
+
104
165
Events
105
166
------
106
167
0 commit comments