unwilling-turquoise
unwilling-turquoise15mo ago

Help with API Response Handling in Voiceflow

Hello everyone, I'm working on a project in Voiceflow where I use an API to check the order status from Airtable. My current flow tries to access the order status using the path response.records[0].fields.Status, but I'm facing issues when incorrect or nonexistent order numbers are entered. In these cases, I would like the chatbot to notify the user that the order number was not found instead of showing an error or inappropriate response. Does anyone have experience with handling this type of situation in Voiceflow? How can I effectively check if the records array is empty before trying to access the fields, and what practices would you recommend for handling errors and providing useful feedback to the user? Any guidance or code examples would be greatly appreciated. Thanks in advance! PD: When running the chat, if I enter a correct order number followed by an incorrect order number, it repeats the same response as for the correct order number
No description
No description
2 Replies
Tico | Voiceflow Assistant
Voiceflow functions have the ability to interact with third-party APIs or your own backend services. This is made possible through a modified fetch API for making network requests. Here's an example of how to make a GET request to retrieve data from an API:
export default async function main(args) {
const response = await fetch('https://cat-fact.herokuapp.com/facts');
const responseBody = await response.json();
// Accessing the response body
// ... process responseBody
}
export default async function main(args) {
const response = await fetch('https://cat-fact.herokuapp.com/facts');
const responseBody = await response.json();
// Accessing the response body
// ... process responseBody
}
To map and process the data from the API response, you can use JavaScript array methods like map:
const facts = responseBody.map(fact => fact.text);
const facts = responseBody.map(fact => fact.text);
You can create traces for each item you want to include in the assistant's response:
return next({ path: 'success', trace: facts.map(text => ({ type: 'text', payload: { message: text } })) });
return next({ path: 'success', trace: facts.map(text => ({ type: 'text', payload: { message: text } })) });
Remember to handle cases where the user has not responded within a certain timeout. This would require a timer function of some kind. If the user has not responded, the next request is simply null to denote that the user has not replied, and the API will handle the following logic:
action: { type: 'no-reply' }
action: { type: 'no-reply' }
By following these steps and specifications, you can implement robust functions within Voiceflow. These functions can transform user input, interact with APIs, and control the flow of the conversation, enhancing the capabilities of your Voiceflow assistant. For more detailed information, you can refer to the Voiceflow Functions Documentation.
W. Williams (SFT)
So, instead of capturing the individual responses, capture the whole response using just the response. Actually, you can capture response.records either way, use a Javascript step and make sure the var you set your response to is equal to 1 (varName.length). then you will be able to handle all scenarios.

Did you find this page helpful?