Quản lý webhook

Theo mặc định, tất cả các nhân viên hỗ trợ được tạo thông qua RBM Management API đều sử dụng một webhook duy nhất cho thông báo. Webhook cấp đối tác này được định cấu hình trong Business Communications Developer Console và áp dụng cho tất cả các nhân viên hỗ trợ trong tài khoản đối tác của bạn.

Ngoài ra, bạn có thể sử dụng API Quản lý RBM để định cấu hình webhook ở cấp nhân viên hỗ trợ. Đây có thể là một lựa chọn phù hợp nếu bạn quản lý nhiều nhân viên hỗ trợ có hành vi riêng biệt.

Đoạn mã trên trang này được lấy từ mẫu Javamẫu Node.js.

Tạo mối tích hợp webhook

Để thêm chế độ tích hợp webhook của nhân viên hỗ trợ, trước tiên, hãy xác định URL cho webhook của bạn và xác định mã thông báo xác thực. Sau đó, hãy định cấu hình webhook để thực hiện theo các bước xác thực. Sau khi định cấu hình, hãy gọi phương thức createWebhookIntegration bằng URL và giá trị mã thông báo. Nếu lệnh gọi đó thành công, bạn có thể tiến hành xác minh và xử lý các tin nhắn đến.

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

Mã này trả về đối tượng webhook mới:

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

Tra cứu các chức năng tích hợp webhook của nhân viên hỗ trợ

Bạn có thể truy xuất danh sách tất cả các chế độ tích hợp webhook của một nhân viên hỗ trợ. Trong thực tế, nhân viên hỗ trợ RBM chỉ tích hợp một webhook duy nhất.

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

Mã này trả về danh sách các tích hợp webhook của nhân viên hỗ trợ:

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

Tra cứu thông tin chi tiết về chế độ tích hợp

Khi tham chiếu đến một chế độ tích hợp, bạn có thể truy xuất thông tin chi tiết về chế độ tích hợp đó.

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

Mã này trả về thông tin chi tiết về quá trình tích hợp 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"
  }
}

Xóa webhook

Bạn có thể xoá một webhook khỏi một nhân viên hỗ trợ bằng cách sử dụng mã tham chiếu của 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);
  });

Một đối tượng trống sẽ được trả về:

{}