如果某个代理所代表的品牌有实体营业地点,您可以将这些营业地点与该代理相关联,以便用户可以通过 Business Messages 向特定营业地点发送消息。Business Messages 按地点 ID 识别营业地点。
借助地点 ID,您可以获悉用户收发信息的实际位置并确定针对用户采取的最合适的操作。
您必须先通过商家资料声明对实体营业地点的所有权,然后才能将营业地点与 Business Messages 代理相关联。
管理多个营业地点
如果品牌属于某个连锁企业,您必须为该连锁企业添加您有权启用消息功能的所有营业地点。如需验证该连锁企业的所有营业地点,您需要针对其中一个营业地点提交一个验证请求。验证该营业地点后,我们会自动验证您添加的其他关联的营业地点。
验证后,如果您添加其他营业地点,则需要再次请求营业地点验证。
如需查看与该代理关联的营业地点,请参阅列出品牌的所有营业地点并按结果的 agent
值过滤结果。
创建营业地点
如需向某个代理添加营业地点,您可以使用 Business Communications API 提交请求,以创建品牌营业地点,并通过向这个营业地点添加该代理的 name
值来关联该代理。
前提条件
在创建营业地点之前,您需要收集以下几项信息:
- 开发机器上的 GCP 项目的服务账号密钥的路径
品牌的
name
,如 Business Communications API 中所示(例如“brands/12345”)如果您不知道品牌的
name
,请参阅brands.list
。代理的
name
,如 Business Communications API 中所示(例如“brands/12345/agents/67890”)如果您不知道代理的
name
,请参阅列出品牌的所有代理。营业地点的地点 ID
使用地点 ID 查找工具识别某个营业地点的地点 ID。如果该营业地点属于上门服务 商家 而非单个地址,请使用上门服务商家地点 ID 查找工具 。
营业地点在一般情况下使用的语言区域,由两个字符的 ISO 639-1 语言代码指定
创建营业地点
收集完信息后,您就可以创建营业地点了。在终端运行以下命令。
将突出显示的变量替换为您在前提条件中确定的值。将 BRAND_ID 替换为相应品牌 name
值中紧随“brands/”后面的部分。例如,如果 name
为“brands/12345”,那么品牌 ID 为“12345”。
# This code creates a location where a brand is available.
# Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/create
# Replace the __BRAND_ID__ and __PLACE_ID__
# Make sure a service account key file exists at ./service_account_key.json
curl -X POST "https://businesscommunications.googleapis.com/v1/brands/__BRAND_ID__/locations" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-communications" \
-H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \
-d '{
"placeId": "__PLACE_ID__",
"agent": "brands/__BRAND_ID__/agents/__AGENT_ID__",
"defaultLocale": "en",
}'
/**
* This code snippet creates a location.
* Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/create
*
* 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 PLACE_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 brandName = 'brands/' + BRAND_ID;
const agentName = 'brands/' + BRAND_ID + 'agents/' + AGENT_ID;
if (authClient) {
const locationObject = {
placeId: PLACE_ID,
agent: agentName,
defaultLocale: 'en',
};
// setup the parameters for the API call
const apiParams = {
auth: authClient,
parent: brandName,
resource: locationObject,
};
bcApi.brands.locations.create(apiParams, {}, (err, response) => {
if (err !== undefined && err !== null) {
console.dir(err);
} else {
// Location 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();
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.businesscommunications.v1.BusinessCommunications;
import com.google.api.services.businesscommunications.v1.model.Location;
import com.google.common.collect.ImmutableMap;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class Main {
/**
* Initializes credentials used by the Business Communications API.
*/
private static BusinessCommunications.Builder getBusinessCommunicationsBuilder() {
BusinessCommunications.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY "));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businesscommunications"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Communications API
builder = new BusinessCommunications
.Builder(httpTransport, jsonFactory, null)
.setApplicationName(credential.getServiceAccountProjectId());
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
// Create client library reference
BusinessCommunications.Builder builder = getBusinessCommunicationsBuilder();
String brandName = "brands/BRAND_ID ";
BusinessCommunications.Brands.Locations.Create request = builder
.build().brands().locations().create(brandName,
new Location()
.setDefaultLocale("LOCALE ")
.setAgent("FULL_AGENT_NAME ")
.setPlaceId("PLACE_ID "));
Location location = request.execute();
System.out.println(location.toPrettyString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
"""This code creates a location where a brand is available.
Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/create
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 (
BusinesscommunicationsBrandsLocationsCreateRequest,
Location
)
# Edit the values below:
BRAND_ID = 'EDIT_HERE'
AGENT_ID = 'EDIT_HERE'
PLACE_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)
locations_service = BusinesscommunicationsV1.BrandsLocationsService(client)
brand_name = 'brands/' + BRAND_ID
agent_name = 'brands/' + BRAND_ID + 'agents/' + AGENT_ID
location = locations_service.Create(BusinesscommunicationsBrandsLocationsCreateRequest(
location=Location(
agent=agent_name,
placeId=PLACE_ID,
defaultLocale='en'
),
parent=brand_name
))
print(location)
如需了解格式设置和值选项,请参阅 brands.locations.create
。
在您创建营业地点后,Business Communications API 会识别其他关联的营业地点,并为同一品牌和代理创建营业地点。
商店信息
当您创建营业地点时,Business Communications API 会返回该营业地点的值,包括 name
和 testUrls
。
将 name
存储在您可以访问的位置。您需要 name
才能更新营业地点。
测试营业地点
每个代理都有测试网址,可让您查看与该代理的对话会以何种方式显示给用户,并让您有机会验证消息功能基础架构。
TestUrl 具有 url
和 surface
属性。如需使用 iOS 设备测试营业地点网址,请使用 Surface 值为 SURFACE_IOS_MAPS
的测试网址。使用已安装 Google 地图应用的 iOS 设备打开网址,即可在与关联的代理对话时享用全面的功能。
Android 设备有两个测试网址。surface
值为 SURFACE_ANDROID_MAPS
的网址会在 Google 地图中会打开对话,并代表在 Google 地图中显示的对话入口点。surface
值为 SURFACE_ANDROID_WEB
的网址会在叠加对话视图中打开对话,并代表所有其他入口点。
对话界面打开后,对话会包含用户会看到的所有品牌信息,当您向代理发送消息时,webhook 会收到消息,包括您与用户沟通时会遇到的完整 JSON 载荷。营业地点信息显示在 context
字段中。
如需打开某个营业地点的测试网址,请点按链接或使用 Business Messages 代理启动器。由于浏览器安全措施,您无法通过复制和粘贴操作或以其他方式手动前往某个测试网址。
获取营业地点信息
如需获取有关某个营业地点的信息(如 locationTestUrl
),您可以从 Business Communications API 获取该信息,前提是您有该营业地点的 name
值。
获取单个营业地点的信息
如需获取营业地点信息,请运行以下命令。将 BRAND_ID 和 LOCATION_ID 替换为该营业地点的 name
中的唯一值。
# This code gets the location where a brand is available.
# Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/get
# Replace the __BRAND_ID__ and __LOCATION_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__/locations/__LOCATION_ID__" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-communications" \
-H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
/**
* This code snippet gets the location of a brand.
* Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/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 BRAND_ID = 'EDIT_HERE';
const LOCATION_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();
if (authClient) {
const apiParams = {
auth: authClient,
name: 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID,
};
bcApi.brands.locations.get(apiParams, {}, (err, response) => {
if (err !== undefined && err !== null) {
console.dir(err);
} else {
// Location 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();
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.businesscommunications.v1.BusinessCommunications;
import com.google.api.services.businesscommunications.v1.model.Location;
import com.google.common.collect.ImmutableMap;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class Main {
/**
* Initializes credentials used by the Business Communications API.
*/
private static BusinessCommunications.Builder getBusinessCommunicationsBuilder() {
BusinessCommunications.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY "));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businesscommunications"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Communications API
builder = new BusinessCommunications
.Builder(httpTransport, jsonFactory, null)
.setApplicationName(credential.getServiceAccountProjectId());
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
// Create client library reference
BusinessCommunications.Builder builder = getBusinessCommunicationsBuilder();
BusinessCommunications.Brands.Locations.Get request = builder
.build().brands().locations().get("brands/BRAND_ID /locations/LOCATION_ID ");
Location location = request.execute();
System.out.println(location.toPrettyString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
"""This code gets the location where a brand is available.
Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/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 (
BusinesscommunicationsBrandsLocationsGetRequest,
Location
)
# Edit the values below:
BRAND_ID = 'EDIT_HERE'
LOCATION_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)
locations_service = BusinesscommunicationsV1.BrandsLocationsService(client)
location_name = 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID
location = locations_service.Get(
BusinesscommunicationsBrandsLocationsGetRequest(name=location_name)
)
print(location)
如需了解格式设置和值选项,请参阅 brands.locations.get
。
列出品牌的所有营业地点
如果您不知道营业地点的 name
,则可以通过省略 GET 请求网址中的 LOCATION_ID 值来获取与品牌关联的所有代理的信息。
# This code gets all locations where a brand is available.
# Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/list
# Replace the __BRAND_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__/locations/" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-communications" \
-H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
/**
* This code snippet lists the locations of a brand.
* Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/list
*
* 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 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();
if (authClient) {
const apiParams = {
auth: authClient,
parent: 'brands/' + BRAND_ID,
};
bcApi.brands.locations.list(apiParams, {}, (err, response) => {
if (err !== undefined && err !== null) {
console.dir(err);
} else {
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();
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.businesscommunications.v1.BusinessCommunications;
import com.google.api.services.businesscommunications.v1.model.Location;
import com.google.common.collect.ImmutableMap;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class Main {
/**
* Initializes credentials used by the Business Communications API.
*/
private static BusinessCommunications.Builder getBusinessCommunicationsBuilder() {
BusinessCommunications.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY "));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businesscommunications"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Communications API
builder = new BusinessCommunications
.Builder(httpTransport, jsonFactory, null)
.setApplicationName(credential.getServiceAccountProjectId());
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
// Create client library reference
BusinessCommunications.Builder builder = getBusinessCommunicationsBuilder();
BusinessCommunications.Brands.Locations.List request
= builder.build().brands().locations().list("brands/BRAND_ID ");
List
"""This code gets all locations where a brand is available.
Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/list
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 (
BusinesscommunicationsBrandsLocationsListRequest,
Location
)
# Edit the values below:
BRAND_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)
locations_service = BusinesscommunicationsV1.BrandsLocationsService(client)
location_name = 'brands/' + BRAND_ID + '/locations'
locations = locations_service.List(
BusinesscommunicationsBrandsLocationsListRequest(name=location_name)
)
print(locations)
如需了解格式设置和值选项,请参阅 brands.locations.list
。
更新营业地点
如需更新营业地点,您可以使用 Business Communications API 执行 PATCH 请求。进行 API 调用时,您可以将要修改字段的名称添加为“updateMask”网址参数的值。
例如,如果您更新 defaultLocale 和 agent 字段,那么“updateMask”网址参数为“updateMask=defaultLocale,agent”。
如需了解格式设置和值选项,请参阅 brands.locations.patch
。
如果您不知道某个营业地点的 name
,请参阅列出品牌的所有营业地点。
示例:更新默认语言区域
# This code updates the default locale of an agent.
# Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/patch
# Replace the __BRAND_ID__ and __LOCATION_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__/locations/__LOCATION_ID__?updateMask=defaultLocale" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-communications" \
-H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \
-d '{
"defaultLocale": "en"
}'
/**
* This code snippet updates the defaultLocale of a Business Messages agent.
* Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/patch
*
* 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 LOCATION_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();
if (authClient) {
const locationObject = {
defaultLocale: 'en'
};
const apiParams = {
auth: authClient,
name: 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID,
resource: locationObject,
updateMask: 'defaultLocale',
};
bcApi.brands.locations.patch(apiParams, {}, (err, response) => {
if (err !== undefined && err !== null) {
console.dir(err);
} else {
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();
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.businesscommunications.v1.BusinessCommunications;
import com.google.api.services.businesscommunications.v1.model.Location;
import com.google.common.collect.ImmutableMap;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class Main {
/**
* Initializes credentials used by the Business Communications API.
*/
private static BusinessCommunications.Builder getBusinessCommunicationsBuilder() {
BusinessCommunications.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY "));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businesscommunications"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Communications API
builder = new BusinessCommunications
.Builder(httpTransport, jsonFactory, null)
.setApplicationName(credential.getServiceAccountProjectId());
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
// Create client library reference
BusinessCommunications.Builder builder = getBusinessCommunicationsBuilder();
BusinessCommunications.Brands.Locations.Patch request =
builder.build().brands().locations().patch("brands/BRAND_ID /locations/LOCATION_ID ",
new Location()
.setDefaultLocale("en "));
request.setUpdateMask("defaultLocale");
Location location = request.execute();
System.out.println(location.toPrettyString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
"""This code updates the default locale of an agent.
Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/patch
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 (
BusinesscommunicationsBrandsLocationsPatchRequest,
Location
)
# Edit the values below:
BRAND_ID = 'EDIT_HERE'
LOCATION_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)
locations_service = BusinesscommunicationsV1.BrandsLocationsService(client)
location = Location(defaultLocale='US')
location_name = 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID
updated_location = locations_service.Patch(
BusinesscommunicationsBrandsLocationsPatchRequest(
location=location,
name=location_name,
updateMask='defaultLocale'
)
)
print(updated_location)
删除营业地点
在您删除代理后,Business Messages 会删除所有营业地点数据。Business Messages 不会删除代理所发送的与营业地点相关的消息,这些消息将传输至用户设备或存储在用户设备上。发送给用户的消息不是营业地点数据。
如果您已尝试对营业地点进行一次或多次验证,删除请求会失败。如需删除您已验证或尝试验证的营业地点,请与我们联系。(您必须先使用 Business Messages Google 账号登录。如需注册账号,请参阅在 Business Messages 中注册。)
如需删除营业地点,请运行以下命令。将 BRAND_ID 和 LOCATION_ID 替换为该营业地点的 name
中的唯一值。
# This code deletes a location where a brand is available.
# Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/delete
# Replace the __BRAND_ID__ and __LOCATION_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__/locations/__LOCATION_ID__" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-communications" \
-H "$(oauth2l header --json ./service_account_key.json businesscommunications)"
/**
* This code snippet deletes a location.
* Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/delete
*
* 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 LOCATION_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();
if (authClient) {
const apiParams = {
auth: authClient,
name: 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID,
};
bcApi.brands.locations.delete(apiParams, {}, (err, response) => {
if (err !== undefined && err !== null) {
console.dir(err);
} else {
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();
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.businesscommunications.v1.BusinessCommunications;
import com.google.common.collect.ImmutableMap;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class Main {
/**
* Initializes credentials used by the Business Communications API.
*/
private static BusinessCommunications.Builder getBusinessCommunicationsBuilder() {
BusinessCommunications.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY "));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businesscommunications"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Communications API
builder = new BusinessCommunications
.Builder(httpTransport, jsonFactory, null)
.setApplicationName(credential.getServiceAccountProjectId());
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
// Create client library reference
BusinessCommunications.Builder builder = getBusinessCommunicationsBuilder();
BusinessCommunications.Brands.Locations.Delete request = builder.build().brands().locations()
.delete("brands/BRAND_ID /locations/LOCATION_ID ");
System.out.println(request.execute().toPrettyString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
"""This code deletes a location where a brand is available.
Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.locations/delete
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 (
BusinesscommunicationsBrandsLocationsDeleteRequest,
LocationEntryPointConfig,
Location
)
# Edit the values below:
BRAND_ID = 'EDIT_HERE'
LOCATION_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)
locations_service = BusinesscommunicationsV1.BrandsLocationsService(client)
location_name = 'brands/' + BRAND_ID + '/locations/' + LOCATION_ID
location = locations_service.Delete(BusinesscommunicationsBrandsLocationsDeleteRequest(
name=location_name
))
print(location)
如需了解格式设置和值选项,请参阅 brands.locations.delete
。
后续步骤
现在,您已经有了包含营业地点的代理,可以设计消息传递流程了。