Reminders and External Events
Reminders and External Events
The ReminderScheduled event and the trigger_intent endpoint let your assistant remind you about things after a given period of time, or to respond to external events (other applications, sensors, etc.). You can find a full example assistant that implements these features here.
Reminders
Instead of an external sensor, you might just want to be reminded about something after a certain amount of time. For this, Rasa provides the special event ReminderScheduled, and another event, ReminderCancelled, to unschedule a reminder.
Scheduling Reminders
Let’s say you want your assistant to remind you to call a friend in 5 seconds. Thus, we define an intent ask_remind_call with some NLU data,
## intent:ask_remind_call
- remind me to call [Albert](name)
- remind me to call [Susan](name)
- later I have to call [Daksh](name)
- later I have to call [Anna](name)
The custom action action_set_reminder should schedule a reminder that, 5 seconds later, triggers an intent EXTERNAL_reminder with all the entities that the user provided in his/her last message. Here is how this can be implemented:
class ActionSetReminder(Action):
def name(self) -> Text:
return "action_set_reminder"
async def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
dispatcher.utter_message("I will remind you in 5 seconds.")
date = datetime.datetime.now() + datetime.timedelta(seconds=5)
entities = tracker.latest_message.get("entities")
reminder = ReminderScheduled(
"EXTERNAL_reminder",
trigger_date_time=date,
entities=entities,
name="my_reminder",
kill_on_user_message=False,
)
return [reminder]
Cancelling Reminders
Sometimes the user may want to cancel a reminder that he has scheduled earlier. This can be accomplished using a custom action such as:
class ForgetReminders(Action):
def name(self) -> Text:
return "action_forget_reminders"
async def run(self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
dispatcher.utter_message(f"Okay, I'll cancel all your reminders.")
return [ReminderCancelled()]
External Events
External events can change the course of an ongoing conversation. For instance, a moisture-sensor attached to a Raspberry Pi can inform your assistant that your favorite plant needs watering, and your assistant should then relay this message to you.
Getting the Conversation ID
To retrieve the Session ID of the conversation, a custom action can be defined that displays the ID in the conversation:
class ActionTellID(Action):
def name(self) -> Text:
return "action_tell_id"
async def run(self,
dispatcher,
tracker: Tracker,
domain: Dict[Text, Any]
) -> List[Dict[Text, Any]]:
conversation_id = tracker.sender_id
dispatcher.utter_message(
f"The ID of this conversation is: " f"{conversation_id}."
)
return []
Responding to External Events
To allow your assistant to respond to messages from the sensor, you will define a new intent such as EXTERNAL_dry_plant that the external sensor can trigger:
class ActionWarnDry(Action):
def name(self) -> Text:
return "action_warn_dry"
async def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]
) -> List[Dict[Text, Any]]:
plant = next(tracker.get_latest_entity_values("plant"), "someone")
dispatcher.utter_message(f"Your {plant} needs some water!")
return []
Now when the sensor triggers the intent, the assistant can respond accordingly.