56
loading...
This website collects cookies to deliver better user experience
app
. You will need it soon..env
file:AIRTABLE_API_KEY=key*****
BASE_ID=app*****
TABLE_NAME=Table%201
TABLE_NAME
is the name of the table in your Rasa dev tutorial workspace. Because it contains a space, the space is encoded for URL as %20
.AIRTABLE_API_KEY
and BASE_ID
in..env
file to .gitignore
, so that you don't accidentaly commit it to your version control system..gitignore
.# keep secret keys out of VCS
.env
actions/actions.py
with this code:# actions/actions.py
load_dotenv()
airtable_api_key = os.getenv("AIRTABLE_API_KEY")
base_id = os.getenv("BASE_ID")
table_name = os.getenv("TABLE_NAME")
source venv/bin/activate
).pip install python-dotenv requests
requirements.txt
file:rasa==2.3.1
python-dotenv==0.15.0
requests==2.25.1
actions/actions.py
, you will need these imports:# actions/actions.py
from typing import Text, List, Optional, Dict, Any
from rasa_sdk.forms import FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk import Tracker, Action
from dotenv import load_dotenv
import os
import requests
import json
import uuid
actions/actions.py
, create a new method.# actions/actions.py
def create_newsletter_record(email, frequency, notifications, can_ask_age, age):
request_url = f"https://api.airtable.com/v0/{base_id}/{table_name}"
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {airtable_api_key}",
}
data = {
"fields": {
"Id": str(uuid.uuid4()),
"Email": email,
"Frequency": frequency,
"Notifications?": notifications,
"Can ask age?": can_ask_age,
"Age": age,
}
}
try:
response = requests.post(
request_url, headers=headers, data=json.dumps(data)
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
print(f"Response status code: {response.status_code}")
return response
uuid.uuid4()
is used to generate a unique identifier for the record.actions/actions.py
, create a new class.Action
class. We can call this action in our rules and stories with - action: submit_newsletter_form
, since submit_newsletter_form is what the name
method returns.# actions/actions.py
class SubmitNewsletterForm(Action):
def name(self) -> Text:
return "submit_newsletter_form"
async def run(
self, dispatcher, tracker: Tracker, domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
email = tracker.get_slot("email")
frequency = tracker.get_slot("frequency")
notifications = tracker.get_slot("notifications")
can_ask_age = tracker.get_slot("can_ask_age")
age = tracker.get_slot("age")
response = create_newsletter_record(email, frequency, notifications, can_ask_age, age)
dispatcher.utter_message("Thanks, your answers have been recorded!")
return []
run
method is called when the action is called.create_newsletter_record
.dispatcher
to utter a custom message.domain.yml
.# domain.yml
# ... previous content ...
actions:
# ... previous actions ...
- submit_newsletter_form
data/stories.yml
and data/rules.yml
.# data/stories.yml
# ... previous content ...
stories:
# ... previous stories ...
- story: interactive_story_1
steps:
- intent: subscribe
- action: newsletter_form
- active_loop: newsletter_form
- slot_was_set:
- requested_slot: email
- intent: chitchat
- action: utter_ask_continue
- intent: affirm
- action: newsletter_form
- slot_was_set:
- requested_slot: email
- slot_was_set:
- requested_slot: frequency
- intent: chitchat
- action: utter_ask_continue
- intent: affirm
- action: newsletter_form
- slot_was_set:
- requested_slot: frequency
- active_loop: null
- action: utter_subscribed
- action: submit_newsletter_form
# data/rules.yml
# ... previous content ...
rules:
# ... previous rules ...
- rule: submit form
condition:
- active_loop: newsletter_form
steps:
- action: newsletter_form
- active_loop: null
- action: utter_subscribed
- action: submit_newsletter_form
rasa train
.rasa run actions
.rasa shell
.Please note that I needed to make some changes to the notifications
slot. Those changes can be seen on GitHub.
git clone --branch 18-custom-submit-action [email protected]:petr7555/rasa-dev-tutorial.git