Embedding your bot on your website
Embedding a bot to your existing site is quite straightforward. You will need to deploy your bot to a server or hosting provider and make it accessible via a URL. You will then be able to add the following script tag to the end of your index.html
page.
NB: Remember to replace <your-url-here> with the URL of your bot!
<script src="<your-url-here>/assets/modules/channel-web/inject.js"></script>
After the import script above you need to initialize the bot to the window
object with the script below.
<script>
window.botpressWebChat.init({ host: '<your-url-here>', botId: '<your-bot-id>' })
</script>
And that's it! Once you deploy the changes to your website, the bot will become available, and its button will appear.
There is an example included in the default botpress installation at http://localhost:3000/assets/modules/channel-web/examples/embedded-webchat.html
Displaying and hiding the webchat programmatically from the website
If the default Botpress button doesn't work for you, it can be changed by adding a click
event listener to any element on the page. You will also need to pass the hideWidget
key to your init
function like this:
<script>
window.botpressWebChat.init({ host: '<your-url-here>', botId: '<your-bot-id>' hideWidget: true })
</script>
Here is some sample code for adding the event listeners to your custom elements:
<script>
document.getElementById('show-bp').addEventListener('click', () => {
window.botpressWebChat.sendEvent({ type: 'show' })
})
document.getElementById('hide-bp').addEventListener('click', () => {
window.botpressWebChat.sendEvent({ type: 'hide' })
})
</script>
Obtaining the User ID of your visitor
It may be useful to fetch the current visitor ID to either save it in your database or to update some attributes in the Botpress DB.
Since the webchat is running in an iframe, communication between frames is done by posting messages. The chat will dispatch an event when the user id is set, which you can listen for on your own page.
window.addEventListener('message', message => {
if (message.data.userId) {
console.log(`The User ID is ` + message.data.userId)
}
})
Customizing the look and feel of the Webchat
The Webchat view is customizable by passing additional params to the init
function, below are the options available:
window.botpressWebChat.init({
host: '<host>',
botId: '<botId>', //The ID for your bot
botName: 'Bot', // Name of your bot
botAvatarUrl: null, // Default avatar URL of the image (e.g., 'https://avatars3.githubusercontent.com/u/1315508?v=4&s=400' )
botConvoTitle: 'Technical Support', // Title of the first conversation with the bot
botConvoDescription: '',
backgroundColor: '#ffffff', // Color of the background
textColorOnBackground: '#666666', // Color of the text on the background
foregroundColor: '#0176ff', // Element background color (header, composer, button..)
textColorOnForeground: '#ffffff', // Element text color (header, composer, button..)
showConversationsButton: true, // Whether or not to show the conversations button
showUserName: false, // Whether or not to show the user's name
showUserAvatar: false, // Whether or not to show the user's avatar
enableTranscriptDownload: false, // Whether or not to show the transcript download button
externalAuthToken: 'my jwt token', // Defines a token that is sent with each messages to Botpress
userId: null // Allows you to override the default user id. Make sure it is not possible to guess it!
})
Configuring the Webchat during a conversation
The method window.botpressWebChat.configure
allows you to change the configuration of the chat during a conversation without having to reload the page