wise-white
wise-white15mo ago

Need Expert Help: Dynamic Buttons in function

Hello. I have this function to generate dynamic buttons from an API response, everything works fine except the "set variable" action, Can someone help please: export default async function main(args) { let { cities } = args.inputVars; cities = JSON.parse(cities); const buttons = cities.map(city => ({ name: city.name, request: { type: 'event_A', payload: { actions: [ { type: 'set-variable', name: 'city', value: 234 } ] } } })); return { trace: [ { type: "text", payload: { message: "Please select a city:" } }, { type: "choice", payload: { buttons: buttons } } ], next: { listen: true, defaultTo: 'success', to: [ ] } }; }
19 Replies
Tico | Voiceflow Assistant
The original output seems to be addressing a problem that is not mentioned or supported by the source documents. The source documents do not provide any information about a "set variable" action issue, parsing a cities input variable, or dynamically generating buttons from parsed cities data. Therefore, the entire original output is inconsistent with the source documents. Based on the information available in the source documents, it's not possible to provide a revised output that addresses the same problem as the original output. The source documents mainly discuss how to use listen functionality, choice traces, and next many commands in Voiceflow, as well as how to make network requests and process input variables in Voiceflow functions. If you have any specific questions or issues related to these topics, feel free to ask, and I'll do my best to provide a helpful response based on the available information.
wise-white
wise-whiteOP15mo ago
Help, please
No description
W. Williams (SFT)
You should use a Javascript step for this.
wise-white
wise-whiteOP15mo ago
how @W. Williams (SFT) please ?
W. Williams (SFT)
buttons? Here is the JS code. You will need to change it to support your data.
choice = {
"buttons": []
}

const choiceItems = apiResponse.products
const choiceButton = (name, click) => ( {
"name": name,
'request': {
'type': 'choice',
'payload': {
click: name
}
}
} )

choiceItems.forEach( ( item, index ) => {
choice.buttons.push( choiceButton(
item.title,
index
) )
} )

choice = JSON.stringify(choice)
choice = {
"buttons": []
}

const choiceItems = apiResponse.products
const choiceButton = (name, click) => ( {
"name": name,
'request': {
'type': 'choice',
'payload': {
click: name
}
}
} )

choiceItems.forEach( ( item, index ) => {
choice.buttons.push( choiceButton(
item.title,
index
) )
} )

choice = JSON.stringify(choice)
choice is a VF var
W. Williams (SFT)
Then do this.
No description
W. Williams (SFT)
that is it
wise-white
wise-whiteOP15mo ago
ok, I see this will show the buttons how about the action that will happen when clicking on the button, how to add it?
W. Williams (SFT)
just put it in the payload
wise-white
wise-whiteOP15mo ago
ok, @W. Williams (SFT) I test your code and it is working fine now I need just the syntax of adding a set-variable action in the payload, I didn't find it in any documentation
W. Williams (SFT)
? what do you mean?
wise-white
wise-whiteOP15mo ago
when I click on any button, I want this product_id = item.id where product_id is a VF variable
W. Williams (SFT)
payload: { actions: [ { type: 'set-variable', name: 'city', value: the-value-you-want } ]} Honestly, I have never done this, but I took the code you had
wise-white
wise-whiteOP15mo ago
cool Ok, I was searching for this specefix syntax for days let me try it
W. Williams (SFT)
I don't know if that works never seen set-variable might be right. we do it using last_event
wise-white
wise-whiteOP15mo ago
No, Unfortunatly it is not working
W. Williams (SFT)
didn't think so. like I said, I used your code what are you trying to do? city name or an id? what data are you passing into the buttons? Can you send the JSON / array / object? @Issamovitch
wise-white
wise-whiteOP15mo ago
city.name is used as the buttons labels, then when clicking on a button the city.id is saved in a VF variable (let's say it is called city_id for example) with a simple api json response : an array of cities, each with a name and an id) { [ { name: "city1", id: 1}, { name: "city2", id: 2}, { name: "city3", id: 3}, ] } Finally After the custom Action that will show the buttons, i just need to add a javascript : city = last_event.payload.id Solved ✅
W. Williams (SFT)
yep

Did you find this page helpful?