Send a message from user

You can programmatically send a message to your bot on the user’s behalf using JavaScript.

Send a message on the user’s behalf

To send a message on a user’s behalf, use the window.botpress.sendMessage method wrapped in an event listener:

window.botpress.on('webchat:ready', () => {
  const message = 'Hi there!' // Replace with your own message
  window.botpress.sendMessage(message)
})

This snippet:

  1. Waits until Webchat is open and ready to receive messages
  2. Sends the message on behalf of the user

Start the conversation on the user’s behalf

To proactively open Webchat and start a conversation on the user’s behalf when they visit your website:

window.botpress.on('webchat:initialized', (event) => {
  window.botpress.open()
})

window.botpress.on('webchat:ready', () => {
  const message = 'Hi there!' // Replace with your own message
  window.botpress.sendMessage(message)
})

This snippet:

  1. Waits until Webchat is initialized
  2. Opens Webchat
  3. Waits until Webchat is ready to receive messages
  4. Sends the message on behalf on the user’s behalf