ตอบสนองต่อเหตุการณ์ด้วย Google Chat, Vertex AI, Apps Script และการตรวจสอบสิทธิ์ผู้ใช้

บทแนะนำนี้แสดงวิธีสร้างแอป Google Chat ที่ ตอบสนองต่อเหตุการณ์แบบเรียลไทม์ เมื่อตอบสนองต่อเหตุการณ์ แอปจะสร้างและป้อนข้อมูลในพื้นที่ทำงานของ Chat, ช่วยให้การแก้ไขเหตุการณ์เป็นไปได้ด้วยข้อความ, คำสั่งแอป และกล่องโต้ตอบ รวมถึงใช้ AI เพื่อสรุปการตอบสนองต่อเหตุการณ์ในเอกสาร Google เอกสาร

เหตุการณ์คือเหตุการณ์ที่ต้องได้รับความสนใจจากทีม บุคคลเพื่อแก้ไขทันที ตัวอย่างเหตุการณ์มีดังนี้

  • มีการสร้างเคสที่ต้องดำเนินการอย่างเร่งด่วนในแพลตฟอร์มการจัดการลูกค้าสัมพันธ์ (CRM) ซึ่งกำหนดให้ทีมบริการต้องทำงานร่วมกันเพื่อแก้ไข
  • ระบบออฟไลน์ ทำให้มีการแจ้งเตือนกลุ่มวิศวกรความเสถียรสำหรับเว็บไซต์ (SRE) เพื่อให้วิศวกรทำงานร่วมกันเพื่อนำระบบกลับมาออนไลน์
  • เกิดแผ่นดินไหวรุนแรง และเจ้าหน้าที่กู้ภัยต้อง ประสานงานการตอบสนอง

สำหรับวัตถุประสงค์ของบทแนะนำนี้ การแจ้งเตือนเหตุการณ์จะเริ่มเมื่อมีผู้รายงานเหตุการณ์ด้วยการคลิกปุ่มจากหน้าเว็บ หน้าเว็บจะจำลอง เหตุการณ์โดยขอให้ผู้ใช้ป้อนข้อมูลเหตุการณ์พื้นฐาน ได้แก่ ชื่อ คำอธิบาย และอีเมลของผู้ตอบ

ดูการทำงานของแอป Chat สำหรับการจัดการเหตุการณ์

  • เว็บไซต์ที่เริ่มเหตุการณ์
    รูปที่ 1 เว็บไซต์ที่ผู้ใช้รายงานเหตุการณ์ได้
  • การแจ้งเตือนว่ามีการสร้างพื้นที่ใน Chat สำหรับเหตุการณ์
    รูปที่ 2 การแจ้งเตือนว่ามีการสร้างพื้นที่ใน Chat สำหรับเหตุการณ์
  • พื้นที่ใน Chat สำหรับการตอบสนองต่อเหตุการณ์
    รูปที่ 3 พื้นที่ใน Chat สำหรับการตอบสนองต่อเหตุการณ์
  • แก้ไขเหตุการณ์ด้วยคำสั่งอย่างรวดเร็ว
    รูปที่ 4 การแก้ไขเหตุการณ์ด้วยคำสั่งด่วน
  • กล่องโต้ตอบการแก้ไขเหตุการณ์
    รูปที่ 5 กล่องโต้ตอบการแก้ไขเหตุการณ์
  • เอกสาร Google เอกสารเกี่ยวกับการแก้ไขเหตุการณ์ที่แชร์ในพื้นที่ทำงาน
    รูปที่ 6 เอกสาร Google เอกสารเกี่ยวกับการแก้ไขเหตุการณ์ที่แชร์ในพื้นที่ทำงาน
  • เอกสารใน Google เอกสารเกี่ยวกับการแก้ไขเหตุการณ์ที่เกี่ยวข้องกับข้อมูลสรุปที่ AI สร้างขึ้น
    รูปที่ 7 เอกสาร Google เอกสารเกี่ยวกับการแก้ไขเหตุการณ์ที่ AI สรุป

ข้อกำหนดเบื้องต้น

หากต้องการเปิดใช้ข้อกำหนดเบื้องต้นเหล่านี้สำหรับองค์กร โปรดขอให้ ผู้ดูแลระบบ Google Workspace เปิดใช้

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
  • เปิดใช้ไดเรกทอรี (การแชร์รายชื่อติดต่อ) สำหรับ Google Workspace แอปเหตุการณ์จะใช้ ไดเรกทอรีเพื่อค้นหาข้อมูลติดต่อของผู้ตอบเหตุการณ์ เช่น ชื่อ และอีเมล ผู้ตอบสนองต่อเหตุการณ์ต้องเป็นผู้ใช้ที่มีบัญชี Google Chat ในองค์กร Google Workspace

วัตถุประสงค์

  • สร้างแอป Chat ที่ตอบสนองต่อเหตุการณ์
  • ช่วยให้ผู้ใช้ตอบสนองต่อเหตุการณ์ได้โดยทำดังนี้
    • การสร้างพื้นที่ตอบสนองต่อเหตุการณ์
    • โพสต์ข้อความที่สรุปเหตุการณ์และการตอบกลับ
    • รองรับการทำงานร่วมกันด้วยฟีเจอร์แอปแชทแบบอินเทอร์แอกทีฟ
  • สรุปการสนทนาและการแก้ไขด้วย Vertex AI

สถาปัตยกรรม

แผนภาพต่อไปนี้แสดงสถาปัตยกรรมของ Google Workspace และ ทรัพยากร Google Cloud ที่แอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์ใช้

สถาปัตยกรรมของแอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์

