คู่มือนี้อธิบายวิธีต่างๆ ที่แอป Google Chat ส่งข้อความได้
- ส่ง SMS และการ์ดแบบเรียลไทม์โดยตอบกลับการโต้ตอบของผู้ใช้
- ส่ง SMS และการ์ดแบบไม่พร้อมกันโดยเรียกใช้เมธอด
create
ในทรัพยากรMessage
- เริ่มหรือตอบชุดข้อความ
- ส่งและตั้งชื่อข้อความ
ทรัพยากร Message
แสดงถึงข้อความหรือการ์ดใน Google Chat คุณสามารถcreate
, get
, update
หรือdelete
ข้อความใน Google Chat API โดยการเรียกใช้เมธอดที่เกี่ยวข้อง ดูข้อมูลเพิ่มเติมเกี่ยวกับ SMS และข้อความการ์ดได้ที่ภาพรวมข้อความ Google Chat
แทนที่จะเรียกเมธอด create
ในทรัพยากร Message
ของ Google Chat API เพื่อส่ง SMS หรือข้อความการ์ดแบบไม่พร้อมกัน
แอป Google Chat จะสร้างข้อความเพื่อตอบกลับการโต้ตอบของผู้ใช้แบบเรียลไทม์ได้ด้วย การตอบสนองต่อการโต้ตอบของผู้ใช้ไม่จำเป็นต้องมีการตรวจสอบสิทธิ์และรองรับข้อความประเภทอื่นๆ ซึ่งรวมถึงกล่องโต้ตอบแบบอินเทอร์แอกทีฟและตัวอย่างลิงก์ โปรดดูรายละเอียดที่หัวข้อรับและตอบสนองต่อการโต้ตอบกับแอป Google Chat
ข้อกำหนดเบื้องต้น
Node.js
- บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้าง แอป Chat ให้ทำตาม quickstartนี้
- กำหนดค่าการให้สิทธิ์เพื่อให้แอป Chat ส่งข้อความแบบไม่พร้อมกัน ไม่ต้องกำหนดค่าการให้สิทธิ์เพื่อส่งข้อความแบบเรียลไทม์
- การส่ง SMS จะรองรับวิธีการให้สิทธิ์ทั้ง 2 วิธีต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้ด้วยขอบเขตการให้สิทธิ์
chat.messages.create
หรือchat.messages
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้ด้วยขอบเขตการให้สิทธิ์
- การส่ง
ข้อความการ์ด
ต้องมี
การตรวจสอบสิทธิ์แอป
ที่มีขอบเขตการให้สิทธิ์
chat.bot
- การส่ง SMS จะรองรับวิธีการให้สิทธิ์ทั้ง 2 วิธีต่อไปนี้
Python
- Python 3.6 ขึ้นไป
- เครื่องมือจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้างและเผยแพร่ แอป Chat โปรดดูที่ สร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์เพื่อให้แอป Chat ส่งข้อความแบบไม่พร้อมกัน ไม่ต้องกำหนดค่าการให้สิทธิ์เพื่อส่งข้อความแบบเรียลไทม์
- การส่ง SMS จะรองรับวิธีการให้สิทธิ์ทั้ง 2 วิธีต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้ด้วยขอบเขตการให้สิทธิ์
chat.messages.create
หรือchat.messages
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้ด้วยขอบเขตการให้สิทธิ์
- การส่ง
ข้อความการ์ด
ต้องมี
การตรวจสอบสิทธิ์แอป
ที่มีขอบเขตการให้สิทธิ์
chat.bot
- การส่ง SMS จะรองรับวิธีการให้สิทธิ์ทั้ง 2 วิธีต่อไปนี้
Apps Script
- บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้าง แอป Chat ให้ทำตาม quickstartนี้
ส่งข้อความตัวอักษร
ส่วนนี้จะอธิบายวิธีส่งข้อความตัวอักษรใน 2 วิธีต่อไปนี้
- ส่งข้อความแบบเรียลไทม์โดยตอบกลับการโต้ตอบของผู้ใช้
- ส่ง SMS โดยการเรียกใช้ Google Chat API แบบไม่พร้อมกัน
ส่งข้อความแบบเรียลไทม์
ในตัวอย่างนี้ แอป Chat จะสร้างและส่ง SMS เมื่อมีการเพิ่มพื้นที่ทำงาน ดูข้อมูลเกี่ยวกับแนวทางปฏิบัติแนะนําในการเตรียมความพร้อมผู้ใช้งานได้ที่เริ่มต้นใช้งานที่เป็นประโยชน์กับผู้ใช้และพื้นที่ทำงาน
หากต้องการส่ง SMS เมื่อผู้ใช้เพิ่มแอป Chat ไปยังพื้นที่ทำงาน แอป Chat จะตอบสนองต่อADDED_TO_SPACE
เหตุการณ์การโต้ตอบ หากต้องการตอบกลับเหตุการณ์การโต้ตอบ ADDED_TO_SPACE
รายการด้วย SMS ให้ใช้โค้ดต่อไปนี้
Node.js
/**
* Sends an onboarding message when the Chat app is added to a space.
*
* @param {Object} event The event object from Chat API.
* @return {Object} Response from the Chat app. An onboarding message that
* introduces the app and helps people get started with it.
*/
exports.onMessage = function onMessage(req, res) {
if (req.method === 'GET' || !req.body.message) {
res.send(
'Hello! This function is meant to be used in a Google Chat space.');
}
// Send an onboarding message when added to a Chat space
if(req.body.type === 'ADDED_TO_SPACE') {
res.json({
'text': 'Hi, Cymbal at your service. I help you manage your calendar
from Google Chat. Take a look at your schedule today by typing
`/checkCalendar`, or schedule a meeting with `/scheduleMeeting`. To
learn what else I can do, type `/help`.'
});
}
};
Apps Script
/**
* Sends an onboarding message when the Chat app is added to a space.
*
* @param {Object} event The event object from Chat API.
* @return {Object} Response from the Chat app. An onboarding message that
* introduces the app and helps people get started with it.
*/
function onAddToSpace(event) {
return {
'text': 'Hi, Cymbal at your service. I help you manage your calendar
from Google Chat. Take a look at your schedule today by typing
`/checkCalendar`, or schedule a meeting with `/scheduleMeeting`. To learn
what else I can do, type `/help`.'
}
}
ตัวอย่างโค้ดแสดงข้อความต่อไปนี้
ส่งข้อความแบบไม่พร้อมกัน
ส่วนต่อไปนี้จะอธิบายวิธีส่งข้อความแบบไม่พร้อมกันด้วยการตรวจสอบสิทธิ์แอปและการตรวจสอบสิทธิ์ผู้ใช้
หากต้องการส่งข้อความ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์
chat.bot
หากเป็นการตรวจสอบสิทธิ์ผู้ใช้ ให้ระบุขอบเขตการให้สิทธิ์chat.messages.create
- เรียกใช้เมธอด
create
ในทรัพยากรMessage
ส่ง SMS ที่มีการตรวจสอบสิทธิ์แอป
วิธีส่ง SMS ด้วยการตรวจสอบสิทธิ์แอปมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_create_text_message_app.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_create_text_message_app.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Create a Chat message. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The message to create. body={'text': 'Hello, world!'} ).execute() print(result)
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอดspaces.list()
ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_create_text_message_app.py
โดย Chat API จะส่งคืนอินสแตนซ์ Message
ที่มีรายละเอียดข้อความที่ส่ง
ส่ง SMS ที่มีการตรวจสอบสิทธิ์ผู้ใช้
วิธีส่ง SMS ที่มีการตรวจสอบสิทธิ์ผู้ใช้มีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_create_text_message_user.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_create_text_message_user.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] def main(): ''' Authenticates with Chat API via user credentials, then creates a text message in a Chat space. ''' # Start with no credentials. creds = None # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The message to create. body={'text': 'Hello, world!'} ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอดspaces.list()
ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_create_text_message_user.py
โดย Chat API จะส่งคืนอินสแตนซ์ Message
ที่มีรายละเอียดข้อความที่ส่ง
ส่งข้อความการ์ด
ส่วนนี้จะอธิบายวิธีส่งข้อความการ์ดด้วย 2 วิธีดังนี้
- ส่งข้อความการ์ดแบบเรียลไทม์โดยตอบกลับการโต้ตอบของผู้ใช้
- ส่งข้อความการ์ดโดยเรียกใช้ Google Chat API แบบไม่พร้อมกัน
ส่งข้อความสำหรับการ์ดแบบเรียลไทม์
แอป Chat สามารถสร้างข้อความการ์ดเพื่อตอบกลับการโต้ตอบของผู้ใช้ เช่น เมื่อผู้ใช้ส่งข้อความในแอป Chat หรือเพิ่มแอป Chat ไปยังพื้นที่ทำงาน หากต้องการดูข้อมูลเพิ่มเติม เกี่ยวกับการตอบกลับการโต้ตอบของผู้ใช้ โปรดดูที่หัวข้อรับและตอบกลับเหตุการณ์การโต้ตอบ ในแอป Chat
ในตัวอย่างนี้ ผู้ใช้ส่งข้อความไปยังแอป Chat และแอป Chat จะตอบสนองด้วยการส่งข้อความการ์ดที่แสดงชื่อและรูปโปรไฟล์ของผู้ใช้ ดังนี้
Node.js
Python
Apps Script
ส่งข้อความการ์ดแบบไม่พร้อมกัน
หากต้องการส่งข้อความในการ์ด ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์
chat.bot
คุณไม่สามารถส่งข้อความการ์ดที่มีการตรวจสอบสิทธิ์ผู้ใช้ - เรียกใช้เมธอด
create
ในทรัพยากรMessage
ต่อไปนี้เป็นตัวอย่างข้อความการ์ด
วิธีส่งข้อความการ์ดที่มีการตรวจสอบสิทธิ์แอปมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_create_card_message.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_create_card_message.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Create a Chat message. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The message to create. body= { 'cardsV2': [{ 'cardId': 'createCardMessage', 'card': { 'header': { 'title': 'A card message!', 'subtitle': 'Created with the Chat API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() print(result)
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_create_card_message.py
เริ่มหรือตอบชุดข้อความ
หากต้องการเริ่มชุดข้อความ ให้ส่งข้อความแล้วปล่อย
thread.name
ว่างไว้ เนื่องจาก Google Chat จะป้อนข้อมูลดังกล่าวเมื่อสร้างชุดข้อความ หากต้องการปรับแต่งชื่อของชุดข้อความ ให้ระบุช่อง thread.threadKey
หากต้องการตอบกลับชุดข้อความ ให้ส่งข้อความที่ระบุช่อง threadKey
หรือ name
ของชุดข้อความ หากชุดข้อความสร้างขึ้นโดยบุคคลหรือแอป Chat อื่น คุณต้องใช้ช่อง thread.name
หากไม่พบชุดข้อความที่ตรงกัน คุณสามารถระบุว่าข้อความควรเริ่มชุดข้อความใหม่หรือโพสต์ไม่สำเร็จโดยการตั้งค่าช่อง messageReplyOption
ต่อไปนี้เป็นวิธีเริ่มหรือตอบชุดข้อความด้วยช่อง threadKey
ที่กําหนดเป็น nameOfThread
Python
- สร้างไฟล์ชื่อ
chat_create_message_thread.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_create_message_thread.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Create a Chat message. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # Whether to start a thread or reply to an existing one. # # Required when threading is enabled in a space unless starting a # thread. Ignored in other space types. Threading is enabled when # space.spaceThreadingState is THREADED_MESSAGES. # # REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD replies to an existing thread # if one exists, otherwise it starts a new one. messageReplyOption='REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD', # The message body. body={ # The message to create. 'text': 'Start or reply to another message in a thread!', # The thread to start or reply to. 'thread': { 'threadKey': 'nameOfThread' } } ).execute() print(result)
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_create_message_thread.py
โดย Chat API จะส่งคืนอินสแตนซ์ Message
ที่มีรายละเอียดข้อความที่ส่ง
ส่งและตั้งชื่อข้อความ
ส่วนนี้จะอธิบายวิธีส่งข้อความด้วยชื่อที่กำหนดเอง คุณใช้
ชื่อข้อความเพื่อ
รับ
อัปเดต
หรือ
ลบข้อความได้
การกำหนดชื่อที่กำหนดเองยังช่วยให้แอป Chat เรียกคืนข้อความได้โดยไม่ต้องบันทึกข้อความ name
จาก
เนื้อหาการตอบกลับ
ที่ส่งกลับเมื่อส่งข้อความ
การกำหนดชื่อที่กำหนดเองจะไม่แทนที่ช่อง name
ที่สร้างขึ้น ซึ่งเป็นชื่อทรัพยากรของข้อความ แต่จะตั้งชื่อที่กําหนดเองเป็นช่อง clientAssignedMessageId
แทน ซึ่งคุณใช้อ้างอิงระหว่างการประมวลผลในภายหลังได้ เช่น อัปเดตหรือการลบข้อความ
ชื่อที่กำหนดเองมีข้อกำหนดต่อไปนี้
- เริ่มต้นด้วย
client-
ตัวอย่างเช่นclient-custom-name
เป็นชื่อที่กำหนดเองที่ถูกต้อง แต่custom-name
ไม่ใช่ - มีเฉพาะตัวอักษรพิมพ์เล็ก ตัวเลข และขีดกลาง
- มีความยาวไม่เกิน 63 อักขระ
- การระบุชื่อที่กำหนดเองที่ใช้แล้วขณะที่ส่งข้อความแสดงข้อผิดพลาด แต่วิธีการอื่นๆ เช่น
update
และdelete
ทำงานได้ตามที่คาดไว้
วิธีส่งและตั้งชื่อข้อความมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_create_named_message.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_create_named_message.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Create a Chat message with a custom name. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # Custom name for the message used to facilitate later operations. messageId='client-custom-name', # The message to create. body={'text': 'Hello, world!'} ).execute() print(result)
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_create_named_message.py
โดย Chat API จะส่งคืนอินสแตนซ์ Message
ที่มีรายละเอียดข้อความที่ส่ง
แก้ปัญหา
เมื่อแอป Google Chat หรือการ์ดแสดงข้อผิดพลาด อินเทอร์เฟซ Chat จะแสดงข้อความว่า "เกิดข้อผิดพลาด" หรือ "ดำเนินการตามคำขอของคุณไม่ได้" บางครั้ง UI ของ Chat ไม่แสดงข้อความแสดงข้อผิดพลาด แต่แอปหรือการ์ด Chat อาจให้ผลลัพธ์ที่ไม่คาดคิด เช่น ข้อความการ์ดอาจไม่ปรากฏ
แม้ว่าข้อความแสดงข้อผิดพลาดอาจไม่แสดงใน UI ของ Chat แต่ก็มีข้อมูลบันทึกที่สื่อความหมายและข้อมูลบันทึกเพื่อช่วยให้คุณแก้ไขข้อผิดพลาดเมื่อเปิดการบันทึกข้อผิดพลาดสําหรับแอป Chat ไว้ หากต้องการความช่วยเหลือในการดู แก้ไขข้อบกพร่อง และแก้ไขข้อผิดพลาด โปรดดูหัวข้อแก้ปัญหาและแก้ไขข้อผิดพลาดของ Google Chat
หัวข้อที่เกี่ยวข้อง
- จัดรูปแบบข้อความ
- ดูรายละเอียดเกี่ยวกับข้อความ
- แสดงข้อความในพื้นที่ทำงาน
- อัปเดตข้อความ
- ลบข้อความ
- ระบุผู้ใช้ในข้อความใน Google Chat
- ส่งข้อความไปยัง Google Chat ด้วยเว็บฮุคขาเข้า