為對話洞察事件啟用 Pub/Sub 通知

啟用 Pub/Sub 通知後,每當 CX Insights 事件完成時,您都會收到通知。您可以設定 CX Insights,在所有事件或只有特定事件發生時傳送通知。如要進一步瞭解可觸發 Pub/Sub 通知的事件,請參閱參考說明文件

必要條件

  1. 按照指示建立 Pub/Sub 主題和提取訂閱項目

啟用 Pub/Sub 通知

您可以設定 CX Insights,只在特定事件發生時傳送通知,或在所有事件發生時傳送通知。下列程式碼範例會設定 CX Insights,只在每次建立對話或分析時傳送通知。

REST

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • TOPIC_ID:通知主題的 ID (例如,每次建立對話時都會發出通知)。每個通知主題都應有專屬 ID。

HTTP 方法和網址:

PATCH https://contactcenterinsights.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/settings?updateMask=pubsub_notification_settings

JSON 要求主體:

{
  "pubsub_notification_settings": {
    "create-conversation": "projects/PROJECT_ID/topics/TOPIC_ID_1",
    "create-analysis": "projects/PROJECT_ID/topics/TOPIC_ID_2"
  },
}

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "name": "projects/PROJECT_ID/locations/us-central1/settings",
  "createTime": "2021-01-20T10:10:10.123000Z",
  "updateTime": "2021-01-20T11:11:11.456000Z",
  "pubsubNotificationSettings": {
    "create-conversation": "projects/PROJECT_ID/topics/TOPIC_ID_1",
    "create-analysis": "projects/PROJECT_ID/topics/TOPIC_ID_2"
  }
}

Python

如要向 CX Insights 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.api_core import protobuf_helpers
from google.cloud import contact_center_insights_v1


def enable_pubsub_notifications(
    project_id: str, topic_create_conversation: str, topic_create_analysis: str
) -> None:
    """Enables Cloud Pub/Sub notifications for specified events.

    Args:
        project_id:
            The project identifier. For example, 'my-project'.
        topic_create_conversation:
            The Cloud Pub/Sub topic to notify of conversation creation events.
            Format is 'projects/{project_id}/topics/{topic_id}'.
            For example, 'projects/my-project/topics/my-topic'.
        topic_create_analysis:
            The Cloud Pub/Sub topic to notify of analysis creation events.
            Format is 'projects/{project_id}/topics/{topic_id}'.
            For example, 'projects/my-project/topics/my-topic'.

    Returns:
        None.
    """
    # Construct a settings resource.
    settings = contact_center_insights_v1.Settings()
    settings.name = (
        contact_center_insights_v1.ContactCenterInsightsClient.settings_path(
            project_id, "us-central1"
        )
    )
    settings.pubsub_notification_settings = {
        "create-conversation": topic_create_conversation,
        "create-analysis": topic_create_analysis,
    }

    update_mask = protobuf_helpers.field_mask(None, type(settings).pb(settings))

    # Call the Insights client to enable Pub/Sub notifications.
    insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
    insights_client.update_settings(settings=settings, update_mask=update_mask)
    print("Enabled Pub/Sub notifications")

Java

如要向 CX Insights 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
import com.google.cloud.contactcenterinsights.v1.Settings;
import com.google.cloud.contactcenterinsights.v1.SettingsName;
import com.google.protobuf.FieldMask;
import java.io.IOException;

public class EnablePubSubNotifications {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my_project_id";
    String topicCreateConversation = "projects/my_project_id/topics/my_topic_id";
    String topicCreateAnalysis = "projects/my_project_id/topics/my_other_topic_id";

    enablePubSubNotifications(projectId, topicCreateConversation, topicCreateAnalysis);
  }

  public static void enablePubSubNotifications(
      String projectId, String topicCreateConversation, String topicCreateAnalysis)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
      // Construct a settings resource.
      SettingsName name = SettingsName.of(projectId, "us-central1");
      Settings settings =
          Settings.newBuilder()
              .setName(name.toString())
              .putPubsubNotificationSettings("create-conversation", topicCreateConversation)
              .putPubsubNotificationSettings("create-analysis", topicCreateAnalysis)
              .build();

      // Construct an update mask.
      FieldMask updateMask =
          FieldMask.newBuilder().addPaths("pubsub_notification_settings").build();

      // Call the Insights client to enable Pub/Sub notifications.
      Settings response = client.updateSettings(settings, updateMask);
      System.out.printf("Enabled Pub/Sub notifications");
    }
  }
}

Node.js

如要向 CX Insights 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'my_project_id';
// const topicCreateConversation = 'projects/my_project_id/topics/my_topic_id';
// const topicCreateAnalysis = 'projects/my_project_id/topics/my_other_topic_id';

// Imports the Contact Center Insights client.
const {
  ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');

// Instantiates a client.
const client = new ContactCenterInsightsClient();

async function enablePubSubNotifications() {
  const [settings] = await client.updateSettings({
    settings: {
      name: client.settingsPath(projectId, 'us-central1'),
      pubsubNotificationSettings: {
        'create-conversation': topicCreateConversation,
        'create-analysis': topicCreateAnalysis,
      },
    },
    updateMask: {
      paths: ['pubsub_notification_settings'],
    },
  });
  console.info(`Enabled Pub/Sub notifications for ${settings.name}`);
}
enablePubSubNotifications();

Pub/Sub 訊息內容

Pub/Sub 訊息的內容取決於觸發該 Pub/Sub 通知的事件。

觸發條件 訊息資料 訊息屬性
create-analysis 長時間執行的作業,其回應為分析 {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
create-conversation 對話 {"conversation_name": "projects/{project}/locations/{location}/conversations/{conversation}"}
export-insights-data 回應為空的長時間執行作業 {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
ingest-conversations 回應為空的長時間執行作業 {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
update-conversation 對話 {"conversation_name": "projects/{project}/locations/{location}/conversations/{conversation}"}
upload-conversation 回應為空的長時間執行作業 {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}