Webhook を管理

RBM Management API で作成されたすべてのエージェントは、デフォルトで通知に 1 つの Webhook を使用します。このパートナー レベルの Webhook は Business Communications Developer Console で構成され、パートナー アカウント内のすべてのエージェントに適用されます。

また、RBM Management API を使用してエージェント レベルの Webhook を構成することもできます。異なる動作で複数のエージェントを管理する場合は、この方法が適しています。

このページのコード スニペットは、Java サンプルNode.js サンプルから抜粋したものです。

Webhook 統合を作成する

エージェント Webhook 統合を追加するには、まず Webhook の URL を定義し、検証トークンを定義します。次に、検証手順に沿って Webhook を構成します。構成したら、URL とトークンの値を指定して createWebhookIntegration メソッドを呼び出します。この呼び出しが成功したら、着信メッセージの確認と処理に進みます。

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);
  }
);

このコードは、新しい Webhook オブジェクトを返します。

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

エージェントの Webhook 統合を検索する

エージェントに属するすべての Webhook 統合のリストを取得できます。実際には、RBM エージェントには 1 つの Webhook 統合しか含まれません。

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);
  });

このコードは、エージェントの Webhook 統合のリストを返します。

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

統合の詳細を確認する

統合への参照を指定すると、その詳細を取得できます。

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);
  });

このコードは、Webhook 統合の詳細を返します。

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

Webhook を削除する

Webhook は、その参照を使用してエージェントから削除できます。

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);
  });

空のオブジェクトが返されます。

{}