Skip to content

Instantly share code, notes, and snippets.

@mirosh-kavinda
Last active October 31, 2022 17:26
Show Gist options
  • Save mirosh-kavinda/9fc47319a6f3d05ec43c892b8dd66afe to your computer and use it in GitHub Desktop.
Save mirosh-kavinda/9fc47319a6f3d05ec43c892b8dd66afe to your computer and use it in GitHub Desktop.
Add hubspot chatflow to React application

#This Gist reveals how to add the HubSpot to your react application interact-with-bot-on-page

In The App.js file add these lines to embed the form to your web application -----App.js The whole code snipet of Chatflow lies on -----ChatBot.js

File hierarchy hook .......chatBot.js

import useHubspotChat from "./hooks/chatBot";
export default function App() {
useHubspotChat();
return (
<div>
<div>
<Navbar />
<div className="App ">
/// --------------------
</div>
</div>
)}
</div>
);
}
import React from 'react';
// Create a hook to seperate out logic.
const useHubspotChat = () => {
const [ hasLoaded, setHasLoaded ] = React.useState(false);
const [ activeConversation, setActiveConversation ] = React.useState(false);
const eventRef = React.useRef(null);
React.useEffect(() => {
console.log('hey')
// Add event listener.
window.hsConversationsOnReady = [() => {
setHasLoaded(true);
}];
// Create script component.
let script = document.createElement('script');
//add your portalId below to get the embeded chatBot
script.src=`//js.hs-scripts.com/23220584.js`;
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
window.hsConversationsOnReady = [];
}
}, []);
// Subscripe to conversation events.
React.useEffect(() => {
eventRef.current = (payload) => {
setActiveConversation(payload.conversation.conversationId);
}
}, [hasLoaded]);
const openHandler = React.useCallback(() => {
if (hasLoaded) {
window.HubSpotConversations.widget.open();
}
}, [hasLoaded]);
const closeHandler = React.useCallback(() => {
if (hasLoaded) {
window.HubSpotConversations.widget.close();
}
}, [hasLoaded])
return {
hasLoaded,
activeConversation,
openHandler,
closeHandler
};
}
export default useHubspotChat;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment