將 Amazon Connect 聊天整合到行動應用程式 - Amazon Connect

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將 Amazon Connect 聊天整合到行動應用程式

本主題說明如何在行動應用程式中建立自訂的 Amazon Connect 聊天使用者介面。這需要使用您自己的聊天後端。您還必須使用 Amazon Connect StartChatContactAPI啟動聯繫,並使用參與者服務APIs來管理聊天參與。

對於 iOS 應用程序,我們建議使用 Amazon Connect 聊天SDK的 iOS。它處理用戶端聊天的邏輯和 WebSocket. 如需詳細資訊,請參閱 Amazon Connect 聊天SDK的 iOS

注意

StartChatContactAPI需要 AWS 簽名版本 4 簽名。因此,初始請求應通過您的個人聊天後端進行路由。您可以直接從行動應用程式處理對 Amazon Connect 參與者服務 (ACPS) 的後續API呼叫。

整合工作流

下圖顯示了使用移動應用程序和代理的客戶之間的編程流程。圖表中的編號文字對應於影像下方的編號文字。

顯示 Amazon Connect 聊天程序流程的圖表。

在圖中:

  1. 當客戶在行動應用程式中開始聊天時,應用程式會 Amazon Connect 用 StartChatContactAPI. 這需要特定參數 (例如API端點、執行個體和聯絡流程),才能驗證並啟動聊天。IDs

  2. 與您的後端系統進StartChatContactAPI行交互,以獲取參與者令牌和作為聊天會話的唯一標識符的聯繫人 ID。

  3. 應用程式的後端會使用參與者權杖和聯絡人 ID 與 Amazon Connect 通訊,並設定客戶的聊天工作階段。

  4. Amazon Connect 參與者SDK使用參與者令牌激活,為聊天準備應用程序。

  5. 參加者會SDK使用工作階段詳細資料來建立與 Amazon Connect 的安全通訊線路。

  6. Amazon Connect 通過提供一個 WebSocket URL通過參與者響應SDK。該應用程序使用它URL來創建實時消息傳遞的 WebSocket 連接。

  7. Amazon Connect 會根據聯絡流程和路由設定檔為聊天指派支援代理。

  8. 來自代理程式的訊息會透過 WebSocket 連線傳送。行動應用程式會接聽, WebSocket 以接收並顯示來自客服的訊息,並將客戶訊息傳回。

必要條件

您必須具備下列先決條件,才能將 Amazon Connect 聊天與行動應用程式整合:

安裝程式庫

您可以透過安裝適用於 iOS 和 Android 的程式庫來啟動整合程序。您必須安裝這兩組資源庫。

  • iOS:轉到aws-sdk-ios存儲庫 GitHub 並安裝以下內容:

    pod 'AWSCore' pod 'AWSConnectParticipant' pod 'Starscream', '~> 4.0'
  • Android:轉到aws-sdk-android存儲庫 GitHub 並安裝以下內容:

    implementation("com.amazonaws:aws-android-sdk-core:2.73.0") implementation("com.amazonaws:aws-android-sdk-connectparticipant:2.73.0")

設定 AWS 認證

安裝兩組程式庫之後,您可以在 Amazon Connect 服務和 Amazon Connect 參與者服務中心註冊。若要這麼做,請在每個作業系統的 Config 檔案中,使用您的 AWS 認證,accessKey並以空字串 (“”) 取代和secretKey值,如下列範例所示。

重要

您必須指定要測試的區域。

  • iOS:用AWSStaticCredentialsProvider於 Amazon Connect 服務和參與者服務註冊。

    private let config = Config() let credentials = AWSStaticCredentialsProvider(accessKey: "", secretKey: "") let participantService = AWSServiceConfiguration(region: config.region, credentialsProvider: credentials)! AWSConnectParticipant.register(with: participantService, forKey: "") connectParticipantClient = AWSConnectParticipant.init(forKey: "")
  • 安卓:初始化AmazonConnectParticipantAsyncClient並設置區域。

    private var connectParticipantClient: AmazonConnectParticipantAsyncClient = AmazonConnectParticipantAsyncClient() private val chatConfiguration = Config init { connectParticipantClient.setRegion(Region.getRegion(chatConfiguration.region)) }