สถาปัตยกรรมแสดงวิธีที่แอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์ ประมวลผลเหตุการณ์และการแก้ไข

  1. ผู้ใช้เริ่มเหตุการณ์จากเว็บไซต์ภายนอกที่โฮสต์ใน Apps Script

  2. เว็บไซต์จะส่งคำขอ HTTP แบบไม่พร้อมกันไปยังแอป Google Chat ซึ่งโฮสต์ใน Apps Script ด้วย

  3. แอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์จะประมวลผลคำขอโดยทำดังนี้

    1. บริการ Admin SDK ของ Apps Script จะรับข้อมูลสมาชิกในทีม เช่น รหัสผู้ใช้และอีเมล

    2. เมื่อใช้ชุดคำขอ HTTP ไปยัง Chat API โดยใช้ บริการ Chat ขั้นสูงของ Apps Script แอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์ จะสร้างพื้นที่ Chat สำหรับเหตุการณ์ ป้อนข้อมูลสมาชิกในทีม และส่งข้อความไปยังพื้นที่ทำงาน

  4. สมาชิกในทีมพูดคุยเกี่ยวกับเหตุการณ์ในพื้นที่ใน Chat

  5. สมาชิกในทีมเรียกใช้คำสั่งด่วนเพื่อส่งสัญญาณการแก้ไขปัญหา

    1. การเรียก HTTP ไปยัง Chat API โดยใช้บริการ Chat ขั้นสูงของ Apps Script จะแสดงข้อความทั้งหมดของพื้นที่ทำงานใน Chat

    2. Vertex AI จะรับข้อความที่แสดงและสร้างข้อมูลสรุป

    3. บริการ DocumentApp ของ Apps Script จะสร้างเอกสาร ในเอกสาร และเพิ่มข้อมูลสรุปของ Vertex AI ลงใน เอกสาร

    4. แอป Google Chat สำหรับการตอบสนองต่อเหตุการณ์จะเรียกใช้ Chat API เพื่อส่งข้อความที่แชร์ลิงก์ไปยังสรุป เอกสารในเอกสาร

เตรียมสภาพแวดล้อม

ส่วนนี้แสดงวิธีสร้างและกำหนดค่าโปรเจ็กต์ Google Cloud สำหรับ แอป Chat

สร้างโปรเจ็กต์ Google Cloud

คอนโซล Google Cloud

  1. ใน Google Cloud Console ให้ไปที่เมนู > IAM และผู้ดูแลระบบ > สร้างโปรเจ็กต์

    ไปที่สร้างโปรเจ็กต์

  2. ในช่องชื่อโปรเจ็กต์ ให้ป้อนชื่อที่สื่อความหมายสำหรับโปรเจ็กต์

    ไม่บังคับ: หากต้องการแก้ไขรหัสโปรเจ็กต์ ให้คลิกแก้ไข คุณจะเปลี่ยนรหัสโปรเจ็กต์ไม่ได้ หลังจากสร้างโปรเจ็กต์แล้ว ดังนั้นให้เลือกรหัสที่ตรงกับความต้องการตลอดอายุการใช้งานของ โปรเจ็กต์

  3. ในช่องสถานที่ตั้ง ให้คลิกเรียกดูเพื่อแสดงสถานที่ตั้งที่อาจเป็นไปได้สำหรับ โปรเจ็กต์ จากนั้นคลิกเลือก
  4. คลิกสร้าง Google Cloud Console จะนำคุณไปยังหน้าแดชบอร์ดและสร้างโปรเจ็กต์ภายในไม่กี่นาที

gcloud CLI

ในสภาพแวดล้อมการพัฒนาอย่างใดอย่างหนึ่งต่อไปนี้ ให้เข้าถึง Google Cloud CLI (gcloud)

  • Cloud Shell: หากต้องการใช้เทอร์มินัลออนไลน์ที่มีการตั้งค่า gcloud CLI ไว้แล้ว ให้เปิดใช้งาน Cloud Shell
    เปิดใช้งาน Cloud Shell
  • เชลล์ภายในเครื่อง: หากต้องการใช้สภาพแวดล้อมการพัฒนาภายใน ติดตั้งและ เริ่มต้น gcloud CLI
    หากต้องการสร้างโปรเจ็กต์ Cloud ให้ใช้คำสั่ง gcloud projects create
    gcloud projects create PROJECT_ID
    แทนที่ PROJECT_ID โดยตั้งรหัสสำหรับโปรเจ็กต์ที่ต้องการสร้าง

เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ Cloud

คอนโซล Google Cloud

  1. ในคอนโซล Google Cloud ให้ไปที่การเรียกเก็บเงิน คลิก เมนู > การเรียกเก็บเงิน > โปรเจ็กต์ของฉัน

    ไปที่การเรียกเก็บเงินสำหรับโปรเจ็กต์ของฉัน

  2. ในเลือกองค์กร ให้เลือกองค์กรที่เชื่อมโยงกับ โปรเจ็กต์ Google Cloud
  3. ในแถวของโปรเจ็กต์ ให้เปิดเมนูการดำเนินการ () คลิกเปลี่ยนการเรียกเก็บเงิน แล้วเลือก บัญชี Cloud Billing
  4. คลิกตั้งค่าบัญชี

gcloud CLI

  1. หากต้องการแสดงรายการบัญชีสำหรับการเรียกเก็บเงินที่ใช้ได้ ให้เรียกใช้คำสั่งต่อไปนี้
    gcloud billing accounts list
  2. ลิงก์บัญชีสำหรับการเรียกเก็บเงินกับโปรเจ็กต์ Google Cloud โดยทำดังนี้
    gcloud billing projects link PROJECT_ID --billing-account=BILLING_ACCOUNT_ID

    แทนที่ค่าต่อไปนี้

    • PROJECT_ID คือรหัสโปรเจ็กต์ของ โปรเจ็กต์ Cloud ที่คุณต้องการเปิดใช้การเรียกเก็บเงิน
    • BILLING_ACCOUNT_ID คือรหัสบัญชีสำหรับการเรียกเก็บเงินที่จะลิงก์กับ โปรเจ็กต์ Google Cloud

