How do I create dynamic choice buttons?
# šŸ¤help
r
Suppose I call an API and retrieve product information that includes a choice of 4 different colors. I would then want to use a Single Choice prompt to ask the user which color they wanted - and display the 4 available colors as response buttons. Can I do this in BotPress Cloud? I tried using the {{ workflow.choices[0] }} syntax on the item list for the card - but it treated the rest as a literal value instead of substituting. Is there a way to present a dynamic list of choices where the choice description and number of possible choices is dynamically generated mid-conversation?
Iā€™m getting frustrated! Is there some cache of documentation thatā€™s not on the website??? Iā€™ve almost got this working by using a ā€œBeforeOutgoingMessageā€ hook. I found I could replace the ā€œoutgoingEvent.payload[ā€œoptionsā€] array with the list of choices I want to be displayed - and that actually works (it displays my replacement list of buttons) - but when the user clicks on one of them, the variable still gets set to one of the default options that were set before I replaced them. I tested this by creating a SingleChoice card that says ā€œWhat is your favorite color?ā€ and i configured a hard coded list of 3 options: ā€œBlueā€, ā€œRedā€ and ā€œGreenā€. Then using the hook, I replace them with a list of 4 options: ā€œRedā€, ā€œPurpleā€, ā€œGreenā€ and ā€œMauveā€. My new options get displayed correctly - but when I click on Purple or Yellow, the variable stores the value ā€œBlueā€ (which is not even in the list anymore). šŸ™„ Can someone please tell me what the ā€œrightā€ way to do this is ??? Or point me to a document that explains it???
@early-train-33247 Hi! Iā€™ve had this issue posted for a day and a half so far and havenā€™t gotten any responses. Is this something you can help me with - or maybe tell me how I can get an answer?
e
Hey @rapid-manchester-41665, here is the format you must follow for custom choices:
The variable needs to be an array with object following this structure

https://cdn.discordapp.com/attachments/1133263674392445079/1133901448112001104/image.pngā–¾

That's if you need the value to be assigned to the variable to be different from the label
But if the value can be the same as the label, the items can be a list of strings ["Blue", "Red", "Green"]
Let me know if that helps
r
Hi @early-train-33247 - thanks for responding - but what I need to know is where I need to create that object. I replaced the object in outgoingEvent.payload[ā€œoptionsā€] and it did indeed change the displayed buttons - but when I selected one of them, it returned the prior value for that button index - not the new one I had specified in the ā€œvalueā€. So there must be some other place that also needs to be updatedā€¦???
Ok - my probablement was that I was trying to do this via a Hook and by modifying the event object directly. I didnā€™t realize that I could insert a variable into the choices section of the Single Choice. But now I do - and it works. Thank you.
e
Hey @rapid-manchester-41665, there is no need to use the hooks because you can add a variable directly in the Items section of the capture (also Action buttons for Cards and Carousels). I thought you knew this already because in your original question you mentioned you "tried using the {{ workflow.choices[0] }} syntax on the item list for the card"
Glad to know it worked out! šŸš€ I also have reported to the team that there should be more information about that!
r
Yup! I did try to use the substitution - but I was using it in the wrong spot! Instead of substituting the entire list (which works), I created a list with 3 items using the UI - and then for each list item, I tried to substitute values {{workflow,choiceItem1}}, {{workflow,choiceItem2}}, {{workflow,choiceItem3}} (which doesnā€™t work). If I had been more clear and you had realized that was what I meant, all you needed to say was ā€œDonā€™t substitute the items, substitute the entire list of choicesā€, we would have been done two days ago! šŸ˜† But thank you anyhow.
e
Everything takes some trial and error to be learned haha. At least now you know the exact format for custom choices! You're most welcome!
16 Views