
In today’s privacy-conscious world, providing users with a clear and easy way to end a conversation is crucial for a great customer experience. When a user in a Salesforce Enhanced Chat (previously known as Messaging for Web) types “STOP” or “END,” they expect the conversation to terminate. However, by default, this doesn’t automatically close the session when engaged with Agentforce Agent. Instead, it transfers to the Human Agents.
This guide will show you how to use a simple JavaScript snippet to listen for opt-out keywords and automatically end the chat session, creating a more intuitive and user-friendly experience.
The Challenge: Lingering Chat Sessions
In Salesforce Enhanced Chat, when a user sends a message like “STOP” or “CANCEL,” the message is simply sent to the agent. The chat session remains active until the user or the agent manually closes the window. This can lead to confusion and frustration, as the user’s intent to opt out isn’t immediately acted upon. We need a way to programmatically detect these keywords and terminate the session on the user’s behalf.
The Solution: onEmbeddedMessageSent and clearSession()
Salesforce provides the tools we need right within the Embedded Service framework. The solution involves two key components:
onEmbeddedMessageSent: This is a JavaScript event that fires every time the end-user sends a message. We can create a “listener” for this event to inspect the message content before it’s officially sent.embeddedservice_bootstrap.userVerificationAPI.clearSession(): This powerful API function does exactly what its name implies—it ends the current chat session, clears the conversation from the user’s view, and resets the chat widget to its pre-chat state.
By combining these two, we can create a simple rule: listen for every message, check if it’s an opt-out keyword, and if it is, call the clearSession() function to end the conversation.
The Code: Putting It All Together
Here is the complete JavaScript snippet you’ll need.
window.addEventListener("onEmbeddedMessageSent", (event) => {
console.log("START:: Message Sent");
const tempContent = event.detail;
const {
conversationEntry
} = tempContent;
console.log('conversationEntry -->', JSON.stringify(conversationEntry));
if (conversationEntry?.sender?.role && conversationEntry.entryPayload) {
let strRole = conversationEntry.sender.role;
console.log('strRole =>', strRole);
let objEntryPayload = conversationEntry.entryPayload;
console.log('objEntryPayload =>', JSON.stringify(objEntryPayload));
if (strRole === 'EndUser' && objEntryPayload) {
try {
const entryPayloadObj = JSON.parse(objEntryPayload);
const result = entryPayloadObj?.abstractMessage?.staticContent?.text;
console.log('result -->', result);
const optOutKeywords = ['end', 'stop'];
if (optOutKeywords.includes(result.toLowerCase())) {
embeddedservice_bootstrap.userVerificationAPI
.clearSession()
.then(() => {
console.log("Session cleared successfully.");
})
.catch((error) => {
console.error("Error clearing session:", error);
})
.finally(() => {
console.log("clearSession finally.");
window.alert("You have opted out of Chat!!!");
});
}
} catch (e) {
console.error("Failed to parse entryPayload:", e);
}
}
}
console.log("END:: Message Sent");
});
Check the following image for reference for the JavaScript code snippet placement/position.

Salesforce Article:
https://developer.salesforce.com/docs/service/messaging-web/guide/event-listeners.html