Chatbot button appearing twice
# 🤝help
c
I am using react for my website and am trying to integrate botpress into it. However, the chatbot button appears twice on my pages. I followed the react tutorial in the Botpress documentation.
g
Could you write down the code (without your personal information from the bot script)
c
Copy code
import React, { useEffect } from 'react'
 
const Chatbot = () => {
  useEffect(() => {
    const script = document.createElement('script')
    script.src = 'https://cdn.botpress.cloud/webchat/v1/inject.js'
    script.async = true
    document.body.appendChild(script)
 
    script.onload = () => {
      window.botpressWebChat.init({
        botId: '',
        hostUrl: 'https://cdn.botpress.cloud/webchat/v1',
        messagingUrl: 'https://messaging.botpress.cloud',
        clientId: '',
      })
    }
  }, [])
 
  return <div id="webchat"/>
}
 
export default Chatbot
then in my app.jsx file, i imported the chatbot
and put a simple div with the chatbot in it
Copy code
<div>
        <Chatbot/>
      </div>
g
I don't really know how to use React code, I personally edit everything directy in an html code
c
Ok i fixed it myself. Here is the modified code for anyone who needs it. Just change the useEffect to the following:
Copy code
useEffect(() => {
  const existingScript = document.getElementById('botpress-webchat-script');
  if (!existingScript) {
    const script = document.createElement('script');
    script.id = 'botpress-webchat-script';
    script.src = 'https://cdn.botpress.cloud/webchat/v1/inject.js';
    script.async = true;
    document.body.appendChild(script);

    script.onload = () => {
      window.botpressWebChat.init({
        botId: '',
        hostUrl: 'https://cdn.botpress.cloud/webchat/v1',
        messagingUrl: 'https://messaging.botpress.cloud',
        clientId: 'd590412c-38c5-4946-87cc-4e8e3f9ef4fd',
      });
    };
  }
}, []);