通过 RBM Management API 创建的所有代理默认使用单个 webhook 来接收通知。此合作伙伴级 webhook 在 RCS for Business 开发者控制台中配置,适用于合作伙伴账号中的所有代理。
或者,您也可以使用 RBM Management API 配置代理级 Webhook。如果您管理多个行为各异的代理,这可能是一个不错的选择。
本页上的代码段摘自 Java 示例和 Node.js 示例。
创建网络钩子集成
如需添加代理网络钩子集成,请先定义网络钩子的网址并定义验证令牌。然后,配置网络钩子以遵循验证步骤。
配置完成后,使用网址和令牌值调用 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 集成的列表。实际上,RBM 代理始终只有一个网络钩子集成。如需了解详情,请参阅 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); });
此代码会返回网络钩子集成详细信息:
{
"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 的引用从代理中移除 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); });
返回一个空对象:
{}