I am trying to build a chatbot which mainly comprises of 2 tasks: Task 1. Ask users to share their pincode and choose the category. Fetch results using these two inputs and display the message. Task 2. 1. After the flow ends - If a user says “corona help“ - prompt him asking if he wants to get them for the above given pincode if not - give him a provision to input a new pincode.
Follow similar flow for category as well. For example ( let us say user asked for - Free Food in 600036 )
User : corona help Bot : Do you want to get the resources for - 600036. Press yes to confirm and no to change another pincode User : no Bot: Please share the pincode User : 500081 Bot : Do you want to get “ Free Food” . Press yes to confirm and no to change User : Yes Bot : Show results for 500081 and Free Food
I have already done with task 1 but having difficulty with task 2. I want to know how to ask questions and fill the required slots if a particular intent is triggered.
Here’s my rules.yml:
version: "2.0"
rules:
- rule: trigger corona help
steps:
- intent: corona_help
- action: action_check_user_intent
- rule: Activate form
steps:
- intent: greet
- action: user_details_form
- active_loop: user_details_form
- rule: Submit form
condition:
# Condition that form is active.
- active_loop: user_details_form
steps:
# Form is deactivated
- action: user_details_form
- active_loop: null
- slot_was_set:
- requested_slot: null
# The actions we want to run when the form is submitted.
- action: action_submit
Actions.py:
class ValidateUserDetailsForm(FormValidationAction):
def name(self) -> Text:
return "validate_user_details_form"
def validate_pin_code(self,
slot_value: Any,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict,
) -> Dict[Text, Any]:
print("HERE1")
url = f"https://api.postalpincode.in/pincode/{slot_value}"
r = requests.get(url)
data = json.loads(r.content)
status = data[0]['Status']
print(status)
if status == 'Error':
dispatcher.utter_message(text="Invalid PIN code. Enter again.")
return {"pin_code": None}
else:
return {"pin_code": slot_value}
class ActionCheckUserIntent(Action):
def name(self) -> Text:
return "action_check_user_intent"
def run(
self,
dispatcher,
tracker: Tracker,
domain: "DomainDict",
) -> List[Dict[Text, Any]]:
intent = tracker.latest_message['intent'].get('name')
print(intent)
pin_code = tracker.get_slot("pin_code")
print(pin_code)
category = tracker.get_slot("category")
if intent == "corona_help":
dispatcher.utter_message(response="utter_ask_confirm_pin_code")
return {"confirm_pin_code": None, "confirm_category": None}
class ActionSubmit(Action):
def name(self) -> Text:
return "action_submit"
def run(
self,
dispatcher,
tracker: Tracker,
domain: "DomainDict",
) -> List[Dict[Text, Any]]:
pin_code = tracker.get_slot("pin_code")
category = tracker.get_slot("category")
print("HERE2")
pin_code_url = f"https://api.postalpincode.in/pincode/{pin_code}"
r1 = requests.get(pin_code_url)
data1 = json.loads(r1.content)
city = data1[0]['PostOffice'][0]['District']
print("HERE3")
city_url = "http://ec2-3-23-130-174.us-east-2.compute.amazonaws.com:8000/cities"
r2 = requests.get(city_url)
data2 = json.loads(r2.content)
cities = data2['data']
if city in cities:
city = city.replace(" ", "%20")
category = category.replace(" ", "%20")
print("HERE4")
category_url = f"http://ec2-3-23-130-174.us-east-2.compute.amazonaws.com:8000/resource?city={city}&category={category}"
r = requests.get(category_url)
data = json.loads(r.content)
data = data['data']
print(data)
if not data:
print("HERE5")
dispatcher.utter_message(text="No resources found.")
return []
# return [AllSlotsReset()]
contact = data[0]["contact"]
description = data[0]["description"]
organisation = data[0]["organisation"]
phone = data[0]["phone"]
state = data[0]["state"]
category = category.replace("%20", " ")
dispatcher.utter_message(response="utter_submit",
pin_code=pin_code,
category=category,
contact = contact,
description = description,
organisation = organisation,
phone = phone,
state = state
)
# return [AllSlotsReset()]
# elif intent == "corona_help":
else:
dispatcher.utter_message(text="No resources found.")
# return [AllSlotsReset()]
Any ideas how to achieve this?