-
Notifications
You must be signed in to change notification settings - Fork 49
/
actions.py
103 lines (82 loc) · 3.15 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
# 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/core/actions/#custom-actions/
# This is a simple example for a custom action which utters "Hello World!"
from typing import Any, Text, Dict, List, Union
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
details = {
'ashish': '+91 8209829808',
'innovate': '+91 9413995563'
}
class ActionHelloWorld(Action):
def name(self) -> Text:
return "action_your_num"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# dispatcher.utter_message(text="Hello World!")
print(tracker.get_slot('num'))
# dispatcher.utter_template('utter_your_num', tracker, number=details[str(tracker.get_slot('NAME')).lower()])
return []
class ActionFormInfo(FormAction):
def name(self) -> Text:
"""Unique identifier of the form"""
return "form_info"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["NAME", "BRAND"]
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_message(template="utter_submit", name=tracker.get_slot('NAME'),
headset=tracker.get_slot('BRAND'))
return []
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 {
"name": [self.from_entity(entity="NAME", intent='my_name_is'),
self.from_text()],
"headset": [self.from_entity(entity="BRAND", intent="headset"),
self.from_text()],
}
@staticmethod
def brand_db() -> List[Text]:
"""Database of supported cuisines"""
return [
"samsung",
"One plus",
"I-phone",
]
def validate_brand(
self,
value: Text,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> Dict[Text, Any]:
"""Validate cuisine value."""
print(value)
if value.lower() in self.cuisine_db():
# validation succeeded, set the value of the "cuisine" slot to value
return {"BRAND": value}
else:
print(value)
dispatcher.utter_message(template="utter_wrong_value")
# validation failed, set this slot to None, meaning the
# user will be asked for the slot again
return {"BRAND": None}