RBM Management API で作成されたすべてのエージェントは、デフォルトで通知に単一の Webhook を使用します。このパートナー レベルの Webhook は、ビジネス コミュニケーション デベロッパー コンソールで設定され、パートナー アカウント内のすべてのエージェントに適用されます。
また、RBM Management API を使用してエージェント レベルの Webhook を構成することもできます。動作が異なる複数のエージェントを管理している場合は、この方法が適しています。
このページのコード スニペットは、Java サンプルと Node.js サンプルから取得したものです。
Webhook 統合を作成する
エージェント Webhook 統合を追加するには、まず Webhook の URL と検証トークンを定義します。次に、検証手順に沿って Webhook を構成します。構成したら、URL とトークンの値を指定して 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 エージェントには 1 つの 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); });
空のオブジェクトが返されます。
{}