https://discord.gg/botpress logo
#šŸ’»developers
Title
# šŸ’»developers
q

quick-musician-29561

03/20/2024, 9:33 AM
In the first Node -Ask the chatbot user who they are looking for and store the answer in the 'requestedUser' variable -Fetch the data from your database and filter it by the 'requestedUser'; store the filtered data in the 'userInfo' variable Second Node -Ask the chatbot user what information they need and store the result in the 'requestedInfo' variable -Filter the 'userInfo' data by the 'requestedInfo' and store the result in the 'answer' variable -Display the answer to the chatbot user First Execute code card
Copy code
js
const requestedUser = workflow.requestedUser

axios.get('https://jsonplaceholder.typicode.com/users')
  .then(response => {
    const users = response.data;
    const filteredUsers = users.filter(u => u.name === requestedUser);
    console.log(filteredUsers);
    workflow.userInfo = filteredUsers
  })
  .catch(error => console.error(error));
Second Execute code card
Copy code
js
const userInfo = workflow.userInfo[0]
const requestedInfo = workflow.requestedInfo

function filterUserInfo(userInfo, requestedInfo) {
  if (userInfo.hasOwnProperty(requestedInfo)) {
    return userInfo[requestedInfo];
  } else {
    console.log(`The requested information "${requestedInfo}" is not available.`);
    return null;
  }
}

const answer = filterUserInfo(userInfo, requestedInfo);
console.log(userInfo);
workflow.answer = answer