หลังจากลงทะเบียนเป็นพาร์ทเนอร์แล้ว คุณจะเปิดใช้การสนทนาสำหรับแบรนด์ที่คุณจัดการได้โดยสร้างตัวแทน Business Messages ให้กับแบรนด์เหล่านั้น
Agent เป็นเอนทิตีการสนทนาที่ผู้ใช้โต้ตอบด้วย คุณสร้างตัวแทน 1 คนสำหรับแต่ละแบรนด์ที่คุณจัดการ คุณสร้างและจัดการตัวแทนด้วย Business Communications API และควบคุมการรับส่งข้อความสำหรับตัวแทนได้ด้วย Business Messages API
ตัวแทนครอบคลุมการดำเนินงานทางธุรกิจของแบรนด์ เช่น การสนับสนุนออนไลน์และสถานที่ตั้งจริง (หากมี) แต่ละข้อความจะมีบริบทที่ผู้ใช้เริ่มต้นการสนทนา โครงสร้างพื้นฐานของการกำหนดเส้นทางข้อความจะตรวจพบว่าผู้ใช้ดูที่ตั้งธุรกิจที่เฉพาะเจาะจงหรือมองหาการสนับสนุนทั่วไปหรือไม่ และสามารถกำหนดเส้นทางข้อความไปยังปลายทางที่ถูกต้องได้
ตัวอย่างเช่น หากคุณจัดการแบรนด์ Growing Tree Bank ซึ่งมีเว็บไซต์และสถานที่ตั้ง 2 แห่ง คุณจะสร้างตัวแทน "Growing Tree Bank" ขึ้นมา 1 รายการ ผู้ใช้จะพบกับ Agent ผ่านการค้นหาทั่วไปหรือการค้นหาตามสถานที่ได้ และบริบทที่ผู้ใช้พบ Agent จะถูกส่งไปพร้อมกับแต่ละข้อความไปยังเว็บฮุคของคุณ คุณใช้บริบทดังกล่าวในการกำหนดเส้นทางข้อความไปยังเจ้าหน้าที่ที่สถานที่ตั้งหรือทีมสนับสนุนทางเว็บไซต์ ซึ่งจะเป็นผู้สร้างคำตอบและสนทนากับผู้ใช้ต่อไป
ความคาดหวัง
ก่อนสร้างตัวแทน โปรดทําความเข้าใจสิ่งที่ตัวแทนคาดหวังใน Business Messages
- ตัวแทนควรปฏิบัติตามหลักเกณฑ์การออกแบบที่ระบุไว้ในการออกแบบการสนทนาสำหรับ Business Messages
ตัวแทนควรมีเจ้าหน้าที่ที่พร้อมตอบคำถามเมื่อระบบอัตโนมัติดำเนินการตามคำขอหรือเมื่อผู้ใช้ส่งคำขอไม่ได้
ตัวแทนควรรักษาคะแนนความพึงพอใจของลูกค้า (CSAT) ไว้ที่อย่างน้อย 80% และอัตราการตอบกลับของผู้ขาย (MRR) อย่างน้อย 95% ตามที่ระบุไว้ในเมตริก
สร้างตัวแทน
หากต้องการสร้างตัวแทน คุณต้องรวบรวมและส่งข้อมูลเกี่ยวกับแบรนด์และวิธีที่คุณต้องการให้ตัวแทนปรากฏต่อผู้ใช้
หากคุณจัดการหลายแบรนด์ ให้ทำขั้นตอนข้างต้นซ้ำเพื่อสร้างตัวแทนสำหรับแต่ละแบรนด์
ข้อกำหนดเบื้องต้น
ก่อนที่จะสร้างตัวแทนสำหรับแบรนด์ที่คุณจัดการ คุณต้องรวบรวมข้อมูลต่อไปนี้
สภาพแวดล้อมในการพัฒนาซอฟต์แวร์
ข้อมูลเกี่ยวกับสภาพแวดล้อมในการพัฒนาซอฟต์แวร์
โปรเจ็กต์ GCP ที่เปิดใช้ Business Communications API
หากต้องการเปิดใช้ API โปรดดูลงทะเบียนกับ Business Messages
เส้นทางไปยังคีย์บัญชีบริการของโครงการ GCP ในเครื่องการพัฒนาของคุณ
รายละเอียดแบรนด์
ข้อมูลเกี่ยวกับแบรนด์ที่ตัวแทนเป็นตัวแทน
- ชื่อแบรนด์
- เว็บไซต์ของแบรนด์
- ตัวเลือกการติดต่อในเว็บไซต์ (ตามที่ให้คำจำกัดความไว้ใน
OPTION
) - หมายเลขโทรศัพท์สำหรับผู้บริโภค
- หมายเลขโทรศัพท์ที่เปิดใช้สำหรับการโอนสาย
ลักษณะที่ปรากฏและนโยบายของตัวแทน
ข้อมูลเกี่ยวกับวิธีที่ตัวแทนปรากฏต่อผู้ใช้และวิธีการทำงานของตัวแทน
- ชื่อตัวแทนตามที่ต้องการให้ปรากฏในการสนทนากับผู้ใช้
- นโยบายความเป็นส่วนตัวในฐานะ URL ที่เผยแพร่ต่อสาธารณะซึ่งขึ้นต้นด้วย "https://"
(แนะนำ) โลโก้ตัวแทน (1024x1024 พิกเซล) ในรูปแบบ URL ที่เผยแพร่ต่อสาธารณะ
ในการสนทนา โลโก้จะแสดงเป็นวงกลมเส้นผ่านศูนย์กลาง 1024 พิกเซล ตรวจสอบว่าโลโก้แสดงผลเป็นวงกลม
(ไม่บังคับ) รหัสตัวแทนที่กำหนดเอง ซึ่งระบุแบรนด์ในข้อความที่เว็บฮุคได้รับ
การโต้ตอบกับตัวแทน
ข้อมูลเกี่ยวกับวิธีที่ตัวแทนโต้ตอบกับผู้ใช้
ภาษาที่ตัวแทนของคุณมักจะใช้ในการดำเนินการ ซึ่งจะระบุโดยรหัสภาษา ISO 639-1 สองอักขระ
ข้อความต้อนรับของตัวแทน
ตัวแทนพูดทักทายผู้ใช้ว่าอย่างไร
ข้อความเริ่มการสนทนาของตัวแทน
รายการคำแนะนำซึ่งจะอยู่ในรูปแบบชิปการสนทนา (ตามที่ให้คำนิยามไว้ใน SuggestedReply) สำหรับการมีส่วนร่วมกับตัวแทน
- ข้อความที่แสดงต่อผู้ใช้ในชิป
- ข้อมูลระบบรายงานผล Conversion ซึ่งเป็นสตริงที่ส่งกลับไปยังเว็บฮุคของคุณในเพย์โหลดข้อความ หากผู้ใช้แตะชิป
เวลาแชทสดและอัตโนมัติ
เวลาเริ่มต้นรายวัน โดยเป็น
TimeOfDay
ออบเจ็กต์เช่น 8:15 น. คือ
{ "hours": 8, "minutes": 15, }
เวลาสิ้นสุดรายวัน โดยเป็น
TimeOfDay
ออบเจ็กต์เช่น 19:30 น. คือ
{ "hours": 19, "minutes": 30, }
วันที่เริ่มต้น ซึ่งเป็นวันแรกของสัปดาห์ที่ตัวแทนพร้อมพูดคุย (ตามที่กำหนดใน
DayOfWeek
)วันที่สิ้นสุด วันสุดท้ายของสัปดาห์ที่ตัวแทนพร้อมแชท (ตามที่กำหนดไว้ใน
DayOfWeek
)เขตเวลาที่ตัวแทนดำเนินการ (ตามที่ระบุไว้ในฐานข้อมูลเขตเวลาของ IANA เช่น "อเมริกา/Los_Angeles")
จุดแรกเข้าของตัวแทน
ข้อมูลเกี่ยวกับจุดที่ผู้ใช้จะเริ่มสนทนากับตัวแทนได้
จุดแรกเข้าของตัวแทนที่อนุญาต ซึ่งผู้ใช้สามารถเริ่มการสนทนากับตัวแทน (
NON_LOCAL
และ/หรือLOCATION
)
ข้อมูลเกี่ยวกับจุดแรกเข้า NON_LOCAL
(ไม่เกี่ยวข้องกับจุดแรกเข้าของ Google Ads)
ภูมิภาคที่อนุญาต (เป็นรหัสภูมิภาค CLDR) เพื่อจำกัดการเข้าถึงระดับภูมิภาคให้กับจุดแรกเข้า
NON_LOCAL
จุดตัวแทนจะมีหมายเลขโทรศัพท์ที่เชื่อมโยงกับหมายเลขได้เพียง 1 หมายเลข ไม่ว่าจะภูมิภาคใดก็ตาม หากต้องการหมายเลขโทรศัพท์ที่ต่างกันตามภูมิภาค คุณต้องสร้างตัวแทนที่แตกต่างกันสำหรับแต่ละภูมิภาค
หากต้องการเปิดใช้จุดแรกเข้าของ
NON_LOCAL
ในทุกภูมิภาคที่ใช้ได้ ให้ใช้001
เป็นรหัสภูมิภาคทั่วโลก
ติดตั้งและทดสอบ oauth2l
คำขอ Curl ทั้งหมดในเอกสารประกอบของเว็บไซต์นี้ใช้ oauth2l สำหรับการตรวจสอบสิทธิ์ หากต้องการใช้บรรทัดคำสั่งเพื่อเชื่อมต่อกับ Business Communications และ Business Messages API ให้ติดตั้ง oauth2l
วิธีติดตั้ง oauth2l ด้วย Python 3
ดาวน์โหลด oauth2l
เปลี่ยนไดเรกทอรีเป็น "./oauth2l/python"
ติดตั้ง oauth2l ด้วยคำสั่งต่อไปนี้
sudo python setup.sh build && sudo python setup.py install
ทดสอบว่า oauth2l สามารถสร้างโทเค็นการให้สิทธิ์ได้
oauth2l header --json resources/bm-agent-service-account-credentials.json businesscommunications
ตรวจสอบว่าเอาต์พุตสร้างโทเค็นการให้สิทธิ์ของแบบฟอร์มดังนี้
Authorization: Bearer AUTHORIZATION_BEARER_TOKEN
มีวิธีอื่นๆ ในการติดตั้ง oauth2l สำหรับระบบปฏิบัติการต่างๆ ไม่ว่าคุณจะใช้วิธีติดตั้งแบบใด โปรดตรวจสอบว่าคุณสามารถสร้างโทเค็นการให้สิทธิ์ตามข้อมูลเข้าสู่ระบบ JSON ของบัญชีบริการได้ก่อนที่จะดำเนินการต่อ
สร้างตัวแทน
เมื่อคุณรวบรวมข้อมูลเรียบร้อยแล้ว ก็ถึงเวลาสร้างตัวแทน
สร้างแบรนด์ที่ตัวแทนเป็นตัวแทน หากมีแบรนด์อยู่แล้ว ให้ดาวน์โหลด
name
ของแบรนด์และข้ามไปยังขั้นตอนถัดไปเรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัล
cURL
# This code creates a brand. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/create # Make sure a service account key file exists at ./service_account_key.json curl -X POST "https://businesscommunications.googleapis.com/v1/brands" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "displayName": "My first brand curl" }'
Node.js
/** * This code snippet creates a brand. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/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 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) { // Setup the parameters for the API call const apiParams = { auth: authClient, resource: { displayName: 'My first brand', }, }; bcApi.brands.create(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Brand created console.log(response.data); } }); } else { console.log('Authentication failure.'); } } /** * Initializes the Google credentials for calling the * Business Communcations 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('Failed to connect'); } 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.Brand; 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(); // Create a request to create a new brand BusinessCommunications.Brands.Create request = builder .build().brands().create(new Brand().setDisplayName("BRAND_NAME")); Brand brand = request.execute(); // Print newly created brand object System.out.println(brand.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }
รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business CommunicationsPython
"""This code creates a brand. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/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 ( Brand) # Edit the values below: 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) brands_service = BusinesscommunicationsV1.BrandsService(client) brand = brands_service.Create(Brand(displayName='My first brand')) print(brand)
จัดเก็บค่า
name
ที่ API แสดงผล คุณจะต้องใช้รหัสนี้ในการอัปเดต หรือสร้างตัวแทนหากต้องการอัปเดตหรือค้นหาแบรนด์ โปรดดู
brands
สร้างตัวแทน แทนที่ BRAND_ID ด้วยส่วนค่า
name
ของแบรนด์ที่ตามหลัง "brands/" เช่น หากname
คือ "brands/12345" รหัสแบรนด์จะเป็น "12345"เรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัล
cURL
# This code creates a Business Messages agent. # Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/agents?method=api#create_the_agent # Replace the __BRAND_ID__ with a brand id that you can create agents for # 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" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json ./service_account_key.json businesscommunications)" \ -d '{ "displayName": "My first agent", "businessMessagesAgent": { "logoUrl": "https://developers.google.com/identity/images/g-logo.png", "entryPointConfigs": [ { "allowedEntryPoint": "NON_LOCAL" } ], "customAgentId": "'$(uuidgen)'", "nonLocalConfig": { "regionCodes": [ "US", "CA" ], "contactOption": { "url": "https://www.your-company-website.com", "options": [ "EMAIL", "PHONE" ] }, "enabledDomains": [ "your-company-website.com" ], "phoneNumber": { "number": "+10000000000" }, "callDeflectionPhoneNumbers": [ { "number": "+10000000000" }, { "number": "+10000000000" } ] }, "conversationalSettings": { "en": { "welcomeMessage": { "text": "This is a sample welcome message" }, "privacyPolicy": { "url": "https://www.your-company-website.com/privacy" }, "conversationStarters": [ { "suggestion": { "reply": { "text": "Option 1", "postbackData": "postback_option_1" } } } ] } }, "defaultLocale": "en", "primaryAgentInteraction": { "interactionType": "HUMAN", "humanRepresentative": { "humanMessagingAvailability": { "hours": [ { "startTime": { "hours": 8, "minutes": 30 }, "endTime": { "hours": 20, "minutes": 0 }, "timeZone": "America/Los_Angeles", "startDay": "MONDAY", "endDay": "SATURDAY" } ] } } } } }'
Node.js
/** * This code snippet creates a Business Messages agent. * Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/agents?method=api#create_the_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 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 brandName = 'brands/' + BRAND_ID; if (authClient) { const agentObject = { displayName: 'My first agent', businessMessagesAgent: { customAgentId: uuidv4(), // Optional logoUrl: 'https://developers.google.com/identity/images/g-logo.png', entryPointConfigs: [ { allowedEntryPoint: 'NON_LOCAL', } ], nonLocalConfig: { // Configuration options for launching on non-local entry points // List of phone numbers for call deflection, values must be globally unique callDeflectionPhoneNumbers: [ { number: '+10000000000' }, { number: '+10000000001' }, ], // Contact information for the agent that displays with the messaging button contactOption: { options: [ 'EMAIL', 'PHONE' ], url: 'https://www.your-company-website.com', }, // Domains enabled for messaging within Search, values must be globally unique enabledDomains: [ 'your-company-website.com' ], // Agent's phone number. Overrides the `phone` field // for conversations started from non-local entry points phoneNumber: { number: '+10000000000' }, // List of CLDR region codes for countries where the agent is allowed to launch `NON_LOCAL` entry points regionCodes: [ 'US', 'CA' ] }, // Must match a conversational setting locale defaultLocale: 'en', conversationalSettings: { en: { privacyPolicy: { url: 'https://www.your-company-website.com/privacy' }, welcomeMessage: { text: 'This is a sample welcome message' }, conversationStarters: [ { suggestion: { reply: { text: 'Option 1', postbackData: 'postback_option_1', }, }, }, ], }, }, primaryAgentInteraction: { interactionType: 'HUMAN', humanRepresentative: { humanMessagingAvailability: { hours: [ { startTime: { hours: 8, minutes: 30 }, startDay: 'MONDAY', endDay: 'SATURDAY', endTime: { hours: 20, minutes: 0 }, timeZone: 'America/Los_Angeles', }, ], }, }, }, }, }; // Setup the parameters for the API call const apiParams = { auth: authClient, parent: brandName, resource: agentObject }; bcApi.brands.agents.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();
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.Brand; 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 = "BRAND_ID"; BusinessCommunications.Brands.Agents.Create request = builder .build().brands().agents().create(brandName, new Agent() .setDisplayName("AGENT_NAME") .setBusinessMessagesAgent(new BusinessMessagesAgent() .setCustomAgentId("CUSTOM_ID") // Optional .setLogoUrl("LOGO_URL") .setEntryPointConfigs(Arrays.asList( new BusinessMessagesEntryPointConfig() .setAllowedEntryPoint( "ENTRY_POINT_1"), new BusinessMessagesEntryPointConfig() .setAllowedEntryPoint( "ENTRY_POINT_2") )) // Configuration options for launching on non-local entry points .setNonLocalConfig(new NonLocalConfig() // List of phone numbers for call deflection, values must be globally unique .setCallDeflectionPhoneNumbers(Arrays.asList(new Phone().setNumber("DEFLECTION_PHONE_NUMBER_1"), new Phone().setNumber("DEFLECTION_PHONE_NUMBER_2"))) // Contact information for the agent that displays with the messaging button .setContactOption(new ContactOption().setOptions(Arrays.asList( "CONTACT_OPTION_1", "CONTACT_OPTION_2") ).setUrl("WEBSITE_URL")) // Domains enabled for messaging within Search, values must be globally unique .setEnabledDomains(Arrays.asList("ENABLED_DOMAIN_1", "ENABLED_DOMAIN_2")) // Agent's phone number. Overrides the `phone` field for // conversations started from non-local entry points .setPhoneNumber(new Phone().setNumber("CONTACT_PHONE_NUMBER")) // List of regions where this agent will be available .setRegionCodes(Arrays.asList("REGION_CODE"))) .setDefaultLocale("LOCALE") // Create a map between the language code and the initial conversation settings .setConversationalSettings(ImmutableMap.of("LOCALE", new ConversationalSetting() .setPrivacyPolicy(new PrivacyPolicy().setUrl("PRIVACY_POLICY_URL")) .setWelcomeMessage(new WelcomeMessage().setText("WELCOME_MESSAGE")) .setConversationStarters(Arrays.asList( new ConversationStarters().setSuggestion(new Suggestion() .setReply(new SuggestedReply().setText("SUGGESTION_TEXT").setPostbackData("SUGGESTION_POSTBACK_DATA"))) )))) .setPrimaryAgentInteraction(new SupportedAgentInteraction() .setInteractionType(InteractionType.HUMAN.toString()) .setHumanRepresentative(new HumanRepresentative() .setHumanMessagingAvailability(new MessagingAvailability() // Create a list of available hours .setHours(Arrays.asList(new Hours() .setStartTime(new TimeOfDay().setHours(START_TIME_HOURS).setMinutes(START_TIME_MINUTES)) .setStartDay("BEGINNING_DAY_OF_WEEK") .setEndDay("END_DAY_OF_WEEK") .setEndTime(new TimeOfDay().setHours(END_TIME_HOURS).setMinutes(END_TIME_MINUTES)) .setTimeZone("TIME_ZONE")))))) )); Agent agent = request.execute(); System.out.println(agent.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }
รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business CommunicationsPython
"""This code creates a Business Messages agent. Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/agents?method=api#create_the_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 ( Agent, BusinesscommunicationsBrandsAgentsCreateRequest, BusinessMessagesAgent, BusinessMessagesEntryPointConfig, ContactOption, ConversationalSetting, ConversationStarters, Hours, HumanRepresentative, MessagingAvailability, NonLocalConfig, Phone, PrivacyPolicy, Suggestion, SuggestedReply, SupportedAgentInteraction, TimeOfDay, WelcomeMessage ) # 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) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) brand_name = 'brands/ '+ BRAND_ID agent = Agent( displayName='My first agent', businessMessagesAgent=BusinessMessagesAgent( customAgentId='CUSTOM_ID', # Optional logoUrl='https://developers.google.com/identity/images/g-logo.png', entryPointConfigs=[BusinessMessagesEntryPointConfig( allowedEntryPoint=BusinessMessagesEntryPointConfig.AllowedEntryPointValueValuesEnum.LOCATION ), BusinessMessagesEntryPointConfig( allowedEntryPoint=BusinessMessagesEntryPointConfig.AllowedEntryPointValueValuesEnum.NON_LOCAL )], nonLocalConfig=NonLocalConfig( # List of phone numbers for call deflection, values must be globally unique # Generating a random phone number for demonstration purposes # This should be replaced with a real brand phone number callDeflectionPhoneNumbers=[Phone(number='+10000000000'), Phone(number='+10000000001')], # Contact information for the agent that displays with the messaging button contactOption=ContactOption( options=[ContactOption.OptionsValueListEntryValuesEnum.EMAIL, ContactOption.OptionsValueListEntryValuesEnum.PHONE], url='https://www.your-company-website.com' ), # Domains enabled for messaging within Search, values must be globally unique # Generating a random URL for demonstration purposes # This should be replaced with a real brand URL enabledDomains=['your-company-website.com'], # Agent's phone number. Overrides the `phone` field for conversations started from non-local entry points phoneNumber=Phone(number='+10000000000'), # List of CLDR region codes for countries where the agent is allowed to launch `NON_LOCAL` entry points. # Example is for launching in Canada and the USA regionCodes=['US', 'CA'] ), defaultLocale='en', conversationalSettings=BusinessMessagesAgent.ConversationalSettingsValue( additionalProperties=[BusinessMessagesAgent.ConversationalSettingsValue.AdditionalProperty( key='en', value=ConversationalSetting( privacyPolicy=PrivacyPolicy(url='https://www.your-company-website.com/privacy'), welcomeMessage=WelcomeMessage(text='This is a sample welcome message'), conversationStarters=[ ConversationStarters( suggestion=Suggestion( reply=SuggestedReply(text='Option 1', postbackData='postback_option_1') ) ) ] ) )] ), primaryAgentInteraction=SupportedAgentInteraction( interactionType=SupportedAgentInteraction.InteractionTypeValueValuesEnum.HUMAN, humanRepresentative=HumanRepresentative( humanMessagingAvailability=MessagingAvailability(hours=[ Hours( startTime=TimeOfDay(hours=8, minutes=30), startDay=Hours.StartDayValueValuesEnum.MONDAY, endDay=Hours.EndDayValueValuesEnum.SATURDAY, endTime=TimeOfDay(hours=20, minutes=0), timeZone='America/Los_Angeles' ) ]) ) ), ) ) new_agent = agents_service.Create( BusinesscommunicationsBrandsAgentsCreateRequest( agent=agent, parent=brand_name ) ) print(new_agent)
ดูตัวเลือกการจัดรูปแบบและค่าได้ที่
brands.agents
หากแบรนด์มีสถานที่ตั้งที่คุณต้องการเชื่อมโยงกับตัวแทนของคุณ ให้ดูเพิ่มสถานที่ตั้ง
ตัวอย่าง: สร้างตัวแทนสำหรับ Growing Tree Bank
curl -X POST "https://businesscommunications.googleapis.com/v1/brands" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json /path/to/service/account/key.json businesscommunications)" \ -d "{ 'displayName': 'Growing Tree Bank' }" # Fetch returned brand name value of "brands/12345" curl -X POST "https://businesscommunications.googleapis.com/v1/brands/12345/agents" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json /path/to/service/account/key.json businesscommunications)" \ -d "{ 'displayName': 'Growing Tree Bank', 'businessMessagesAgent': { 'logoUrl': 'https://www.growingtreebank.com/images/logo.png', 'entryPointConfigs': [ { 'allowedEntryPoint': 'NON_LOCAL', }, { 'allowedEntryPoint': 'LOCATION', }, ], 'customAgentId': 'growing-tree-bank', 'nonLocalConfig': { 'regionCodes': ['001'], 'contactOption': { 'url': 'https://www.growingtreebank.com', 'options': [ 'EMAIL', 'PHONE', ], }, 'enabledDomains': [ 'https://www.growingtreebank.com', ], 'phoneNumber': { 'number': '+12223334444', }, 'callDeflectionPhoneNumbers': [ { 'number': '+12223334444', }, { 'number': '+56667778888', }, ], }, 'conversationalSettings': { 'en': { 'welcomeMessage': { 'text': 'Thanks for contacting Growing Tree Bank. What can I help with today?', }, 'privacyPolicy': { 'url': 'https://www.growingtreebank.com/privacy', }, 'conversationStarters': [ { 'suggestion': { 'reply': { 'text': 'Set up an account', 'postbackData': 'new-account', }, }, }, ], }, }, 'defaultLocale': 'en', 'primaryAgentInteraction': { 'interactionType': 'BOT', 'botRepresentative': { 'botMessagingAvailability': { 'hours': [ { 'startTime': { 'hours': '8', 'minutes': '00', }, 'endTime': { 'hours': '17', 'minutes': '30', }, 'timeZone': 'America/Los_Angeles', 'startDay': 'MONDAY', 'endDay': 'FRIDAY', }, ], }, }, }, }, }"
จัดเก็บข้อมูลสำคัญ
เมื่อสร้าง Agent Business Communications API จะแสดงค่าของ Agent รวมถึง name
และ testUrls
ตั้งค่าเว็บฮุคระดับตัวแทน
คุณได้รับข้อความที่ส่งไปยังตัวแทนของคุณที่เว็บฮุค หากต้องการให้ระบบส่งข้อความสำหรับตัวแทนที่เจาะจงไปยังเว็บฮุคอื่นแทน คุณสามารถตั้งค่าเว็บฮุคระดับตัวแทนได้
หากต้องการตั้งค่าเว็บฮุคระดับตัวแทน ให้ใช้ Developer Console
ทดสอบตัวแทน
ตัวแทนแต่ละรายจะมี URL ทดสอบที่ช่วยให้คุณเห็นว่าการสนทนากับตัวแทนนั้นจะปรากฏต่อผู้ใช้ในลักษณะใด และให้โอกาสคุณยืนยันโครงสร้างพื้นฐานของการรับส่งข้อความ
TestUrl
มีแอตทริบิวต์ url
และ surface
หากต้องการทดสอบกับอุปกรณ์ iOS ให้ใช้ URL ทดสอบที่มีค่าแพลตฟอร์มเป็น SURFACE_IOS_MAPS
การเปิด URL ในอุปกรณ์ iOS ที่มีแอป Google Maps ติดตั้งไว้จะเปิดการสนทนาที่มีฟังก์ชันครบกับตัวแทน
อุปกรณ์ Android มี URL ทดสอบ 2 รายการ โดย URL ที่มีค่า surface
คือ SURFACE_ANDROID_MAPS
การสนทนาที่เปิดอยู่ใน Google Maps และแสดงถึงจุดแรกเข้าของการสนทนาที่ปรากฏใน Google Maps URL ที่มีค่า surface
เป็นการสนทนาที่เปิดอยู่ SURFACE_ANDROID_WEB
ในมุมมองการสนทนาแบบวางซ้อนและแสดงถึงจุดแรกเข้าอื่นๆ ทั้งหมด
เมื่อแพลตฟอร์มการสนทนาเปิดขึ้น การสนทนาจะรวมข้อมูลการสร้างแบรนด์ทั้งหมดที่ผู้ใช้จะเห็น และเมื่อคุณส่งข้อความถึงตัวแทน เว็บฮุคจะได้รับข้อความ รวมถึงเพย์โหลด JSON แบบเต็มที่คุณคาดหวังได้เมื่อสื่อสารกับผู้ใช้
หากต้องการเปิด URL ทดสอบของตัวแทน ให้แตะลิงก์หรือใช้ Business Messages Agent Launcher ในอุปกรณ์เคลื่อนที่
รับข้อมูลตัวแทน
หากต้องการดูข้อมูลเกี่ยวกับตัวแทน เช่น agentTestUrl
คุณสามารถดูข้อมูลจาก Business Communications API ตราบใดที่คุณมีค่า name
ของตัวแทน
รับข้อมูลสำหรับตัวแทนรายเดียว
หากต้องการรับข้อมูลเอเจนต์ ให้เรียกใช้คำสั่งต่อไปนี้ แทนที่ BRAND_ID และ AGENT_ID ด้วยค่าที่ไม่ซ้ำกันจาก name
ของ Agent
cURL
# This code gets the agent. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/get # 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__" \ -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 an agent. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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 AGENT_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 agentName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: agentName, }; bcApi.brands.agents.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();
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.Agent; import java.io.FileInputStream; import java.util.Arrays; 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 agentName = "brands/BRAND_ID/agents/AGENT_ID"; BusinessCommunications.Brands.Agents.Get request = builder .build().brands().agents().get(agentName); Agent agent = request.execute(); System.out.println(agent.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
"""This code gets the agent. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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, BusinesscommunicationsBrandsAgentsGetRequest, ) # 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) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID agent = agents_service.Get(BusinesscommunicationsBrandsAgentsGetRequest( name=agent_name )) print(agent)
แสดงรายการตัวแทนทั้งหมดของแบรนด์
หากไม่ทราบ name
ของตัวแทน คุณสามารถดูข้อมูลของตัวแทนทั้งหมดที่เชื่อมโยงกับแบรนด์ได้โดยละเว้นค่า AGENT_ID จาก URL คำขอ GET
cURL
# This code lists all agents from a brand. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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__/agents/" \ -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 agents of a brand. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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(); const brandName = 'brands/' + BRAND_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, parent: brandName, }; bcApi.brands.agents.list(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();
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.Agent; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; 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.Agents.List request = builder .build().brands().agents().list(brandName); Listรหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communicationsagents = request.execute().getAgents(); agents.stream().forEach(agent -> { try { System.out.println(agent.toPrettyString()); } catch (IOException e) { e.printStackTrace(); } }); } catch (Exception e) { e.printStackTrace(); } } }
Python
"""This code lists all agents from a brand. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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 ( Agent, BusinesscommunicationsBrandsAgentsGetRequest, ) # 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) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) brand_name = 'brands/' + BRAND_ID agents = agents_service.List(BusinesscommunicationsBrandsAgentsListRequest( parent=brand_name )) print(agents)
อัปเดตข้อมูลตัวแทน
หากต้องการอัปเดตตัวแทน คุณจะต้องส่งคำขอแพตช์ด้วย Business Communications API เมื่อเรียก API คุณต้องใส่ชื่อช่องที่กำลังแก้ไขเป็นค่าสำหรับพารามิเตอร์ของ URL "updateMask"
เช่น หากคุณอัปเดตช่อง displayName
และ customAgentId
พารามิเตอร์ของ URL "updateMask" จะเป็น "updateMask=displayName,businessMessagesAgent.customAgentId"
ดูตัวเลือกการจัดรูปแบบและค่าได้ที่ brands.agents.patch
หากไม่ทราบname
ของตัวแทน โปรดดูแสดงตัวแทนทั้งหมดของแบรนด์
หลังจากยืนยันตัวแทนแล้ว คุณจะอัปเดตได้เฉพาะช่องต่อไปนี้
conversationalSetting
customAgentId
defaultLocale
primaryAgentInteraction
additionalAgentInteractions
phone
หากต้องการอัปเดตช่องอื่นๆ หลังจากยืนยันตัวแทนแล้ว โปรด โปรดติดต่อเรา (คุณต้องลงชื่อเข้าใช้ด้วยบัญชี Google ของ Business Messages ก่อน หากต้องการลงทะเบียนบัญชี โปรดดูลงทะเบียนกับ Business Messages)
ตัวอย่าง: อัปเดตชื่อที่แสดง
cURL
curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/BRAND_ID/agents/AGENT_ID?updateMask=displayName" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json PATH_TO_SERVICE_ACCOUNT_KEY businesscommunications)" \ -d "{ 'displayName': 'Growing Tree Bank', }"
Node.js
const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); // Initialize the Business Communications API let 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'); /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client let 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); } }); }); } async function main() { let authClient = await initCredentials(); let agentName = 'brands/BRAND_ID/agents/AGENT_ID'; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: agentName, updateMask: 'displayName', resource: { displayName: 'Growing Tree Bank', } }; bcApi.brands.agents.patch(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent found console.log(response.data); } }); } else { console.log('Authentication failure.'); } } main();โค้ดนี้อิงตามไลบรารีของไคลเอ็นต์สำหรับ Node.js Business Communications
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.Agent; import java.io.FileInputStream; import java.util.Arrays; 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 agentName = "brands/BRAND_ID/agents/AGENT_ID"; Agent agent = new Agent().setDisplayName("Growing Tree Bank"); BusinessCommunications.Brands.Agents.Patch request = builder .build().brands().agents().patch(agentName, agent); request.setUpdateMask("displayName"); Agent updatedAgent = request.execute(); System.out.println(updatedAgent.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import ( BusinesscommunicationsV1 ) from businesscommunications.businesscommunications_v1_messages import ( Agent, BusinesscommunicationsBrandsAgentsPatchRequest, ) SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = 'PATH_TO_SERVICE_ACCOUNT_KEY' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) agent_name = 'brands/BRAND_ID/agents/AGENT_ID' agent=Agent( displayName='Growing Tree Bank' ) updated_agent = agents_service.Patch( BusinesscommunicationsBrandsAgentsPatchRequest( agent=agent, name=agent_name, updateMask='displayName' ) ) print(updated_agent)โค้ดนี้อิงตาม ไลบรารีของไคลเอ็นต์ Python Business Communications
ตัวอย่าง: ระบุกลุ่มจุดแรกเข้า NON_LOCAL
และ LOCATION
cURL
curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/BRAND_ID/agents/AGENT_ID?updateMask=businessMessagesAgent.entryPointConfigs" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json PATH_TO_SERVICE_ACCOUNT_KEY businesscommunications)" \ -d "{ 'businessMessagesAgent': { 'entryPointConfigs': [ { 'allowedEntryPoint': 'NON_LOCAL', }, { 'allowedEntryPoint': 'LOCATION', }, ], }, }"
Node.js
const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); // Initialize the Business Communications API let 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'); /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client let 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); } }); }); } async function main() { let authClient = await initCredentials(); let agentName = 'brands/BRAND_ID/agents/AGENT_ID'; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: agentName, updateMask: 'businessMessagesAgent.entryPointConfigs', resource: { businessMessagesAgent: { entryPointConfigs: [ { allowedEntryPoint: 'LOCATION', }, { allowedEntryPoint: 'NON_LOCAL', }, ], } } }; bcApi.brands.agents.patch(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent found console.log(response.data); } }); } else { console.log('Authentication failure.'); } } main();โค้ดนี้อิงตามไลบรารีของไคลเอ็นต์สำหรับ Node.js Business Communications
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.*; import java.io.FileInputStream; import java.util.Arrays; 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 agentName = "brands/BRAND_ID/agents/AGENT_ID"; Agent agent = new Agent().setBusinessMessagesAgent( new BusinessMessagesAgent().setEntryPointConfigs(Arrays.asList( new BusinessMessagesEntryPointConfig() .setAllowedEntryPoint( "NON_LOCAL"), new BusinessMessagesEntryPointConfig() .setAllowedEntryPoint( "LOCATION")))); BusinessCommunications.Brands.Agents.Patch request = builder .build().brands().agents().patch(agentName, agent); request.setUpdateMask("businessMessagesAgent.entryPointConfigs"); Agent updatedAgent = request.execute(); System.out.println(updatedAgent.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import ( BusinesscommunicationsV1 ) from businesscommunications.businesscommunications_v1_messages import ( Agent, BusinessMessagesAgent, BusinessMessagesEntryPointConfig, BusinesscommunicationsBrandsAgentsPatchRequest, ) SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = 'PATH_TO_SERVICE_ACCOUNT_KEY' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) agent_name = 'brands/BRAND_ID/agents/AGENT_ID' agent=Agent( businessMessagesAgent=BusinessMessagesAgent( entryPointConfigs=[BusinessMessagesEntryPointConfig( allowedEntryPoint=BusinessMessagesEntryPointConfig.AllowedEntryPointValueValuesEnum.NON_LOCAL ), BusinessMessagesEntryPointConfig( allowedEntryPoint=BusinessMessagesEntryPointConfig.AllowedEntryPointValueValuesEnum.LOCATION )] ) ) updated_agent = agents_service.Patch( BusinesscommunicationsBrandsAgentsPatchRequest( agent=agent, name=agent_name, updateMask='businessMessagesAgent.entryPointConfigs' ) ) print(updated_agent)โค้ดนี้อิงตาม ไลบรารีของไคลเอ็นต์ Python Business Communications
ตัวอย่าง: อัปเดตข้อความต้อนรับ
หากอัปเดตช่องภายใน conversationalSettings
เช่น welcomeMessage
คุณต้องอัปเดตช่องทั้งหมดภายในออบเจ็กต์ และต้องระบุภาษาของการตั้งค่า (เป็นรหัสภาษา ISO 639-1 2 อักขระ)
cURL
curl -X PATCH \ "https://businesscommunications.googleapis.com/v1/brands/BRAND_ID/agents/AGENT_ID?updateMask=businessMessagesAgent.conversationalSettings.en" \ -H "Content-Type: application/json" \ -H "User-Agent: curl/business-communications" \ -H "$(oauth2l header --json PATH_TO_SERVICE_ACCOUNT_KEY businesscommunications)" \ -d "{ 'businessMessagesAgent': { 'conversationalSettings': { 'en': { 'welcomeMessage': { 'text': 'Thanks for contacting Growing Tree Bank. What can I help with today?', }, 'offlineMessage': { 'text': 'We\'re closed for the night. Please reach out to us again tomorrow.', }, 'privacyPolicy': { 'url': 'https://www.growingtreebank.com/privacy', }, 'conversationStarters': [ { 'suggestion': { 'reply': { 'text': 'Set up an account', 'postbackData': 'new-account', }, }, }, { 'suggestion': { 'reply': { 'text': 'Look up account information', 'postbackData': 'account-lookup', }, }, }, ], }, }, }, }"
Node.js
const businesscommunications = require('businesscommunications'); const {google} = require('googleapis'); // Initialize the Business Communications API let 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'); /** * Initializes the Google credentials for calling the * Business Messages API. */ async function initCredentials() { // Configure a JWT auth client let 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); } }); }); } async function main() { let authClient = await initCredentials(); let agentName = 'brands/BRAND_ID/agents/AGENT_ID'; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: agentName, updateMask: 'businessMessagesAgent.conversationalSettings.en', resource: { businessMessagesAgent: { conversationalSettings: { en: { privacyPolicy: { url: 'https://www.growingtreebank.com/privacy' }, welcomeMessage: { text: 'Thanks for contacting Growing Tree Bank. What can I help with today?' }, offlineMessage: { text: 'We\'re closed for the night. Please reach out to us again tomorrow.' }, conversationStarters: [ { suggestion: { reply: { text: 'Set up an account', postbackData: 'new-account', }, }, }, { suggestion: { reply: { text: 'Look up account information', postbackData: 'account-lookup', }, }, }, ], }, } } } }; bcApi.brands.agents.patch(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent found console.log(response.data); } }); } else { console.log('Authentication failure.'); } } main();โค้ดนี้อิงตามไลบรารีของไคลเอ็นต์สำหรับ Node.js Business Communications
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.*; import com.google.common.collect.ImmutableMap; import java.io.FileInputStream; import java.util.Arrays; 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 agentName = "brands/BRAND_ID/agents/AGENT_ID"; Agent agent = new Agent().setBusinessMessagesAgent( new BusinessMessagesAgent().setConversationalSettings(ImmutableMap.of("en", new ConversationalSetting() .setPrivacyPolicy(new PrivacyPolicy().setUrl("https://www.growingtreebank.com/privacy")) .setWelcomeMessage(new WelcomeMessage().setText("Thanks for contacting Growing Tree Bank. What can I help with today?")) .setOfflineMessage(new OfflineMessage().setText("We're closed for the night. Please reach out to us again tomorrow.")) .setConversationStarters(Arrays.asList( new ConversationStarters().setSuggestion(new Suggestion() .setReply(new SuggestedReply() .setText("Set up an account") .setPostbackData("new-account"))), new ConversationStarters().setSuggestion(new Suggestion() .setReply(new SuggestedReply() .setText("Look up account information") .setPostbackData("account-lookup"))) ))))); BusinessCommunications.Brands.Agents.Patch request = builder .build().brands().agents().patch(agentName, agent); request.setUpdateMask("businessMessagesAgent.conversationalSettings.en"); Agent updatedAgent = request.execute(); System.out.println(updatedAgent.toPrettyString()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
from oauth2client.service_account import ServiceAccountCredentials from businesscommunications.businesscommunications_v1_client import ( BusinesscommunicationsV1 ) from businesscommunications.businesscommunications_v1_messages import ( Agent, BusinessMessagesAgent, ConversationStarters, ConversationalSetting, OfflineMessage, PrivacyPolicy, WelcomeMessage, BusinesscommunicationsBrandsAgentsPatchRequest, ) SCOPES = ['https://www.googleapis.com/auth/businesscommunications'] SERVICE_ACCOUNT_FILE = 'PATH_TO_SERVICE_ACCOUNT_KEY' credentials = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_FILE, scopes=SCOPES) client = BusinesscommunicationsV1(credentials=credentials) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) agent_name = 'brands/BRAND_ID/agents/AGENT_ID' agent=Agent( businessMessagesAgent=BusinessMessagesAgent( conversationalSettings=BusinessMessagesAgent.ConversationalSettingsValue( additionalProperties=[BusinessMessagesAgent.ConversationalSettingsValue.AdditionalProperty( key='en', value=ConversationalSetting( privacyPolicy=PrivacyPolicy(url='https://www.growingtreebank.com/privacy'), welcomeMessage=WelcomeMessage(text='Thanks for contacting Growing Tree Bank. What can I help with today?'), offlineMessage=OfflineMessage(text='We\'re closed for the night. Please reach out to us again tomorrow.'), conversationStarters=[ ConversationStarters( suggestion=Suggestion( reply=SuggestedReply(text='Set up an account', postbackData='new-account') ) ), ConversationStarters( suggestion=Suggestion( reply=SuggestedReply(text='Look up account information', postbackData='account-lookup') ) )] ) ) ] ) ) ) updated_agent = agents_service.Patch( BusinesscommunicationsBrandsAgentsPatchRequest( agent=agent, name=agent_name, updateMask='businessMessagesAgent.conversationalSettings.en' ) ) print(updated_agent)โค้ดนี้อิงตาม ไลบรารีของไคลเอ็นต์ Python Business Communications
ลบตัวแทน
เมื่อลบตัวแทน Business Messages จะลบข้อมูลตัวแทนทั้งหมด Business Messages จะไม่ลบข้อความที่ตัวแทนของคุณส่งในระหว่างการส่งหรือจัดเก็บไว้ในอุปกรณ์ของผู้ใช้ ทั้งนี้ข้อความถึงผู้ใช้ไม่ใช่ข้อมูลตัวแทน
การลบคำขอจะล้มเหลวหากตัวแทนมีสถานที่ตั้งที่เชื่อมโยงหรือคุณได้พยายามยืนยันตัวแทนอย่างน้อย 1 ครั้งแล้ว หากต้องการอัปเดตสถานที่ตั้ง โปรดดูเพิ่มสถานที่
คุณลบตัวแทนที่ได้รับการยืนยันแล้วไม่ได้ หากต้องการลบตัวแทนที่คุณยืนยันแล้วหรือพยายามยืนยัน โปรด โปรดติดต่อเรา (คุณต้องลงชื่อเข้าใช้ด้วยบัญชี Google ของ Business Messages ก่อน หากต้องการลงทะเบียนบัญชี โปรดดูลงทะเบียนกับ Business Messages)
หากต้องการลบ Agent ให้เรียกใช้คำสั่งต่อไปนี้ แทนที่ BRAND_ID และ AGENT_ID ด้วยค่าที่ไม่ซ้ำกันจาก name
ของ Agent
cURL
# This code deletes an agent. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/delete # Replace the __BRAND_ID__ and __AGENT_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__" \ -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 agent. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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 AGENT_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 agentName = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: agentName, }; bcApi.brands.agents.delete(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Agent removed 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.*; import java.io.FileInputStream; import java.util.Arrays; 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 agentName = "brands/BRAND_ID/agents/AGENT_ID"; BusinessCommunications.Brands.Agents.Delete request = builder.build().brands().agents().delete(agentName); System.out.println(request.execute()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
"""This code deletes an agent. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands.agents/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 ( Agent, BusinesscommunicationsBrandsAgentsDeleteRequest, ) # 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) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID agent = agents_service.Delete(BusinesscommunicationsBrandsAgentsDeleteRequest( name=agent_name )) print(agent)
ดูตัวเลือกการจัดรูปแบบและค่าได้ที่ brands.agents.delete
ลบแบรนด์
เมื่อลบแบรนด์ คุณจะส่งคำขอลบด้วย Business Communications API ลบคำขอไม่สำเร็จหากคุณมีตัวแทนหรือสถานที่ที่เกี่ยวข้องกับแบรนด์อย่างน้อย 1 รายการ แม้ว่าตัวแทนเหล่านั้นจะอยู่ในผลิตภัณฑ์อื่นก็ตาม
หากต้องการลบแบรนด์ ให้เรียกใช้คำสั่งต่อไปนี้ แทนที่ BRAND_ID ด้วยค่าที่ไม่ซ้ำกันจาก name
ของแบรนด์
cURL
# This code deletes a brand. # Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/delete # Replace the __BRAND_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__" \ -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 brand. * Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/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 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; if (authClient) { // Setup the parameters for the API call const apiParams = { auth: authClient, name: brandName, }; bcApi.brands.delete(apiParams, {}, (err, response) => { if (err !== undefined && err !== null) { console.dir(err); } else { // Brand removed 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.*; import java.io.FileInputStream; import java.util.Arrays; 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.Delete request = builder.build().brands().delete(brandName); System.out.println(request.execute()); } catch (Exception e) { e.printStackTrace(); } } }รหัสนี้อิงจาก ไลบรารีของไคลเอ็นต์ Java Business Communications
Python
"""This code deletes a brand. Read more: https://developers.google.com/business-communications/business-messages/reference/business-communications/rest/v1/brands/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 ( BusinesscommunicationsBrandsAgentsDeleteRequest ) # 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) agents_service = BusinesscommunicationsV1.BrandsAgentsService(client) brand_name = 'brands/' + BRAND_ID agent = agents_service.Delete(BusinesscommunicationsBrandsAgentsDeleteRequest( name=brand_name )) print(agent)
ดูตัวเลือกการจัดรูปแบบและค่าได้ที่ brands.delete
ขั้นตอนถัดไป
เมื่อมีตัวแทนแล้ว โปรดทำตามขั้นตอนต่อไปนี้
- เพิ่มสถานที่ ให้กับตัวแทนของคุณ
- ออกแบบประสบการณ์การสนทนา
- พัฒนาขั้นตอนการส่งข้อความ