เปิดใช้ API

คอนโซล Google Cloud

  1. ในคอนโซล Google Cloud ให้เปิดใช้ Google Chat API, Google Docs API, Admin SDK API และ Vertex AI API

    เปิดใช้ API

  2. ยืนยันว่าคุณกำลังเปิดใช้ API ในโปรเจ็กต์ Cloud ที่ถูกต้อง แล้วคลิกถัดไป

  3. ยืนยันว่าคุณเปิดใช้ API ที่ถูกต้อง แล้วคลิกเปิดใช้

gcloud CLI

  1. หากจำเป็น ให้ตั้งค่าโปรเจ็กต์ Cloud ปัจจุบันเป็นโปรเจ็กต์ที่คุณสร้างด้วยคำสั่ง gcloud config set project

    gcloud config set project PROJECT_ID

    แทนที่ PROJECT_ID ด้วยรหัสโปรเจ็กต์ของ โปรเจ็กต์ Cloud ที่คุณสร้าง

  2. เปิดใช้ Google Chat API, Google Docs API, Admin SDK API และ Vertex AI API ด้วยคำสั่ง gcloud services enable ดังนี้

    gcloud services enable chat.googleapis.com docs.googleapis.com admin.googleapis.com aiplatform.googleapis.com

ตั้งค่าการตรวจสอบสิทธิ์และการให้สิทธิ์

การตรวจสอบสิทธิ์และการให้สิทธิ์ช่วยให้ แอป Chat เข้าถึงทรัพยากรใน Google Workspace และ Google Cloud เพื่อประมวลผลการตอบสนองต่อเหตุการณ์ได้

ในบทแนะนำนี้ คุณจะเผยแพร่แอปภายใน ดังนั้นจึงใช้ข้อมูลตัวยึดตำแหน่งได้ ก่อนเผยแพร่แอปภายนอก ให้แทนที่ข้อมูลตัวยึดตำแหน่ง ด้วยข้อมูลจริงสำหรับหน้าจอคำยินยอม

  1. ใน Google Cloud Console ให้ไปที่เมนู > Google Auth platform > การสร้างแบรนด์

    ไปที่การสร้างแบรนด์

  2. หากกำหนดค่า Google Auth platformแล้ว คุณจะกำหนดค่าการตั้งค่าหน้าจอขอความยินยอม OAuth ต่อไปนี้ได้ในการสร้างแบรนด์ กลุ่มเป้าหมาย และการเข้าถึงข้อมูล หากเห็นข้อความที่ระบุว่า Google Auth platform ยังไม่ได้กำหนดค่า ให้คลิกเริ่มต้นใช้งาน

    1. ในส่วนข้อมูลแอป ในชื่อแอป ให้พิมพ์ Incident Management with User Auth
    2. ในอีเมลสนับสนุนผู้ใช้ ให้เลือกอีเมลหรือ Google Group ที่เหมาะสม
    3. คลิกถัดไป
    4. ในส่วนกลุ่มเป้าหมาย ให้เลือกภายใน หากเลือกภายในไม่ได้ ให้เลือกภายนอก
    5. คลิกถัดไป
    6. ในส่วนข้อมูลติดต่อ ให้ป้อนอีเมลที่คุณต้องการ รับการแจ้งเตือนเกี่ยวกับการเปลี่ยนแปลงในโปรเจ็กต์
    7. คลิกถัดไป
    8. ในส่วนเสร็จสิ้น ให้อ่าน นโยบายข้อมูลผู้ใช้ของบริการ Google API และหากยอมรับ ให้เลือก ฉันยอมรับบริการ Google API: นโยบายข้อมูลผู้ใช้
    9. คลิกต่อไป
    10. คลิกสร้าง
    11. หากเลือกภายนอกสำหรับประเภทผู้ใช้ ให้เพิ่มผู้ใช้ทดสอบโดยทำดังนี้
      1. คลิกกลุ่มเป้าหมาย
      2. ในส่วนผู้ใช้ทดสอบ ให้คลิกเพิ่มผู้ใช้
      3. ป้อนอีเมลของคุณและผู้ใช้ทดสอบที่ได้รับอนุญาตอื่นๆ แล้วคลิกบันทึก
  3. คลิกการเข้าถึงข้อมูล > เพิ่มหรือนำขอบเขตออก แผงจะปรากฏขึ้นพร้อมรายการขอบเขต สำหรับ API แต่ละรายการที่คุณเปิดใช้ในโปรเจ็กต์ Google Cloud

    1. ในส่วนเพิ่มขอบเขตด้วยตนเอง ให้วางขอบเขตต่อไปนี้

      • https://www.googleapis.com/auth/chat.spaces.create
      • https://www.googleapis.com/auth/chat.memberships
      • https://www.googleapis.com/auth/chat.memberships.app
      • https://www.googleapis.com/auth/chat.messages
      • https://www.googleapis.com/auth/documents
      • https://www.googleapis.com/auth/admin.directory.user.readonly
      • https://www.googleapis.com/auth/script.external_request
      • https://www.googleapis.com/auth/userinfo.email
      • https://www.googleapis.com/auth/cloud-platform
    2. คลิกเพิ่มลงในตาราง

    3. คลิกอัปเดต

    4. หลังจากเลือกขอบเขตที่แอปของคุณต้องการแล้ว ให้คลิกบันทึกในหน้าการเข้าถึงข้อมูล

สร้างและติดตั้งใช้งานแอป Chat

ในส่วนต่อไปนี้ คุณจะคัดลอกและอัปเดตทั้งโปรเจ็กต์ Apps Script ที่มีโค้ดแอปพลิเคชันที่จำเป็นทั้งหมดสำหรับแอป Chat จึงไม่จำเป็นต้องคัดลอกและวางแต่ละไฟล์

