My workflow asks users questions to determine their interest in one of 3 areas of an app. The AI Agent is instructed to ask a few questions until it knows exactly what their interested in then route them to proper sub-workflow. Here is the hangup:
If the initial response by the user is not clear enough for the AI Agent to determine what the user is interested in, then the AI Agent will generate a question to ask for clarity. When I test by answering the initial question with an irrelevant question such as “Hi there” the AI Agent generates a question for the user to clarify what their current interest is. The question is then sent to the Respond to Webhook for output to the HTML/Javascript doc to illicit the chat response from the user. The question successfully displays in the chat. Here is where the problem occurs in both of 2 scenarios -
-
I have tested this with just the Respond to Webhook (without the Wait node) an the workflow does not wait for a response, it will continue on its own, resulting in a false route through the Switch node.
-
Due to scenario 1, I added a Wait node after the Respond to Webhook node. That seemed to stop the workflow from automatically cycling through again without waiting for the user response in chat. This presented a another problem; Now the workflow waits, but the user response triggers built in error in the HTML/Javascript doc.
Pertinent information:
N8N Cloud version 1.76.2
HTML/Javascript doc is hosted on VPS.
Workflow pasted below
It looks like your topic is missing some important information. Could you provide the following if applicable.
- n8n version:
- Database (default: SQLite):
- n8n EXECUTIONS_PROCESS setting (default: own, main):
- Running n8n via (Docker, npm, n8n cloud, desktop app):
- Operating system:
In case it is needed, here is the HTML/Javascript doc code:
<body>
<div class="container">
<h1>MarketHive</h1>
<div class="chat-container" id="chatContainer">
</div>
<div class="input-container">
<input type="text" id="userInput" placeholder="Type your message..." autocomplete="off">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
const WEBHOOK_URL = '[ insert webhook URL Here]';
const chatContainer = document.getElementById('chatContainer');
const userInput = document.getElementById('userInput');
// User data
const userData = {
name: "Jake",
userID: "274828"
};
// Track the current webhook URL
let currentWebhookUrl = WEBHOOK_URL;
// Add initial bot message
addMessage(`Hi ${userData.name}, What can I help you with today; learning, trading, or investing?`, 'bot');
function addMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', `${sender}-message`);
messageDiv.textContent = text;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
async function sendMessage() {
const message = userInput.value.trim();
if (!message) return;
addMessage(message, 'user');
userInput.value = '';
try {
const response = await fetch(currentWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: message,
name: userData.name,
userID: userData.userID
})
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const rawText = await response.text();
console.log('Raw response:', rawText);
try {
const responseData = JSON.parse(rawText);
console.log('Parsed response:', responseData);
// Check for Wait node's webhook URL
if (responseData.resumeUrl) {
currentWebhookUrl = responseData.resumeUrl;
console.log('Switching to resume URL:', currentWebhookUrl);
} else {
// Reset to initial webhook if no resume URL
currentWebhookUrl = WEBHOOK_URL;
}
if (responseData.question) {
addMessage(responseData.question, 'bot');
} else if (responseData.answer) {
addMessage(responseData.answer, 'bot');
} else {
console.log('Response format:', responseData);
addMessage("Unexpected response format", 'bot');
}
} catch (e) {
console.error('JSON parse error:', e);
addMessage("Error parsing response", 'bot');
}
} catch (error) {
console.error('Error:', error);
addMessage('Sorry, there was an error processing your message.', 'bot');
}
}
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>