บทแนะนำนี้จะแสดงวิธีสร้างแอป Google Chat ที่ช่วยผู้ใช้ Google Chat จัดการรายชื่อติดต่อส่วนตัวและทางธุรกิจ แอป Chat จะแจ้งให้ผู้ใช้กรอกแบบฟอร์มติดต่อในข้อความการ์ดและกล่องโต้ตอบเพื่อรวบรวมข้อมูล
ดูการทำงานของแอป Chat ดังนี้
-
รูปที่ 2 แอป Chat จะเปิดกล่องโต้ตอบที่ผู้ใช้สามารถ กรอกข้อมูลรายชื่อติดต่อ -
รูปที่ 3 แอป Chat แสดงกล่องโต้ตอบการยืนยัน ที่ผู้ใช้จะตรวจสอบและยืนยันข้อมูลได้ การส่ง -
รูปที่ 4 หลังจากผู้ใช้ส่ง แบบฟอร์ม แล้วแอปแชทจะส่งหมายเลข SMS เพื่อยืนยันการส่ง -
รูปที่ 5 แอป Chat ยังแจ้งให้ผู้ใช้เพิ่มรายชื่อติดต่อ จากการ์ดในข้อความ
ข้อกำหนดเบื้องต้น
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
วัตถุประสงค์
- ออกแบบและสร้างอินเทอร์เฟซผู้ใช้ (UI)
เป็นออบเจ็กต์
card
และแสดง UI ในข้อความและกล่องโต้ตอบ - รับและประมวลผลข้อมูลที่ผู้ใช้ส่งโดยใช้ วิดเจ็ตอินพุตฟอร์ม
- ตอบกลับคำสั่งเครื่องหมายทับโดยการส่ง ข้อความที่มีข้อความ การ์ด และวิดเจ็ตอุปกรณ์เสริม
สถาปัตยกรรม
แอป Chat สร้างขึ้นใน Google Apps Script และใช้เหตุการณ์การโต้ตอบเพื่อประมวลผลและตอบกลับผู้ใช้ Chat
ต่อไปนี้คือวิธีที่ผู้ใช้อาจโต้ตอบกับแอปแชทตามปกติ
ผู้ใช้เปิดข้อความส่วนตัวด้วยแอป Chat หรือเพิ่มแอป Chat ไปยังพื้นที่ทำงานที่มีอยู่
แอป Chat จะแจ้งให้ผู้ใช้เพิ่มรายชื่อติดต่อโดยการสร้างและแสดงแบบฟอร์มติดต่อเป็นออบเจ็กต์
card
แอป Chat จะตอบสนองต่อผู้ใช้ด้วยวิธีต่อไปนี้เพื่อแสดงแบบฟอร์มติดต่อ- ตอบกลับการ @พูดถึงและข้อความส่วนตัวด้วยข้อความการ์ดที่ มีแบบฟอร์มติดต่อ
- ตอบคำสั่งเครื่องหมายทับ
/addContact
โดยเปิดกล่องโต้ตอบที่มี แบบฟอร์มติดต่อ - ตอบคำสั่งเครื่องหมายทับ
/about
ด้วย SMS ที่มีแอตทริบิวต์ ปุ่มเพิ่มรายชื่อติดต่อที่ผู้ใช้สามารถคลิกเพื่อเปิดกล่องโต้ตอบที่มี แบบฟอร์มติดต่อ
เมื่อเห็นแบบฟอร์มติดต่อ ผู้ใช้จะป้อนข้อมูลติดต่อในช่องและวิดเจ็ตต่อไปนี้
- ชื่อและนามสกุล: ก
textInput
เป็นวิดเจ็ตที่ยอมรับสตริง - วันเกิด:
dateTimePicker
วิดเจ็ตที่รับเฉพาะวันที่ - ประเภทรายชื่อติดต่อ: ก
selectionInput
วิดเจ็ตของปุ่มตัวเลือกที่ให้ผู้ใช้เลือกและส่งสตริงเดียวได้ (Personal
หรือWork
) - ปุ่มตรวจสอบและส่ง: อาร์เรย์
buttonList
ที่มีวิดเจ็ตbutton
ที่ผู้ใช้คลิกเพื่อส่งค่าที่ป้อน
- ชื่อและนามสกุล: ก
แอป Google Chat จะจัดการ
CARD_CLICKED
เหตุการณ์การโต้ตอบเพื่อประมวลผลค่าที่ผู้ใช้ป้อน และแสดงค่าในการ์ดยืนยันผู้ใช้ตรวจสอบการ์ดยืนยันและคลิกปุ่มส่ง เพื่อสรุปข้อมูลติดต่อ
แอป Google Chat จะส่งข้อความส่วนตัวเพื่อยืนยันการส่ง
เตรียมสภาพแวดล้อม
ส่วนนี้จะแสดงวิธีสร้างและกำหนดค่าโปรเจ็กต์ Google Cloud สำหรับ แอป Chat
สร้างโปรเจ็กต์ Google Cloud
คอนโซล Google Cloud
- ในคอนโซล Google Cloud ให้ไปที่เมนู > IAM และ ผู้ดูแลระบบ > สร้างโครงการ
-
ในช่องชื่อโปรเจ็กต์ ให้ป้อนชื่อที่สื่อความหมายสำหรับโปรเจ็กต์ของคุณ
ไม่บังคับ: หากต้องการแก้ไขรหัสโปรเจ็กต์ ให้คลิกแก้ไข เปลี่ยนรหัสโปรเจ็กต์ไม่ได้ หลังจากสร้างโปรเจ็กต์แล้ว ดังนั้นให้เลือกรหัสที่ตรงกับความต้องการของคุณตลอดอายุการใช้งาน
- ในช่องสถานที่ ให้คลิกเรียกดูเพื่อแสดงสถานที่ที่เป็นไปได้สำหรับโปรเจ็กต์ จากนั้นคลิกเลือก
- คลิกสร้าง คอนโซล Google Cloud จะไปยังหน้าแดชบอร์ดและสร้างโปรเจ็กต์แล้ว ภายในไม่กี่นาที
gcloud CLI
เข้าถึง Google Cloud CLI (gcloud
) ในสภาพแวดล้อมการพัฒนาอย่างใดอย่างหนึ่งต่อไปนี้
-
Cloud Shell: หากต้องการใช้เทอร์มินัลออนไลน์ที่มีการตั้งค่า gcloud CLI ไว้แล้ว ให้เปิดใช้งาน Cloud Shell
เปิดใช้งาน Cloud Shell -
เชลล์ในเครื่อง: หากต้องการใช้สภาพแวดล้อมการพัฒนาซอฟต์แวร์ในเครื่อง ให้ติดตั้งและเริ่มต้น CLI ของ gcloud
หากต้องการสร้างโปรเจ็กต์ที่อยู่ในระบบคลาวด์ ให้ใช้คำสั่งgcloud projects create
: แทนที่ PROJECT_ID ด้วยการตั้งค่ารหัสสำหรับโปรเจ็กต์ที่ต้องการสร้างgcloud projects create PROJECT_ID
ตั้งค่าการตรวจสอบสิทธิ์และการให้สิทธิ์
แอป Google Chat กำหนดให้คุณกำหนดค่าหน้าจอขอความยินยอม OAuth เพื่อให้ผู้ใช้สามารถให้สิทธิ์แอปของคุณในแอปพลิเคชัน Google Workspace ซึ่งรวมถึง Google Chat
ในบทแนะนำนี้ คุณจะได้ทำให้แอป Chat ใช้งานได้สำหรับ และการใช้งานภายใน ดังนั้นสามารถใช้ข้อมูลตัวยึดตำแหน่งสำหรับ หน้าจอคำยินยอม ก่อนเผยแพร่แอป Chat ให้แทนที่ ข้อมูลตัวยึดตำแหน่งที่มีข้อมูลจริง
ในคอนโซล Google Cloud ให้ไปที่ เมนู > API และ บริการ > หน้าจอขอความยินยอม OAuth
ในส่วนประเภทผู้ใช้ ให้เลือกภายใน แล้วคลิกสร้าง
ในชื่อแอป ให้พิมพ์
Contact Manager
ในอีเมลการสนับสนุนผู้ใช้ ให้เลือกที่อยู่อีเมลของคุณหรืออีเมล กลุ่ม Google
ป้อนอีเมลของคุณในส่วนข้อมูลติดต่อของนักพัฒนาแอป
คลิกบันทึกและต่อไป
ในหน้าขอบเขต ให้คลิกบันทึกและต่อไป (แอปแชทไม่จําเป็นต้องใช้ขอบเขต OAuth)
ตรวจสอบสรุป แล้วคลิกกลับไปที่หน้าแดชบอร์ด
สร้างและติดตั้งใช้งานแอป Chat
ในส่วนต่อไปนี้ ให้คัดลอกและอัปเดต โครงการ Apps Script ที่มีแอปพลิเคชันที่จำเป็นทั้งหมด สำหรับแอป Chat เพื่อจะได้ไม่ต้องคัดลอก วางแต่ละไฟล์
นอกจากนี้ คุณยังดูทั้งโปรเจ็กต์ได้ใน GitHub
ภาพรวมของแต่ละไฟล์มีดังนี้
main.gs
จัดการตรรกะของแอปทั้งหมด รวมถึงเหตุการณ์การโต้ตอบเกี่ยวกับเวลาที่ผู้ใช้ส่งข้อความ ไปยังแอป Chat ให้คลิกปุ่มจาก ข้อความแอปใน Chat หรือเปิดและปิดกล่องโต้ตอบ
ดูรหัส
main.gs
contactForm.gs
มีวิดเจ็ตที่รับข้อมูลแบบฟอร์มจากผู้ใช้ วิดเจ็ตการป้อนข้อมูลแบบฟอร์มเหล่านี้จะแสดงในการ์ดที่ปรากฏในข้อความและกล่องโต้ตอบ
ดูรหัส
contactForm.gs
appsscript.json
ไฟล์ Manifest ของ Apps Script ที่กําหนดค่าและกำหนดค่าโปรเจ็กต์ Apps Script สําหรับแอป Chat
ดูรหัส
appsscript.json
ค้นหาหมายเลขและรหัสโปรเจ็กต์ในระบบคลาวด์
ไปที่โปรเจ็กต์ที่อยู่ในระบบคลาวด์ในคอนโซล Google Cloud
คลิกการตั้งค่าและยูทิลิตี > การตั้งค่าโปรเจ็กต์
ดูค่าต่างๆ ในช่องหมายเลขโปรเจ็กต์และรหัสโปรเจ็กต์ คุณ ใช้ในส่วนต่อไปนี้
สร้างโปรเจ็กต์ Apps Script
วิธีสร้างโครงการ Apps Script และเชื่อมต่อกับ โปรเจ็กต์ที่อยู่ในระบบคลาวด์:
- คลิกปุ่มต่อไปนี้เพื่อเปิดโปรเจ็กต์ Apps Script จัดการรายชื่อติดต่อใน Google Chat
เปิดโปรเจ็กต์ - คลิก ภาพรวม
- ในหน้าภาพรวม ให้คลิก ทําสําเนา
ตั้งชื่อสำเนาโครงการ Apps Script ดังนี้
คลิกสำเนาของ "จัดการรายชื่อติดต่อใน Google Chat"
ในชื่อโปรเจ็กต์ ให้พิมพ์
Contact Manager - Google Chat app
คลิกเปลี่ยนชื่อ
ตั้งค่าโปรเจ็กต์ Cloud ของโปรเจ็กต์ Apps Script
- ในโปรเจ็กต์ Apps Script ให้คลิก Project Settings
- ในส่วนโปรเจ็กต์ Google Cloud Platform (GCP) ให้คลิกเปลี่ยนโปรเจ็กต์
- วางหมายเลขโปรเจ็กต์ GCP ในหมายเลขโปรเจ็กต์ GCP
- คลิกตั้งค่าโปรเจ็กต์ โปรเจ็กต์ที่อยู่ในระบบคลาวด์และ Apps Script เชื่อมต่อโปรเจ็กต์แล้ว
สร้าง Apps Script ที่นำไปใช้งาน
เมื่อใส่โค้ดทั้งหมดแล้ว ให้นำ Apps Script ไปใช้งาน คุณจะใช้รหัสการทำให้ใช้งานได้เมื่อกำหนดค่า แอป Chat ใน Google Cloud
ใน Apps Script ให้เปิด
คลิกทำให้ใช้งานได้ > ทำให้ใช้งานได้ใหม่
หากยังไม่ได้เลือกส่วนเสริม ให้ดำเนินการดังนี้ เลือกประเภท คลิกประเภทการทำให้ใช้งานได้ แล้วเลือกส่วนเสริม
ในคำอธิบาย ให้ป้อนคำอธิบายสำหรับเวอร์ชันนี้ เช่น
Test of Contact Manager
คลิกทำให้ใช้งานได้ รายงาน Apps Script สำเร็จแล้ว การทำให้ใช้งานได้ และระบุรหัสการทำให้ใช้งานได้
คลิก
คัดลอกเพื่อคัดลอก รหัสการทำให้ใช้งานได้ แล้วคลิกเสร็จสิ้น
กำหนดค่าแอป Chat ในคอนโซล Google Cloud
ส่วนนี้จะแสดงวิธีกำหนดค่า Google Chat API ในคอนโซล Google Cloud ที่มีข้อมูลเกี่ยวกับแอป Chat ของคุณ รวมถึง รหัสการทำให้ใช้งานได้ที่คุณเพิ่งสร้างจาก Apps Script
ในคอนโซล Google Cloud ให้คลิกเมนู > ผลิตภัณฑ์เพิ่มเติม > Google Workspace > คลังผลิตภัณฑ์ > Google Chat API > จัดการ > การกำหนดค่า
ในชื่อแอป ให้พิมพ์
Contact Manager
ใน URL รูปโปรไฟล์ ให้พิมพ์
https://developers.google.com/chat/images/contact-icon.png
พิมพ์
Manage your personal and business contacts
ในคำอธิบายคลิกปุ่มสลับเปิดใช้ฟีเจอร์แบบอินเทอร์แอกทีฟไปที่ตำแหน่งเปิด
ในส่วนฟังก์ชัน ให้เลือกช่องทําเครื่องหมายรับข้อความแบบ 1:1 และ เข้าร่วมพื้นที่ทำงานและการสนทนากลุ่ม
เลือก Apps Script ในส่วนการตั้งค่าการเชื่อมต่อ
วางรหัสการทำให้ใช้งานได้ของ Apps Script ในรหัสการทำให้ใช้งานได้ ที่คุณคัดลอกในส่วนก่อนหน้านี้เมื่อสร้าง การทำให้ Apps Script ใช้งานได้
ในส่วนคำสั่งเครื่องหมายทับ ให้ตั้งค่าคำสั่งเครื่องหมายทับ
/about
และ/addContact
ดังนี้- คลิกเพิ่มคำสั่งเครื่องหมายทับเพื่อตั้งค่าคำสั่งเครื่องหมายทับรายการแรก
- ใน ชื่อ ให้พิมพ์
/about
- ใน Command ID ให้พิมพ์
1
- ในคำอธิบาย ให้พิมพ์
Learn how to use this Chat app to manage your contacts
- เลือกเปิดกล่องโต้ตอบ
- คลิกเสร็จสิ้น
- คลิกเพิ่มคำสั่งเครื่องหมายทับเพื่อตั้งค่าคำสั่งเครื่องหมายทับอื่น
- พิมพ์
/addContact
ในชื่อ - ในรหัสคําสั่ง ให้พิมพ์
2
- ในคำอธิบาย ให้พิมพ์
Submit information about a contact
- เลือกเปิดกล่องโต้ตอบ
- คลิกเสร็จสิ้น
ในส่วนระดับการเข้าถึง ให้เลือก ทำให้แอป Chat นี้ใช้งานได้กับบุคคลหรือกลุ่มที่ต้องการใน YOUR DOMAIN และป้อนอีเมลของคุณ
เลือกบันทึกข้อผิดพลาดในการบันทึกในส่วนบันทึก
คลิกบันทึก ข้อความที่บันทึกไว้สำหรับการกำหนดค่าจะปรากฏขึ้น
แอป Chat พร้อมให้ติดตั้งและทดสอบใน Chat แล้ว
ทดสอบแอป Chat
หากต้องการทดสอบแอป Chat ให้เปิดพื้นที่ทำงานของข้อความส่วนตัวด้วยแอป Chat แล้วส่งข้อความ โดยทำดังนี้
เปิด Google Chat โดยใช้บัญชี Google Workspace ที่คุณให้ไว้เมื่อเพิ่มตัวเองเป็นผู้ทดสอบที่เชื่อถือได้
- คลิก แชทใหม่
- ในช่องเพิ่มผู้คนอย่างน้อย 1 คน ให้พิมพ์ชื่อ แอป Chat
เลือกแอปใน Chat จากผลการค้นหา โดยตรง ข้อความจะเปิดขึ้น
ในข้อความส่วนตัวใหม่ที่มีแอป Chat พิมพ์
/addContact
แล้วกด Enterในกล่องโต้ตอบที่เปิดขึ้น ให้ป้อนข้อมูลติดต่อ:
- ป้อนชื่อในช่องข้อความชื่อและนามสกุล
- เลือกวันที่ในเครื่องมือเลือกวันที่วันเกิด
- ในส่วนประเภทรายชื่อติดต่อ ให้เลือกปุ่มตัวเลือกที่ทำงานหรือส่วนตัว
คลิกตรวจสอบและส่ง
ในกล่องโต้ตอบการยืนยัน ให้ตรวจสอบข้อมูลที่คุณส่ง แล้วคลิกส่ง แอป Chat ตอบกลับด้วยข้อความ ข้อความว่า
✅ CONTACT NAME has been added to your contacts.
นอกจากนี้ คุณยังทดสอบและส่งแบบฟอร์มติดต่อในรายการต่อไปนี้ได้ ด้วยวิธีต่อไปนี้
- ใช้คำสั่งเครื่องหมายทับ
/about
การตอบกลับของแอป Chat ด้วยข้อความและปุ่มวิดเจ็ตอุปกรณ์เสริมที่เขียนว่าAdd a contact
คุณสามารถคลิกปุ่มเพื่อเปิดกล่องโต้ตอบที่มีแบบฟอร์มติดต่อ - ส่งข้อความส่วนตัวหาแอป Chat โดยไม่มี
คําสั่งเครื่องหมายทับ เช่น
Hello
แอป Chat การตอบกลับด้วยข้อความและการ์ดที่มีแบบฟอร์มติดต่อ
- ใช้คำสั่งเครื่องหมายทับ
ล้างข้อมูล
เราขอแนะนำให้คุณลบโปรเจ็กต์ Cloud เพื่อหลีกเลี่ยงการเรียกเก็บเงินจากบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในบทแนะนำนี้
- ในคอนโซล Google Cloud ให้ไปที่หน้าจัดการทรัพยากร คลิก เมนู > IAM และผู้ดูแลระบบ > จัดการทรัพยากร
- ในรายการโปรเจ็กต์ ให้เลือกโปรเจ็กต์ที่ต้องการลบแล้วคลิก ลบ
- ในกล่องโต้ตอบ ให้พิมพ์รหัสโปรเจ็กต์ แล้วคลิกปิดเพื่อลบโปรเจ็กต์
หัวข้อที่เกี่ยวข้อง
- ตอบกลับคำสั่งเครื่องหมายทับ
- รวบรวมและประมวลผลข้อมูลจากผู้ใช้ Google Chat
- เปิดกล่องโต้ตอบแบบอินเทอร์แอกทีฟ
- ดูตัวอย่างแอป Google Chat อื่นๆ