ฟังก์ชันบางอย่างมีขีดล่างที่ท้ายชื่อ เช่น concatenateAllSpaceMessages_() จาก ChatApp.gs ขีดล่างจะซ่อน ฟังก์ชันจากหน้าเว็บการเริ่มต้นเหตุการณ์เมื่อเปิดในเบราว์เซอร์ ดูข้อมูลเพิ่มเติมได้ที่ ฟังก์ชันส่วนตัว

Apps Script รองรับไฟล์ 2 ประเภท ได้แก่ .gsสคริปต์และ.html ไฟล์ หากต้องการปฏิบัติตามการรองรับนี้ ระบบจะรวม JavaScript ฝั่งไคลเอ็นต์ของแอปไว้ ภายในแท็ก <script /> และรวม CSS ไว้ภายในแท็ก <style /> ภายใน ไฟล์ HTML

คุณเลือกดูทั้งโปรเจ็กต์ใน GitHub ได้

ดูใน GitHub

ภาพรวมของแต่ละไฟล์มีดังนี้

Consts.gs

กำหนดค่าคงที่ที่ไฟล์โค้ดอื่นๆ อ้างอิง รวมถึงรหัสโปรเจ็กต์ Cloud, รหัสตำแหน่ง Vertex AI, รหัสคำสั่งด่วนสำหรับปิดเหตุการณ์ และโมเดล Gemini

ดูรหัส Consts.gs

apps-script/chat/incident-response-user-auth/Consts.gs
const PROJECT_ID = 'replace-with-your-project-id';
const VERTEX_AI_LOCATION_ID = 'us-central1';
const CLOSE_INCIDENT_COMMAND_ID = 1;
const MODEL_ID = 'gemini-2.5-flash-lite';
ChatApp.gs

จัดการเหตุการณ์การโต้ตอบในแชท ซึ่งรวมถึง ข้อความ การคลิกปุ่ม คำสั่งแอป และกล่องโต้ตอบ ตอบกลับClose incident คำสั่งด่วนโดยเปิดกล่องโต้ตอบเพื่อรวบรวมรายละเอียดการแก้ปัญหา เหตุการณ์ อ่านข้อความในพื้นที่โดยเรียกใช้เมธอด spaces.messages.list ใน Chat API รับรหัสผู้ใช้โดยใช้บริการไดเรกทอรี Admin SDK ใน Apps Script

ดูรหัส ChatApp.gs

apps-script/chat/incident-response-user-auth/ChatApp.gs
/**
 * Responds to a MESSAGE event in Google Chat.
 * 
 * It always responds with a simple "Hello" text message.
 *
 * @param {Object} event the event object from Google Chat
 */
function onMessage(event) {
  return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
    text: "Hello from Incident Response app!"
  }}}}};
}

/**
 * Responds to an APP_COMMAND event in Google Chat.
 *
 * @param {Object} event the event object from Google Chat
 */
function onAppCommand(event) {
  if (event.chat.appCommandPayload.appCommandMetadata.appCommandId != CLOSE_INCIDENT_COMMAND_ID) {
    return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
      text: "Command not recognized. Use the quick command `Close incident` to close the incident managed by this space."
    }}}}};
  }
  return { action: { navigations: [{ pushCard: { sections: [{
    header: "Close Incident",
    widgets: [{
      textInput: {
        label: "Please describe the incident resolution",
        type: "MULTIPLE_LINE",
        name: "description"
      }
    }, {
      buttonList: { buttons: [{
        text: "Close Incident",
        onClick: { action: { function: "closeIncident" }}
      }]}
    }]
  }]}}]}};
}

/**
 * Responds to a BUTTON_CLICKED event in Google Chat from Close Incident dialog.
 *
 * @param {Object} event the event object from Google Chat
 */
function closeIncident(event) {
  if (event.chat.buttonClickedPayload.isDialogEvent) {
    if (event.chat.buttonClickedPayload.dialogEventType == 'SUBMIT_DIALOG') {
      return processSubmitDialog_(event);
    }
    return { action: { navigations: [{ endNavigation: {
      action: "CLOSE_DIALOG" }
    }]}};
  }
}

/**
 * Responds to a BUTTON_CLICKED event in Google Chat from Close Incident dialog submission.
 *
 * It creates a Doc with a summary of the incident information and posts a message
 * to the space with a link to the Doc.
 *
 * @param {Object} event the event object from Google Chat
 */
function processSubmitDialog_(event) {
  const resolution = event.commonEventObject.formInputs.description.stringInputs.value[0];
  const space = event.chat.buttonClickedPayload.space;
  const chatHistory = concatenateAllSpaceMessages_(space.name);
  const chatSummary = summarizeChatHistory_(chatHistory);
  const docUrl = createDoc_(space.displayName, resolution, chatHistory, chatSummary);
  return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
    text: `Incident closed with the following resolution: ${resolution}\n\nHere is the automatically generated post-mortem:\n${docUrl}`
  }}}}};
}

/**
 * Lists all the messages in the Chat space, then concatenate all of them into
 * a single text containing the full Chat history.
 *
 * For simplicity for this demo, it only fetches the first 100 messages.
 *
 * @return {string} a text containing all the messages in the space in the format:
 *          Sender's name: Message
 */
function concatenateAllSpaceMessages_(spaceName) {
  // Call Chat API method spaces.messages.list
  const response = Chat.Spaces.Messages.list(spaceName, { 'pageSize': 100 });
  const messages = response.messages;
  // Fetch the display names of the message senders and returns a text
  // concatenating all the messages.
  let userMap = new Map();
  return messages
    .map(message => `${getUserDisplayName_(userMap, message.sender.name)}: ${message.text}`)
    .join('\n');
}

/**
 * Obtains the display name of a user by using the Admin Directory API.
 *
 * The fetched display name is cached in the provided map, so we only call the API
 * once per user.
 *
 * If the user does not have a display name, then the full name is used.
 *
 * @param {Map} userMap a map containing the display names previously fetched
 * @param {string} userName the resource name of the user
 * @return {string} the user's display name
 */
