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!
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…
Get your BOT-code from the official site or install it via NPM if available.
Add a HTML-Component onto your page (or use CUSTOM-ELEMENT).
Use Wix-Members-BACKEND-API to get the needed MEMBER-ID (IDs).
Once you have retrieved your MEMBER-ID, send it over to your BOT, by using the HTML-Component or Custom-Element.
Let your HTML-Component also respond, to be able to send back data to your wix.page.
You will need knowledge about…
Wix-Members-Backend-API
How are working HTML-Component and Custom-Elements
Knowledge about NPM-based installations and how to use them.
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…