import React, { useState, useEffect, useRef } from ‘react’;
import {
Button,
Modal,
ModalBody,
Text,
TextArea,
Box,
Flex,
hubspot,
} from ‘@hubspot/ui-extensions’;
// hubspot.extend(({ context, actions }) => {
// console.log(‘
Available actions:’, actions);
// console.log(‘
fetchHubSpotApi:’, actions.fetchHubSpotApi);
// console.log(‘
Type of fetchHubSpotApi:’, typeof actions.fetchHubSpotApi);
// return (
// <Extension
// context={context}
// fetchProperties={actions.fetchCrmObjectProperties}
// fetchHubSpotApi={actions.fetchHubSpotApi} //
correctly pass
// />
// );
// });
hubspot.extend(({ context, actions }) => {
console.log(‘
Available actions:’, actions);
return (
\<Extension
context={context}
fetchProperties={actions.fetchCrmObjectProperties}
// ❌ remove fetchHubSpotApi
/>
);
});
const Extension = ({ context, fetchProperties, fetchHubSpotApi }) => {
return (
\<Button
overlay={
\<Modal id="chatbot-modal" title="MCUTILITY" width="md">
\<ModalBody>
\<Box direction="column" gap="md" padding="md">
\<ChatBotUI
context={context}
fetchProperties={fetchProperties}
// fetchHubSpotApi={fetchHubSpotApi} // ✅ pass correctly
/>
\</Box>
\</ModalBody>
\</Modal>
}
>
Send SMS
\</Button>
);
};
const ChatBotUI = ({ context, fetchProperties }) => {
const [messages, setMessages] = useState([
{ from: 'bot', text: 'Hi there! How can I assist you today?' },
]);
const refChatUi = useRef(null);
const [input, setInput] = useState(‘’);
const [contactDetails, setContactDetails] = useState(null);
const [loadingContact, setLoadingContact] = useState(true);
const [errorContact, setErrorContact] = useState(null);
const [sendingSMS, setSendingSMS] = useState(false);
const [smsError, setSmsError] = useState(null);
useEffect(() => {
const getContactData = async () => {
if (
context.crm &&
context.crm.objectId &&
context.crm.objectTypeId === '0-1'
) {
try {
setLoadingContact(true);
const properties = await fetchProperties(\[
'firstname',
'lastname',
'email',
'phone',
'company',
]);
setContactDetails(properties);
} catch (error) {
setErrorContact('Failed to load contact details.');
} finally {
setLoadingContact(false);
}
} else {
setErrorContact('Extension not running on a contact record.');
setLoadingContact(false);
}
};
getContactData();
}, [context, fetchProperties]);
useEffect(() => {
if (refChatUi.current) {
refChatUi.current.scrollTop = refChatUi.current.scrollHeight;
}
}, [messages]);
const handleSend = async () => {
if (!input.trim()) return;
setMessages((prev) => […prev, { from: ‘user’, text: input }]);
if (!contactDetails || !contactDetails.phone) {
const err = 'Contact phone number not available.';
setSmsError(err);
setMessages((prev) => \[...prev, { from: 'bot', text: \`Error: ${err}\` }]);
setInput('');
return;
}
const phoneNumber = contactDetails.phone;
const portalId = context.portal?.id;
if (!portalId) {
const err = 'HubSpot portal ID not found.';
setSmsError(err);
setMessages((prev) => \[...prev, { from: 'bot', text: \`Error: ${err}\` }]);
setInput('');
return;
}
try {
setSendingSMS(true);
setSmsError(null);
// ✅ Call your own external API directly
const response = await fetch(‘https://mediumpurple-deer-340527.hostingersite.com/send-message’, {
method: ‘POST’, //
Correct method
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
number: phoneNumber,
portalId,
message: input,
}),
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.message \|\| 'Failed to send SMS');
}
setMessages((prev) => \[
...prev,
{
from: 'bot',
text: \`SMS sent to ${phoneNumber}. You said: "${input}".\`,
},
]);
setInput('');
} catch (error) {
const err = \`Failed to send SMS: ${error.message}\`;
setSmsError(err);
setMessages((prev) => \[...prev, { from: 'bot', text: err }]);
} finally {
setSendingSMS(false);
}
};
return (
\<Box direction="column">
\<Box
padding="sm"
style={{
border: '1px solid #e0e0e0',
borderRadius: '8px',
marginBottom: '12px',
}}
>
\<Text variant="small" style={{ fontWeight: 'bold' }}>
Contact Details:
\</Text>
{loadingContact && \<Text variant="small">Loading contact...\</Text>}
{errorContact && (
\<Text variant="small" style={{ color: 'red' }}>
{errorContact}
\</Text>
)}
{contactDetails && (
\<Box direction="column" gap="extraSmall">
{contactDetails.firstname && (
\<Text variant="small">
First Name: {contactDetails.firstname}
\</Text>
)}
{contactDetails.lastname && (
\<Text variant="small">
Last Name: {contactDetails.lastname}
\</Text>
)}
{contactDetails.email && (
\<Text variant="small">Email: {contactDetails.email}\</Text>
)}
{contactDetails.phone && (
\<Text variant="small">Phone: {contactDetails.phone}\</Text>
)}
{contactDetails.company && (
\<Text variant="small">Company: {contactDetails.company}\</Text>
)}
\</Box>
)}
\</Box>
{sendingSMS && (
\<Text variant="small" style={{ color: 'blue' }}>
Sending SMS...
\</Text>
)}
{smsError && (
\<Text variant="small" style={{ color: 'red' }}>
{smsError}
\</Text>
)}
\<Box
direction="column"
ref={refChatUi}
style={{
height: '300px',
overflowY: 'auto',
padding: '8px',
border: '1px solid #ddd',
borderRadius: '8px',
marginBottom: '12px',
}}
>
{messages.map((msg, idx) => (
\<Flex
key={idx}
justifyContent={msg.from === 'user' ? 'flex-end' : 'flex-start'}
>
\<Box
padding="sm"
style={{
backgroundColor:
msg.from === 'user' ? '#DCF8C6' : '#F1F0F0',
borderRadius: '16px',
maxWidth: '70%',
}}
>
\<Text>{msg.text}\</Text>
\</Box>
\</Flex>
))}
\</Box>
\<Box direction="column" gap="xs">
\<TextArea
placeholder="Type your message..."
value={input}
onChange={(value) => setInput(value)}
rows={2}
disabled={sendingSMS}
/>
\<Button onClick={handleSend} disabled={sendingSMS}>
Send
\</Button>
\</Box>
\</Box>
);
};
thank you for your response this is my full code