function getUserDisplayName_(userMap, userName) {
  if (userMap.has(userName)) {
    return userMap.get(userName);
  }
  let displayName = 'Unknown User';
  try {
    const user = AdminDirectory.Users.get(
      userName.replace("users/", ""),
      { projection: 'BASIC', viewType: 'domain_public' });
    displayName = user.name.displayName ? user.name.displayName : user.name.fullName;
  } catch (e) {
    // Ignore error if the API call fails (for example, because it's an
    // out-of-domain user or Chat app) and just use 'Unknown User'.
  }
  userMap.set(userName, displayName);
  return displayName;
}
ChatSpaceCreator.gs

รับข้อมูลแบบฟอร์มที่ผู้ใช้ป้อนในหน้าเว็บการเริ่มต้นเหตุการณ์ และใช้ข้อมูลดังกล่าวเพื่อตั้งค่าพื้นที่ Chat โดยการสร้างและป้อนข้อมูล จากนั้นโพสต์ข้อความเกี่ยวกับ เหตุการณ์

ดูรหัส ChatSpaceCreator.gs

apps-script/chat/incident-response-user-auth/ChatSpaceCreator.gs
/**
 * Handles an incident by creating a chat space with the provided title and members, and posting a message.
 * All the actions are done using user credentials.
 *
 * @param {Object} formData - The data submitted by the user. It should contain the fields:
 *                           - title: The display name of the chat space.
 *                           - description: The description of the incident.
 *                           - users: A comma-separated string of user emails to be added to the space.
 * @return {string} The resource name of the new space.
 */
function handleIncident(formData) {
  const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
  const spaceName = setUpSpace_(formData.title, users);
  addAppToSpace_(spaceName);
  createMessage_(spaceName, formData.description);
  return spaceName;
}

/**
 * Creates a chat space.
 *
 * @return {string} the resource name of the new space.
 */
function setUpSpace_(displayName, users) {
  const memberships = users.map(email => ({
    member: {
      name: `users/${email}`,
      type: "HUMAN"
    }
  }));
  const request = {
    space: {
      displayName: displayName,
      spaceType: "SPACE"
    },
    memberships: memberships
  };
  // Call Chat API method spaces.setup
  const space = Chat.Spaces.setup(request);
  return space.name;
}

/**
 * Adds this Chat app to the space.
 *
 * @return {string} the resource name of the new membership.
 */
function addAppToSpace_(spaceName) {
  const request = {
    member: {
      name: "users/app",
      type: "BOT"
    }
  };
  // Call Chat API method spaces.members.create
  const membership = Chat.Spaces.Members.create(request, spaceName);
  return membership.name;
}

/**
 * Creates a chat message.
 *
 * @param {string} spaceName - The resource name of the space.
 * @param {string} message - The text to be posted.
 * @return {string} the resource name of the new message.
 */
function createMessage_(spaceName, message) {
  const request = {
    text: message
  };
  // Call Chat API method spaces.messages.create
  const result = Chat.Spaces.Messages.create(request, spaceName);
  return result.name;
}
DocsApi.gs

เรียกใช้ Google เอกสาร API เพื่อสร้างเอกสาร Google เอกสารใน Google ไดรฟ์ของผู้ใช้ และเขียนสรุปข้อมูลเหตุการณ์ที่สร้างใน VertexAiApi.gs ลงในเอกสาร

ดูรหัส DocsApi.gs

apps-script/chat/incident-response-user-auth/DocsApi.gs
/**
 * Creates a Doc in the user's Google Drive and writes a summary of the incident information to it.
 *
 * @param {string} title The title of the incident
 * @param {string} resolution Incident resolution described by the user
 * @param {string} chatHistory The whole Chat history be included in the document
 * @param {string} chatSummary A summary of the Chat conversation to be included in the document
 * @return {string} the URL of the created Doc
 */
function createDoc_(title, resolution, chatHistory, chatSummary) {
  let doc = DocumentApp.create(title);
  let body = doc.getBody();
  body.appendParagraph(`Post-Mortem: ${title}`).setHeading(DocumentApp.ParagraphHeading.TITLE);
  body.appendParagraph("Resolution").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendParagraph(resolution);
  body.appendParagraph("Summary of the conversation").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendParagraph(chatSummary);
  body.appendParagraph("Full Chat history").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendParagraph(chatHistory);
  return doc.getUrl();
}
VertexAiApi.gs

สรุปการสนทนาในพื้นที่ทำงานของ Chat โดยใช้ Vertex AI ระบบจะโพสต์สรุปนี้ในเอกสารที่สร้างขึ้นเป็นพิเศษใน DocsAPI.gs

ดูรหัส VertexAiApi.gs

apps-script/chat/incident-response-user-auth/VertexAiApi.gs
/**
 * Summarizes a Chat conversation using the Vertex AI text prediction API.
 *
 * @param {string} chatHistory The Chat history that will be summarized.
 * @return {string} The content from the text prediction response.
 */
