MCPcopy Create free account
hub / github.com/Bitbox-Connect/Bitbox / MiniChatbot

Function MiniChatbot

client/src/component/MiniChatbot.jsx:7–159  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import { GoogleGenerativeAI } from '@google/generative-ai';
6
7const MiniChatbot = () => {
8 const [isOpen, setIsOpen] = useState(false);
9 const [messages, setMessages] = useState([{ text: 'Hi! How can I help you today?', sender: 'bot' }]);
10 const [userMessage, setUserMessage] = useState('');
11
12 const toggleChatbot = () => {
13 setIsOpen(!isOpen);
14 };
15
16 const sendMessage = async () => {
17 if (userMessage.trim() === '') return;
18
19 const newMessages = [...messages, { text: userMessage, sender: 'user' }];
20 setMessages(newMessages);
21 setUserMessage('');
22
23 // Initialize the GoogleGenerativeAI client
24 const genAI = new GoogleGenerativeAI('AIzaSyDcBsz-OK0rZ5cSY5EkoMIodpax63m_FWs'); // Use env variable for API key
25 const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
26
27 try {
28 // Generate content based on the user's message
29 const prompt = `
30 You can do Hi hello but
31 You are an AI assistant for an educational platform. Only respond to questions related to education and all.
32 If the question is unrelated, respond with: "I can only assist with questions related to our learning platform and its services."
33 Here's the user's question: '${userMessage}'
34 `;
35
36
37 const result = await model.generateContent(prompt);
38
39 // Access the generated content, assuming response structure
40 const botResponse = result.response.text(); // Adjust based on API's actual response
41
42 setMessages([...newMessages, { text: botResponse, sender: 'bot' }]);
43 } catch (error) {
44 console.error('Error generating content:', error);
45 setMessages([...newMessages, { text: 'Oops, something went wrong!', sender: 'bot' }]);
46 }
47 };
48
49 const [isVisible, setIsVisible] = useState(false);
50
51 useEffect(() => {
52 const handleScroll = () => {
53 if (window.pageYOffset > 20) {
54 setIsVisible(true);
55 } else {
56 setIsVisible(false);
57 }
58 };
59
60 window.addEventListener('scroll', handleScroll);
61
62 return () => {
63 window.removeEventListener('scroll', handleScroll);
64 };

Callers

nothing calls this directly

Calls 1

sendMessageFunction · 0.85

Tested by

no test coverage detected