根據預設,所有透過 RBM Management API 建立的代理程式都會使用單一 webhook 傳送通知。這項合作夥伴層級 Webhook 已在 Business Communications 開發人員控制台中設定,並套用至合作夥伴帳戶中的所有服務專員。
或者,您也可以使用 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); });
系統會傳回空白物件:
{}