function summarizeChatHistory_(chatHistory) {
  const API_ENDPOINT = `https://${VERTEX_AI_LOCATION_ID}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${VERTEX_AI_LOCATION_ID}/publishers/google/models/${MODEL_ID}:generateContent`;
  const prompt =
    "Summarize the following conversation between Engineers resolving an incident"
      + " in a few sentences. Use only the information from the conversation.\n\n"
      + chatHistory;
  // Get the access token.
  const accessToken = ScriptApp.getOAuthToken();

  const headers = {
    'Authorization': 'Bearer ' + accessToken,
    'Content-Type': 'application/json',
  };
  const payload = {
    'contents': {
      'role': 'user',
      'parts' : [
        {
          'text': prompt
        }
      ]
    }
  }
  const options = {
    'method': 'post',
    'headers': headers,
    'payload': JSON.stringify(payload),
    'muteHttpExceptions': true,
  };
  try {
    const response = UrlFetchApp.fetch(API_ENDPOINT, options);
    const responseCode = response.getResponseCode();
    const responseText = response.getContentText();

    if (responseCode === 200) {
      const jsonResponse = JSON.parse(responseText);
      console.log(jsonResponse)
      if (jsonResponse.candidates && jsonResponse.candidates.length > 0) {
        return jsonResponse.candidates[0].content.parts[0].text; // Access the summarized text
      } else {
        return "No summary found in response.";
      }

    } else {
      console.error("Vertex AI API Error:", responseCode, responseText);
      return `Error: ${responseCode} - ${responseText}`;
    }
  } catch (e) {
    console.error("UrlFetchApp Error:", e);
    return "Error: " + e.toString();
  }
}
WebController.gs

แสดงเว็บไซต์การเริ่มต้นเหตุการณ์

ดูรหัส WebController.gs

apps-script/chat/incident-response-user-auth/WebController.gs
/**
 * Serves the web page from Index.html.
 */
function doGet() {
  return HtmlService
    .createTemplateFromFile('Index')
    .evaluate();
}

/**
 * Serves the web content from the specified filename.
 */
function include(filename) {
  return HtmlService
    .createHtmlOutputFromFile(filename)
    .getContent();
}

/**
 * Returns the email address of the user running the script.
 */
function getUserEmail() {
  return Session.getActiveUser().getEmail();
}
Index.html

HTML ที่ประกอบเป็นเว็บไซต์การเริ่มต้นเหตุการณ์

ดูรหัส Index.html

apps-script/chat/incident-response-user-auth/Index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
    <?!= include('Stylesheet'); ?>
  </head>
  <body>
    <div class="container">
      <div class="content">
        <h1>Incident Manager</h1>
        <form id="incident-form" onsubmit="handleFormSubmit(this)">
          <div id="form">
            <p>
              <label for="title">Incident title</label><br/>
              <input type="text" name="title" id="title" />
            </p>
            <p>
              <label for="users">Incident responders</label><br/>
              <small>
                Please enter a comma-separated list of email addresses of the users
                that should be added to the space.
                Do not include <?= getUserEmail() ?> as it will be added automatically.
              </small><br/>
              <input type="text" name="users" id="users" />
            </p>
            <p>
              <label for="description">Initial message</label></br>
              <small>This message will be posted after the space is created.</small><br/>
              <textarea name="description" id="description"></textarea>
            </p>
            <p class="text-center">
              <input type="submit" value="CREATE CHAT SPACE" />
            </p>
          </div>
          <div id="output" class="hidden"></div>
          <div id="clear" class="hidden">
            <input type="reset" value="CREATE ANOTHER INCIDENT" onclick="onReset()" />
          </div>
        </form>
      </div>
    </div>
    <?!= include('JavaScript'); ?>
  </body>
</html>
JavaScript.html

จัดการลักษณะการทำงานของแบบฟอร์ม รวมถึงการส่ง ข้อผิดพลาด และการล้างข้อมูล สำหรับเว็บไซต์การเริ่มต้นเหตุการณ์ โดยจะรวมไว้ ใน Index.html โดยฟังก์ชัน include ที่กำหนดเองใน WebController.gs

ดูรหัส JavaScript.html

apps-script/chat/incident-response-user-auth/JavaScript.html
<script>
  var formDiv = document.getElementById('form');
  var outputDiv = document.getElementById('output');
  var clearDiv = document.getElementById('clear');

  function handleFormSubmit(formObject) {
    event.preventDefault();
    outputDiv.innerHTML = 'Please wait while we create the space...';
    hide(formDiv);
    show(outputDiv);
    google.script.run
      .withSuccessHandler(updateOutput)
      .withFailureHandler(onFailure)
      .handleIncident(formObject);
  }

  function updateOutput(response) {
    var spaceId = response.replace('spaces/', '');
    outputDiv.innerHTML =
      '<p>Space created!</p><p><a href="https://mail.google.com/chat/#chat/space/'
        + spaceId
        + '" target="_blank">Open space</a></p>';
    show(outputDiv);
    show(clearDiv);
  }

  function onFailure(error) {
    outputDiv.innerHTML = 'ERROR: ' + error.message;
    outputDiv.classList.add('error');
    show(outputDiv);
    show(clearDiv);
  }

  function onReset() {
    outputDiv.innerHTML = '';
    outputDiv.classList.remove('error');
    show(formDiv);
    hide(outputDiv);
    hide(clearDiv);
  }

  function hide(element) {
    element.classList.add('hidden');
  }

  function show(element) {
    element.classList.remove('hidden');
  }
</script>
Stylesheet.html

CSS สำหรับเว็บไซต์การเริ่มต้นเหตุการณ์ โดยจะรวมไว้ใน Index.html ด้วยฟังก์ชัน include ที่กำหนดเองใน WebController.gs

ดูรหัส Stylesheet.html

