I want to ask a set of questions(like a questionnaire or survey) with multiple choice options and then according to the answers given (i wanna build some logic like basic if -else conditions) and then display the output to the user.
Welcome to the forum
Take a look at Forms, they’re made for saving your user’s answers in Slots as defined by a Slot Mapping.
After asking for all the slots, the form will deactivate, and you can add a rule to activate a Custom Action after deactivation.
In that Custom Action, you can write any Python code. In there, you can access the slot values inside the Tracker with tracker.get_slot(slotname)
, and use the Dispatcher to utter a message according to the slot values, or even activate Events by returning a list of events in the run()
method of the Custom Action.
You can customize your Forms even more by doing logic while the form is running to ask for the next slot, validate an input, and more.
Here is example of Action server asking 20 questions and after all replied gives user a list of wrongly answered questions. Hope this helps
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
class KyselyForm(FormAction):
def name(self):
return "kysely_form"
@staticmethod
def required_slots(tracker):
return ["kysymys01", "kysymys02", "kysymys03", "kysymys04", "kysymys05", "kysymys06", "kysymys07", "kysymys08", "kysymys09", "kysymys10", "kysymys11", "kysymys12", "kysymys13", "kysymys14", "kysymys15", "kysymys16", "kysymys17", "kysymys18", "kysymys19", "kysymys20", "kysymys21", "kysymys22"]
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
return {
"kysymys01": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys02": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys03": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys04": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys05": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys06": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys07": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys08": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys09": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys10": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys11": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys12": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys13": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys14": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys15": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys16": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys17": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys18": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys19": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys20": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys21": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
"kysymys22": [
self.from_intent(intent="juu_vastaus", value=True),
self.from_intent(intent="ei_vastaus", value=False),
],
}
def submit(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict]:
dispatcher.utter_message("Kiitos! Olet vastannut kaikkiin kysymyksiin. Alla linkit kohtiin joihin vastasit ei/en osaa")
if tracker.get_slot("kysymys01") == False:
dispatcher.utter_message("www.oppialiasaa1.com")
if tracker.get_slot("kysymys02") == False:
dispatcher.utter_message("www.oppialiasaa2.com")
if tracker.get_slot("kysymys03") == False:
dispatcher.utter_message("www.oppialiasaa3.com")
if tracker.get_slot("kysymys04") == False:
dispatcher.utter_message("www.oppialiasaa5.com")
if tracker.get_slot("kysymys05") == False:
dispatcher.utter_message("www.oppialiasaa5.com")
if tracker.get_slot("kysymys06") == False:
dispatcher.utter_message("www.oppialiasaa6.com")
if tracker.get_slot("kysymys07") == False:
dispatcher.utter_message("www.oppialiasaa7.com")
if tracker.get_slot("kysymys08") == False:
dispatcher.utter_message("www.oppialiasaa8.com")
if tracker.get_slot("kysymys09") == False:
dispatcher.utter_message("www.oppialiasaa9.com")
if tracker.get_slot("kysymys10") == False:
dispatcher.utter_message("www.oppialiasaa10.com")
if tracker.get_slot("kysymys11") == False:
dispatcher.utter_message("www.oppialiasaa11.com")
if tracker.get_slot("kysymys12") == False:
dispatcher.utter_message("www.oppialiasaa12.com")
if tracker.get_slot("kysymys13") == False:
dispatcher.utter_message("www.oppialiasaa13.com")
if tracker.get_slot("kysymys14") == False:
dispatcher.utter_message("www.oppialiasaa14.com")
if tracker.get_slot("kysymys15") == False:
dispatcher.utter_message("www.oppialiasaa15.com")
if tracker.get_slot("kysymys16") == False:
dispatcher.utter_message("www.oppialiasaa16.com")
if tracker.get_slot("kysymys17") == False:
dispatcher.utter_message("www.oppialiasaa17.com")
if tracker.get_slot("kysymys18") == False:
dispatcher.utter_message("www.oppialiasaa19.com")
if tracker.get_slot("kysymys19") == False:
dispatcher.utter_message("www.oppialiasaa19.com")
if tracker.get_slot("kysymys20") == False:
dispatcher.utter_message("www.oppialiasaa20.com")
if tracker.get_slot("kysymys21") == False:
dispatcher.utter_message("www.oppialiasaa21.com")
if tracker.get_slot("kysymys22") == False:
dispatcher.utter_message("http://www.oppialiasaa22.com")
dispatcher.utter_message("")
dispatcher.utter_message("Tässä kaikki tällä kertaa")
return []