skip to Main Content

I have a Chatbot component in my Next.js app and I want anyone to use to as a chatbot in their website using <script tag. Suppose I put component in my public folder to have public access.

How can I implement this kind of functionality so that anyone can add this link in the script tag of their application and have access to my Chatbot

<script type="module" src="https://my-app-url/Chatbot.js" defer></script>

enter image description here

3

Answers


  1. The simplest way would be to use an iframe.

    You can host your Next.js app with a page that only contains the chatbot component and set the background color as transparent.

    background-color : transparent;
    

    Then, anyone with access to your app can use an iframe to embed the chatbot in their own website. Adding allowtransparency="true" will achieve your desired effect.

    <iframe src="https://your-nextjs-app/chatbot-page" allowtransparency="true"></iframe> 
    

    more info on iframe allowtransparency.


    To create the iframe with javascript:

    // Create the iframe element
    var iframe = document.createElement('iframe');
    
    // Set the iframe attributes
    iframe.src = 'https://your-nextjs-app/chatbot-page';
    iframe.style.position = 'fixed';
    iframe.style.bottom = '0';
    iframe.style.right = '0';
    iframe.style.width = '400px';
    iframe.style.height = '600px';
    iframe.style.border = 'none';
    iframe.style.opacity = '0.5';
    iframe.style.zIndex = '9999';
    iframe.allowtransparency = true;
    iframe.style.backgroundColor = 'transparent';
    
    // Append the iframe to the document body
    document.body.appendChild(iframe);
    
    Login or Signup to reply.
  2. You should build the component, create HTML template and share the CDN Link

    Host the HTML file (ChatbotTemplate.html) and the built JavaScript file (Chatbot.build.js) on a server or CDN accessible to others.

    — HTML Template

    <div id="chatbot-container"></div>
    <script type="module" src="https://my-app-url/Chatbot.build.js" defer></script>
    

    Users can then include the following script tag in their HTML file to embed the chatbot:

    <script type="module" src="https://your-cdn-url/Chatbot.build.js" defer></script>
    
    Login or Signup to reply.
  3. Building a single react component with create-react-app

    Step 1: initiate a new react project with create-react-app

    run npx create-react-app chatbot-component

    Step 2: configure the project to build a single component.

    Remove everything except public/index.html and src/index.js

    Your folder structure should look like this:

    folder structure

    Delete everything in index.html, and replace everything in index.js with this:

    import React from 'react';
    import ReactDOM from 'react-dom/client';
    
    const Component = () => {
      return (
        <h1>hello world</h1>
      );
    };
    
    const div = document.createElement("div");
    
    const root = ReactDOM.createRoot(div);
    root.render(
      <React.StrictMode>
        <Component />
      </React.StrictMode>
    );
    
    // append element to body or wherever you want.
    document.body.appendChild(div);
    

    Step 3: run build

    run npm run build to build the project.

    You should see this in you build folder:

    enter image description here

    The main.{hash}.js is what you want, you can rename it and host it on a cdn. Any HTML page with this script should see <h1>Hello world</h1> in their body.

    Here’s an example of the build result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search