Saving user variable with API Call
# 🤝help
w
I am just beating my head into a wall right now so I figured I will throw this out to the ethers here Why is my code not saving the variable? In my logs it is correctly pulling the right line needed from the API response but botpress won't save it to the variable. I feel like I am missing something super simple but I just can't see it if (data.result && data.result.id) { // Log the task ID to verify it's being accessed correctly console.log('Task ID from response:', data.result.id); // Save the task ID to the user object user.browseaitaskID = data.result.id; console.log('Task ID saved to user.browseaitaskID:', user.browseaitaskID); } else { console.error('Response did not contain a task ID'); } The part that is really driving me nuts is when I look the logs it says: "14:50:23 info card:Execute JSON POST API call to Browse AI with user URL action Task ID saved to user.browseaitaskID: d214f7f6-2f73-4fdd-8cbb-264a295ce0ac Task ID saved to user.browseaitaskID: d214f7f6-2f73-4fdd-8cbb-264a295ce0ac {"scope":"bot-action","cardId":"card:Execute JSON POST API call to Browse AI with user URL","nodeId":"node:Set_the_URLTo_Correct_Syntax","workflowId":"flow:Groupon Template Builder (Facebook)"}" but the variable shows empty in Botpress
n
It's been raised by myself and other too Spent days on this!! @white-hamburger-48697 @cold-motherboard-82208 @crooked-van-25152 See https://discord.com/channels/1108396290624213082/1240040947228872844
w
@nice-airplane-37559 yeah I don't understand. I have loaded in their bot example to use an OpenAI assistant to see if I can figure out what the heck is going on. It is sending and getting a reply no issue, but it just won't save. const user = event.state.user; // Assuming
user
is part of the BotPress session state async function postToBrowseAI() { const url = 'https://api.browse.ai/v2/robots/bot.botID/tasks'; const headers = { 'Authorization': 'Bearer bot.browseaitoken', 'Content-Type': 'application/json' }; const body = JSON.stringify({ inputParameters: { originUrl: user.grouponformattedURL // Use the URL from the user object } }); try { const response = await fetch(url, { method: 'POST', headers: headers, body: body }); if (!response.ok) { throw new Error(
HTTP error! status: ${response.status}
); } const data = await response.json(); console.log('Full response from Browse AI:', data); // Check if data.result and data.result.id are defined if (data.result && data.result.id) { // Log the task ID to verify it's being accessed correctly console.log('Task ID from response:', data.result.id); // Save the task ID to the user object user.browseaitaskID = data.result.id; console.log('Task ID saved to user.browseaitaskID:', user.browseaitaskID); // Update the BotPress session state event.state.user = user; // Log the updated user object console.log('Updated user object:', user); } else { console.error('Response did not contain a task ID'); } } catch (error) { console.error('Error making POST request:', error); } } // Call the function to make the API request postToBrowseAI();
n
Don't forget to use the ".wait" syntax with axios, that said it still receives the data in .response! If this is a bug then I'd like to know rather than spend days trying options @bumpy-butcher-41910
@white-hamburger-48697 Try adding another await call eg await new Promise((resolve) => setTimeout(resolve, 3000)); after the let response = await axios.get(url, { headers }); EG let response = await axios.get(url, { headers }); await new Promise((resolve) => setTimeout(resolve, 3000)); let recordData = response.data;
g
Any updates on this? I'm currently facing the same problem right now
3 Views