หน้านี้อธิบายวิธีตั้งค่าและตอบกลับคำสั่งในฐานะแอป Google Chat
คำสั่งช่วยให้ผู้ใช้ค้นพบและใช้ฟีเจอร์หลักของแอปใน Chat โดยมีเพียงแอปใน Chat เท่านั้นที่เห็นเนื้อหาของคำสั่ง เช่น หากผู้ใช้ส่งข้อความที่มีคำสั่งเครื่องหมายทับ ข้อความนั้นจะปรากฏให้ผู้ใช้และแอปใน Chat เห็นเท่านั้น
หากต้องการตัดสินใจว่าควรสร้างคำสั่งหรือไม่ และเพื่อทำความเข้าใจวิธีออกแบบการโต้ตอบของผู้ใช้ โปรดดูหัวข้อกำหนดเส้นทางของผู้ใช้ทั้งหมด
ประเภทคำสั่งของแอปใน Chat
คุณสามารถสร้างคำสั่งแอปใน Chat เป็นคำสั่งเครื่องหมายทับ คำสั่งด่วน หรือการดำเนินการกับข้อความได้ หากต้องการใช้คำสั่งแต่ละประเภท ผู้ใช้สามารถทำสิ่งต่อไปนี้-
คำสั่งเครื่องหมายทับ: ผู้ใช้สามารถเลือกคำสั่งเครื่องหมายทับจากเมนูหรือพิมพ์เครื่องหมายทับ (
/) แล้วตามด้วยข้อความที่กำหนดไว้ล่วงหน้า เช่น/aboutโดยปกติแล้ว แอปใน Chat จะต้องมีข้อความอาร์กิวเมนต์สำหรับคำสั่งเครื่องหมายทับสร้างคำสั่งเครื่องหมายทับหากแอปใน Chat ต้องการข้อมูลเพิ่มเติมจากผู้ใช้ เช่น คุณสามารถสร้างคำสั่งเครื่องหมายทับชื่อ
/searchซึ่งจะทำงานหลังจากที่ผู้ใช้ป้อนวลีเพื่อค้นหา เช่น/search receipts -
คำสั่งด่วน: ผู้ใช้จะใช้คำสั่งโดยเปิดเมนูจากส่วนตอบกลับของข้อความแชท หากต้องการใช้คำสั่ง ให้คลิกเพิ่ม
แล้วเลือกคำสั่งจากเมนู
สร้างคำสั่งด่วนหากแอปใน Chat สามารถตอบกลับผู้ใช้ได้ทันทีโดยไม่ต้องรอข้อมูลเพิ่มเติม เช่น คุณสามารถสร้างคำสั่งด่วนชื่อรูปภาพแบบสุ่มซึ่งจะตอบกลับด้วยรูปภาพทันที
-
การดำเนินการกับข้อความ: ผู้ใช้จะใช้การดำเนินการกับข้อความโดยวางเมาส์เหนือข้อความแล้วคลิกเมนู 3 จุด หากต้องการใช้คำสั่ง ให้เปิดเมนู 3 จุด แล้วเลือกคำสั่งจากเมนู
สร้างการดำเนินการกับข้อความหากแอปใน Chat สามารถดำเนินการตามบริบทของข้อความได้
รูปภาพต่อไปนี้แสดงวิธีที่ผู้ใช้ค้นพบเมนูสำหรับคำสั่งเครื่องหมายทับและคำสั่งด่วน รวมถึงการดำเนินการกับข้อความ
ข้อกำหนดเบื้องต้น
HTTP
แอป Google Chat ที่รับและตอบสนองต่อการโต้ตอบของผู้ใช้ หากต้องการสร้าง ให้ทำตามคู่มือเริ่มใช้งานฉบับย่อของ HTTP
Apps Script
แอป Google Chat ที่รับและตอบสนองต่อการโต้ตอบของผู้ใช้ หากต้องการสร้าง ให้ทำตามคู่มือเริ่มใช้งานฉบับย่อของ Apps Script
ตั้งค่าคำสั่ง
ส่วนนี้จะอธิบายวิธีทำตามขั้นตอนต่อไปนี้เพื่อตั้งค่าคำสั่ง
- สร้างชื่อและคำอธิบายสำหรับคำสั่ง
- กำหนดค่าคำสั่งในคอนโซล Google Cloud
- ไม่บังคับ: แมปคำสั่งกับพรอมต์เริ่มต้น
ตั้งชื่อและอธิบายคำสั่ง
ชื่อของคำสั่งคือสิ่งที่ผู้ใช้พิมพ์หรือเลือกเพื่อเรียกใช้แอปใน Chat โดยคำอธิบายสั้นๆ จะปรากฏใต้ชื่อด้วย เพื่อแจ้งให้ผู้ใช้ทราบเพิ่มเติมเกี่ยวกับวิธีใช้คำสั่ง
เมื่อเลือกชื่อและคำอธิบายสำหรับคำสั่ง ให้พิจารณาคำแนะนำต่อไปนี้
วิธีตั้งชื่อคำสั่ง
- ใช้คำหรือวลีสั้นๆ ที่สื่อความหมายและนำไปใช้ได้จริงเพื่อให้ผู้ใช้เข้าใจคำสั่งได้อย่างชัดเจน เช่น ใช้
Remind meแทนชื่อCreate a reminder - ลองใช้ชื่อที่ไม่ซ้ำกันหรือชื่อที่ใช้กันทั่วไปสำหรับคำสั่งของคุณ หากคำสั่งอธิบายการโต้ตอบหรือฟีเจอร์ทั่วไป คุณสามารถใช้ชื่อทั่วไปที่ผู้ใช้รู้จักและคาดหวัง เช่น
SettingsหรือFeedbackหรือลองใช้ชื่อคำสั่งที่ไม่ซ้ำกัน เพราะหากชื่อคำสั่งของคุณเหมือนกับแอปใน Chat อื่นๆ ผู้ใช้จะต้องกรองคำสั่งที่คล้ายกันเพื่อค้นหาและใช้คำสั่งของคุณ
วิธีอธิบายคำสั่ง
- เขียนคำอธิบายให้สั้นและชัดเจนเพื่อให้ผู้ใช้ทราบว่าจะเกิดอะไรขึ้นเมื่อใช้คำสั่ง
- แจ้งให้ผู้ใช้ทราบหากมีข้อกำหนดการจัดรูปแบบสำหรับคำสั่ง เช่น หากคุณสร้างคำสั่งเครื่องหมายทับที่ต้องใช้ข้อความอาร์กิวเมนต์ ให้ตั้งค่าคำอธิบายเป็น
Remind me to do [something] at [time] - แจ้งให้ผู้ใช้ทราบว่าแอปใน Chat จะตอบกลับทุกคนในพื้นที่ทำงานหรือตอบกลับผู้ใช้ที่เรียกใช้คำสั่งแบบส่วนตัว เช่น สำหรับคำสั่งด่วน
Aboutคุณอาจอธิบายเป็นLearn about this app (Only visible to you)
กำหนดค่าคำสั่งในคอนโซล Google Cloud
หากต้องการสร้างคำสั่งเครื่องหมายทับ คำสั่งด่วน หรือการดำเนินการกับข้อความ คุณต้องระบุข้อมูลเกี่ยวกับคำสั่งหรือการดำเนินการในการกำหนดค่าแอปใน Chat สำหรับ Google Chat API
หากต้องการกำหนดค่าคำสั่งใน Google Chat API ให้ทำตามขั้นตอนต่อไปนี้
ในคอนโซล Google Cloud ให้คลิกเมนู > API และบริการ > API และบริการที่เปิดใช้ > Google Chat API
คลิกการกำหนดค่า
ในส่วนการตั้งค่าการเชื่อมต่อ ให้ไปที่ทริกเกอร์ แล้วระบุรายละเอียดปลายทาง คุณต้องใช้ทริกเกอร์นี้ในส่วนต่อไปนี้เพื่อตอบสนองต่อคำสั่ง
- URL ปลายทาง HTTP: คุณสามารถระบุ URL ปลายทาง HTTP ทั่วไปได้ 1 รายการที่นี่ หรือหากต้องการใช้ปลายทาง HTTP ที่แตกต่างกันสำหรับทริกเกอร์ที่แตกต่างกัน ให้ระบุปลายทางโดยตรงในช่องคำสั่งแอป
- Apps Script: ป้อนรหัสการติดตั้งใช้งาน Apps Script โดยค่าเริ่มต้น ระบบจะเรียกใช้ฟังก์ชัน
onAppCommandหากต้องการใช้ฟังก์ชัน Apps Script อื่น ให้ระบุชื่อฟังก์ชันที่กำหนดเองในช่องคำสั่งแอป
ในส่วนคำสั่ง ให้คลิกเพิ่มคำสั่ง
ป้อนข้อมูลต่อไปนี้เกี่ยวกับคำสั่ง
- รหัสคำสั่ง: ตัวเลขตั้งแต่ 1 ถึง 1000 ที่แอปใน Chat ใช้เพื่อจดจำคำสั่งและแสดงผลลัพธ์
- คำอธิบาย: ข้อความที่อธิบายวิธีใช้และจัดรูปแบบคำสั่ง คำอธิบายมีความยาวได้สูงสุด 50 อักขระ
- ประเภทคำสั่ง: เลือกคำสั่งด่วน คำสั่งเครื่องหมายทับ หรือการดำเนินการกับข้อความ
- ระบุชื่อคำสั่งดังนี้
- ชื่อคำสั่งด่วน: ชื่อที่แสดงซึ่งผู้ใช้เลือกจากเมนูเพื่อเรียกใช้คำสั่ง ซึ่งมีความยาวได้สูงสุด 50 อักขระและมีอักขระพิเศษได้ เช่น
Remind me - ชื่อคำสั่งเครื่องหมายทับ: ข้อความที่ผู้ใช้พิมพ์เพื่อเรียกใช้คำสั่งในข้อความ ต้องขึ้นต้นด้วยเครื่องหมายทับ มีเฉพาะข้อความ และมีความยาวได้สูงสุด 50 อักขระ เช่น
/remindMe - ชื่อการดำเนินการกับข้อความ: ชื่อที่แสดงซึ่งผู้ใช้เลือกจากเมนูเพื่อเรียกใช้การดำเนินการกับข้อความ ซึ่งมีความยาวได้สูงสุด 50 อักขระและมีสัญลักษณ์พิเศษได้ เช่น
Remind me
- ชื่อคำสั่งด่วน: ชื่อที่แสดงซึ่งผู้ใช้เลือกจากเมนูเพื่อเรียกใช้คำสั่ง ซึ่งมีความยาวได้สูงสุด 50 อักขระและมีอักขระพิเศษได้ เช่น
ไม่บังคับ: ข้อความแจ้งเตือนการโหลด: ข้อความแจ้งเตือนแบบชั่วคราวที่จะแสดงต่อผู้ใช้ขณะที่ระบบกำลังดำเนินการกับข้อความ ใช้ได้กับการดำเนินการกับข้อความที่ไม่เปิดกล่องโต้ตอบเท่านั้น
ไม่บังคับ: หากต้องการให้แอปใน Chat ตอบกลับคำสั่งด้วยกล่องโต้ตอบ ให้เลือกช่องทำเครื่องหมายเปิดกล่องโต้ตอบ
คลิกบันทึก
ตอนนี้คำสั่งได้รับการกำหนดค่าสำหรับแอปใน Chat แล้ว
แมปคำสั่งกับพรอมต์เริ่มต้น
คุณสามารถแสดงคำสั่งเป็นพรอมต์เริ่มต้นเพื่อให้ผู้ใช้เห็นเป็นชิปแบบอินเทอร์แอกทีฟเมื่อเริ่มข้อความส่วนตัวแบบ 1:1 ที่ว่างเปล่ากับแอปใน Chat ของคุณ
วิธีแมปคำสั่งกับพรอมต์เริ่มต้น
- ตรวจสอบว่าคำสั่งของคุณไม่จำเป็นต้องใช้อาร์กิวเมนต์ที่กำหนดเองเพิ่มเติม (ระบบรองรับเฉพาะคำสั่งที่ไม่มีอาร์กิวเมนต์หรืออาร์กิวเมนต์พื้นฐานเป็นพรอมต์เริ่มต้น)
- ในคอนโซล Google Cloud ให้ไปที่หน้าการกำหนดค่า Chat API
- ในส่วนฟีเจอร์แบบอินเทอร์แอกทีฟ > พรอมต์เริ่มต้น ให้คลิกเพิ่มพรอมต์
- ตั้งค่าอันดับ (1-3) สำหรับลำดับการแสดงผล
- ในส่วนการเลือกประเภท ให้เลือกพรอมต์คำสั่ง แล้วเลือกคำสั่งจากเมนูแบบเลื่อนลง
- คลิกเสร็จสิ้น แล้วคลิกบันทึก
ตอบสนองต่อคำสั่ง
เมื่อผู้ใช้ใช้คำสั่ง แอปใน Chat ของคุณจะได้รับออบเจ็กต์เหตุการณ์
เพย์โหลดเหตุการณ์ (event.chat.appCommandPayload) มีออบเจ็กต์ appCommandPayload พร้อมรายละเอียดเกี่ยวกับคำสั่งที่เรียกใช้ (รวมถึงรหัสคำสั่งและประเภทคำสั่ง) เพื่อให้คุณแสดงผลการตอบกลับที่เหมาะสมได้
ระบบจะส่งออบเจ็กต์เหตุการณ์ไปยังปลายทาง HTTP หรือฟังก์ชัน Apps Script ที่คุณระบุไว้เมื่อกำหนดค่าทริกเกอร์คำสั่งแอป
/help เป็นการส่วนตัวเพื่ออธิบายวิธีรับการสนับสนุนตอบกลับคำสั่งเครื่องหมายทับหรือคำสั่งด่วน
โค้ดต่อไปนี้แสดงตัวอย่างแอปใน Chat ที่ตอบกลับคำสั่งเครื่องหมายทับ /about ด้วย SMS หากต้องการตอบกลับคำสั่งเครื่องหมายทับหรือคำสั่งด่วน แอปใน Chat จะจัดการออบเจ็กต์เหตุการณ์ (event.chat.appCommandPayload) จากทริกเกอร์คำสั่งแอป
เมื่อเพย์โหลดของออบเจ็กต์เหตุการณ์มีรหัสคำสั่งที่ตรงกัน แอปใน Chat จะแสดงผลการดำเนินการ DataActions พร้อมออบเจ็กต์ createMessageAction (hostAppDataAction.chatDataAction.createMessageAction) ดังนี้
Node.js
Python
Java
Apps Script
หากต้องการใช้ตัวอย่างโค้ดนี้ ให้แทนที่ ABOUT_COMMAND_ID ด้วยรหัสคำสั่งที่คุณระบุเมื่อกำหนดค่าคำสั่งใน Chat API
ตอบกลับการดำเนินการกับข้อความ
โค้ดต่อไปนี้แสดงตัวอย่างแอปใน Chat ที่ตอบกลับการดำเนินการกับข้อความเตือนฉันด้วย SMS หากต้องการตอบสนองต่อการดำเนินการกับข้อความ แอปใน Chat จะจัดการออบเจ็กต์เหตุการณ์จากทริกเกอร์คำสั่งแอป เมื่อเพย์โหลดของออบเจ็กต์เหตุการณ์มีรหัสคำสั่งการดำเนินการกับข้อความ แอปใน Chat จะแสดงผลการดำเนินการ DataActions พร้อมออบเจ็กต์ createMessageAction ดังนี้
Node.js
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @param {Object} res The HTTP response object.
* @return {Object} The JSON response message with a confirmation.
*/
function onAppCommand(event, res) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} =
event.chat.appCommandPayload.appCommandMetadata;
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.chat.appCommandPayload.message.text;
// Return a response that includes details from the original message.
return res.json({
"hostAppDataAction": {
"chatDataAction": {
"createMessageAction": {
"message": {
"text": `Setting a reminder for message: "${messageText}"`
}
}
}
}
});
}
}
Python
def on_app_command(event):
"""Responds to an APP_COMMAND interaction event from Google Chat.
Args:
event (dict): The interaction event from Google Chat.
Returns:
dict: The JSON response message with a confirmation.
"""
# Collect the command ID and type from the event metadata.
payload = event.get('chat', {}).get('appCommandPayload', {})
metadata = payload.get('appCommandMetadata', {})
if metadata.get('appCommandType') == 'MESSAGE_ACTION' and \
metadata.get('appCommandId') == REMIND_ME_COMMAND_ID:
# Message actions can access the context of the message they were
# invoked on, such as the text or sender of that message.
message_text = payload.get('message', {}).get('text')
# Return a response that includes details from the original message.
return {
"hostAppDataAction": {
"chatDataAction": {
"createMessageAction": {
"message": {
"text": f'Setting a reminder for message: "{message_text}"'
}
}
}
}
}
Java
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param event The interaction event from Google Chat.
* @param response The HTTP response object.
*/
void onAppCommand(JsonObject event, HttpResponse response) throws Exception {
// Collect the command ID and type from the event metadata.
JsonObject payload = event.getAsJsonObject("chat").getAsJsonObject("appCommandPayload");
JsonObject metadata = payload.getAsJsonObject("appCommandMetadata");
String appCommandType = metadata.get("appCommandType").getAsString();
if (appCommandType.equals("MESSAGE_ACTION")) {
int commandId = metadata.get("appCommandId").getAsInt();
if (commandId == REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
String messageText = payload.getAsJsonObject("message").get("text").getAsString();
// Return a response that includes details from the original message.
JsonObject responseMessage = new JsonObject();
responseMessage.addProperty("text", "Setting a reminder for message: " + messageText);
JsonObject createMessageAction = new JsonObject();
createMessageAction.add("message", responseMessage);
JsonObject chatDataAction = new JsonObject();
chatDataAction.add("createMessageAction", createMessageAction);
JsonObject hostAppDataAction = new JsonObject();
hostAppDataAction.add("chatDataAction", chatDataAction);
JsonObject finalResponse = new JsonObject();
finalResponse.add("hostAppDataAction", hostAppDataAction);
response.getWriter().write(finalResponse.toString());
}
}
}
Apps Script
/**
* Responds to an APP_COMMAND interaction event in Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @return {Object} The JSON response message with a confirmation.
*/
function onAppCommand(event) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} =
event.chat.appCommandPayload.appCommandMetadata;
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.chat.appCommandPayload.message.text;
// Return a response that includes details from the original message.
return CardService.newChatResponseBuilder()
.setText("Setting a reminder for message: " + messageText)
.build();
}
}
หากต้องการใช้ตัวอย่างโค้ดนี้ ให้แทนที่ REMIND_ME_COMMAND_ID ด้วยรหัสคำสั่งที่คุณระบุไว้เมื่อกำหนดค่าคำสั่งใน Chat API
ทดสอบคำสั่ง
หากต้องการทดสอบคำสั่งและโค้ด โปรดดูหัวข้อทดสอบฟีเจอร์แบบอินเทอร์แอกทีฟสำหรับแอป Google Chat
ดูวิธีทดสอบและใช้คำสั่งใน UI ของ Chat ได้ที่หัวข้อใช้แอปใน Google Chat ในเอกสารความช่วยเหลือของ Google Chat
หัวข้อที่เกี่ยวข้อง
แอปใน Chat ที่ไม่ใช่ส่วนเสริม: ตอบสนองต่อคำสั่ง
เอกสารประกอบต่อไปนี้ใช้กับแอปใน Chat ที่ไม่ใช่ส่วนเสริม Google Workspace หากต้องการย้ายข้อมูลแอป Chat ที่ไม่ใช่ส่วนเสริม โปรดดูหัวข้อแปลงแอป Google Chat เป็นส่วนเสริม Google Workspace
เมื่อผู้ใช้ใช้คำสั่ง แอปใน Chat ที่ไม่ใช่ส่วนเสริมจะได้รับเหตุการณ์การโต้ตอบและสามารถตอบกลับโดยแสดงผลออบเจ็กต์ Message โดยตรง
เพย์โหลดของเหตุการณ์มีข้อมูลเมตาพร้อมรายละเอียดเกี่ยวกับคำสั่งที่เรียกใช้ (รวมถึงรหัสคำสั่งและประเภทคำสั่ง) เพื่อให้คุณแสดงผลการตอบกลับที่เหมาะสมได้
หากต้องการตอบสนองต่อคำสั่งแต่ละประเภทในแอป Chat ที่ไม่ใช่ส่วนเสริม คุณต้องจัดการประเภทเหตุการณ์และออบเจ็กต์ข้อมูลเมตาที่แตกต่างกันในเพย์โหลดเหตุการณ์ ดังนี้
| ประเภทคำสั่ง | ประเภทของกิจกรรม | ข้อมูลเมตาของคำสั่ง |
|---|---|---|
| คำสั่งเครื่องหมายทับ | MESSAGE |
message.slashCommand
หรือ message.annotation.slashCommand |
| คำสั่งด่วน | APP_COMMAND |
appCommandMetadata
|
| การดำเนินการกับข้อความ | APP_COMMAND |
appCommandMetadata
|
ตอบกลับคำสั่งเครื่องหมายทับ
โค้ดต่อไปนี้แสดงตัวอย่างแอปใน Chat ที่ไม่ใช่ส่วนเสริมซึ่งตอบกลับคำสั่งเครื่องหมายทับ /about แอปใน Chat จะจัดการเหตุการณ์การโต้ตอบ MESSAGE ตรวจหาว่าเหตุการณ์การโต้ตอบมีรหัสคำสั่งที่ตรงกันหรือไม่ และแสดงผลออบเจ็กต์ Message ส่วนตัว ดังนี้
Node.js
Apps Script
Python
Java
แทนที่ ABOUT_COMMAND_ID ด้วยรหัสคำสั่งที่คุณระบุเมื่อกำหนดค่าคำสั่งในคอนโซล Google Cloud
ตอบสนองต่อคำสั่งด่วน
โค้ดต่อไปนี้แสดงตัวอย่างแอปใน Chat ที่ไม่ใช่ส่วนเสริมซึ่งตอบกลับคำสั่งด่วน Help แอปใน Chat จะจัดการเหตุการณ์การโต้ตอบ APP_COMMAND ตรวจหาว่าเหตุการณ์การโต้ตอบมีรหัสคำสั่งที่ตรงกันหรือไม่ และแสดงผลออบเจ็กต์ Message ส่วนตัว ดังนี้
Node.js
Apps Script
Python
Java
แทนที่ HELP_COMMAND_ID ด้วยรหัสคำสั่งที่คุณระบุเมื่อกำหนดค่าคำสั่งในคอนโซล Google Cloud
ตอบกลับการดำเนินการกับข้อความ
โค้ดต่อไปนี้แสดงตัวอย่างแอปใน Chat ที่ไม่ใช่ส่วนเสริมซึ่งตอบกลับการดำเนินการกับข้อความเตือนฉัน แอปใน Chat จะจัดการเหตุการณ์การโต้ตอบ APP_COMMAND ตรวจหาว่าเหตุการณ์การโต้ตอบมีรหัสคำสั่งที่ตรงกันหรือไม่ และแสดงผลออบเจ็กต์ Message ส่วนตัว ดังนี้
Node.js
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @param {Object} res The HTTP response object.
* @return {Object} The JSON response message with a confirmation.
*/
function handleAppCommand(event, res) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} = event.appCommandMetadata;
// Use appCommandType to detect message actions.
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.message.text;
// Return a response that includes details from the original message.
return res.send({
text: `Setting a reminder for this message: "${messageText}"`
});
}
}
Apps Script
/**
* Responds to an APP_COMMAND interaction event in Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @return {Object} The JSON response message with a confirmation.
*/
function onAppCommand(event) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} = event.appCommandMetadata;
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.message.text;
// Return a response that includes details from the original message.
return { "text": "Setting a reminder for message: " + messageText };
}
}
Python
def handle_app_command(event):
"""Responds to an APP_COMMAND interaction event from Google Chat.
Args:
event (dict): The interaction event from Google Chat.
Returns:
dict: The JSON response message with a confirmation.
"""
# Collect the command ID and type from the event metadata.
metadata = event.get('appCommandMetadata', {})
if metadata.get('appCommandType') == 'MESSAGE_ACTION' and \
metadata.get('appCommandId') == REMIND_ME_COMMAND_ID:
# Message actions can access the context of the message they were
# invoked on, such as the text or sender of that message.
message_text = event.get('message', {}).get('text')
# Return a response that includes details from the original message.
return {
"text": f'Setting a reminder for message: "{message_text}"'
}
Java
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param event The interaction event from Google Chat.
* @param response The HTTP response object.
*/
void handleAppCommand(JsonObject event, HttpResponse response) throws Exception {
// Collect the command ID and type from the event metadata.
JsonObject metadata = event.getAsJsonObject("appCommandMetadata");
String appCommandType = metadata.get("appCommandType").getAsString();
if (appCommandType.equals("MESSAGE_ACTION")) {
int commandId = metadata.get("appCommandId").getAsInt();
if (commandId == REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
String messageText = event.getAsJsonObject("message").get("text").getAsString();
// Return a response that includes details from the original message.
JsonObject responseMessage = new JsonObject();
responseMessage.addProperty("text", "Setting a reminder for message: " + messageText);
response.getWriter().write(responseMessage.toString());
}
}
}
แทนที่ REMIND_ME_COMMAND_ID ด้วยรหัสคำสั่งที่คุณระบุเมื่อกำหนดค่าคำสั่งในคอนโซล Google Cloud