Zarządzaj webhookami

Wszyscy agenci utworzeni za pomocą interfejsu RBM Management API domyślnie używają jednego webhooka do powiadomień. Ten webhook na poziomie partnera jest konfigurowany w Konsoli programisty RCS dla firm i dotyczy wszystkich agentów na koncie partnera.

Możesz też użyć interfejsu RBM Management API do skonfigurowania webhooków na poziomie agenta. Może to być dobre rozwiązanie, jeśli zarządzasz wieloma agentami o różnych zachowaniach.

Fragmenty kodu na tej stronie pochodzą z przykładowych fragmentów kodu w Javie i przykładowych fragmentów kodu w Node.js.

Tworzenie integracji webhooka

Aby dodać integrację webhooka agenta, najpierw zdefiniuj adres URL webhooka i token weryfikacyjny. Następnie skonfiguruj webhooka, aby wykonać kroki weryfikacji. Po skonfigurowaniu wywołaj metodę createWebhookIntegration z wartościami adresu URL i tokena. Jeśli połączenie się powiedzie, możesz przejść do weryfikacji i obsługi wiadomości przychodzących.

Więcej informacji znajdziesz w artykułach brands.agents.integrations i brands.agents.integrations.create.

Node.js

const businessCommunicationsApiHelper =
  require('@google/rbm-businesscommunications');

const privateKey =
  require('../../resources/businesscommunications-service-account-credentials.json');

businessCommunicationsApiHelper.initBusinessCommunucationsApi(privateKey);

const url = 'https://myrbmserviceendpoint.somewhere.com/callback';

const token = '123456';

businessCommunicationsApiHelper
  .createWebhookIntegration(agent.name, url, token)
  .then((response) => {
    console.log(JSON.stringify(response.data, null, 2));
  }).catch((err) => {
    console.log(err);
  }
);

Ten kod zwraca nowy obiekt webhooka:

{
  "name": "brands/40bd963f-ff92-425c-b273-8f0892d2d017/agents/my_new_agent_hfaoplpu_agent/integrations/4efdb82f-fd6d-4ba4-8ea3-f2f4a60d1547",
  "status": "ENABLED",
  "agentWebhookIntegration": {
    "webhookUri": "https://myrbmserviceendpoint.somewhere.com/callback"
  }
}

Sprawdzanie integracji webhooka agenta

Możesz pobrać listę wszystkich integracji webhooków należących do agenta. W praktyce agent RBM ma tylko 1 integrację webhooka. Więcej informacji znajdziesz brands.agents.integrations.list.

Node.js

const businessCommunicationsApiHelper =
  require('@google/rbm-businesscommunications');

const privateKey =
  require('../../resources/businesscommunications-service-account-credentials.json');

businessCommunicationsApiHelper.initBusinessCommunucationsApi(privateKey);

businessCommunicationsApiHelper
  .listAgentIntegrations(agent.name)
  .then((response) => {
    console.log(JSON.stringify(response.data, null, 2));
    datastore.saveJsonData('integrations', response.data);
  }).catch((err) => {
    console.log(err);
  });

Ten kod zwraca listę integracji webhooków agenta:

{
  "integrations": [
    {
      "name": "brands/40bd963f-ff92-425c-b273-8f0892d2d017/agents/my_new_agent_hfaoplpu_agent/integrations/4efdb82f-fd6d-4ba4-8ea3-f2f4a60d1547",
      "status": "ENABLED",
      "agentWebhookIntegration": {
        "webhookUri": "https://myrbmserviceendpoint.somewhere.com/callback"
      }
    }
  ]
}

Sprawdzanie szczegółów integracji

Szczegóły integracji możesz pobrać, jeśli masz do niej odniesienie. Więcej informacji znajdziesz w sekcji brands.agents.integrations.get.

Node.js

const businessCommunicationsApiHelper =
  require('@google/rbm-businesscommunications');

const privateKey =
  require('../../resources/businesscommunications-service-account-credentials.json');

businessCommunicationsApiHelper.initBusinessCommunucationsApi(privateKey);

businessCommunicationsApiHelper
  .getAgentIntegration(integrations.integrations[0].name)
  .then((response) => {
    console.log(JSON.stringify(response.data, null, 2));
  }).catch((err) => {
    console.log(err);
  });

Ten kod zwraca szczegóły integracji webhooka:

{
  "name": "brands/40bd963f-ff92-425c-b273-8f0892d2d017/agents/my_new_agent_hfaoplpu_agent/integrations/4efdb82f-fd6d-4ba4-8ea3-f2f4a60d1547",
  "status": "ENABLED",
  "agentWebhookIntegration": {
    "webhookUri": "https://myrbmserviceendpoint.somewhere.com/callback"
  }
}

Usuwanie webhooka

Możesz usunąć webhooka z agenta, używając jego odwołania. Więcej informacji znajdziesz brands.agents.integrations.delete.

Node.js

const businessCommunicationsApiHelper =
  require('@google/rbm-businesscommunications');

const privateKey =
  require('../../resources/businesscommunications-service-account-credentials.json');

businessCommunicationsApiHelper.initBusinessCommunucationsApi(privateKey);

businessCommunicationsApiHelper
  .deleteAgentIntegration(integrations.integrations[0].name)
  .then((response) => {
    console.log(JSON.stringify(response.data, null, 2));
  }).catch((err) => {
    console.log(err);
  });

Zwracany jest pusty obiekt:

{}