File size: 3,533 Bytes
c4f4b3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<html>
<head>
  <title>URL-based Chatbot</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
</head>
<body>
  <div class="container">
  <h2> made by zeroxdesignart.com<h2>
    <h1>URL-based Chatbot</h1>
    <div id="chatContainer" class="card">
      <div class="card-body" id="chatBody">
        <div class="mb-3">
          <label for="urlInput" class="form-label">Enter URL:</label>
          <input type="text" class="form-control" id="urlInput">
        </div>
        <div id="chatMessages"></div>
        <div id="loadingIndicator" style="display: none;">
          <div class="spinner-border" role="status">
            <span class="visually-hidden">Loading...</span>
          </div>
        </div>
      </div>
      <div class="card-footer">
        <div class="input-group">
          <input type="text" class="form-control" id="messageInput">
          <button class="btn btn-primary" id="sendMessageButton">Send</button>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script>
  <script>
    const chatContainer = document.getElementById('chatContainer');
    const chatBody = document.getElementById('chatBody');
    const chatMessages = document.getElementById('chatMessages');
    const urlInput = document.getElementById('urlInput');
    const messageInput = document.getElementById('messageInput');
    const sendMessageButton = document.getElementById('sendMessageButton');
    const loadingIndicator = document.getElementById('loadingIndicator');

    const apiUrl = 'https://www.literallyanything.io/api/integrations/chatgpt';

    function addMessage(content, role) {
      const messageDiv = document.createElement('div');
      messageDiv.classList.add('alert', 'alert-primary');
      messageDiv.role = 'alert';
      messageDiv.textContent = content;
      messageDiv.style.wordWrap = 'break-word';

      if (role === 'assistant') {
        messageDiv.classList.add('text-end');
      }

      chatMessages.appendChild(messageDiv);
      chatBody.scrollTop = chatBody.scrollHeight;
    }

    async function sendMessage() {
      const url = urlInput.value.trim();
      const message = messageInput.value.trim();

      if (!url) {
        alert('Please enter a URL');
        return;
      }

      if (!message) {
        alert('Please enter a message');
        return;
      }

      addMessage(`User: ${message}`, 'user');
      messageInput.value = '';

      showLoadingIndicator();

      try {
        const response = await fetch(apiUrl, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            systemPrompt: `The user provided a URL: ${url}`,
            prompts: [
              { role: 'user', content: message }
            ]
          })
        });

        const data = await response.json();
        const assistantResponse = data.response;

        addMessage(`Assistant: ${assistantResponse}`, 'assistant');
      } catch (error) {
        console.error(error);
        alert('An error occurred. Please try again.');
      }

      hideLoadingIndicator();
    }

    function showLoadingIndicator() {
      loadingIndicator.style.display = 'block';
    }

    function hideLoadingIndicator() {
      loadingIndicator.style.display = 'none';
    }

    sendMessageButton.addEventListener('click', sendMessage);
  </script>
</body>
</html>