如何将Dialogflow与网站自定义悬浮聊天框集成
嘿,这个需求我之前做过好几次,其实核心思路就是绕过Dialogflow默认的Widget样式,用自定义UI接管交互,同时调用它的底层API来处理消息通信,下面给你一步步拆解实现方法:
1. 先隐藏Dialogflow默认Widget
首先我们不需要默认的聊天界面,所以引入Dialogflow组件时要把它隐藏起来,只保留核心的通信能力:
<!-- 引入Dialogflow核心脚本 --> <script src="https://www.gstatic.com/dialogflow-console/fast/messenger/bootstrap.js?v=1"></script> <!-- 隐藏默认的聊天组件,只用来初始化会话 --> <df-messenger id="df-messenger" intent="WELCOME" chat-title="CustomChatbot" agent-id="你的AgentID" language-code="zh-CN" style="display: none;" ></df-messenger>
2. 构建自定义悬浮聊天框UI
用HTML+CSS实现你想要的悬浮样式,这里给一个基础示例(你可以完全按照自己的设计调整):
<!-- 悬浮触发按钮 --> <div class="chat-toggle" id="chatToggle">💬</div> <!-- 聊天窗口容器 --> <div class="chat-window" id="chatWindow"> <div class="chat-header"> <span>我的智能助手</span> <button class="close-btn" id="closeChat">×</button> </div> <div class="chat-messages" id="chatMessages"></div> <div class="chat-input"> <input type="text" id="userInput" placeholder="输入你的问题..."> <button id="sendBtn">发送</button> </div> </div>
对应的CSS样式:
/* 悬浮触发按钮 */ .chat-toggle { position: fixed; bottom: 20px; right: 20px; width: 60px; height: 60px; border-radius: 50%; background: #2563eb; color: white; display: flex; align-items: center; justify-content: center; font-size: 24px; cursor: pointer; box-shadow: 0 2px 12px rgba(0,0,0,0.15); transition: background 0.3s; } .chat-toggle:hover { background: #1d4ed8; } /* 聊天窗口 */ .chat-window { position: fixed; bottom: 90px; right: 20px; width: 360px; height: 520px; background: white; border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,0.12); display: none; flex-direction: column; overflow: hidden; } /* 聊天头部 */ .chat-header { padding: 16px; background: #2563eb; color: white; display: flex; justify-content: space-between; align-items: center; } .close-btn { background: transparent; border: none; color: white; font-size: 20px; cursor: pointer; } /* 消息列表 */ .chat-messages { flex: 1; padding: 16px; overflow-y: auto; background: #f8fafc; } /* 输入区域 */ .chat-input { display: flex; padding: 12px; border-top: 1px solid #e2e8f0; } .chat-input input { flex: 1; padding: 10px; border: 1px solid #cbd5e1; border-radius: 8px 0 0 8px; outline: none; } .chat-input button { padding: 10px 20px; background: #2563eb; color: white; border: none; border-radius: 0 8px 8px 0; cursor: pointer; transition: background 0.3s; } .chat-input button:hover { background: #1d4ed8; } /* 消息气泡样式 */ .user-message { background: #2563eb; color: white; padding: 10px 14px; border-radius: 16px 16px 0 16px; margin: 8px 0; max-width: 75%; align-self: flex-end; } .bot-message { background: white; color: #1e293b; padding: 10px 14px; border-radius: 16px 16px 16px 0; margin: 8px 0; max-width: 75%; align-self: flex-start; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
3. 用JS连接自定义UI和Dialogflow
这一步是核心,我们要监听自定义输入的发送事件,把消息传给Dialogflow,再把返回的消息渲染到自定义聊天框:
// 获取DOM元素 const chatToggle = document.getElementById('chatToggle'); const chatWindow = document.getElementById('chatWindow'); const closeChat = document.getElementById('closeChat'); const chatMessages = document.getElementById('chatMessages'); const userInput = document.getElementById('userInput'); const sendBtn = document.getElementById('sendBtn'); // 切换聊天窗口显示/隐藏 chatToggle.addEventListener('click', () => { chatWindow.style.display = chatWindow.style.display === 'flex' ? 'none' : 'flex'; }); closeChat.addEventListener('click', () => { chatWindow.style.display = 'none'; }); // 发送消息到Dialogflow function sendToDialogflow(text) { const dfInstance = document.getElementById('df-messenger'); dfInstance.sendMessage(text); } // 添加消息到聊天框的辅助函数 function addMessage(text, sender) { const messageDiv = document.createElement('div'); messageDiv.className = sender === 'user' ? 'user-message' : 'bot-message'; messageDiv.textContent = text; chatMessages.appendChild(messageDiv); // 自动滚动到底部 chatMessages.scrollTop = chatMessages.scrollHeight; } // 处理用户输入 function handleUserInput() { const text = userInput.value.trim(); if (!text) return; // 显示用户消息 addMessage(text, 'user'); userInput.value = ''; // 发送给Dialogflow sendToDialogflow(text); } // 绑定发送事件 sendBtn.addEventListener('click', handleUserInput); userInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') handleUserInput(); }); // 监听Dialogflow的回复事件 document.addEventListener('df-response', (event) => { const botReply = event.detail.response.text; addMessage(botReply, 'bot'); }); // 监听欢迎消息(可选,初始化时自动发送欢迎语) document.addEventListener('df-welcome', (event) => { const welcomeText = event.detail.response.text; addMessage(welcomeText, 'bot'); });
4. 额外优化小技巧
- 加载状态:发送消息后可以添加一个“正在输入...”的提示,直到收到回复再移除,提升体验。
- 动画效果:给聊天窗口的显示/隐藏添加
transition过渡动画,让交互更流畅。 - 移动端适配:在小屏幕上把聊天窗口宽度设为90%,避免超出屏幕范围。
- 上下文维护:Dialogflow的Web Client会自动维护会话上下文,所以多轮对话不用额外处理状态。
内容的提问来源于stack exchange,提问作者user9463688
相关产品推荐
相关产品推荐

