Implement Virtual and Live Agent chat

  • Release version: Washingtondc
  • Updated February 1, 2024
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Implement Virtual and Live Agent chat

    The Mobile SDK allows ServiceNow customers to implement Virtual and Live Agent chat services in their iOS applications using the NowChatService API. Customers can create a chat user interface, start chat sessions, and configure chat settings efficiently.

    Show full answer Show less

    Key Features

    • Chat User Interface Creation: Use the makeChatScreen() function to initialize and create the chat UI.
    • Starting Chat Sessions: The startChat() method initializes chat sessions, with options for context variables to enhance chat interactions.
    • Theme Customization: Customize the chat UI colors by passing a theme object in the makeChatUI() call, ensuring alignment with your application’s branding.

    Key Outcomes

    By configuring Virtual Agent in your ServiceNow instance and utilizing the Mobile SDK, you can enhance user engagement through seamless chat experiences. Expect improved customer interactions and the ability to pass specific context variables for tailored service delivery.

    The Mobile SDK enables you to easily implement Virtual and Live Agent chat services within your iOS application.

    You use the NowChatService API to create the chat user interface. Once the UI is created, you must then start the chat session, and then start the chat service. Similar to other Mobile SDK feature services, the NowChatService API provides two implementations for some of its methods, including the startChat() method. One implementation returns a Combine publisher, and the other calls a completion handler with the return results.

    The following is a snippet from the sample application that shows how to initialize and start a chat UI and session.

    // Create the chat UI
    func makeChatScreen() -> UIViewController? {
      guard let chatService = chatService else { return nil }
      let result = chatService.makeChatUI(theme: CarrascoChatTheme(baseTheme: CarrascoTheme()))
      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()
      }
    }

    Before you can leverage the chat functionality within your application, you must configure Virtual Agent within your ServiceNow instance. For details, see Virtual Agent.

    Passing context variables to Live Agent and Virtual Agent chat

    You can pass chat context variables when starting a chat session by passing the contextData parameter in the NowChatService - startChat(contextData: [String: Any]?) or NowChatService - startChat(contextData: [String: Any]? = nil, _ completion: @escaping (Result<Void, NowChatServiceError>)) functions. For additional information on chat context variables, see Live agent chat context variables.

    func startChat() {
      guard let chatService = chatService else {
        debugPrint("Chat service is invalid")
        viewState = makeViewState()
        return
      }
      let contextData = ["sys_id": "123456789", "table_name": "wm_task", "active": true] as [String: Any]
      chatService.startChat(contextData: contextData) { [weak self] result in
        if case .failure(let error) = result {
          debugPrint("Chat session initialization failed with error: \(error)")
          self?.resetChat()
        }
      }
    }

    Theme the chat user interface

    You can customize the colors of the Live Agent and Virtual Agent chat UI by passing a theme object in the makeChatUI() call. For a list of all of the elements that you can customize, see NowChatColoring protocol - iOS. By default, the chat UI uses the NowUIColor theme for all NowSDK UI elements. Refer to the NowChatThemeable protocol - iOS for sample code snippets on how to apply a theme to your chat UI.