forked from Chinese-NLP-book/rasa_chinese_book_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
67 lines (55 loc) · 2.17 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
from typing import Any, Text, Dict, List
from rasa_sdk import Tracker, Action
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
from rasa.core.actions.forms import FormAction
from rasa_sdk.forms import REQUESTED_SLOT
class TicketFormAction(FormAction):
def name(self) -> Text:
return "ticket_form"
def required_slots(self, tracker: Tracker) -> List[Text]:
return ["city_depart", "city_arrive", "date"]
def extract_other_slots(
self,
dispatcher: "CollectingDispatcher",
tracker: "Tracker",
domain: Dict[Text, Any],
) -> Dict[Text, Any]:
slot_to_fill = tracker.get_slot(REQUESTED_SLOT)
if not slot_to_fill:
return super().extract_other_slots(dispatcher, tracker, domain)
else:
return {}
def slot_mappings(self):
return {
"city_depart": [
self.from_entity(entity="city", role="departure"),
self.from_entity(entity="city", intent="info_city"),
],
"city_arrive": [
self.from_entity(entity="city", role="destination"),
self.from_entity(entity="city", intent="info_city"),
],
}
def submit(
self, dispatch: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]
) -> List[Dict]:
# don't using template alone,
# since the system tracker is not updated yet when render the template,
# using current tracker instead
dispatch.utter_message(template="utter_ask_confirm", **tracker.slots)
return []
class ActionBuyTicket(Action):
def name(self) -> Text:
return "action_buy_ticket"
def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
city_arrive = tracker.get_slot("city_arrive")
city_depart = tracker.get_slot("city_depart")
date = tracker.get_slot("date")
dispatcher.utter_message(f"BOT 动作:购买「{date}」从「{city_depart}」出发到「{city_arrive}」的车票")
return []