Send Message as the End User in Enhanced Web Chat
Send search queries, user actions, or any text to the Agentforce Service Agent, bot, or service rep in the chat window by displaying it as if the end user had sent it.
This sample code illustrates how you can use this API.
1<script>
2 // In this example, we will send a message once bot has joined the conversation.
3 window.addEventListener("onEmbeddedMessagingFirstBotMessageSent", (event) => {
4 console.log("Received the onEmbeddedMessagingFirstBotMessageSent event…")
5 sendMessage();
6 });
7
8 function sendMessage() {
9 embeddedservice_bootstrap.utilAPI.sendTextMessage("Text");
10 .then(() => {
11 // Success handler used to perform actions when the message is sent successfully.
12 // For example, create a method that logs the message was successfull.
13 logMessageSuccessfullySent();
14 }).catch(() => {
15 // Error handler used to perform actions if the message fails to send.
16 // For example, log the failure.
17 logMessageFailed();
18 }).finally(() => {
19 // Finally handler used to perform any clean-up actions
20 // regardless of whether the message was successfully sent or not.
21 // For example, create a method that logs attempt to send message.
22 logMessageAttempt();
23 });
24 }
25</script>Use the event handlers in the utilAPI.sendMessage() method to perform actions after the message is sent.
then- Use this handler to perform actions when the message is sent successfully.catch- Use this handler to perform actions if the message fails to send.finally- Use this handler to perform clean-up actions, regardless of whether the message was successfully sent or not.