Zarządzaj webhookami

Wszystkie agenty utworzone przy użyciu interfejsu RBM Management API domyślnie używają jednego webhooka na potrzeby powiadomień. Ten webhook na poziomie partnera jest skonfigurowany w konsoli programisty komunikacji biznesowej i dotyczy wszystkich agentów na koncie partnera.

webhooki na poziomie agenta możesz też skonfigurować za pomocą interfejsu RBM Management API. Może to być dobre rozwiązanie, jeśli zarządzasz wieloma agentami, których działanie jest różne.

Fragmenty kodu widoczne na tej stronie pochodzą z przykładów w języku Java i przykładów Node.js.

Tworzenie integracji webhooka

Aby dodać integrację webhooka agenta, najpierw zdefiniuj adres URL webhooka i zdefiniuj token weryfikacyjny. Następnie skonfiguruj webhooka, aby wykonywał czynności weryfikacji. Po skonfigurowaniu wywołaj metodę createWebhookIntegration z wartościami adresu URL i tokena. Jeśli to wywołanie się powiedzie, możesz kontynuować weryfikowanie i obsługę wiadomości przychodzących.

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

Wyszukiwanie integracji webhooka agenta

Możesz pobrać listę wszystkich integracji webhooka należących do agenta. W praktyce agent RBM może mieć tylko jedną integrację webhooka.

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 webhooka 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"
      }
    }
  ]
}

Wyszukiwanie szczegółów integracji

Ze względu na odniesienie do integracji można pobrać jej szczegóły.

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

Webhooka można usunąć z agenta za pomocą jego odniesienia:

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:

{}