Business Messages 代理支持与
将 Business Messages 代理与 Dialogflow 的其他功能集成 ES 或 Dialogflow CX、 请参阅每个产品的文档。
当用户向集成了 Dialogflow 的代理发送消息时,
Business Messages 将用户消息传递给 Dialogflow,并发送 Dialogflow 的
并在邮件的
dialogflowResponse
对象。您可以将代理配置为
自动将 Dialogflow 的响应发送给用户,无需对您的
部分。请参阅自动回复
了解详情。
Dialogflow 集成
在通过 Business Messages 使用基于 Dialogflow 的自动化功能之前, 您需要启用 Dialogflow 集成。
前提条件
首先,您需要
- Business Messages 代理
- 全球区域且根语言为英语的 Dialogflow 代理 (zh-CN)
如果您没有 Dialogflow 代理,请创建一个。
Dialogflow ES
在启用 Dialogflow ES 集成之前,您需要 Dialogflow 代理的项目 ID。要查找项目 ID
- 导航到 Dialogflow 控制台。
- 选择要连接到 Business Messages 的 Dialogflow 代理。 然后点击齿轮图标 即可。
- 在 Google 项目下,记下项目 ID 值。
Dialogflow CX
在启用 Dialogflow CX 集成之前,您需要 Dialogflow 代理的项目 ID 和代理 ID。要查找这些 ID
- 导航到 Dialogflow CX 控制台。
- 选择您的 Dialogflow 项目。
- 在代理选择器中,点击菜单 旁边的“添加”图标。
- 点击复制名称。此操作会将代理的全名
以下格式:
projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID
。 - 记下项目 ID 和代理 ID 的值。
创建集成
从此处获取合作伙伴的 Dialogflow 服务账号电子邮件地址
dialogflowServiceAccountEmail
。将 将 PARTNER_ID 替换为您的合作伙伴 ID。cURL
# This code gets the partner. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/partners/get # Replace the __PARTNER_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X GET \ "https://businesscommunications.googleapis.com/v1/partners/__PARTNER_ID__" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
Node.js
/** * This code snippet gets a partner. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/partners/get * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const PARTNER_ID = 'EDIT_HERE'; const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); const partnerName = 'partners/' + PARTNER_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: partnerName, }; bcApi.partners.get(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent found console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code gets a partner. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/partners/get This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( Agent, BusinesscommunicationsPartnersGetRequest, ) # Edit the values below: PARTNER_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) partners_service = BusinesscommunicationsV1.PartnersService(client) partner_name = 'partners/' + PARTNER_ID partner = partners_service.Get(BusinesscommunicationsPartnersGetRequest( name=partner_name )) print(partner)
复制服务账号电子邮件地址。此账号将关联你的 Business Messages 和 Dialogflow 代理。
在 Google Cloud 控制台 选择您的 Dialogflow 项目。
前往 IAM 权限。
点击添加,并为新成员输入服务账号电子邮件地址。
对于选择角色,请选择 Dialogflow 控制台代理编辑者。
点击添加其他角色,然后选择 Dialogflow API 客户端。
点击保存。
将您的 Dialogflow 项目与 Business Messages 代理集成。
将 AUTO_RESPONSE_STATUS 替换为 ENABLED 或 DISABLED,具体取决于 设置是否让 Business Messages 自动回复 回答的问题
Dialogflow ES
cURL
# This code creates a Dialogflow ES integration. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_es # Replace the __BRAND_ID__, __AGENT_ID__, __DIALOGFLOW_ES_PROJECT_ID__ and __AUTO_RESPONSE_STATUS__ # Make sure a service account key file exists at ./service_account_key.json curl -X POST \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -d '{ "dialogflowEsIntegration": { "dialogflowProjectId": "__DIALOGFLOW_ES_PROJECT_ID__", "autoResponseStatus": "__AUTO_RESPONSE_STATUS__" } }'
Node.js
/** * This code snippet creates a Dialogflow ES integration. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_es * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const BRAND_ID = 'EDIT_HERE'; const AGENT_ID = 'EDIT_HERE'; const DIALOGFLOW_ES_PROJECT_ID = 'EDIT_HERE' const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); const uuidv4 = require('uuid').v4; // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); const agentName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID; if (authClient) { const integrationObject = { dialogflowEsIntegration: { dialogflowProjectId: DIALOGFLOW_ES_PROJECT_ID, autoResponseStatus: 'ENABLED' } }; // Setup the parameters for the API call const apiParams = { auth: authClient, parent: agentName, resource: integrationObject }; bcApi.brands.agents.integrations.create(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code snippet creates a Dialogflow ES integration. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_es This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest, DialogflowEsIntegration ) # Edit the values below: BRAND_ID = 'EDIT_HERE' AGENT_ID = 'EDIT_HERE' DIALOGFLOW_ES_PROJECT_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client) agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID integration = integrations_service.Create(BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest( integration=DialogflowEsIntegration( autoResponseStatus=DialogflowEsIntegration.AutoResponseStatusValueValuesEnum.ENABLED, dialogflowProjectId=DIALOGFLOW_ES_PROJECT_ID ), parent=agent_name )) print(integration)
Dialogflow CX
cURL
# This code creates a Dialogflow CX integration. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_cx # Replace the __BRAND_ID__, __AGENT_ID__, __DIALOGFLOW_CX_PROJECT_ID__, __DIALOGFLOW_CX_AGENT_ID__ and __AUTO_RESPONSE_STATUS__ # Make sure a service account key file exists at ./service_account_key.json curl -X POST \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -d '{ "dialogflowCxIntegration": { "dialogflowProjectId": "__DIALOGFLOW_CX_PROJECT_ID__", "dialogflowAgentId": "__DIALOGFLOW_CX_AGENT_ID__", "autoResponseStatus": "__AUTO_RESPONSE_STATUS__" } }'
Node.js
/** * This code snippet creates a Dialogflow CX integration. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_cx * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const BRAND_ID = 'EDIT_HERE'; const AGENT_ID = 'EDIT_HERE'; const DIALOGFLOW_CX_AGENT_ID = 'EDIT_HERE' const DIALOGFLOW_CX_PROJECT_ID = 'EDIT_HERE' const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); const uuidv4 = require('uuid').v4; // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); const agentName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID; if (authClient) { const integrationObject = { dialogflowCxIntegration: { dialogflowProjectId: DIALOGFLOW_CX_PROJECT_ID, dialogflowAgentId: DIALOGFLOW_CX_AGENT_ID, autoResponseStatus: 'ENABLED' } }; // Setup the parameters for the API call const apiParams = { auth: authClient, parent: agentName, resource: integrationObject }; bcApi.brands.agents.integrations.create(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code snippet creates a Dialogflow CX integration. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_cx This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest, DialogflowCxIntegration ) # Edit the values below: BRAND_ID = 'EDIT_HERE' AGENT_ID = 'EDIT_HERE' DIALOGFLOW_CX_AGENT_ID = 'EDIT_HERE' DIALOGFLOW_CX_PROJECT_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client) agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID integration = integrations_service.Create(BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest( integration=DialogflowCxIntegration( autoResponseStatus=DialogflowCxIntegration.AutoResponseStatusValueValuesEnum.ENABLED, dialogflowAgentId=DIALOGFLOW_CX_AGENT_ID, dialogflowProjectId=DIALOGFLOW_CX_PROJECT_ID ), parent=agent_name )) print(integration)
如需了解格式设置和值选项,请参阅
Integration
。
Business Messages 和 Dialogflow 连接大约需要两分钟。接收者
查看集成状态,获取集成的
OperationInfo
。
更新集成
如需更新代理的自动响应设置,请运行以下命令。 将 AUTO_RESPONSE_STATUS 替换为“已启用”或 已停用,具体取决于您是否希望 Business Messages 自动 使用 Dialogflow 响应来响应用户。
Dialogflow ES
cURL
# This code updates the Dialogflow association. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/updateDialogflowAssociation # Replace the __BRAND_ID__ and __AGENT_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/dialogflowAssociation?updateMask=enableAutoResponse" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "enableAutoResponse": true }'
Dialogflow CX
cURL
# This code updates the Dialogflow association. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/updateDialogflowAssociation # Replace the __BRAND_ID__ and __AGENT_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/dialogflowAssociation?updateMask=enableAutoResponse" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "enableAutoResponse": true }'
如需了解格式设置和值选项,请参阅 Integration
。
在 Dialogflow 版本之间切换
一个 Business Messages 代理一次只能支持一个 Dialogflow 集成。 要从一个 Dialogflow 版本切换到另一个版本,您需要 然后再创建新的集成
删除集成
如果您需要从 Business Messages 代理中移除 Dialogflow,请删除 进行集成
cURL
# This code deletes an integration. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#delete_the_integration # Replace the __BRAND_ID__, __AGENT_ID__ and __INTEGRATION_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X DELETE \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
Node.js
/** * This code snippet deletes an integration. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#delete_the_integration * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const BRAND_ID = 'EDIT_HERE'; const AGENT_ID = 'EDIT_HERE'; const INTEGRATION_ID = 'EDIT_HERE'; const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); const uuidv4 = require('uuid').v4; // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); const integrationName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID + '/integrations/' + INTEGRATION_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: integrationName, }; bcApi.brands.agents.integrations.delete(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code snippet deletes an integration. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#delete_the_integration This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( BusinesscommunicationsBrandsAgentsIntegrationsDeleteRequest ) # Edit the values below: BRAND_ID = 'EDIT_HERE' AGENT_ID = 'EDIT_HERE' INTEGRATION_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client) integration_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID + '/integrations/' + INTEGRATION_ID integration = integrations_service.Delete(BusinesscommunicationsBrandsAgentsIntegrationsDeleteRequest( name=integration_name )) print(integration)
如需了解格式设置和值选项,请参阅 Integration
。
获取集成信息
如需获取有关集成的信息,您可以使用 Business Communications
只要您拥有集成的 name
值即可。
获取单个集成的信息
如需获取集成信息,请运行以下命令。
cURL
# This code gets information about an integration. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#get_info_for_a_single_integration # Replace the __BRAND_ID__, __AGENT_ID__ and __INTEGRATION_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X GET \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
Node.js
/** * This code snippet gets information about an integration. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#get_info_for_a_single_integration * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const BRAND_ID = 'EDIT_HERE'; const AGENT_ID = 'EDIT_HERE'; const INTEGRATION_ID = 'EDIT_HERE'; const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); const uuidv4 = require('uuid').v4; // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); const integrationName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID + '/integrations/' + INTEGRATION_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: integrationName, }; bcApi.brands.agents.integrations.get(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code snippet gets information about an integration. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#get_info_for_a_single_integration This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( BusinesscommunicationsBrandsAgentsIntegrationsDeleteRequest ) # Edit the values below: BRAND_ID = 'EDIT_HERE' AGENT_ID = 'EDIT_HERE' INTEGRATION_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client) integration_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID + '/integrations/' + INTEGRATION_ID integration = integrations_service.Get(BusinesscommunicationsBrandsAgentsIntegrationsDeleteRequest( name=integration_name )) print(integration)
如需了解格式设置和值选项,请参阅 Integration
。
列出代理的所有集成
如果您不知道集成的名称,则可以获取所有 与代理关联的集成(只需省略 INTEGRATION_ID) 值。
cURL
# This code lists all integrations for an agent. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#list_all_integrations_for_an_agent # Replace the __BRAND_ID__ and __AGENT_ID__ # Make sure a service account key file exists at ./service_account_key.json curl -X GET \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
Node.js
/** * This code snippet lists all integrations for an agent. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#list_all_integrations_for_an_agent * * This code is based on the https://github.com/google-business-communications/nodejs-businesscommunications Node.js * Business Communications client library. */ /** * Edit the values below: */ const BRAND_ID = 'EDIT_HERE'; const AGENT_ID = 'EDIT_HERE'; const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json'; const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); const uuidv4 = require('uuid').v4; // Initialize the Business Communications API const bcApi = new businesscommunications.businesscommunications_v1.Businesscommunications({}); // Set the scope that we need for the Business Communications API const scopes = [ 'https://www.googleapis.com/auth/businesscommunications', ]; // Set the private key to the service account file const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY); async function main() { const authClient = await initCredentials(); if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, parent: 'brands/' + BRAND_ID + '/agents/' + AGENT_ID, }; bcApi.brands.agents.integrations.list(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client const authClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, scopes, ); return new Promise(function(resolve, reject) { // Authenticate request authClient.authorize(function(err, tokens) { if (err) { reject(false); } else { resolve(authClient); } }); }); } main();
Python
"""This code snippet lists all integrations for an agent. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#list_all_integrations_for_an_agent This code is based on the https://github.com/google-business-communications/python-businessmessages Python Business Messages client library. """ from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1 from businesscommunications.businesscommunications_v1_messages import ( BusinesscommunicationsBrandsAgentsIntegrationsListRequest ) # Edit the values below: BRAND_ID = 'EDIT_HERE' AGENT_ID = 'EDIT_HERE' SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = './service_account_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client) agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID integration = integrations_service.List( BusinesscommunicationsBrandsAgentsIntegrationsListRequest(parent=agent_name) ) print(integration)
如需了解格式设置和值选项,请参阅 Integration
。
意图匹配
为 Business Messages 代理启用 Dialogflow 集成后,您的 代理可以使用您的 Dialogflow 项目的已配置意图来了解 无需编写代码即可回答用户的问题。要详细了解 intent,请参阅 Dialogflow ES 的文档 和 Dialogflow CX。
为您打算使用的每个对话选项配置 Dialogflow intent 实现自动化。Business Messages 代理依赖 Dialogflow 了解用户消息
调用 Dialogflow API 时,Business Messages 会将
用户消息载荷
您的 intent 和 fulfillment webhook。当用户消息匹配成功时
通过 intent,您可以在以下位置以 Struct
格式访问此载荷:
QueryParameters
中的 business_messages_payload
字段。
载荷包含用户消息中除 DialogflowResponse
之外的所有字段。
对于 Dialogflow CX,Business Messages 还会将一个名为 channel
且值为 google_business_messages
的会话参数传递给您的意图,您可以在代理中按以下格式引用该参数:$session.params.channel
。
此参数可用于向 Dialogflow 执行方式添加条件,以便在同一 Dialogflow 代理中支持多个渠道。
如需详细了解查询参数,请参阅 Dialogflow ES 和 Dialogflow CX 参考。
前提条件
在 Dialogflow 中创建 NLU 模型时,您可以配置不同的 响应类型。Business Messages 支持“默认”回复, 其中包括:
- 文本
- 自定义负载
- 人工客服转接(仅限 Dialogflow CX)
自定义载荷必须与有效的 Business Messages JSON 消息响应匹配 对象。 为 intent 配置自定义载荷响应时,Business Messages 忽略以下字段:
name
messageId
representative
请参阅以下示例响应。
包含建议的文本
{
"text": "Hello World!",
"fallback": "Hello World!\n\nReply with \"Hello\" or \"Hi!\"",
"suggestions": [
{
"reply": {
"text": "Hello",
"postbackData": "hello-formal"
}
},
{
"reply": {
"text": "Hi!",
"postbackData": "hello-informal"
}
}
]
}
复合信息卡
{
"fallback": "Hello, world!\nSent with Business Messages\n\nReply with \"Suggestion #1\" or \"Suggestion #2\"",
"richCard": {
"standaloneCard": {
"cardContent": {
"title": "Hello, world!",
"description": "Sent with Business Messages.",
"media": {
"height": "TALL",
"contentInfo":{
"altText": "Google logo",
"fileUrl": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
"forceRefresh": "false"
}
},
"suggestions": [
{
"reply": {
"text": "Suggestion #1",
"postbackData": "suggestion_1"
}
},
{
"reply": {
"text": "Suggestion #2",
"postbackData": "suggestion_2"
}
}
]
}
}
}
}
复合信息卡轮播界面
{
"fallback": "Card #1\nThe description for card #1\n\nCard #2\nThe description for card #2\n\nReply with \"Card #1\" or \"Card #2\"",
"richCard": {
"carouselCard": {
"cardWidth": "MEDIUM",
"cardContents": [
{
"title": "Card #1",
"description": "The description for card #1",
"suggestions": [
{
"reply": {
"text": "Card #1",
"postbackData": "card_1"
}
}
],
"media": {
"height": "MEDIUM",
"contentInfo": {
"fileUrl": "https://my.files/cute-dog.jpg",
"forceRefresh": false
}
}
},
{
"title": "Card #2",
"description": "The description for card #2",
"suggestions": [
{
"reply": {
"text": "Card #2",
"postbackData": "card_2"
}
}
],
"media": {
"height": "MEDIUM",
"contentInfo": {
"fileUrl": "https://my.files/elephant.jpg",
"forceRefresh": false
}
}
}
]
}
}
}
人工客服移交
{
"metadata": {}
}
常见问题解答聊天机器人
为 Business Messages 代理启用 Dialogflow ES 集成后, 可以创建常见问题解答聊天机器人当您提供问题和答案时, 支持的知识文档,Business Messages 和 Dialogflow 会创建 理解和响应用户问题所需的基础架构, 而无需编写代码
如需查看常见问题解答聊天机器人的实际运行情况,请与 Business Messages 常见问题解答进行聊天 Bot。
前提条件
在创建常见问题解答聊天机器人之前,你需要将问题和回答作为 知识文档(最多 50 MB):公开提供的 HTML 文件或 CSV 文件。
一般来说,知识文档
- 可以在答案中包含有限的 Markdown,如富媒体 文本。
- 大小不超过 50 MB。
- 问题/答案对不应超过 2000 对。
- 不要为重复的问题提供不同的答案。
对于 HTML 文件
- 来自公共网址的文件必须已被 Google 搜索索引器抓取, 以使它们存在于搜索索引中。您可以通过 Google Search Console。 请注意,索引器不会及时更新您的内容。您必须明确 在源内容发生变化时更新文档。
- Dialogflow 在创建响应时从内容中移除 HTML 标记。因为 因此,最好避免使用 HTML 标记,并尽可能使用纯文本。
- 不支持包含单个问题/答案对的文件。
对于 CSV 文件,
- 文件的第一列必须为问题,第二列必须为答案, 没有标题。
- 文件必须使用英文逗号作为分隔符。
创建常见问题解答聊天机器人
要创建常见问题解答聊天机器人,首先需要创建一个知识库来存储所有的 机器人的数据,然后将一个或多个包含问题/答案对的文档添加到 知识库。
创建知识库
如需创建知识库,请运行以下命令。将
BRAND_ID、AGENT_ID 和 INTEGRATION_ID
替换为文档 name
中的唯一值。将
将 KNOWLEDGE_BASE_DISPLAY_NAME 替换为您的
选择。
创建知识库后,您可以创建文档 。
cURL
# This code creates a knowledge base. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#create-knowledge-base # Replace the __BRAND_ID__, __AGENT_ID__, __INTEGRATION_ID__ and __KNOWLEDGE_BASE_DISPLAY_NAME__ # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__?updateMask=dialogflowEsIntegration.dialogflowKnowledgeBases" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "dialogflowEsIntegration": { "dialogflowKnowledgeBases": [ { "displayName": "__KNOWLEDGE_BASE_DISPLAY_NAME__" } ] } }'
如需了解格式设置和值选项,请参阅 DialogflowKnowledgebase
。
创建知识文档
如需创建知识文档,请运行以下命令。
将文档添加到现有文档列表,如果没有文档列表,则新建一个列表
已存在。现有文件的列表应包含该文件的name
值。
公开 HTML 文件
执行以下变量替换操作:
- BRAND_ID、AGENT_ID 和 INTEGRATION_ID
替换为集成的
name
中的唯一值 - KNOWLEDGE_BASE_DISPLAY_NAME和 DOCUMENT_DISPLAY_NAME: 识别您选择的字符串
利用此知识PUBLIC_URL 文档的公开网址
cURL
# This code creates a knowledge base document from an HTML document and adds it to the knowledge base. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#create-document # Replace the __BRAND_ID__, __AGENT_ID__, __INTEGRATION_ID__, __KNOWLEDGE_BASE_DISPLAY_NAME__, __DOCUMENT_DISPLAY_NAME__ and __PUBLIC_URL__ # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__?updateMask=dialogflowEsIntegration.dialogflowKnowledgeBases" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "dialogflowEsIntegration": { "dialogflowKnowledgeBases": [ { "displayName": "__KNOWLEDGE_BASE_DISPLAY_NAME__", "documents": [ { "displayName": "__DOCUMENT_DISPLAY_NAME__", "faqUrl": "__PUBLIC_URL__" } ] } ] } }'
本地 CSV 文件
执行以下变量替换操作:
- BRAND_ID、AGENT_ID 和 INTEGRATION_ID
替换为集成的
name
中的唯一值 - KNOWLEDGE_BASE_DISPLAY_NAME和 DOCUMENT_DISPLAY_NAME: 识别您选择的字符串
CSV_RAW_BYTES(包含 CSV 文件) 作为 Base64 编码的字符串
cURL
# This code creates a knowledge base document from a CSV document and adds it to the knowledge base. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#create-document # Replace the __BRAND_ID__, __AGENT_ID__, __INTEGRATION_ID__, __KNOWLEDGE_BASE_DISPLAY_NAME__, __DOCUMENT_DISPLAY_NAME__ and __CSV_RAW_BYTES__ # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__?updateMask=dialogflowEsIntegration.dialogflowKnowledgeBases" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "dialogflowEsIntegration": { "dialogflowKnowledgeBases": [ { "displayName": "__KNOWLEDGE_BASE_DISPLAY_NAME__", "documents": [ { "displayName": "__DOCUMENT_DISPLAY_NAME__", "rawContent": "__CSV_RAW_BYTES__" } ] } ] } }'
如需了解格式设置和值选项,请参阅 DialogflowKnowledgebase
。
将文档添加到知识库大约需要两分钟的时间。要查看
文档状态,获取集成的
OperationInfo
。
删除知识文档
如果您需要从 Business Messages 代理中移除问题/答案对, 使用以下命令删除包含这些变量的知识文档。
运行以下命令以删除单个现有文档。将
BRAND_ID、AGENT_ID 和 INTEGRATION_ID
替换为文档 name
中的唯一值。将
将 KNOWLEDGE_BASE_DISPLAY_NAME 替换为适当的字符串。
cURL
# This code deletes a knowledge base document. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#delete_a_knowledge_document # Replace the __BRAND_ID__, __AGENT_ID__, __INTEGRATION_ID__ and __KNOWLEDGE_BASE_DISPLAY_NAME__ # To delete all knowledge bases, set dialogflowKnowledgeBases to an empty list. Otherwise, the list should contain all existing knowledgebases except the one you would like to remove. # Make sure a service account key file exists at ./service_account_key.json curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/agents/__AGENT_ID__/integrations/__INTEGRATION_ID__?updateMask=dialogflowEsIntegration.dialogflowKnowledgeBases" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "dialogflowEsIntegration": { "dialogflowKnowledgeBases": [ { "displayName": "__KNOWLEDGE_BASE_DISPLAY_NAME__" } ] } }'
如需了解格式设置和值选项,请参阅 DialogflowKnowledgebase
。
自动回复
如果您在 Dialogflow 集成期间启用自动回复,则 “信息”应用会通过 Dialogflow 自动回复用户。您的商家 信息代理以最高置信度匹配来响应。使用 Dialogflow ES 集成(如果常见问题解答和 自定义的意向细分受众群,Business Messages 会在响应中提供 置信度。
Business Messages 将所有自动回复消息标记为来自BOT
代表。如果您的代理支持实时代理,
Business Messages 在 REPRESENTATIVE_JOINED
后暂停自动回复
事件
并在 REPRESENTATIVE_LEFT
事件后恢复自动响应。请参阅 Handoff
从聊天机器人到人工客服。
自动回复常见问题解答
使用 Dialogflow ES 集成时,如果常见问题解答的答案可信度最高 Business Messages 将回答映射到短信。如果有 相关但不同的答案,则该消息会显示“查看其他 答案建议。否则,系统会在消息中包含问题和建议 进行回复,询问消息是否满足用户请求。
使用 intent 响应自动回复
intent 响应可以包含以下一项或多项响应。
如果意图响应具有最高置信度匹配,则以下 。
- 如果响应包含至少一个 Text 值,Business Messages 会将此 值转换为文本消息。
- 如果响应至少有一个自定义载荷具有有效的 Business 信息 JSON 对象结构,Business Messages 使用 提供的 JSON 对象。
- 如果响应至少有一条人工客服移交响应,请参阅 通过人工客服请求自动回复。
由于 Dialogflow 可以在一个意图匹配中包含多个响应, Business Messages 发送每次短信、自定义载荷或人工客服移交 作为一条单独的消息回复如果一个 intent 中有多条消息 匹配,但其中一些格式有误,Business Messages 仅发送有效的 自动回复邮件。
通过人工客服请求自动回复
Dialogflow CX 支持人工客服移交 响应。它表明对话应该交由真人处理 它还可以让您为移交流程传递自定义元数据, 过程。如果意图响应具有最高置信度匹配,并且 包括人工客服转接,Business Messages 会向用户发送 人工客服请求的活动 添加到网络钩子。如需处理此事件,请参阅 将聊天机器人移交给人工客服。
使用后备消息进行自动回复
如果 Dialogflow 未获得高置信度的匹配,Business Messages 将发送 作为后备响应在 Dialogflow ES 中,回退的处理方式 Dialogflow CX。
Dialogflow ES
对于常见问题解答聊天机器人,如果找不到常见问题解答的答案,Business Messages 就会发送 提示无法找到答案的后备消息。
对于已配置的意图,如果没有与意图响应匹配的意图, “信息”应用会发送回退 intent 响应。 您可以使用 Dialogflow 提供的后备文本,也可以配置 使用额外的文本和自定义载荷进行后备
这是一个后备意图响应示例 可以接收:
{
"intentResponses": [
{
"intentName": "projects/df-integration/agent/intents/12345",
"intentDisplayName": "Default Fallback Intent",
"intentDetectionConfidence": "1.0",
"fulfillmentMessages": [
{
"text": "One more time?"
}
]
}
]
}
Dialogflow 会预填充 intent_name
和 intent_display_name
。
Dialogflow CX
Dialogflow CX 将后备意图响应作为 内置事件的事件。 如果找不到与意图响应相匹配的内容,Business Messages 会发送 Dialogflow 中非匹配默认事件中的后备消息。您可以 使用 Dialogflow 提供的后备文本,或者配置 提供额外的文本、自定义载荷和人工客服移交选项。
这是一个后备意图响应示例, webhook 可以接收:
{
"intentResponses": [
{
"intentName": "sys.no-match-default",
"intentDisplayName": "Default Fallback Intent",
"intentDetectionConfidence": "0.3",
"fulfillmentMessages": [
{
"text": "I missed that, say that again?"
}
]
}
]
}
Business Messages 硬编码 intent_name
和 intent_display_name
。
Dialogflow 专用字段
启用 Dialogflow 集成后,用户可向代理发送消息
会接收
添加
dialogflowResponse
对象。网络钩子会收到所有用户消息的载荷
Business Messages 是否会自动回复您
。要检查自动回复,请查看
autoResponded
字段并决定是否需要回复用户。
Dialogflow ES
... "dialogflowResponse": { "queryText": "TEXT", "intentResponse": { "intentName": "INTENT_ID", "intentDisplayName": "INTENT_NAME", "intentDetectionConfidence": "CONFIDENCE_NUMERIC", "fulfillmentMessages": [{ "text": "FULFILLMENT_TEXT", "jsonPayload": "JSON", "error": "ERROR_STATUS", }], "faqResponse": { "userQuestion": "USER_QUESTION", "answers": [{ "faqQuestion": "FAQ_QUESTION", "faqAnswer": "FAQ_ANSWER", "matchConfidenceLevel": "CONFIDENCE_LEVEL", "matchConfidence": "CONFIDENCE_NUMERIC", }], }, "autoResponded": "BOOLEAN", "autoRespondedMessages": [{ "message": "MESSAGE_JSON", "responseSource": "SOURCE", }], }, ...
字段 | 说明 |
---|---|
queryText
|
原始对话查询文本。如果使用自动拼写
已针对 Dialogflow 模型 queryText 启用更正功能
包含更正后的用户输入。 |
intentName |
匹配意图的唯一标识符。 |
intentDisplayName |
匹配的意图的名称。 |
intentDetectionConfidence
|
匹配的数字置信度分数
介于 queryText 和 intentName 之间。 |
text |
文本响应。 |
jsonPayload
|
自定义载荷响应。
此字符串与自定义
在 Dialogflow 中定义的载荷。
如果载荷不包含有效的 Business Messages JSON
对象结构,error 可以描述问题。 |
error |
包含 intent 执行方式消息的错误描述。 |
userQuestion |
用户提出的问题,由 Dialogflow 解析。 |
faqQuestion |
Dialogflow 中与用户的问题匹配的一个问题。 |
faqAnswer |
Dialogflow 中与用户的问题匹配的答案。 |
matchConfidenceLevel
|
对于
userQuestion 和faqQuestion 。 |
matchConfidence
|
以下各项之间的匹配项的置信度数字评分:
userQuestion 和faqQuestion 。 |
autoResponded
|
Business Messages 是否自动回复 并向用户提供 Dialogflow 的回答。 |
message |
自动响应的载荷。 |
responseSource
|
自动回复的来源。请参阅
ResponseSource 。 |
Dialogflow CX
... "dialogflowResponse": { "queryText": "TEXT", "intentResponse": { "intentName": "INTENT_ID", "intentDisplayName": "INTENT_NAME", "intentDetectionConfidence": "CONFIDENCE_NUMERIC", "fulfillmentMessages": [{ "text": "FULFILLMENT_TEXT", "jsonPayload": "JSON", "error": "ERROR_STATUS", "liveAgentHandoff": { "metadata": {} } }], "autoResponded": "BOOLEAN", "autoRespondedMessages": [{ "message": "MESSAGE_JSON", "responseSource": "SOURCE", }], }, ...
字段 | 说明 |
---|---|
queryText
|
原始对话查询文本。如果使用自动拼写
已针对 Dialogflow 模型 queryText 启用更正功能
包含更正后的用户输入。 |
intentName |
匹配意图的唯一标识符。 |
intentDisplayName |
匹配的意图的名称。 |
intentDetectionConfidence
|
匹配的数字置信度分数
介于 queryText 和 intentName 之间。 |
text |
文本响应。 |
jsonPayload
|
自定义载荷响应。
此字符串与自定义
在 Dialogflow 中定义的载荷。
如果载荷不包含有效的 Business Messages JSON
对象结构,error 可以描述问题。 |
error |
包含 intent 执行方式消息的错误描述。 |
liveAgentHandoff |
用于人工客服移交流程的自定义元数据。 |
autoResponded
|
Business Messages 是否自动回复 并向用户提供 Dialogflow 的回答。 |
message |
自动响应的载荷。 |
responseSource
|
自动回复的来源。请参阅
ResponseSource 。 |