i want the data fetched from database to be displayed in a button format but anyone tell me how??
Hi @faiza_conte. You will want to fetch the data from the database and fill it into the response using a custom action and the buttons parameter of the utter_message
method
yes…i try to fetch data from database in mysql query…but those fetched datas i want them to be displayed in button to the user
You can create a response with buttons like this (assuming department_data is a list of the string values you want the buttons to display):
buttons =[]
for d in department_data:
buttons.append({"title": d, "payload": "/book"})
dispatcher.utter_message(text="Please select the department:", buttons=buttons)
Where the payload is the intent from selecting the button.
thank you so much amill…i have another questions…can this be used as record in form actions…when form filling can this thing be displayed for user to select? then record it
If what you want is to retrieve the text of the button that the user selects, I found the best way to do this is by storing it to a slot within the button payload. So for example, this I how I have found it to work:
buttons =[]
for d in department_data:
fill_slot = '{"new_slot_name" : "' + d + '"}'
buttons.append({"title": d, "payload": f"/book{fill_slot}"})
dispatcher.utter_message(text="Please select the department:", buttons=buttons)
So your button payload would end up looking something like this:
(/book{"new_slot_name" : "value of d"})
Where the slot new_slot_name (which you will need to define in domain.yml) is stored with whatever that value of d is. Then you just need to use tracker.get_slot(“new_slot_name”) in your next action to retrieve that value.
…
For forms, you can define a response in your domain.yml file to be used with a form, so like this:
utter_ask_option:
- text: "Please choose an option:"
buttons:
- title: "Option 1"
payload: "Option 1"
- title: "Option 2"
payload: "Option 2"
The name of this response will have to conform with the name of the slot you want to fill like in a normal form. This time the payload will just respond with the text “Option 1” or “Option 2” and that should be stored to your slot.
Hope this helps
the first button option was displayed but then when i try to do the next actions by receving the previous with tracker.get_slot to display the next button option here this is the error i get
the first button option was displayed but then when i try to do the next actions by receving the previous with tracker.get_slot to display the next button option here this is the error i get
What does your code for the buttons in the previous action look like - Have you edited the code for creating buttons as in my previous response? If not, the slot will be empty. In which case, your physician_data list is probably empty hence why no buttons are displayed.
here actually am not doing slot filling or form actions…am just doing this as you see in the story
You need to store the title of your button in a slot, so use the code I suggested earlier:
It looks like you have created a department slot, so just change new_slot_name to department and hopefully should work!
the department list is out no prooblem…but after the user selects the department then the doctor list will be displayed,here the problem is the doctor list is not displaying after getting the slot name of the department
if you have zoom maybe i can share a sreen with you if u have time to see it for me
When you use the line:
get_dep_name = tracker.get_slot("department")
is the correct value present in the slot “department”? i.e. does the variable get_dep_name have the correct value?
If it doesn’t this is where your problem is, because presumably your sql results are then returning an empty list, and therefore no buttons are being displayed. You need to edit your code that displays the buttons for departments as in my other replies to solve this.
here is the button list for department works fine it displayed but after i select one of the depaptment to display the doctor found in that department this is where there is no display…th
The issue is that you’re not saving the title of the button that is selected. You need to save this in a slot.
Change this:
buttons =[]
for d in department_data:
buttons.append({"title": d, "payload": "/choose_department"})
dispatcher.utter_message(text="Please select the department:", buttons=buttons)
to this:
buttons =[]
for d in department_data:
fill_slot = '{"department" : "' + d + '"}'
buttons.append({"title": d, "payload": f"/choose_department{fill_slot}"})
dispatcher.utter_message(text="Please select the department:", buttons=buttons)
This will save the title of the button to the slot department, which you then retrieve to use for your sql query.
What is the value of d? It needs to be a string for this to work.
You may have to convert it’s value to a string and store in a new variable which can then be used when creating fill_slot.
as you have seen it in the screenshot it is a string that is list of departments
Your error message is saying that d is a tuple, so you need to convert that to a string (depending on it’s format).
If you do print(d) what would that look like? You can put it in the first line of the for loop for inspection and it will just print to the terminal you are running the actions in.