-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathactions.py
124 lines (87 loc) · 3.35 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions
# This is a simple example for an assistant that schedules reminders and
# reacts to external events.
from typing import Any, Text, Dict, List
import datetime
from rasa_sdk import Action, Tracker
from rasa_sdk.events import ReminderScheduled, ReminderCancelled
from rasa_sdk.executor import CollectingDispatcher
class ActionSetReminder(Action):
"""Schedules a reminder, supplied with the last message's entities."""
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]
class ActionReactToReminder(Action):
"""Reminds the user to call someone."""
def name(self) -> Text:
return "action_react_to_reminder"
async def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
name = next(tracker.get_slot("PERSON"), "someone")
dispatcher.utter_message(f"Remember to call {name}!")
return []
class ActionTellID(Action):
"""Informs the user about the conversation ID."""
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 '{conversation_id}'.")
dispatcher.utter_message(
f"Trigger an intent with: \n"
f'curl -H "Content-Type: application/json" '
f'-X POST -d \'{{"name": "EXTERNAL_dry_plant", '
f'"entities": {{"plant": "Orchid"}}}}\' '
f'"http://localhost:5005/conversations/{conversation_id}'
f'/trigger_intent?output_channel=latest"'
)
return []
class ActionWarnDry(Action):
"""Informs the user that a plant needs water."""
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 []
class ForgetReminders(Action):
"""Cancels all reminders."""
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("Okay, I'll cancel all your reminders.")
# Cancel all reminders
return [ReminderCancelled()]