Getting wixUsers.currentUser.id from an external bot

Hi, JS newbie here. I’m trying to integrate a button in my wix site that launches an external bot (https://landbot.io/) and passes on the wixUsers.currentUser.id, so that the bot knows the user.

I tried that code:

import wixUsers from 'wix-users';

$w.onReady(function () {
    
    if (wixUsers.currentUser.loggedIn) {
       
        const memberId = wixUsers.currentUser.id;
       
        
    } else {
        
    }


var myLandbot;
function initLandbot() {
  if (!myLandbot) {
    var s = document.createElement('script');s.type = 'text/javascript';s.async = true;
    s.addEventListener('load', function() {
      myLandbot = new Landbot.Popup({
        configUrl: 'https://storage.googleapis.com/landbot.pro/v3/xxxxxxxxxxxx/index.json',
      });
    });
    s.src = 'https://cdn.landbot.io/landbot-3/landbot-3.0.0.js';
    var x = document.getElementsByTagName('script')[0];
    x.parentNode.insertBefore(s, x);
  }
}


  $w('#startbot').onClick((event) => {
           initLandbot ();
    });




});

Then I tried to add the member ID to the document body, so that I can read it from the bot:

import wixUsers from 'wix-users';

$w.onReady(function () {
    if (wixUsers.currentUser.loggedIn) {
        const memberId = wixUsers.currentUser.id;

        // Den <body>-Tag auswählen und das data-memberid-Attribut hinzufügen
        document.body.setAttribute('data-memberid', memberId);
    } else {
        console.log("Kein Benutzer eingeloggt");
    }
});

Both methods didn’t work. Is there a way to get the member ID within a code snippet? Thanks so much for your help!

The reason why your code is not working is, that you maybe are trying to put codes like…

document.body.setAttribute('data-memberid', memberId);
var s = document.createElement('script');

…onto your wix-page, which is not possible.

Where do you insert those code-parts?

You can use those only on/inside:
a) HTML-Component
b) Custom-Element
c) somewhere inside one of the Dashboard-options.

But you can not use such code-parts inside Wix-pages.

Thank you, that helps! But is it possible to retrieve the member ID of the currently logged-in user in these? Or do you see any other way that an external application like a chatbot can get it? Any hint highly appreciated…

Depends on how is structured and working your BOT.
I assume, you will have your BOT running inside a HTML-Component or CUSTOM-ELEMENT, or you even install it with NPM, i don’t know anything about your choosen BOT and how it works. But to connect your BOT with your wix-site and sending data over, you will have to connect both.

Your steps would be…

  1. Get your BOT-code from the official site or install it via NPM if available.
  2. Add a HTML-Component onto your page (or use CUSTOM-ELEMENT).
  3. Use Wix-Members-BACKEND-API to get the needed MEMBER-ID (IDs).
  4. Once you have retrieved your MEMBER-ID, send it over to your BOT, by using the HTML-Component or Custom-Element.
  5. Let your HTML-Component also respond, to be able to send back data to your wix.page.

You will need knowledge about…

  1. Wix-Members-Backend-API
  2. How are working HTML-Component and Custom-Elements
  3. Knowledge about NPM-based installations and how to use them.
  4. And you will have to code some code-lines (wix-based js and ordinary HTML+JS)

That’s it, and you will be able to achieve what you want.

Thanks so much! I have all pieces together. The only thing I don’t know is how to build the bridge between 3 and 4, i.e. I don’t know how to pass the member ID to an html component or to get the member ID within an html component…

This is exact what you will need, when using HTM-Component…

…and this one…

Project-flow…

  1. Clicking button on wix-site → firing event to send some signal (or data) to component
  2. HTML-Component (recieves) gets data and handle with the data inside HTML-iframe.
  3. Once data was edited/modified → you are able to send this modified data back to wix-page and doing what ever you need with it.

Only this way you can use code-parts like…
var s = document.createElement('script')

…or…
var x = document.getElementsByTagName('script')[0];

1 Like