-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions.py
More file actions
68 lines (55 loc) · 2.2 KB
/
actions.py
File metadata and controls
68 lines (55 loc) · 2.2 KB
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
# 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!"
import os
from typing import Dict, Text, Any, List, Union, Optional
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
from rasa_sdk.events import SlotSet, Form
from server.github import search_first_repo_with_name
class RepositoryForm(FormAction):
def name(self) -> Text:
return "repository_form"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["repository_name"]
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 {
"repository_name": [
self.from_entity(
entity="repository_name", intent=["request_repository", "inform_repository"]
),
self.from_text(intent=["inform_repository"])
]
}
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"""
repo = search_first_repo_with_name(tracker.get_slot("repository_name"))
if repo is not None:
# utter submit template
dispatcher.utter_message(text='''\
I've found {repo.name}, {repo.stargazers_count}🟊 (stars), {repo.forks_count}ᛦ (forks)
{repo.description}
{repo.clone_url}
'''.format(**locals()))
else:
dispatcher.utter_message(text='''\
I have not found any repository named {tracker.get_slot("repository_name")}
'''.format(**locals()))
return []