Solution for Opening New Tab with Botpress on User...
# 🤝help
f
Hi everyone, I'm looking for a solution where, when a user selects the correct answer (Single Choice), Botpress implemented on the site opens a specific page in a new tab (for example, the client clicks on "Open Messenger" and then a link to m.me/ opens in a new tab). At the moment, I have this code inserted into Execute Code, it works in studio.botpress but unfortunately, after publishing it on the site, it doesn't open anything and an error pops up.
Copy code
const messengerUrl = 'https://m.me/';
window.open(messengerUrl, '_blank');
w
Hi ! you cannot interact with the DOM or the window from an execute code card indeed (in the studio it works, but not if the bot is published unfortunately...)
So what you can do is add javascript to the page where your bot is embedded and trigger the message "open messager"'
Copy code
window.botpressWebChat.onEvent(
      function (event) {
        if (event.type === 'MESSAGE.RECEIVED') {
          if(event.value.payload.text === 'Open Messenger'){
            window.open(messengerUrl, '_blank');
          }
        }
      },
      ['MESSAGE.RECEIVED']
    )
something like this
it's not optimal but it should work 😉
let me know if it works for you @full-analyst-31595
f
thanks a lot for the explanation, I'll test it tomorrow and let you know if it works
w
ok great !
12 Views