Hi I want to create a form which asks the user to choose a value from a list. This list is dynamic and is retrieved from another python call.
The flow would be something like this:
user: “Select filter”
background → retrieves list of filter options
bot: “These are the available filters:
1) country
2) date
…”
user: “by country please”
background → sets filter
I can get the flow to set the slots (as I´ve given it some examples) but I cannot get the bot to print the list of filters to the user. I believe its something to do with my action or story. Tbh I´m unsure how I should set up the story for a flow like this.
ACTION:
import requests
import json
from typing import Any, Text, Dict, List
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk import Action
from rasa_sdk.events import SlotSet
class ActionSelectFilters(Action):
def name(self) -> Text:
return "action_select_filters"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
URL = 'http://localhost:8082/process'
HEADERS = {'content-type': 'application/json', 'charset': 'UTF-8'}
# Retrieve slots
selected_filter = tracker.get_slot("filter")
data = {
"action": "select_filter",
"filter": selected_filter
}
try:
if selected_filter is None: # No filter specified
r = requests.post(url=URL, headers=HEADERS, json=data, verify=False)
output = json.loads(r.text)
dispatcher.utter_message(response="utter_ask_for_filter")
dispatcher.utter_message(response="\n".join(output["filter"]))
return [SlotSet("filter", None)]
else: # Filter specified
r = requests.post(url=URL, headers=HEADERS, json=data, verify=False)
output = json.loads(r.text)
if output and "type" in output and output["type"] == "error":
print(f"Received exception from web service: {output['message']}")
return [] # Finished --> error
elif output["complete"]:
dispatcher.utter_message(response="utter_filter_set")
return [] # Finished
except Exception as ex:
print(f"Exception received: {ex}")