管理 Webhook

透過 RBM Management API 建立的所有代理程式,預設都會使用單一網路鉤子來接收通知。這個合作夥伴層級的 Webhook 是在 RCS for Business 開發人員控制台中設定,適用於合作夥伴帳戶中的所有代理程式。

或者,您也可以使用 RBM Management API 設定代理商層級的 Webhook。如果您管理多個行為不同的代理程式,這或許是不錯的選擇。

本頁面的程式碼片段取自 Java 範例和 Node.js 範例。

建立 Webhook 整合

如要新增代理程式 Webhook 整合功能,請先定義 Webhook 的網址,並定義驗證符記。然後設定 Webhook,按照驗證步驟操作。 設定完成後,請使用網址和權杖值呼叫 createWebhookIntegration 方法。如果該呼叫成功,您可以繼續驗證及處理傳入的訊息。

詳情請參閱 brands.agents.integrations 和 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);
  }
);

這段程式碼會傳回新的 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 代理程式只會有單一 Webhook 整合。詳情請參閱 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);
  });

這段程式碼會傳回代理的 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"
      }
    }
  ]
}

查詢整合項目的詳細資料

如果您有整合項目的參照,可以擷取整合詳細資料。詳情請參閱 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);
  });

這段程式碼會傳回 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。詳情請參閱 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);
  });

系統會傳回空白物件:

{}