apps-script/chat/incident-response-user-auth/Stylesheet.html
<style>
  * {
    box-sizing: border-box;
  }
  body {
    font-family: Roboto, Arial, Helvetica, sans-serif;
  }
  div.container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0;
  }
  div.content {
    width: 80%;
    max-width: 1000px;
    padding: 1rem;
    border: 1px solid #999;
    border-radius: 0.25rem;
    box-shadow: 0 2px 2px 0 rgba(66, 66, 66, 0.08), 0 2px 4px 2px rgba(66, 66, 66, 0.16);
  }
  h1 {
    text-align: center;
    padding-bottom: 1rem;
    margin: 0 -1rem 1rem -1rem;
    border-bottom: 1px solid #999;
  }
 #output {
    text-align: center;
    min-height: 250px;
  }
  div#clear {
    text-align: center;
    padding-top: 1rem;
    margin: 1rem -1rem 0 -1rem;
    border-top: 1px solid #999;
  }
  input[type=text], textarea {
    width: 100%;
    padding: 1rem 0.5rem;
    margin: 0.5rem 0;
    border: 0;
    border-bottom: 1px solid #999;
    background-color: #f0f0f0;
  }
  textarea {
    height: 5rem;
  }
  small {
    color: #999;
  }
  input[type=submit], input[type=reset] {
    padding: 1rem;
    border: none;
    background-color: #6200ee;
    color: #fff;
    border-radius: 0.25rem;
    width: 25%;
  }
  .hidden {
    display: none;
  }
  .text-center {
    text-align: center;
  }
  .error {
    color: red;
  }
</style>

ค้นหาหมายเลขและรหัสโปรเจ็กต์ Cloud

  1. ไปที่โปรเจ็กต์ Cloud ใน Google Cloud Console

    ไปที่คอนโซล Google Cloud

  2. คลิกการตั้งค่าและยูทิลิตี > การตั้งค่าโปรเจ็กต์

  3. จดค่าในช่องหมายเลขโปรเจ็กต์และรหัสโปรเจ็กต์ คุณ ใช้ได้ในส่วนต่อไปนี้

สร้างโปรเจ็กต์ Apps Script

วิธีสร้างโปรเจ็กต์ Apps Script และเชื่อมต่อกับโปรเจ็กต์ Cloud

  1. คลิกปุ่มต่อไปนี้เพื่อเปิดโปรเจ็กต์ Apps Script ของแอปแชทการจัดการเหตุการณ์พร้อมการให้สิทธิ์ผู้ใช้
    เปิดโปรเจ็กต์
  2. คลิก ภาพรวม
  3. ในหน้าภาพรวม ให้คลิก ไอคอนสำหรับการทำสำเนา ทำสำเนา
  4. ตั้งชื่อสำเนาโปรเจ็กต์ Apps Script โดยทำดังนี้

    1. คลิกสำเนาแอปในแชทการจัดการเหตุการณ์ที่มีการให้สิทธิ์ผู้ใช้

    2. ในชื่อโปรเจ็กต์ ให้พิมพ์ Incident Management Chat app with User Auth

    3. คลิกเปลี่ยนชื่อ

  5. ในสำเนาโปรเจ็กต์ Apps Script ให้ไปที่Consts.gs ไฟล์และตั้งค่า PROJECT_ID ด้วยรหัสของโปรเจ็กต์ Cloud

ตั้งค่าโปรเจ็กต์ Cloud ของโปรเจ็กต์ Apps Script

  1. ในโปรเจ็กต์ Apps Script ให้ คลิก ไอคอนสำหรับการตั้งค่าโปรเจ็กต์ การตั้งค่าโปรเจ็กต์
  2. ในส่วนโปรเจ็กต์ Google Cloud Platform (GCP) ให้คลิกเปลี่ยนโปรเจ็กต์
  3. ในหมายเลขโปรเจ็กต์ GCP ให้วางหมายเลขโปรเจ็กต์ของโปรเจ็กต์ Cloud
  4. คลิกตั้งค่าโปรเจ็กต์ ตอนนี้โปรเจ็กต์ Cloud และโปรเจ็กต์ Apps Script เชื่อมต่อกันแล้ว

สร้างการทำให้ใช้งานได้สำหรับการทดสอบ Apps Script

เมื่อมีโค้ดทั้งหมดแล้ว ให้ทำให้โปรเจ็กต์ Apps Script ใช้งานได้ คุณใช้รหัสการติดตั้งใช้งานเมื่อกำหนดค่า แอป Chat ใน Google Cloud

  1. เปิดโปรเจ็กต์ของแอปการตอบสนองต่อเหตุการณ์ใน Apps Script

    ไปที่ Apps Script

  2. คลิกทำให้ใช้งานได้ > ทดสอบการติดตั้งใช้งาน

  3. หากยังไม่ได้เลือกส่วนเสริมของ Google Workspace และเว็บแอป ให้คลิกประเภทการทำให้ใช้งานได้ ไอคอนสำหรับการตั้งค่าโปรเจ็กต์ ข้างเลือกประเภท แล้วเลือกส่วนเสริมของ Google Workspace และเว็บแอป

  4. Apps Script จะให้รหัสการทำให้ใช้งานได้และ URL สำหรับ เว็บแอป

  5. จด URL ของเว็บแอปไว้เพื่อเข้าชมในภายหลังเมื่อคุณเริ่มเหตุการณ์ คัดลอกรหัสการติดตั้งใช้งาน คุณใช้รหัสนี้ขณะกําหนดค่าแอป Chat ในคอนโซล Google Cloud

  6. คลิกเสร็จสิ้น

กำหนดค่าแอป Chat ใน Google Cloud Console

