一、系统概述
本文将介绍一个基于MySQL数据库和简单PHP程序实现的网页客服关键词自动回复系统。这个方案适合中小型企业或个人开发者,无需复杂的人工智能技术,通过基本的数据库查询和字符串匹配就能实现自动回复功能。
演示网站:gofly.v1kf.com
二、数据库设计
首先创建MySQL数据库表存储关键词和对应回复:
CREATE DATABASE customer_service;
USE customer_service;
CREATE TABLE auto_replies (
id INT AUTO_INCREMENT PRIMARY KEY,
keyword VARCHAR(50) NOT NULL,
response TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
插入一些示例数据:
INSERT INTO auto_replies (keyword, response) VALUES
('退货', '我们的退货政策是7天无理由退货,详情请查看退货页面'),
('运费', '普通地区运费10元,满99元包邮'),
('客服', '正在为您转接人工客服,请稍候...');
三、PHP实现代码
1. 数据库连接文件(db_connect.php)
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'customer_service';
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
2. 关键词匹配处理(process_message.php)
<?php
require 'db_connect.php';
// 获取用户发送的消息
$userMessage = isset($_POST['message']) ? trim($_POST['message']) : '';
if (!empty($userMessage)) {
// 查询所有关键词
$sql = "SELECT keyword, response FROM auto_replies";
$result = $conn->query($sql);
$reply = "抱歉,我不理解您的问题。请尝试其他关键词或联系人工客服。";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// 简单关键词匹配(区分大小写)
if (strpos($userMessage, $row['keyword']) !== false) {
$reply = $row['response'];
break; // 找到第一个匹配的关键词就返回
}
}
}
echo $reply;
} else {
echo "请输入您的问题";
}
$conn->close();
?>
3. 简单的前端界面(index.html)
<!DOCTYPE html>
<html>
<head>
<title>简易客服系统</title>
<style>
#chat-box { height: 300px; border: 1px solid #ccc; overflow-y: scroll; padding: 10px; }
#message-input { width: 80%; padding: 8px; }
button { padding: 8px 15px; }
</style>
</head>
<body>
<h1>网页客服系统</h1>
<div id="chat-box"></div>
<input type="text" id="message-input" placeholder="请输入您的问题...">
<button onclick="sendMessage()">发送</button>
<script>
function sendMessage() {
const input = document.getElementById('message-input');
const message = input.value.trim();
if (message) {
// 显示用户消息
addMessage('user', message);
input.value = '';
// 发送到服务器处理
fetch('process_message.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'message=' + encodeURIComponent(message)
})
.then(response => response.text())
.then(reply => {
// 显示回复
addMessage('bot', reply);
});
}
}
function addMessage(sender, text) {
const chatBox = document.getElementById('chat-box');
const div = document.createElement('div');
div.innerHTML = `<strong>${sender === 'user' ? '您' : '客服'}:</strong> ${text}`;
chatBox.appendChild(div);
chatBox.scrollTop = chatBox.scrollHeight;
}
// 按Enter键发送
document.getElementById('message-input').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>
四、功能扩展建议
1. 改进关键词匹配
// 在process_message.php中改进匹配逻辑(不区分大小写)
if (stripos($userMessage, $row['keyword']) !== false) {
$reply = $row['response'];
break;
}
2. 添加同义词支持
修改数据库结构:
ALTER TABLE auto_replies ADD COLUMN synonyms TEXT;
更新查询逻辑:
// 查询时检查同义词
$keywords = explode(',', $row['keyword'] . ',' . $row['synonyms']);
foreach ($keywords as $kw) {
if (stripos($userMessage, trim($kw)) !== false) {
$reply = $row['response'];
break 2; // 跳出两层循环
}
}
3. 添加管理后台
简单的管理界面(admin.php):
<?php
require 'db_connect.php';
// 添加新关键词
if (isset($_POST['add'])) {
$keyword = $conn->real_escape_string($_POST['keyword']);
$response = $conn->real_escape_string($_POST['response']);
$sql = "INSERT INTO auto_replies (keyword, response) VALUES ('$keyword', '$response')";
$conn->query($sql);
}
// 删除关键词
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
$sql = "DELETE FROM auto_replies WHERE id = $id";
$conn->query($sql);
}
// 获取所有自动回复规则
$sql = "SELECT * FROM auto_replies";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>客服自动回复管理</title>
</head>
<body>
<h1>自动回复管理</h1>
<form method="post">
<input type="text" name="keyword" placeholder="关键词" required>
<textarea name="response" placeholder="回复内容" required></textarea>
<button type="submit" name="add">添加规则</button>
</form>
<h2>现有规则</h2>
<table border="1">
<tr>
<th>ID</th>
<th>关键词</th>
<th>回复内容</th>
<th>操作</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= htmlspecialchars($row['keyword']) ?></td>
<td><?= htmlspecialchars($row['response']) ?></td>
<td><a href="?delete=<?= $row['id'] ?>">删除</a></td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
<?php $conn->close(); ?>
五、部署说明
- 将上述文件上传到支持PHP的Web服务器
- 创建MySQL数据库并导入SQL结构
- 根据需要修改db_connect.php中的数据库连接信息
- 通过index.html访问客服界面
- 通过admin.php管理关键词和回复
六、系统优化方向
- 添加用户会话管理:使用session或cookie记录对话历史
- 实现多关键词匹配:找出消息中所有匹配的关键词,组合回复
- 添加模糊匹配:使用LIKE或全文索引提高匹配灵活性
- 记录对话日志:创建新表记录用户问题和系统回复
- 添加转人工功能:当自动回复不满足时转接人工客服
这个简易系统虽然功能基础,但已经能够满足许多场景下的自动客服需求,后续可以根据实际业务需求逐步扩展功能。