Botpress Web Embedding get User Name in the conversation

I have a bot embedded to my wordpress website. It works fine. I need to get the username of the user who is logged in to my wordpress website and greet the user with his name when the user interacts with the botpress bot.
Is there a possible way to get the login name of the wordpress user and use it inside botpress flow?

Hello @Buddhilive, that will require some development. You need to send a custom event to Botpress with the desired informations, and then tell Botpress to handle that information.

  1. In the page where you load the webchat, add the following code. You can add any information you’d like to store on botpress:
  window.botpressWebChat.sendEvent({ 
    type: 'set_name', 
    firstname: 'Someone',
    lastname: 'Hislastname',
    username: 'anything'
  })

2 . Create a hook that will intercept that specific event, and will save the informations:

 async function hook() {
    if(event.type === 'set_name'){
      event.state.user.firstname = event.payload.firstname
      event.state.user.lastname = event.payload.lastname
      event.state.user.username = event.payload.username

     // Prevents your bot from replying to the user for that specific event
     event.setFlag(bp.IO.WellKnownFlags.SKIP_DIALOG_ENGINE, true);
     // Makes sure that the information you set to "event.state.user" is saved
     event.setFlag(bp.IO.WellKnownFlags.FORCE_PERSIST_STATE, true);
    }
  }
1 Like