Guest User

Untitled

a guest
Jun 7th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 11.03 KB | Source Code | 0 0
  1. <style>
  2.     /* Inter Font */
  3.     @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
  4.  
  5.     /* Base styles for the chatbox and its elements */
  6.     .chat-container {
  7.         font-family: 'Inter', sans-serif;
  8.         max-height: 80vh; /* Limit height for scrollability */
  9.         width: 100%;
  10.         max-width: 100%;
  11.     }
  12.     .chat-messages {
  13.         overflow-y: auto;
  14.         flex-grow: 1;
  15.         padding: 1rem;
  16.         scroll-behavior: smooth;
  17.         display: flex;
  18.         flex-direction: column;
  19.     }
  20.     .message-bubble {
  21.         max-width: 80%;
  22.         padding: 0.75rem 1rem;
  23.         border-radius: 1.25rem;
  24.         margin-bottom: 0.75rem;
  25.         line-height: 1.4;
  26.         word-wrap: break-word;
  27.         overflow-wrap: break-word;
  28.         white-space: normal;
  29.         box-sizing: border-box;
  30.         min-width: 0;
  31.     }
  32.     .user-message {
  33.         background-color: #ff8c6b; /* A soft orange/coral from the image palette */
  34.         color: white;
  35.         align-self: flex-end;
  36.         border-bottom-right-radius: 0.25rem; /* Sharper corner for user */
  37.     }
  38.     .ai-message {
  39.         background-color: #f0f2f5; /* Light gray for AI, keeps it neutral */
  40.         color: #374151; /* Darker text for readability */
  41.         align-self: flex-start;
  42.         border-bottom-left-radius: 0.25rem; /* Sharper corner for AI */
  43.     }
  44.     .loading-spinner {
  45.         border: 4px solid rgba(0, 0, 0, 0.1);
  46.         border-top: 4px solid #ff8c6b; /* Spinner color matching the new theme */
  47.         border-radius: 50%;
  48.         width: 24px;
  49.         height: 24px;
  50.         animation: spin 1s linear infinite;
  51.         margin: 1rem auto;
  52.         display: none; /* Hidden by default */
  53.     }
  54.     @keyframes spin {
  55.         0% { transform: rotate(0deg); }
  56.         100% { transform: rotate(360deg); }
  57.     }
  58.     /* Custom modal styles */
  59.     .modal-overlay {
  60.         position: fixed;
  61.         top: 0;
  62.         left: 0;
  63.         width: 100%;
  64.         height: 100%;
  65.         background-color: rgba(0, 0, 0, 0.5);
  66.         display: flex;
  67.         justify-content: center;
  68.         align-items: center;
  69.         z-index: 1000;
  70.         opacity: 0;
  71.         visibility: hidden;
  72.         transition: opacity 0.3s ease, visibility 0.3s ease;
  73.     }
  74.     .modal-overlay.show {
  75.         opacity: 1;
  76.         visibility: visible;
  77.     }
  78.     .modal-content {
  79.         background-color: white;
  80.         padding: 1.5rem;
  81.         border-radius: 0.75rem;
  82.         box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  83.         max-width: 90%;
  84.         width: 400px;
  85.         text-align: center;
  86.     }
  87.     .modal-button {
  88.         background-color: #ff8c6b; /* Modal button color matching theme */
  89.         color: white;
  90.         padding: 0.75rem 1.5rem;
  91.         border-radius: 0.5rem;
  92.         margin-top: 1rem;
  93.         cursor: pointer;
  94.         transition: background-color 0.2s ease;
  95.     }
  96.     .modal-button:hover {
  97.         background-color: #fa725c; /* Slightly darker hover state */
  98.     }
  99. </style>
  100.  
  101. <script src="https://cdn.tailwindcss.com"></script>
  102.  
  103. <div class="chat-container bg-white rounded-lg shadow-xl flex flex-col">
  104.     <div class="p-4 bg-gradient-to-r from-[#e0b0ff] to-[#ff8c6b] text-white rounded-t-lg flex items-center justify-between">
  105.         <h2 class="text-xl font-semibold">Community Network Advisor</h2>
  106.         </div>
  107.  
  108.     <div id="chatMessages" class="chat-messages flex flex-col p-4 flex-grow">
  109.         <div class="message-bubble ai-message self-start">Hello! I'm your AI. What kind of community do you have, and what are your main goals?
  110.         </div>
  111.     </div>
  112.  
  113.     <div id="loadingSpinner" class="loading-spinner"></div>
  114.  
  115.     <div class="p-4 border-t border-gray-200 flex items-center">
  116.         <input
  117.             type="text"
  118.             id="messageInput"
  119.             placeholder="Your message here..."
  120.             class="flex-grow p-3 border border-gray-300 rounded-full focus:outline-none focus:ring-2 focus:ring-pink-400 mr-3"
  121.         />
  122.         <button
  123.             id="sendMessageBtn"
  124.             class="bg-black hover:bg-gray-800 text-white p-3 rounded-full shadow-md transition duration-200 ease-in-out transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-black flex-shrink-0 w-12 h-12 flex items-center justify-center"
  125.         >
  126.             <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="w-6 h-6 text-white">
  127.                 <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 10.5 12 3m0 0 7.5 7.5M12 3v18" />
  128.             </svg>
  129.         </button>
  130.     </div>
  131. </div>
  132.  
  133. <div id="customModal" class="modal-overlay">
  134.     <div class="modal-content">
  135.         <p id="modalMessage" class="text-gray-700 text-lg"></p>
  136.         <button id="modalCloseBtn" class="modal-button">OK</button>
  137.     </div>
  138. </div>
  139.  
  140. <script type="module">
  141.     // Element references
  142.     const chatMessages = document.getElementById('chatMessages');
  143.     const messageInput = document.getElementById('messageInput');
  144.     const sendMessageBtn = document.getElementById('sendMessageBtn');
  145.     const loadingSpinner = document.getElementById('loadingSpinner');
  146.     const customModal = document.getElementById('customModal');
  147.     const modalMessage = document.getElementById('modalMessage');
  148.     const modalCloseBtn = document.getElementById('modalCloseBtn');
  149.  
  150.     // *** IMPORTANT: Replace 'YOUR_GEMINI_API_KEY' with your actual Gemini API Key ***
  151.     const GEMINI_API_KEY = ''; // <-- Place your API Key here!
  152.  
  153.     let chatHistory = [
  154.         { role: "system", parts: [{ text: "You are a positive and enthusiastic brainstorming assistant for community social networks. Always focus on innovative, beneficial, and wonderful ideas. Do not mention alternatives, drawbacks, or negative aspects. Keep responses extremely concise and inspiring, ideally one or two sentences." }] },
  155.         { role: "user", parts: [{ text: "Hello! I'm here to brainstorm wonderful ideas for my community's social network. Can you help me with that by focusing only on positive possibilities?" }] },
  156.         { role: "model", parts: [{ text: "Absolutely! I'm excited to help you brainstorm amazing possibilities for your community's social network. Let's focus on innovative and positive features. What kind of community do you have, and what are its core interests?" }] }
  157.     ];
  158.  
  159.     // Function to display messages in the chat
  160.     function displayMessage(message, sender) {
  161.         const messageBubble = document.createElement('div');
  162.         messageBubble.classList.add('message-bubble', sender === 'user' ? 'user-message' : 'ai-message');
  163.         messageBubble.classList.add('flex', sender === 'user' ? 'self-end' : 'self-start'); // Align bubbles
  164.         messageBubble.textContent = message;
  165.         chatMessages.appendChild(messageBubble);
  166.         chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom
  167.     }
  168.  
  169.     // Function to show custom modal
  170.     function showModal(message) {
  171.         modalMessage.textContent = message;
  172.         customModal.classList.add('show');
  173.     }
  174.  
  175.     // Function to hide custom modal
  176.     function hideModal() {
  177.         customModal.classList.remove('show');
  178.     }
  179.  
  180.     // Event listener for modal close button
  181.     modalCloseBtn.addEventListener('click', hideModal);
  182.  
  183.     // Function to send message to Gemini API
  184.     async function sendMessage() {
  185.         const userMessage = messageInput.value.trim();
  186.         if (userMessage === '') {
  187.             showModal('Please enter a message.');
  188.             return;
  189.         }
  190.  
  191.         displayMessage(userMessage, 'user');
  192.         chatHistory.push({ role: "user", parts: [{ text: userMessage }] });
  193.         messageInput.value = ''; // Clear input
  194.  
  195.         loadingSpinner.style.display = 'block'; // Show loading spinner
  196.         sendMessageBtn.disabled = true; // Disable send button
  197.  
  198.         try {
  199.             const generationConfig = {
  200.                 maxOutputTokens: 50
  201.             };
  202.  
  203.             const payload = {
  204.                 contents: chatHistory,
  205.                 generationConfig: generationConfig
  206.             };
  207.             // Use the GEMINI_API_KEY defined above
  208.             const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${GEMINI_API_KEY}`;
  209.  
  210.             const response = await fetch(apiUrl, {
  211.                 method: 'POST',
  212.                 headers: { 'Content-Type': 'application/json' },
  213.                 body: JSON.stringify(payload)
  214.             });
  215.  
  216.             if (!response.ok) {
  217.                 const errorData = await response.json();
  218.                 throw new Error(`API error: ${response.status} ${response.statusText} - ${errorData.error.message}`);
  219.             }
  220.  
  221.             const result = await response.json();
  222.  
  223.             if (result.candidates && result.candidates.length > 0 &&
  224.                 result.candidates[0].content && result.candidates[0].content.parts &&
  225.                 result.candidates[0].content.parts.length > 0) {
  226.                 const aiResponseText = result.candidates[0].content.parts[0].text;
  227.                 displayMessage(aiResponseText, 'ai');
  228.                 chatHistory.push({ role: "model", parts: [{ text: aiResponseText }] });
  229.             } else {
  230.                 showModal('Sorry, I could not get a response from Gemini. Please try again.');
  231.                 console.error('Unexpected API response structure:', result);
  232.             }
  233.         } catch (error) {
  234.             console.error('Error sending message to Gemini:', error);
  235.             showModal('There was a problem sending your message. Please try again later.');
  236.         } finally {
  237.             loadingSpinner.style.display = 'none'; // Hide loading spinner
  238.             sendMessageBtn.disabled = false; // Enable send button
  239.             chatMessages.scrollTop = chatMessages.scrollHeight; // Ensure scroll to bottom
  240.         }
  241.     }
  242.  
  243.     // Event listeners for sending messages
  244.     sendMessageBtn.addEventListener('click', sendMessage);
  245.     messageInput.addEventListener('keypress', (event) => {
  246.         if (event.key === 'Enter') {
  247.             sendMessage();
  248.         }
  249.     });
  250.  
  251.     chatMessages.scrollTop = chatMessages.scrollHeight; // Ensure scroll to bottom on load
  252. </script>
Advertisement
Add Comment
Please, Sign In to add comment