Passing context variables to Live Agent and Virtual Agent chat
Summarize
Summary of Passing context variables to Live Agent and Virtual Agent chat
This content explains how ServiceNow customers can pass context variables and configure chat sessions when using Live Agent and Virtual Agent chat capabilities. It focuses on initiating chat sessions with customizable parameters to enhance user experience and control chat behavior.
Show less
Key Features
- Passing Context Variables: Use the
startChat(contextData:completion:)function ofNowChatServiceto send chat context variables at the start of a chat session, enabling contextualized conversations. - Chat Configuration Parameters: When starting a chat, an optional
NowChatConfigurationparameter can be passed to modify chat behavior and UI elements, including:- closePrompt: Customizes the exit prompt with header, message, accept button (closes chat), and decline button (dismisses prompt).
- disabledFeatures: Allows disabling specific chat features by referencing the
NowChatConfiguration.Featureenum. - conversationOptions: Applies conversation options like forcing new conversations or ending on exit, using
NowChatConvestation.ConversationOptionenum. - uiConfiguration: Enables customization of UI components such as the close button and attachment upload button visibility.
- UI Customization Examples: Demonstrates how to create a customized chat interface including a close button with either text or image, and how to configure the attachment upload button visibility.
- Service Initialization and Chat Startup: Shows the initialization process for the chat service and handling success or failure states during chat UI creation and chat session start.
What You Can Expect
- Ability to start chat sessions with relevant context data to streamline support and automate responses.
- Enhanced control over chat window behavior, including exit prompts and feature availability, improving user interaction and compliance with organizational policies.
- Customization of chat UI elements to align with branding and user experience requirements.
- Robust error handling during chat service initialization and session startup, ensuring smooth integration and operational reliability.
You can pass chat context variables when starting a chat session by passing the contextData parameter.
You can pass this value using the NowChatService - startChat(contextData: [String: Any]? = nil, _ completion: @escaping (Result<Void, NowChatServiceError>) -> Void) functions.
For additional information on chat context variables, see Live agent chat context variables.
In addition, you can also pass an optional NowChatConfiguration parameter when starting a chat session to modify some of the behavior of NowChat.
- closePrompt: Prompt text that appears before exiting a chat window. You define this prompt text through the following parameters:
- header: Nullable string value that appears on the prompt’s header.
- message: String value that appears on the prompt’s main body.
- acceptButtonTitle: String value that appears on the primary button of the prompt. This button closes the chat window.
- declineButtonTitle: String value that appears on the secondary button of the prompt. This button dismisses the prompt without closing the chat window.
- disabledFeatures: List of NowChat features to disable. Refer to the NowChatConfiguration.Feature enum class for the list of features that you can disable.
- conversationOptions: List of conversation options to apply to NowChat. Refer to the NowChatConvestation.ConversationOption enum class for the list of options that you can apply.
- uiConfiguration: UIConfiguration value used to configure UI components in NowChat.
// Close Button Type
func closeButtonText(title: String?) -> NowChatConfiguration.CloseButtonType? {
guard let title else { return nil }
return .text(title)
}
func closeButtonImage(name imageName: String?) -> NowChatConfiguration.CloseButtonType? {
guard let imageName,
let buttonImage = UIImage(named: imageName) else { return nil }
return .image(buttonImage)
}
// Create the chat UI
func makeChatScreen() -> UIViewController? {
guard let chatService = chatService else { return nil }
let closePrompt = NowChatConfiguration.ClosePrompt(header: "Close Window",
message: "Are you sure you want to close the chat window?",
acceptButtonTitle: "Yes",
declineButtonTitle: "No")
let attachmentUploadButton = NowChatConfiguration.AttachmentUploadButton(isVisible: false) // default isVisible = true
let chatConfiguration = NowChatConfiguration(closePrompt: closePrompt,
disabledFeatures: [.startNewConversation],
conversationOptions: [.forceNewConversation, .endConversationOnExit],
uiConfiguration: NowChatConfiguration.UIConfiguration(closeButton: closeButtonImage(name: "arrow.left"), attachmentUploadButton: attachmentUploadButton))
let result = chatService.makeChatUI(theme: CarrascoChatTheme(chatColors: ChatColors()), chatConfiguration: chatConfiguration)
switch result {
case .success(let chatViewController):
return chatViewController
case .failure(let error):
debugPrint("Chat screen creation failed with error: \(error)")
return nil
}
}
// Start the chat session
func startChat() {
guard let chatService = chatService else {
debugPrint("Chat service is invalid")
viewState = makeViewState()
return
}
chatService.startChat { [weak self] result in
if case .failure(let error) = result {
debugPrint("Chat session initialization failed with error: \(error)")
self?.resetChat()
}
}
}
// Initialize the chat service
private func initializeChatService() {
NowChat.makeChatService(instanceUrl: instanceUrl, delegate: self) { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let service):
self.chatService = service
case .failure(let error):
debugPrint("Creating the chat service failed with error: \(error)")
}
self.viewState = self.makeViewState()
}
}