如果服務專員代表的品牌有實體店面,你就可以將品牌建立關聯 聯絡服務專員的位置,以便使用者透過 Business Messages。Business Messages 會根據地點 ID 識別地點。
您可利用地點 ID 辨別使用者可傳送訊息的實際位置 判斷回應使用者的最佳途徑
你必須透過以下方式聲明實體地點的擁有權: 商家檔案 ,才能將地點與 Business Messages 服務專員建立關聯
管理多個位置
如果品牌是連鎖品牌的一員,請務必加入該連鎖店的所有營業地點, 您有權啟用訊息功能。如要驗證連鎖店的所有營業地點, 您就會讓 驗證 要求一個位置我們驗證完營業地點後,就會自動 驗證您新增的其他相關地點。
驗證完成後,如果您新增其他地點,請 重新驗證地點。
如要查看與虛擬服務專員相關聯的位置,請參閱列出
品牌,並依 agent 值篩選結果。
建立地點
如要為服務專員新增地點,請透過「Business
通訊
API
建立品牌位置,並新增服務專員的
name 值設為位置。
必要條件
建立位置資訊前,您必須收集幾個項目:
- 開發機器上 GCP 專案服務帳戶金鑰的路徑
品牌的
name,與 Business Communications API 中顯示的相同 (適用於 例如:「brands/12345」)如果不知道品牌的
name,請參閱brands.list。代理程式的
name,與 Business Communications API 一樣 (針對 例如:「brands/12345/agents/67890」如果您不知道代理程式的
name,請參閱列出 品牌。地點的地點 ID
使用「地點 ID」 Finder。 如果該地點為「服務區域」 商家 請使用區域服務商家地點 ID,而非單一地址 搜尋器 。
位置通常在使用的語言代碼,由 雙字元 ISO 639-1 語言 程式碼
建立地點
資訊收集完畢後,就可以建立地點。在 終端機上,執行下列指令。
將醒目顯示的變數換成您在
必要條件。將 BRAND_ID 替換為
品牌的 name 值,接在「brands/」後舉例來說,如果 name 是
「brands/12345」,品牌 ID 為「12345」。
cURL
# 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",
}'
Node.js
/**
* 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();
Java
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();
}
}
}
這個程式碼是根據
Java 企業
通訊用戶端程式庫。Python
"""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_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。
cURL
# 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)"
Node.js
/**
* 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();
Java
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();
}
}
}
這個程式碼是根據
Java 企業
通訊用戶端程式庫。Python
"""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 值
要求網址
cURL
# 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)"
Node.js
/**
* 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();
Java
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 locations = request.execute().getLocations();
locations.stream().forEach(location -> {
try {
System.out.println(location.toPrettyString());
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
這個程式碼是根據
Java 企業
通訊用戶端程式庫。Python
"""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。
更新位置
如要更新地點,請向商家提出 PATCH 要求 通訊 API。發出 API 呼叫時,您會納入 也就是要編輯的「updateMask」值網址參數。
舉例來說,如果您更新 defaultLocale 和代理程式欄位, 「updateMask」網址參數是「updateMask=defaultLocale,agent」。
如需格式設定和值選項,請參閱:
brands.locations.patch。
如果您不知道某地點的 name,請參閱列出
品牌。
範例:更新預設語言代碼
cURL
# 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"
}'
Node.js
/**
* 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();
Java
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();
}
}
}
這個程式碼是根據
Java 企業
通訊用戶端程式庫。Python
"""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 Google 帳戶。如要註冊帳戶,請參閱 向商家註冊 訊息)。
如要刪除位置,請執行下列指令。取代 BRAND_ID
和 LOCATION_ID 替換為地點 name 中的不重複值。
cURL
# 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)"
Node.js
/**
* 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();
Java
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();
}
}
}
這個程式碼是根據
Java 企業
通訊用戶端程式庫。Python
"""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。
後續步驟
建立含有地點的服務專員後,現在可以設計訊息 流程。