Activer les notifications Pub/Sub pour les événements Insights conversationnels

L'activation des notifications Pub/Sub vous permet de recevoir une notification chaque fois qu'un événement CX Insights est terminé. Vous pouvez configurer CX Insights pour qu'il envoie une notification pour tous les événements ou uniquement pour certains. Pour en savoir plus sur les événements qui peuvent déclencher des notifications Pub/Sub, consultez la documentation de référence.

Prérequis

  1. Suivez les instructions pour créer un sujet et un abonnement pull Pub/Sub.

Activer les notifications Pub/Sub

Vous pouvez configurer CX Insights pour qu'il envoie une notification pour des événements spécifiques uniquement ou pour tous les événements. L'exemple de code suivant configure CX Insights pour n'envoyer une notification que lorsqu'une conversation ou une analyse est créée.

REST

Avant d'utiliser les données de requête, effectuez les remplacements suivants :

  • PROJECT_ID : ID de votre projet Google Cloud .
  • TOPIC_ID : ID du sujet de notification (par exemple, notification à chaque création d'une conversation). Chaque thème de notification doit avoir un ID unique.

Méthode HTTP et URL :

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

Corps JSON de la requête :

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

Pour envoyer votre requête, développez l'une des options suivantes :

Vous devriez recevoir une réponse JSON de ce type :

{
  "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

Pour vous authentifier auprès de CX Insights, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

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

Pour vous authentifier auprès de CX Insights, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.


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

Pour vous authentifier auprès de CX Insights, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

/**
 * 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();

Contenu du message Pub/Sub

Le contenu du message Pub/Sub dépend de l'événement qui déclenche cette notification Pub/Sub.

Déclencheur Données des messages Attributs du message
create-analysis Opération de longue durée dont la réponse est une analyse {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
create-conversation Conversation {"conversation_name": "projects/{project}/locations/{location}/conversations/{conversation}"}
export-insights-data Opération de longue durée dont la réponse est vide {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
ingest-conversations Opération de longue durée dont la réponse est vide {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}
update-conversation Conversation {"conversation_name": "projects/{project}/locations/{location}/conversations/{conversation}"}
upload-conversation Opération de longue durée dont la réponse est vide {"operation_name": "projects/{project}/locations/{location}/operations/{operation}"}