文章摘要
文章介绍了如何通过 HTML 和 JavaScript 构建一个模拟微信对话框的聊天程序。程序可预设关键词及对应的回复内容,支持文字、图片和视频三种格式,并可扩展多个关键词。改进版增加了用户和机器人头像与昵称显示,视觉效果更佳,同时取消了部分关键词提示,使交互更自然。其中包括完整的前端代码实现,涵盖样式设置(CSS)、消息发送逻辑、关键词匹配及其多媒体响应处理,并支持回车键发送消息,提升了用户体验。
— 文章部分摘要由DeepSeek深度思考而成
其实,很简单,只需要输入文字,AI 就给我发了。(只需要你的描述 ai 听得懂)
程序功能:
源码内自定义 “预设词” 和回复预设词的内容。
支持文字,图片,视频。三类。
可预设多个。。
复制源码到下方测试。
图片演示:
![图片[1]|聊天预设源码|不死鸟资源网](https://busi.net/wp-content/uploads/2025/06/20250605112550919-image-546x1024.png)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>微信对话框模式</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
width: 100%;
max-width: 400px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.chat-box {
height: 600px;
overflow-y: auto;
padding: 10px;
border-bottom: 1px solid #ddd;
}
.chat-box .message {
margin-bottom: 10px;
display: flex;
align-items: flex-end;
}
.chat-box .message.user {
justify-content: flex-end;
}
.chat-box .message.bot {
justify-content: flex-start;
}
.chat-box .message .bubble {
padding: 8px 12px;
border-radius: 15px;
max-width: 70%;
word-wrap: break-word;
}
.chat-box .message.user .bubble {
background-color: #1aad19;
color: #fff;
}
.chat-box .message.bot .bubble {
background-color: #f0f0f0;
color: #000;
}
.chat-box .message img {
max-width: 100%;
height: auto;
border-radius: 5px;
}
.chat-box .message video {
max-width: 100%;
height: auto;
border-radius: 5px;
}
.input-box {
padding: 10px;
}
.input-box input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
margin-bottom: 10px;
}
.input-box button {
width: 100%;
padding: 10px;
background-color: #1aad19;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.input-box button:hover {
background-color: #168a14;
}
</style>
</head>
<body>
<div class="container">
<div class="chat-box" id="chatBox">
<!-- 聊天内容将在这里显示 -->
</div>
<div class="input-box">
<input type="text" id="userInput" placeholder="请输入消息...">
<button onclick="sendMessage()">发送</button>
</div>
</div>
<script>
const chatBox = document.getElementById('chatBox');
const userInput = document.getElementById('userInput');
const keywords = {
'图片': {
type: 'image',
content: 'https://img2.baidu.com/it/u=1095762139,1355893018&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800' // 替换为实际图片路径
},
'文字': {
type: 'text',
content: '小归客最帅。'
},
'视频': {
type: 'video',
content: 'https://v.nrzj.vip/video.php' // 替换为实际视频路径
},
'关键词4': {
type: 'image',
content: 'images/image2.jpg' // 替换为实际图片路径
},
// 添加更多关键词和回复内容
};
function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
// 显示用户消息
const userMessage = document.createElement('div');
userMessage.className = 'message user';
const userBubble = document.createElement('div');
userBubble.className = 'bubble';
userBubble.textContent = message;
userMessage.appendChild(userBubble);
chatBox.appendChild(userMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
// 处理关键词回复
if (keywords[message]) {
const botMessage = document.createElement('div');
botMessage.className = 'message bot';
const botBubble = document.createElement('div');
botBubble.className = 'bubble';
if (keywords[message].type === 'image') {
const img = document.createElement('img');
img.src = keywords[message].content;
botBubble.appendChild(img);
} else if (keywords[message].type === 'text') {
botBubble.textContent = keywords[message].content;
} else if (keywords[message].type === 'video') {
const video = document.createElement('video');
video.src = keywords[message].content;
video.controls = true;
botBubble.appendChild(video);
}
botMessage.appendChild(botBubble);
chatBox.appendChild(botMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
} else {
const botMessage = document.createElement('div');
botMessage.className = 'message bot';
const botBubble = document.createElement('div');
botBubble.className = 'bubble';
botBubble.textContent = '抱歉,我不明白您的意思。';
botMessage.appendChild(botBubble);
chatBox.appendChild(botMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
}
// 清空输入框
userInput.value = '';
}
// 按下回车键发送消息
userInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>
改进版:
增加了头像,名字。看起来更好看了。使用视频接口,每发一次“视频”都不一样了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>微信对话框模式</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 80vh;
background-color: #f0f0f0;
}
.container {
width: 100%;
max-width: 800px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.code-container {
position: relative;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
overflow-x: auto;
}
.chat-box {
height: 400px;
overflow-y: auto;
padding: 10px;
border-bottom: 1px solid #ddd;
}
.chat-box .message {
margin-bottom: 10px;
display: flex;
flex-direction: column; /* 垂直排列 */
align-items: flex-start; /* 靠左对齐 */
}
.chat-box .message.user {
align-items: flex-end; /* 靠右对齐 */
}
.chat-box .message .avatar {
width: 40px;
height: 40px;
border-radius: 50%;
margin-bottom: 5px; /* 头像与气泡的间距 */
}
.chat-box .message .bubble {
padding: 8px 12px;
border-radius: 15px;
max-width: 70%;
word-wrap: break-word;
margin-top: 5px; /* 气泡与昵称的间距 */
}
.chat-box .message.user .bubble {
background-color: #1aad19;
color: #fff;
}
.chat-box .message.bot .bubble {
background-color: #f0f0f0;
color: #000;
}
.chat-box .message img {
max-width: 100%;
height: auto;
border-radius: 5px;
}
.chat-box .message video {
max-width: 100%;
height: auto;
border-radius: 5px;
}
.chat-box .message .nickname {
font-size: 12px;
color: #666;
margin-bottom: 5px; /* 昵称与气泡的间距 */
}
.input-box {
padding: 10px;
}
.input-box input[type="text"] {
width: 94%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
margin-bottom: 10px;
}
.input-box button {
width: 100%;
padding: 10px;
background-color: #1aad19;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.input-box button:hover {
background-color: #168a14;
}
</style>
</head>
<body>
<div class="container">
<div class="chat-box" id="chatBox">
<!-- 聊天内容将在这里显示 -->
</div>
<div class="code-container">
请不要发送“图片”,“视频”,“大佬论坛”等关键字
</div>
<div class="input-box">
<input type="text" id="userInput" placeholder="请输入消息...">
<button onclick="sendMessage()">发送</button>
</div>
</div>
<script>
const chatBox = document.getElementById('chatBox');
const userInput = document.getElementById('userInput');
const keywords = {
'图片': {
type: 'image',
content: 'https://img1.baidu.com/it/u=1483323185,583750794&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500' // 替换为实际图片路径
},
'大佬论坛': {
type: 'text',
content: '请访问https://www.dalao.net'
},
'视频': {
type: 'video',
content: 'http://api.yujn.cn/api/zzxjj.php' // 替换为实际视频路径
},
'关键词4': {
type: 'image',
content: 'images/image2.jpg' // 替换为实际图片路径
},
// 添加更多关键词和回复内容
};
const userAvatar = 'https://img0.baidu.com/it/u=375650515,2207803266&fm=253&fmt=auto&app=120&f=JPEG?w=332&h=332'; // 用户头像路径
const botAvatar = 'https://img1.baidu.com/it/u=1638917562,1246152123&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'; // 机器人头像路径
const userName = '游客用户'; // 用户昵称
const botName = '智障机器人'; // 机器人昵称
function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
// 显示用户消息
const userMessage = document.createElement('div');
userMessage.className = 'message user';
const userAvatarImg = document.createElement('img');
userAvatarImg.src = userAvatar;
userAvatarImg.className = 'avatar';
const userNickname = document.createElement('div');
userNickname.className = 'nickname';
userNickname.textContent = userName;
const userBubble = document.createElement('div');
userBubble.className = 'bubble';
userBubble.textContent = message;
userMessage.appendChild(userAvatarImg);
userMessage.appendChild(userNickname);
userMessage.appendChild(userBubble);
chatBox.appendChild(userMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
// 处理关键词回复
if (keywords[message]) {
const botMessage = document.createElement('div');
botMessage.className = 'message bot';
const botAvatarImg = document.createElement('img');
botAvatarImg.src = botAvatar;
botAvatarImg.className = 'avatar';
const botNickname = document.createElement('div');
botNickname.className = 'nickname';
botNickname.textContent = botName;
const botBubble = document.createElement('div');
botBubble.className = 'bubble';
if (keywords[message].type === 'image') {
const img = document.createElement('img');
img.src = keywords[message].content;
botBubble.appendChild(img);
} else if (keywords[message].type === 'text') {
botBubble.textContent = keywords[message].content;
} else if (keywords[message].type === 'video') {
const video = document.createElement('video');
video.src = keywords[message].content;
video.controls = true;
botBubble.appendChild(video);
}
botMessage.appendChild(botAvatarImg);
botMessage.appendChild(botNickname);
botMessage.appendChild(botBubble);
chatBox.appendChild(botMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
} else {
const botMessage = document.createElement('div');
botMessage.className = 'message bot';
const botAvatarImg = document.createElement('img');
botAvatarImg.src = botAvatar;
botAvatarImg.className = 'avatar';
const botNickname = document.createElement('div');
botNickname.className = 'nickname';
botNickname.textContent = botName;
const botBubble = document.createElement('div');
botBubble.className = 'bubble';
botBubble.textContent = '抱歉,我不明白您的意思。';
botMessage.appendChild(botAvatarImg);
botMessage.appendChild(botNickname);
botMessage.appendChild(botBubble);
chatBox.appendChild(botMessage);
// 滚动到聊天框底部
chatBox.scrollTop = chatBox.scrollHeight;
}
// 清空输入框
userInput.value = '';
}
// 按下回车键发送消息
userInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>
本站文章部分内容可能来源于网络,仅供大家学习参考,如有侵权,请联系站长📧cutwork@qq.com进行删除处理!
THE END