Redirect based on specific Salesforce Messaging for Web Message

Redirect based on specific Salesforce Messaging for Web Message

onEmbeddedMessageSent event can be listened to check whether a specific message was sent. The event payload contains more useful information like the exact message, who sent the message(agent, user, etc), identifier and so on. window.open() JavaScript method can be used to redirect the user to a different page when a certain or specific message is sent.

window.addEventListener() method can be used to listen to the onEmbeddedMessageSent event from Salesforce Messaging for Web.

In this Blog Post, I have redirected the users to my blog whenever agent sends a message which contains the ‘Redirect to Payment Page:’ text.

Event Listener Code:

You can use the following code to listen to the onEmbeddedMessageSent event for your reference.

window.addEventListener( "onEmbeddedMessageSent", ( event ) => {

	console.log( "START:: Message Sent" );
	console.log( "Event detail: ", JSON.stringify( event.detail ) );
		
	let tempContent = event.detail;

		if ( tempContent.conversationEntry.sender.role ) {

		let strRole = tempContent.conversationEntry.sender.role;
		console.log( 'strRole =>', strRole );

		if ( strRole == 'Agent' ) {

			if ( tempContent.conversationEntry.entryPayload ) {
			
				tempContent = JSON.parse( tempContent.conversationEntry.entryPayload );
			
				if ( tempContent.abstractMessage.staticContent.text ) {
					
					let strMessage = tempContent.abstractMessage.staticContent.text;
					console.log( strMessage );
					
					if ( strMessage.includes( 'Redirect to Payment Page:' ) ) {
			
						window.open( 'https://www.infallibletechie.com', '_self' );
			
					}
				}
			
			}

		}

	}

	console.log( "END:: Message Sent" );

} );

You should add the above code along with the Embedded Service Deployment code snippet.

Salesforce Article:

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

Leave a Reply