wooden-painting-80124
01/20/2024, 6:27 PMbotpressWebChat.sendpayload()
. Reason being, I'm trying to send an authorization token to my bot (made in the cloud studio + embedded on page) to get user related data from inside the bot, from an API.
My initial thoughts were to use sendPayload
once the bot was loaded, like so...
<script src="https://cdn.botpress.cloud/webchat/v1/inject.js"></script>
<script>
window.botpressWebChat.init({
//...config info here
});
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendEvent({ type: 'show' });
window.botpressWebChat.sendPayload({ type: trigger, payload: { token: 'token_string' }});
}, ['LIFECYCLE.LOADED']);
</script>
This did not work. sendPayload
also did not work outside of the onEvent
callback, so I assumed the issue was it was was too early in the bot's lifecycle for it to receive data.
Looking at this page (https://botpress.com/docs/cloud/toolbox/events-triggers/), I assumed the next stage in the cycle after LIFECYLE.LOAD
, LIFECYLE.READY
, might be the stage in the lifecycle to attempt to issue a payload.
I tried the following:
<script src="https://cdn.botpress.cloud/webchat/v1/inject.js"></script>
<script>
window.botpressWebChat.init({
//...config info here
});
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendEvent({ type: 'show' });
}, ['LIFECYCLE.LOADED']);
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendPayload({ type: 'trigger', payload: { token: 'token_string' }});
}, ['LIFECYCLE.READY']);
</script>
wooden-painting-80124
01/20/2024, 6:27 PMLIFECYCLE.LOADED
callback.
I reversed the order of assigning the onEvent
handlers (so LIFECYLE.LOADED
came after LIFECYCLE.READY
) and this fixed the issue of the bot not showing, but still the LIFECYCLE.READY
callback never fired.
My guess at this point is that this is a bug, or some custom, pre-load behavior that only allows LIFECYCLE.LOAD
callbacks to be registered?
So I then tried delaying the assignment of the LIFECYLE.READY
handler by assigning it in a setTimeout
, so as not interfere with the "pre-load" state.
I aimed for a timeout that triggered roughly AFTER the bot had loaded and rendered on page.
<script>
window.botpressWebChat.init({
//...config info here
});
window.botpressWebChat.onEvent(function () {
console.log("LOADED!")
window.botpressWebChat.sendEvent({ type: 'show' })
}, ['LIFECYCLE.LOADED']);
</script>
<script>
setTimeout( function () {
window.botpressWebChat.onEvent(function () {
console.log("Sending Payload...");
window.botpressWebChat.sendPayload({ type: 'trigger', payload: { token: 'token_string' }});
}, ['LIFECYCLE.READY');
}, 3500);
</script>
wooden-painting-80124
01/20/2024, 6:27 PMLIFECYCLE.READY
(it occurs before my timeout expires) OR the event is never triggered OR it's an event that I need to trigger?" *
Either way, after all of this, the only code that seems to work and send the token is simply removing the ready state handler, and using sendPayload
after some timeout.
<script>
window.botpressWebChat.init({
//...config info here
});
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendEvent({ type: 'show' })
}, ['LIFECYCLE.LOADED']);
</script>
<script>
setTimeout( function () {
console.log("Sending Payload...");
window.botpressWebChat.sendPayload({ type: 'trigger', payload: { token: 'token_string' }});
}, 3500);
</script>
My issue with this however is that it seems 'hacky' as I'm just guessing when the bot will appear on page. I can't guarantee 3500ms is the case for all users, particularly mobile, off WIFI, and if I set a longer timeout, users will be waiting unnessicarly for the bot to function (the token I want to send is need at the very beginning.
So my questions are : When is the first time I can use botpressWebChat.sendPayload()
? Does this time align with a any event listed here: https://botpress.com/docs/cloud/toolbox/events-triggers/ ? And, if so, how do I register a callback for that event without preventing the chat interface from showing on page?
Thanks for any help in advance, and apologies if any assumptions I have made above come across as 'ignorant' above. I just thought it better to explain my problem + thinking fully so I can arrive at the correct answer quicker.fresh-fireman-491
01/20/2024, 6:40 PMwooden-painting-80124
01/20/2024, 7:09 PMwooden-painting-80124
01/20/2024, 7:11 PM'LIFECYLE.READY'
event.fresh-fireman-491
01/20/2024, 7:14 PMfresh-fireman-491
01/20/2024, 7:14 PMwooden-painting-80124
01/20/2024, 7:41 PMUSER.CONNECTED
event in the init script, as I clear the "show" event from occuring. So the bot doesn't show.
<script src="https://cdn.botpress.cloud/webchat/v1/inject.js"></script>
<script>
window.botpressWebChat.init({
//...config info here
});
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendEvent({ type: 'show' });
}, ['LIFECYCLE.LOADED']);
window.botpressWebChat.onEvent(function () {
window.botpressWebChat.sendPayload({ type: 'trigger', payload: { token: 'token_string' }});
}, ['USER.CONNECTED']);
</script>
If I reverse the order, the USER.CONNECTED
event never fires, but the bot shows. So I never recieved the payload.
After that, I'm back in the same situation, using a setTimeout
to time when to register the USER.CONNECTED
callback with onEvent
.
Am I missing something?fresh-fireman-491
01/20/2024, 8:09 PMfresh-fireman-491
01/20/2024, 8:13 PMfresh-fireman-491
01/20/2024, 8:13 PMfresh-fireman-491
01/20/2024, 8:13 PMfresh-fireman-491
01/20/2024, 8:13 PMfresh-fireman-491
01/20/2024, 8:14 PMwooden-painting-80124
01/20/2024, 8:26 PMwooden-painting-80124
01/20/2024, 8:27 PMfresh-fireman-491
01/20/2024, 9:03 PMwooden-painting-80124
01/20/2024, 9:16 PMinject.js
, from what I understand (and someone please say if I'm wrong), you can only assign ONE 'onEvent' call back in intial setup, before the bot has been intialized. Otherwise you overwrite the onEvent
handler.
The workd around is nesting the LIFECYLE.READY
handler within the LIFECYLE.LOADED
handler, so that once more event handlers can be set, it is set.
Working code below:
window.botpressWebChat.onEvent(function (event) {
if (event.type === 'LIFECYCLE.LOADED') {
window.botpressWebChat.sendEvent({ type: 'show' });
// console.log("LOADED!"); <- sanity check
window.botpressWebChat.onEvent(function(event) {
if (event.type === 'LIFECYCLE.READY') {
//console.log("Running READY callback") <- sanity check
window.botpressWebChat.sendPayload({
type: 'trigger',
payload: {
token: 'token_string',
},
});
}
}, ['LIFECYCLE.READY']);
}
}, ['LIFECYCLE.LOADED']);
fresh-fireman-491
01/20/2024, 9:21 PMwooden-painting-80124
01/20/2024, 9:24 PMwooden-painting-80124
01/20/2024, 9:24 PMfresh-fireman-491
01/20/2024, 9:28 PMwooden-painting-80124
01/20/2024, 9:30 PM