ส่วนนี้จะแสดงวิธีกำหนดค่า Google Chat API ในคอนโซล Google Cloud พร้อมข้อมูลเกี่ยวกับแอป Chat รวมถึง รหัสของการนำไปใช้งานที่คุณเพิ่งสร้างจากโปรเจ็กต์ Apps Script

  1. ในคอนโซล Google Cloud ให้คลิกเมนู > ผลิตภัณฑ์เพิ่มเติม > Google Workspace > คลังผลิตภัณฑ์ > Google Chat API > จัดการ > การกำหนดค่า

    ไปที่การกำหนดค่า Chat API

  2. ในชื่อแอป ให้พิมพ์ Incident Management with User Auth

  3. ใน URL ของรูปโปรไฟล์ ให้พิมพ์ https://developers.google.com/chat/images/quickstart-app-avatar.png

  4. ในคำอธิบาย ให้พิมพ์ Responds to incidents

  5. คลิกปุ่มเปิด/ปิดเปิดใช้ฟีเจอร์แบบอินเทอร์แอกทีฟไปที่ตำแหน่งเปิด

  6. ในส่วนฟังก์ชันการทำงาน ให้เลือกเข้าร่วมพื้นที่ทำงานและการสนทนากลุ่ม

  7. เลือก Apps Script ในส่วนการตั้งค่าการเชื่อมต่อ

  8. ในรหัสการทำให้ใช้งานได้ ให้วางรหัสการทำให้ใช้งานได้ของ Apps Script ที่คัดลอกไว้ก่อนหน้านี้จากการทำให้ใช้งานได้ของโปรเจ็กต์ Apps Script

  9. ลงทะเบียนคำสั่งด่วนที่แอป Chat ที่ใช้งานได้อย่างสมบูรณ์ใช้

    1. ในส่วนคำสั่ง ให้คลิกเพิ่มคำสั่ง

    2. ในรหัสคำสั่ง ให้พิมพ์ 1

    3. ในคำอธิบาย ให้พิมพ์ Closes the incident being discussed in the space.

    4. ในส่วนประเภทคำสั่ง ให้เลือกคำสั่งด่วน

    5. ในชื่อคำสั่งด่วน ให้พิมพ์ Close incident

    6. เลือกกล่องโต้ตอบ

    7. คลิกเสร็จสิ้น ระบบจะลงทะเบียนและแสดงคำสั่ง

  10. ในส่วนระดับการเข้าถึง ให้เลือก ทำให้แอป Chat นี้พร้อมใช้งานสำหรับบุคคลและกลุ่มที่เฉพาะเจาะจงในโดเมน Workspace ของคุณ แล้วป้อนอีเมล

  11. ในส่วนบันทึก ให้เลือกบันทึกข้อผิดพลาดไปยังการบันทึก

  12. คลิกบันทึก ข้อความว่าบันทึกการกำหนดค่าแล้วจะปรากฏขึ้น ซึ่งหมายความว่าแอปพร้อมทดสอบแล้ว

ทดสอบแอป Chat

หากต้องการทดสอบแอป Chat สำหรับการจัดการเหตุการณ์ ให้เริ่มเหตุการณ์จากหน้าเว็บและตรวจสอบว่าแอป Chat ทำงานตามที่คาดไว้หรือไม่ โดยทำดังนี้

  1. ไปที่ URL ของเว็บแอปที่นำไปใช้งานของ Apps Script

  2. เมื่อ Apps Script ขอสิทธิ์เข้าถึงข้อมูล ให้คลิกตรวจสอบสิทธิ์ ลงชื่อเข้าใช้ด้วยบัญชี Google ที่เหมาะสม ในโดเมน Google Workspace แล้วคลิกอนุญาต

  3. หน้าเว็บการเริ่มต้นเหตุการณ์จะเปิดขึ้น ป้อนข้อมูลการทดสอบ

    1. ในชื่อเหตุการณ์ ให้พิมพ์ The First Incident
    2. ไม่บังคับ: ในผู้ตอบสนองต่อเหตุการณ์ ให้ป้อนอีเมลของ ผู้ตอบสนองต่อเหตุการณ์คนอื่นๆ โดยผู้ใช้ดังกล่าวต้องมี บัญชี Google Chat ในองค์กร Google Workspace หรือ การสร้างพื้นที่ทำงานจะล้มเหลว โปรดอย่าป้อนอีเมลของคุณเองเนื่องจากระบบจะรวมอีเมลของคุณโดยอัตโนมัติ
    3. ในข้อความเริ่มต้น ให้พิมพ์ Testing the incident management Chat app.
  4. คลิกสร้างพื้นที่ทำงานใน Chat ข้อความ creating space จะปรากฏขึ้น

  5. หลังจากสร้างพื้นที่ทำงานแล้ว Space created! ข้อความจะปรากฏขึ้น คลิกเปิดพื้นที่ทำงาน ซึ่งจะเปิดพื้นที่ทำงานใน Chat ในแท็บใหม่

  6. คุณและผู้ตอบสนองต่อเหตุการณ์คนอื่นๆ สามารถส่งข้อความในพื้นที่ทำงานได้ (ไม่บังคับ) แอปจะสรุปข้อความเหล่านี้โดยใช้ Vertex AI และแชร์เอกสารสรุป

  7. หากต้องการสิ้นสุดการตอบสนองต่อเหตุการณ์และเริ่มกระบวนการแก้ไข ให้เปิดใช้คำสั่งด่วน Close incident ในแชทพื้นที่ทำงาน กล่องโต้ตอบการจัดการเหตุการณ์จะเปิดขึ้น

  8. ในปิดเหตุการณ์ ให้ป้อนคำอธิบายสำหรับการแก้ไขเหตุการณ์ เช่น Test complete

  9. คลิกปิดเหตุการณ์

แอปการจัดการเหตุการณ์จะแสดงข้อความในพื้นที่ สรุปข้อความด้วย Vertex AI วางสรุปในเอกสาร Google เอกสาร และแชร์เอกสารในพื้นที่

ล้างข้อมูล

เราขอแนะนำให้คุณลบโปรเจ็กต์ Cloud เพื่อหลีกเลี่ยงการเรียกเก็บเงินจากบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในบทแนะนำนี้

  1. ใน Google Cloud Console ให้ไปที่หน้าจัดการทรัพยากร คลิก เมนู > IAM และผู้ดูแลระบบ > จัดการทรัพยากร

    ไปที่เครื่องมือจัดการทรัพยากร

  2. ในรายการโปรเจ็กต์ ให้เลือกโปรเจ็กต์ที่ต้องการลบ แล้วคลิก ลบ
  3. ในกล่องโต้ตอบ ให้พิมพ์รหัสโปรเจ็กต์ แล้วคลิกปิดเพื่อลบ โปรเจ็กต์