You can use custom actions.
In your custom action, you can connect to a database or read a CSV file like in any other Python code.
If you use slots, you can get the value of the slot in the custom action by using tracker.get_slot(slot_name)
.
You do your stuff with the CSV as with any other Python code and get the output, build a reply based on the output, and use the dispatcher.utter_message(reply)
function to make the chatbot utter the message.
For example, in the run()
method of your custom action:
# get the location slot
location = tracker.get_slot('location')
# read the CSV file
with open('universities.csv', 'r') as file:
reader = csv.DictReader(file)
# get a list of universities in the desired location
output = [row for row in reader if row['Location'] == location]
if output: # there is at least one value
# build your reply according to the output
reply = f"This is a list of universities in {location}:"
reply += "\n- " + "\n- ".join([item['Name'] for item in output])
# utter the message
dispatcher.utter_message(reply)
else: # the list is empty
dispatcher.utter_message(f"I could not find universities in {location}")