How to set a user variable during init?
# 🤝help
m
I have created a user variable called VIP (user.VIP) in my flow. I want to set this using window.botpressWebChat.init however I can't seem to get the syntax correct. Please can someone provide an example? Thanks1
I have tried adding userData: { "user.VIP": "1" } to window.botpressWebChat.init({ });
a
Hey @miniature-book-74584 there are two parts to this: 1. Send info from the webpage to the bot 2. Catch this info in the bot and do something with it For the first part, you can look at our documentation here https://botpress.com/docs/cloud/toolbox/events-triggers/#sending-custom-information and use code like this in your website:
Copy code
js
  document.addEventListener("LIFECYCLE.READY", () => {
    window.botpressWebChat.sendPayload({
      type: 'trigger',
      payload: {
        myCustomProperty: 'hello',
      },
    });
  });
Then, inside your bot you can catch this with a trigger and use an execute code card to save the info to a user variable
Copy code
js
if(event.payload.myCustomProperty && event.payload.myCustomProperty == "myValue"){
  user.var = event.payload.myCustomProperty
}
m
Ahha, ok thanks Gordy let me try that!
a
You'll need to use a custom trigger to catch it

https://cdn.discordapp.com/attachments/1144600525250236447/1144602988250419260/image.png

m
ok, so have added
document.addEventListener("LIFECYCLE.READY", () => { window.botpressWebChat.sendPayload({ type: 'trigger', payload: { myCustomProperty: 'vip', }, }); });
In to a on the page...
Then added a trigger block
Trigger1
Do I add this code...
if(event.payload.myCustomProperty && event.payload.myCustomProperty == "vip"){ user.VIP = "1" }
To the trigger itself?
Or Do I need to add to "execute code" section?
a
add the execute code section after the trigger
m
Seems my page is not reporting LIFECYCLE.READY ... I can see LIFECYCLE.LOADED but then I receive [webchat] Cannot send data until the webchat is ready... any ideas?
ok so now payload sending... 🙂
However can't seem to get variable to stick 😦
Tried user.vip, event.session.vip_state ... but won't show in {{user.vip}} or {{event.session.vip_state}} in a card..
I can see trigger acticating in log...
Is there any way to view variables being set?
Ok got it working following the approach on your video here, thanks!

https://www.youtube.com/watch?v=FKH3b9NlyaI

47 Views