https://discord.gg/botpress logo
#🤝help
How do I create dynamic choice buttons?
# 🤝help
r

rapid-manchester-41665

07/25/2023, 5:05 AM
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

early-train-33247

07/26/2023, 11:19 PM
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

rapid-manchester-41665

07/27/2023, 2:20 AM
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

early-train-33247

07/27/2023, 6:18 PM
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

rapid-manchester-41665

07/27/2023, 8:37 PM
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

early-train-33247

07/28/2023, 10:25 PM
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!
11 Views