注意

您可以從中下載 Config 文件 GitHub。您必須編輯這兩個 Config 檔案。

開始聊天

接下來,您必須集成 Amazon Connect StartChatContactAPI提供的。通過註冊客戶的意圖與代理聊天API開始對話。

注意

若要部署 StartChatContact Lambda 函數,請使用上的CloudFront 範本 GitHub。

對於 iOS 和安卓系統,StartChatContactAPI通話需要以下幾條信息:

  • InstanceId:Amazon Connect 執行個體的識別碼。

  • ContactFlowId:處理聊天的聯繫流程的標識符。

  • ParticipantDetails:有關參與者的資訊,例如客戶的顯示名稱。

  • Attributes:有關聯絡人的其他資訊,可能有助於在 Amazon Connect 中進行路由或處理聊天。

重要

您必須使用 AWS 簽名版本 4 (SIGv4) 來簽署API呼叫。Sigv4 程序會將驗證資訊新增至使用HTTP傳送的 AWS API要求。對於行動用戶端,我們建議您在伺服器端執行簽署程序。您的行動應用程式會將請求傳送到您的伺服器,然後簽署請求並將其轉寄至 Amazon Connect。這有助於保護您的 AWS 憑證。

適用於 iOS 系統:

func startChatSession(displayName: String, completion: @escaping (Result<StartChatResponse, Error>) -> Void) { // Your back end server will handle SigV4 signing and make the API call to Amazon Connect // Use the completion handler to process the response or error }

若為 Android:

// Make a network call to your back end server suspend fun startChatSession(displayName: String): StartChatResponse { // Your back end server will handle SigV4 signing if needed and make the // API call to Amazon Connect // Handle the response or error accordingly }

建立參與者連線

您可以使用從StartChatContactAPI通話收到的詳細資料來建立參與者連線。然後,您撥打 Amazon Connect 參與者服務SDK,該服務返回建立連接 WebSocket URL所需的服務。

下面的例子演示了如何建立 iOS 的連接:

// Swift code snippet for iOS participant connection setup /// Creates the participant's connection. https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html /// - Parameter: participantToken: The ParticipantToken as obtained from StartChatContact API response. func createParticipantConnection() { let createParticipantConnectionRequest = AWSConnectParticipantCreateParticipantConnectionRequest() createParticipantConnectionRequest?.participantToken = self.participantToken createParticipantConnectionRequest?.types = ["WEBSOCKET", "CONNECTION_CREDENTIALS"] connectParticipantClient? .createParticipantConnection (createParticipantConnectionRequest!) .continueWith(block: { (task) -> Any? in self.connectionToken = task.result!.connectionCredentials!.connectionToken self.websocketUrl = task.result!.websocket!.url return nil } ).waitUntilFinished() }

下面的例子演示了如何建立安卓系統的連接:

// Kotlin code snippet for Android participant connection setup /// Creates the participant's connection. https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html /// - Parameter: participantToken: The ParticipantToken as obtained from StartChatContact API response. fun createParticipantConnection( _participantToken: String, handler: AsyncHandler<CreateParticipantConnectionRequest, CreateParticipantConnectionResult> ) { val createParticipantConnectionRequest = CreateParticipantConnectionRequest().apply { setType(listOf("WEBSOCKET", "CONNECTION_CREDENTIALS")) participantToken = _participantToken } connectParticipantClient.createParticipantConnectionAsync( createParticipantConnectionRequest, handler ) }

使用 WebSocket 連線

使用參SDK與者獲 WebSocket URL得聊天連接。

  • 要實施 WebSocket 管理,使用現有的解決方案或實施自己的解決方案

  • 要處理 Websocket 的消息和事件,實現您自己的解決方案,或使用我們的解決方案適用於 iOSAndroid 的解決方案。

  • 請確定您涵蓋所有訊息和事件類型:

    • 案例 typing = "application/vnd.amazonaws.connect.event.typing"

    • 案例 messageDelivered = "application/vnd.amazonaws.connect.event.message.delivered"

    • 案例 messageRead = "application/vnd.amazonaws.connect.event.message.read"

    • 案例 metaData = "application/vnd.amazonaws.connect.event.message.metadata"

    • 案例 joined = "application/vnd.amazonaws.connect.event.participant.joined"

    • 案例 left = "application/vnd.amazonaws.connect.event.participant.left"

    • 案例 ended = "application/vnd.amazonaws.connect.event.chat.ended"

    • 案例 plainText = "text/plain"

    • 案例 richText = "text/markdown"

    • 案例 interactiveText = "application/vnd.amazonaws.connect.message.interactive"

下圖顯示 Amazon Connect 聊天的預設執行個體。圖像中的數字對應於下面的編號文本。

顯示 Amazon Connect 聊天窗口組件的圖像。

在圖像中,從頂部附近開始:

  1. 輸入指示器會顯示輸入訊息的人員。

  2. 此範例使用純文字訊息。如果需要,您可以啟用降價的使用。

  3. 互動式清單控制項為使用者提供選項清單,例如客戶服務或帳單。

  4. 互動式快速回覆可針對常見問題或片語提供預先編程的回應。

傳送訊息

您可以使用該SendMessage功能發送聊天消息。

/// To send a message using the Participant SDK. /// - Parameters: /// - messageContent: The content of the message. /// - connectionToken: The authentication token associated with the connection - Received from Participant Connection /// - contentType: text/plain, text/markdown, application/json, and application/vnd.amazonaws.connect.message.interactive.response func sendChatMessage(messageContent: String) { let sendMessageRequest = AWSConnectParticipantSendMessageRequest() sendMessageRequest?.connectionToken = self.connectionToken sendMessageRequest?.content = messageContent sendMessageRequest?.contentType = "text/plain" connectParticipantClient? .sendMessage(sendMessageRequest!) .continueWith(block: { (task) -> Any? in return nil }) }

處理事件

使用參與者SDK中的SendEvent函數處理事件,例如輸入和讀取回條。

/// Sends an event such as typing, joined, left etc. /// - Parameters: /// - contentType: The content type of the request /// - content: The content of the event to be sent (for example, message text). For content related to message receipts, this is supported in the form of a JSON string. func sendEvent(contentType: ContentType, content: String = "") { let sendEventRequest = AWSConnectParticipantSendEventRequest() sendEventRequest?.connectionToken = self.connectionToken sendEventRequest?.contentType = contentType.rawValue sendEventRequest?.content = content // Set the content here connectParticipantClient? .sendEvent(sendEventRequest!) .continueWith(block: { (task) -> Any? in return nil }) }

結束對話

若要結束對話,請使用參加者中的disconnectParticipant功能SDK。

/// Disconnects a participant. /// - Parameter: connectionToken: The authentication token associated with the connection - Received from Participant Connection func endChat() { let disconnectParticipantRequest = AWSConnectParticipantDisconnectParticipantRequest() disconnectParticipantRequest?.connectionToken = self.connectionToken connectParticipantClient?.disconnectParticipant(disconnectParticipantRequest!) .continueWith(block: { (task) -> Any? in return nil }).waitUntilFinished() self.websocketUrl = nil }

來源碼

下列範例專案 GitHub 示範如何實作 Amazon Connect 聊天室。

Amazon Connect 聊天SDK的 iOS

SDK適用於 iOS 的 Amazon Connect 聊天是一種基於 Swift 的功能,SDK可簡化 iOS 應用程序的聊天整合過程。這SDK有助於處理類似於 Amazon Connect ChatJS 程式庫的用戶端聊天邏輯和後端通訊。

SDK包裝了 Amazon Connect 參與者服務,APIs並摘要了聊天會話和 WebSocket的管理。這可讓您專注於使用者介面和體驗,同時仰賴 Amazon Connect Chat SDK 與所有後端服務互動。這種方法仍然需要您使用自己的聊天後端來呼叫 Amazon Connect StartChatContact API 來發起聯繫。

如需詳細資訊,請參閱SDK適用於 iOS 的 Amazon Connect 聊天 GitHub頁面。