From 78d64562a0c6c3d248c454a9a77338e5d1c08d2a Mon Sep 17 00:00:00 2001 From: CXM <chenzixin202209@163.com> Date: Thu, 21 Mar 2024 16:36:32 +0000 Subject: [PATCH] create chatbot manage page,and connect to admin page, update chatbot function. --- Admin/QA_manage.php | 215 +++++++++++++++++++++++++++++++++++ Admin/admin_page.php | 2 +- Presentation/chatbotpage.php | 40 ++++--- 3 files changed, 241 insertions(+), 16 deletions(-) create mode 100644 Admin/QA_manage.php diff --git a/Admin/QA_manage.php b/Admin/QA_manage.php new file mode 100644 index 0000000..a4d7b64 --- /dev/null +++ b/Admin/QA_manage.php @@ -0,0 +1,215 @@ +<?php +include_once '../Logic/config.php'; +// 准备SQL查询 +$stmt = DB->prepare("SELECT * FROM responses"); +$stmt->execute(); +$responses = $stmt->fetchAll(PDO::FETCH_ASSOC); + + +function editMessageInfo($data, $id) +{ +// 初始化SQL语句的基础部分 + $sql = "UPDATE responses SET "; + $params = []; + $updates = []; + + // 遍历$data数组,只添加非空字段到SQL更新语句中 + foreach ($data as $key => $value) { + // 确保只更新存在的字段 + if (!empty($value) && in_array($key, ['pattern', 'response'])) { + $updates[] = "`$key` = ?"; + $params[] = $value; + } + } + // 如果没有需要更新的数据,则直接返回 + if (empty($updates)) { + return false; + } + + // 将所有更新拼接到基础SQL语句后面 + $sql .= implode(', ', $updates); + $sql .= " WHERE id = ?"; + $params[] = $id; + + // 准备和执行SQL语句 + $stmt = DB->prepare($sql); + return $stmt->execute($params); +} +function addMessage($pattern, $response) { + $sql = "INSERT INTO responses(pattern, response) VALUES (?, ?)"; + $stmt = DB->prepare($sql); + $found = true; + return $stmt->execute([$pattern, $response]); +} +function deleteMessage($id) { + $sql = "DELETE FROM responses WHERE id = ?"; + $stmt = DB->prepare($sql); + return $stmt->execute([$id]); +} + + +// 对表单进行处理 +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $id = isset($_POST['id']) ? $_POST['id'] : null; + // 处理表单提交的数据 + if ($_POST["formType"] == "editMessageForm") { + $data = [ + 'pattern' => $_POST['keyword'], + 'response' => $_POST['answer'] + ]; + editMessageInfo($data, $id); + echo "<script>window.location.href ='../Admin/QA_manage.php';</script>"; + } elseif ($_POST["formType"] == "addMessageForm") { + $pattern = $_POST['keyword']; + $response = $_POST['answer']; + addMessage($pattern, $response); + echo "<script>window.location.href ='../Admin/QA_manage.php';</script>"; + } elseif ($_POST["formType"] == "delMessageForm") { + deleteMessage($id); + echo "6"; + echo "<script>window.location.href ='../Admin/QA_manage.php';</script>"; + } +} +?> +<!DOCTYPE html> +<html lang="zh-CN"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Responses Management</title> + <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> +</head> +<body class="bg-gray-100"> +<div class="container mx-auto p-4"> + <table class="min-w-full max-w-4xl mx-auto border-collapse"> + <thead class="bg-gray-800 text-white"> + <tr class="border-b"> + <th class="px-4 py-2 text-left rounded-tl-lg">Keyword</th> + <th class="px-4 py-2 text-left">Answers</th> + <th class="px-4 py-2 text-left rounded-tr-lg">Operations</th> + </tr> + </thead> + <tbody> + <?php foreach ($responses as $response) { ?> + <tr class="border-b"> + <td class="px-4 py-2"><?php echo htmlspecialchars($response['pattern']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($response['response']) ?></td> + <td class="px-4 py-2 text-center"> + <!-- 编辑按钮 --> + <button class="edit-btn text-blue-500 hover:text-blue-700 mx-2" + data-id="<?php echo htmlspecialchars($response['id']) ?>" + data-type="edit"> + <i class="fas fa-edit"></i> + </button> + <!-- 删除按钮 --> + <form action="../Admin/QA_manage.php" method="post" class="inline-block"> + <input type="hidden" name="id" + value="<?php echo htmlspecialchars($response['id']) ?>"> + <input type="hidden" name="formType" value="delMessageForm"> + <button type="submit" class="text-red-500 hover:text-red-700 mx-2"> + <i class="fas fa-trash-alt"></i> + </button> + </form> + </td> + </tr> + <?php } ?> + <!-- 增加按钮 --> + <tr class="border-b"> + <td colspan="6" class="px-4 py-2 text-center"> + <button class="add-btn text-green-500 hover:text-green-700 mx-2" data-type="add"> + <i class="fas fa-plus-square"></i> + </button> + </td> + </tr> + </tbody> + </table> +</div> + +<!-- 弹出框,初始时隐藏 --> +<div id="modal" + class="hidden fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full flex items-center justify-center"> + <div class="bg-white p-8 rounded-lg shadow-lg max-w-lg w-full mx-auto" id="modal-content"> + <!-- 弹出框内容 --> + </div> +</div> + +<div class="fixed bottom-0 right-0 p-4"> + <button onclick="window.location.reload()" + class="px-4 py-2 bg-gray-800 text-white rounded-md shadow-md hover:bg-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50"> + <i class="fas fa-sync-alt mr-2"></i> + </button> +</div> + +<script> + document.addEventListener("DOMContentLoaded", () => { + const editButtons = document.querySelectorAll('.edit-btn'); + const addButtons = document.querySelectorAll('.add-btn'); + const modal = document.getElementById('modal'); + const modalContent = document.getElementById('modal-content'); + + // 编辑按钮点击事件 + editButtons.forEach(button => { + button.addEventListener('click', function () { + const id = this.getAttribute('data-id'); + + modalContent.innerHTML = ` + <h2 class="text-lg font-bold mb-4">Edit Message Info</h2> + <form id="editMessageForm" class="space-y-4" action="../Admin/QA_manage.php" method="post"> + <input type="hidden" name="id" value="${id}"> + <input type="hidden" name="formType" value="editMessageForm"> + <div> + <label class="block text-sm font-medium text-gray-700" for="account">Keyword (use "|" to seperate keyword)</label> + <input type="text" name="keyword" id="keyword" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="password">Answers</label> + <input type="text" name="answer" id="answer" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div class="flex justify-end"> + <button type="submit" class="inline-flex items-center px-4 py-2 bg-blue-500 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 active:bg-blue-900 focus:outline-none focus:border-blue-900 focus:ring focus:ring-blue-300 disabled:opacity-25 transition"> + Submit + </button> + </div> + </form> + `; + modal.classList.remove('hidden'); + }); + }); + + // 增加按钮点击事件 + addButtons.forEach(button => { + button.addEventListener('click', function () { + modalContent.innerHTML = ` + <h2 class="text-lg font-bold mb-4">Add Message</h2> + <form id="addMessageForm" class="space-y-4" action="../Admin/QA_manage.php" method="post"> + <input type="hidden" name="formType" value="addMessageForm"> + <div> + <label class="block text-sm font-medium text-gray-700" for="account">Keyword (use "|" to seperate keyword)</label> + <input type="text" name="keyword" id="keyword" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="password">Answers</label> + <input type="text" name="answer" id="answer" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div class="flex justify-end"> + <button type="submit" class="inline-flex items-center px-4 py-2 bg-blue-500 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 active:bg-blue-900 focus:outline-none focus:border-blue-900 focus:ring focus:ring-blue-300 disabled:opacity-25 transition"> + Submit + </button> + </div> + </form> + `; + modal.classList.remove('hidden'); + }); + }); + + // 点击模态框外区域关闭模态框 + modal.addEventListener('click', function (e) { + if (e.target === modal) { + modal.classList.add('hidden'); + } + }); + }); +</script> +</body> +</html> diff --git a/Admin/admin_page.php b/Admin/admin_page.php index 431409a..93930df 100644 --- a/Admin/admin_page.php +++ b/Admin/admin_page.php @@ -25,7 +25,7 @@ if (!isset($_SESSION['user_type'])) { <li><a href="#" data-src="uni_manage.php" class="block px-5 py-3 hover:bg-gray-700">Manage uni Map</a></li> <li><a href="#" data-src="user_manage.php" class="block px-5 py-3 hover:bg-gray-700">Manage user detail</a></li> <li><a href="#" data-src="brs_manage.php" class="block px-5 py-3 hover:bg-gray-700">Manage Bristol Map</a></li> - <li><a href="#" data-src="manageQA.php" class="block px-5 py-3 hover:bg-gray-700">Question and answer management</a></li> + <li><a href="#" data-src="QA_manage.php" class="block px-5 py-3 hover:bg-gray-700">Question and answer management</a></li> </ul> </div> <!-- 主内容区 --> diff --git a/Presentation/chatbotpage.php b/Presentation/chatbotpage.php index e591367..a14401c 100644 --- a/Presentation/chatbotpage.php +++ b/Presentation/chatbotpage.php @@ -1,28 +1,38 @@ <?php session_start(); // 开启会话 -require_once '../Logic/responses.php'; // 引入含有预定义回答的文件 +require_once '../Logic/config.php'; require_once "../Logic/ChatbotConfig.php"; // 引入API文件 -// 假设这是 index.php -if ($_SERVER["REQUEST_METHOD"] == "POST") { - // 获取用户输入 - $userInput = $_POST['userInput']; - // 加载预定义的回答 - $predefinedResponses = include '../Logic/responses.php'; +function getPredefinedResponse($userInput) { + $response = false; - // 初始化找到的回答为 false - $foundResponse = false; - $response = ''; - // 使用正则表达式检查用户输入是否匹配预定义的模式 - foreach ($predefinedResponses as $pattern => $predefinedResponse) { + // 查询数据库获取所有预定义的回答 + $stmt = DB->prepare("SELECT * FROM responses"); + $stmt->execute(); + $responses = $stmt->fetchAll(PDO::FETCH_ASSOC); + + // 遍历预定义的回答 + foreach ($responses as $predefinedResponse) { + $pattern = "/" . $predefinedResponse['pattern'] . "/i"; // 构造正则表达式 + // 检查是否匹配 if (preg_match($pattern, $userInput)) { - $foundResponse = true; - $response = $predefinedResponse; + $response = $predefinedResponse['response']; break; // 找到匹配后退出循环 } } + + return $response; +} + +// 假设这是 index.php +if ($_SERVER["REQUEST_METHOD"] == "POST") { + // 获取用户输入 + $userInput = $_POST['userInput']; + // 尝试从数据库中找到预定义的回答 + $response = getPredefinedResponse($userInput); + // 如果没有找到预定义的回答,调用API函数 - if (!$foundResponse) { + if (!$response) { $response = gpt35Api([['role' => 'user', 'content' => $userInput]]); } -- GitLab