Coder Social home page Coder Social logo

jubilant-train's Introduction

<!DOCTYPE html>
<html>
<head>
  <title>私人聊天机器人</title>
  <style>
    .chatbox {
      width: 300px;
      height: 400px;
      border: 1px solid #ccc;
      padding: 10px;
      overflow-y: scroll;
    }
    .user-message {
      color: blue;
    }
    .bot-message {
      color: green;
    }
    input[type="text"] {
      width: 100%;
      padding: 5px;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div id="chat" class="chatbox"></div>
  <input id="user-input" type="text" placeholder="输入消息" autofocus="true" />
  
  <script>
    // 创建聊天框架
    function createChatFrame() {
      const chatbox = document.getElementById('chat');
      const userInput = document.getElementById('user-input');
      userInput.addEventListener('keydown', handleUserInput);

      function handleUserInput(event) {
        if (event.key === 'Enter') {
          const userMessage = userInput.value;
          addMessage(userMessage, 'user-message');
          processUserInput(userMessage);
          userInput.value = '';
        }
      }

      function processUserInput(userMessage) {
        // 发送用户输入到后端服务器
        sendUserMessageToBackend(userMessage)
          .then(botMessage => {
            addMessage(botMessage, 'bot-message');
          })
          .catch(error => {
            console.error('发送用户消息时出错:', error);
            addMessage('抱歉,发送消息时出现了错误', 'bot-message');
          });
      }

      function sendUserMessageToBackend(userMessage) {
        return new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          const endpointUrl = 'YOUR_BACKEND_ENDPOINT_URL';  // 替换为你的后端服务器的URL

          xhr.open('POST', endpointUrl);
          xhr.setRequestHeader('Content-Type', 'application/json');

          xhr.onload = function() {
            if (xhr.status === 200) {
              const response = JSON.parse(xhr.responseText);
              resolve(response.botMessage);
            } else {
              reject('无法从后端服务器获取响应');
            }
          };

          xhr.onerror = function() {
            reject('请求发送失败');
          };

          const requestBody = JSON.stringify({ userMessage });
          xhr.send(requestBody);
        });
      }

      function addMessage(message, className) {
        const messageElement = document.createElement('div');
        messageElement.textContent = message;
        messageElement.classList.add(className);
        chatbox.appendChild(messageElement);
        chatbox.scrollTop = chatbox.scrollHeight;
      }
    }
    
    createChatFrame();
  </script>
</body>
</html>

jubilant-train's People

Contributors

lm9958 avatar

Stargazers

 avatar

Watchers

 avatar

jubilant-train's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.