Making the bot speak first

I don’t quite understand how to implement the code for making the bot speak first before the user accesses chat.

I’ve placed the code:

window.botpressWebChat.sendEvent({
  type: 'proactive-trigger',
  channel: 'web',
  payload: {
    text: 'fake message'
  }
})

into my html page (right below the botpress initialization code) and still within script tags and have saved botspeakfirst.js hook

if (event.type === 'proactive-trigger') {
  // We only want to trigger a proactive message when the session is new,
  // otherwise the conversation will progress everytime the page is refreshed.
  if (event.state.session.lastMessages.length) {
    // This will tell the dialog engine to skip the processing of this event.
    event.setFlag(bp.IO.WellKnownFlags.SKIP_DIALOG_ENGINE, true)
  }
}

into botpress/data/global/hooks/before_incoming_middleware

and the bot does not speak when I click on the botpress webchat button.

What am I doing wrong/missing?

Hi @av_botpress,

I’ve just updated the proactive docs in this PR. It will be merged soon.

In your case, its most likely that the chat didn’t load fast enough to send the event. The updated docs make mention of this. Try to set a timeout before sending the event (like in the new docs).

Let me know how it goes!

I added the timeout code in the same way you did in the update, however, still do not receive a message as soon as the web chat is accessed. Also, not sure if this effects anything, but the website is being hosted locally.

  • Is the botId property correct in your window.botpressWebChat.init() ?
  • If you type window.botpressWebChat in your browser console, do you see a result?
  • Maybe try setting a longer timeout (if you’re on a low spec computer)

Can you paste the content of your <script> tag in your index.html?

  1. Yes it is correct.
  2. It returns :
    {init: ƒ} init: ƒ init(config) __proto__: Object
  3. I also get an error now. Not sure how, since it was working before, but chrome returns (aka bot widget button does not show up in the bottom right corner anymore)
    image
<script src="http://localhost:3000/assets/modules/channel-web/inject.js"></script>
<script>
      window.botpressWebChat.init({ 
        host: 'http://localhost:3000', 
        botId: 'test-bot',
        extraStylesheet:'removedforprivatereasons' 
        });

        setTimeout(function() {
      window.botpressWebChat.sendEvent({
        type: 'proactive-trigger',
        channel: 'web',
        payload: { text: 'fake message' }
      })
    }, 1500)

What version are you on? It think you might have one of the binary that had a regression.

Try to download the latest 11.9.3 version, just replace your binary with the new one and make sure you backup before starting botpress.

Let me know if it works.

One of the binary was built with an unstable version in development by mistake. I’m pretty sure you’re running on it because the webchat changed quite a bit.

I just installed the newest version that was updated today and it’s giving me the same error.

Update: 6/18/19 - Error is gone but the bot still does not talk when the widget is clicked.

This is the code I’ve implemented in my HTML file:

<script src="http://localhost:3000/assets/modules/channel-web/inject.js"></script>

    <script>
      window.botpressWebChat.init({ 
        host: 'http://localhost:3000', 
        botId: 'test-bot', 
        });

      function sendMessageOnClick() {
      const iframe = document.querySelector('#bp-widget')
      const widget = iframe.contentWindow.document.getElementsByTagName('button')[0]
      widget.addEventListener('click', function() {
        window.botpressWebChat.sendEvent({
          type: 'proactive-trigger',
          channel: 'web',
          payload: { text: 'fake message' }
        })
      })
    }

    // Make sure the widget is loaded
    setTimeout(function() {
      sendMessageOnClick()
    }, 1500)
    
    </script>

This is the error I get now in the browsers console (chrome):



Uncaught DOMException: Blocked a frame with origin             splashpage.html:20 
 "null" from accessing a cross-origin frame.
    at sendMessageOnClick (file:///Z:/data/bots/test-bot/Website%20stuff/splashpage.html:20:43)
    at file:///Z:/data/bots/test-bot/Website%20stuff/splashpage.html:34:7
1 Like

We’ve added interactive examples for proactive messages. You can run them on your machine to see how it works:

1 Like

It’s because your HTML file isn’t from the same domain origin that your bot.

If you put your html file in the data/assets/modules/channel-web/ folder and you access it by launching botpress server at the url : http://localhost:3000/assets/modules/channel-web/splashpage.html, it’s working as expected.

1 Like

@xavierbriole
I’ve tried to implement the html page in the channel-web folder but it does not fix anything. The bot still doesn’t speak when the user clicks the widget button.

@rndlaine
I’ve followed the example of sending a bot message on chat widget click, however, it still doesn’t work when I put click the widget button. I’m also getting an error when I try to run the proactive module on my system. When I’m building it (step 3) I get the error seen below.

yarn install v1.16.0
info No lockfile found.
[1/4] Resolving packages...
error Package "module-builder" refers to a non-existing file '"C:\\Users\\user_name\\Desktop\\BOTPRESS\\build\\module-builder"'.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
1 Like