ตอบสนองต่อเหตุการณ์ต่างๆ ด้วย 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 ที่ตอบสนองต่อเหตุการณ์ต่างๆ
  • ช่วยให้ผู้ใช้ตอบสนองต่อเหตุการณ์ต่างๆ ได้ด้วยวิธีต่อไปนี้
    • กำลังสร้างพื้นที่ตอบสนองต่อเหตุการณ์
    • การโพสต์ข้อความที่สรุปเหตุการณ์และการตอบสนอง
    • สนับสนุนการทำงานร่วมกันด้วย ฟีเจอร์แบบอินเทอร์แอกทีฟของแอป 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. บริการ Apps Script Admin SDK จะได้รับข้อมูล ของสมาชิกในทีม เช่น รหัสผู้ใช้และอีเมล

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

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

  5. สมาชิกในทีมจะใช้คำสั่งเครื่องหมายทับเพื่อส่งสัญญาณแจ้งการแก้ปัญหาให้กับเหตุการณ์ดังกล่าว

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

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

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

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

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

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

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

คอนโซล Google Cloud

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

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

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

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

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

gcloud CLI

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

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

เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ที่อยู่ในระบบคลาวด์

คอนโซล Google Cloud

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

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

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

gcloud CLI

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

    แทนที่รายการต่อไปนี้

    • PROJECT_ID คือรหัสโปรเจ็กต์ของโปรเจ็กต์ที่อยู่ในระบบคลาวด์ที่คุณต้องการเปิดใช้การเรียกเก็บเงิน
    • 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 ในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ที่ถูกต้อง แล้วคลิกถัดไป

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

gcloud CLI

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

    gcloud config set project PROJECT_ID
    

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

  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 ให้ไปที่ เมนู > API และบริการ > หน้าจอขอความยินยอม OAuth

    ไปที่หน้าจอขอความยินยอม OAuth

  2. ในส่วนประเภทผู้ใช้ ให้เลือกภายใน แล้วคลิกสร้าง

  3. ในชื่อแอป ให้พิมพ์ Incident Management

  4. ในอีเมลการสนับสนุนผู้ใช้ ให้เลือกอีเมลของคุณหรือกลุ่ม Google ที่เหมาะสม

  5. ป้อนอีเมลของคุณในส่วนข้อมูลติดต่อของนักพัฒนาแอป

  6. คลิกบันทึกและต่อไป

  7. คลิกเพิ่มหรือนำขอบเขตออก แผงจะปรากฏขึ้นพร้อมรายการขอบเขตของ API แต่ละรายการที่คุณเปิดใช้ในโปรเจ็กต์ที่อยู่ในระบบคลาวด์

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

    • 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
  9. คลิกเพิ่มลงในตาราง

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

  11. คลิกบันทึกและต่อไป

  12. ตรวจสอบสรุปการลงทะเบียนแอป จากนั้นคลิกกลับไปที่แดชบอร์ด

สร้างและทำให้แอป Chat ใช้งานได้

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

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

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

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

ดูใน GitHub

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

Consts.gs

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

ดูรหัส Consts.gs

apps-script/incident-response/Consts.gs
const PROJECT_ID = 'replace-with-your-project-id';
const VERTEX_AI_LOCATION_ID = 'us-central1';
const CLOSE_INCIDENT_COMMAND_ID = 1;
ChatApp.gs

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

ดูรหัส ChatApp.gs

apps-script/incident-response/ChatApp.gs
/**
 * Responds to a MESSAGE event in Google Chat.
 *
 * This app only responds to a slash command with the ID 1 ("/closeIncident").
 * It will respond to any other message with a simple "Hello" text message.
 *
 * @param {Object} event the event object from Google Chat
 */
function onMessage(event) {
  if (event.message.slashCommand) {
    return processSlashCommand_(event);
  }
  return { "text": "Hello from Incident Response app!" };
}

/**
 * Responds to a CARD_CLICKED event in Google Chat.
 *
 * This app only responds to one kind of dialog (Close Incident).
 *
 * @param {Object} event the event object from Google Chat
 */
function onCardClick(event) {
  if (event.isDialogEvent) {
    if (event.dialogEventType == 'SUBMIT_DIALOG') {
      return processSubmitDialog_(event);
    }
    return {
      actionResponse: {
        type: "DIALOG",
        dialogAction: {
          actionStatus: "OK"
        }
      }
    };
  }
}

/**
 * Responds to a MESSAGE event with a Slash command in Google Chat.
 *
 * This app only responds to a slash command with the ID 1 ("/closeIncident")
 * by returning a Dialog.
 *
 * @param {Object} event the event object from Google Chat
 */
function processSlashCommand_(event) {
  if (event.message.slashCommand.commandId != CLOSE_INCIDENT_COMMAND_ID) {
    return {
      "text": "Command not recognized. Use the command `/closeIncident` to close the incident managed by this space."
    };
  }
  const 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"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  ];
  return {
    actionResponse: {
      type: "DIALOG",
      dialogAction: {
        dialog: {
          body: {
            sections,
          }
        }
      }
    }
  };
}

/**
 * Responds to a CARD_CLICKED event with a Dialog submission in Google Chat.
 *
 * This app only responds to one kind of dialog (Close Incident).
 * 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.common.formInputs.description[""].stringInputs.value[0];
  const chatHistory = concatenateAllSpaceMessages_(event.space.name);
  const chatSummary = summarizeChatHistory_(chatHistory);
  const docUrl = createDoc_(event.space.displayName, resolution, chatHistory, chatSummary);
  return {
    actionResponse: {
      type: "NEW_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.
 *
 * Messages with slash commands are filtered out, so the returned history will
 * contain only the conversations between users and not app command invocations.
 *
 * @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
    .filter(message => message.slashCommand === undefined)
    .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/incident-response/ChatSpaceCreator.gs
/**
 * Creates a space in Google Chat with the provided title and members, and posts an
 * initial message to it.
 *
 * @param {Object} formData the data submitted by the user. It should contain the fields
 *                          title, description, and users.
 * @return {string} the resource name of the new space.
 */
function createChatSpace(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 space in Google Chat with the provided display name and members.
 *
 * @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",
      externalUserAllowed: true
    },
    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;
}

/**
 * Posts a text message to the space on behalf of the user.
 *
 * @return {string} the resource name of the new message.
 */
function createMessage_(spaceName, text) {
  const request = {
    text: text
  };
  // Call Chat API method spaces.messages.create
  const message = Chat.Spaces.Messages.create(request, spaceName);
  return message.name;
}
DocsApi.gs

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

ดูรหัส DocsApi.gs

apps-script/incident-response/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/incident-response/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 prompt =
    "Summarize the following conversation between Engineers resolving an incident"
      + " in a few sentences. Use only the information from the conversation.\n\n"
      + chatHistory;
  const request = {
    instances: [
      { prompt: prompt }
    ],
    parameters: {
      temperature: 0.2,
      maxOutputTokens: 256,
      topK: 40,
      topP: 0.95
    }
  }
  const fetchOptions = {
    method: 'POST',
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
    contentType: 'application/json',
    payload: JSON.stringify(request)
  }
  const response = UrlFetchApp.fetch(
    `https://${VERTEX_AI_LOCATION_ID}-aiplatform.googleapis.com/v1`
      + `/projects/${PROJECT_ID}/locations/${VERTEX_AI_LOCATION_ID}`
      + "/publishers/google/models/text-bison:predict",
    fetchOptions);
  const payload = JSON.parse(response.getContentText());
  return payload.predictions[0].content;
}
WebController.gs

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

ดูรหัส WebController.gs

apps-script/incident-response/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/incident-response/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/incident-response/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)
      .createChatSpace(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/incident-response/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>

ค้นหาหมายเลขและรหัสโปรเจ็กต์ที่อยู่ในระบบคลาวด์

  1. ในคอนโซล Google Cloud ให้ไปที่โปรเจ็กต์ที่อยู่ในระบบคลาวด์

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

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

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

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

หากต้องการสร้างโปรเจ็กต์ Apps Script และเชื่อมต่อกับโปรเจ็กต์ Cloud ให้ทำดังนี้

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

    1. คลิกสำเนาการตอบกลับเหตุการณ์ด้วย Google Chat

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

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

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

ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของโปรเจ็กต์ Apps Script

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

สร้างการติดตั้งใช้งาน Apps Script

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

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

    ไปที่ Apps Script

  2. คลิกทำให้ใช้งานได้ > การทำให้ใช้งานได้ใหม่

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

  4. ในคำอธิบาย ให้ป้อนคำอธิบายสำหรับเวอร์ชันนี้ เช่น Complete version of incident management app

  5. ในส่วนดำเนินการในฐานะ ให้เลือกผู้ใช้ที่เข้าถึงเว็บแอป

  6. ในส่วนผู้ที่มีสิทธิ์เข้าถึง ให้เลือกทุกคนในองค์กร Workspace ของคุณโดยที่ "องค์กร Workspace ของคุณ" คือชื่อองค์กร Google Workspace ของคุณ

  7. คลิกทำให้ใช้งานได้ รายงาน Apps Script รายงานว่าติดตั้งใช้งานได้สำเร็จแล้ว พร้อมทั้งระบุรหัสการทำให้ใช้งานได้และ URL ของหน้าเว็บการเริ่มต้นเหตุการณ์

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

  9. คลิกเสร็จ

กำหนดค่าแอป Chat ในคอนโซล Google Cloud

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

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

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

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

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

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

  5. คลิกปุ่มสลับเปิดใช้ฟีเจอร์แบบอินเทอร์แอกทีฟเป็นเปิด

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

  7. ในส่วนการตั้งค่าการเชื่อมต่อ ให้เลือกโปรเจ็กต์ Apps Script

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

  9. ลงทะเบียนคำสั่งเครื่องหมายทับที่แอป Chat ที่ใช้งานอย่างเต็มรูปแบบใช้

    1. ในส่วนคำสั่งเครื่องหมายทับ ให้คลิกเพิ่มคำสั่งเครื่องหมายทับ

    2. ใน Name ให้พิมพ์ /closeIncident

    3. ใน Command ID ให้พิมพ์ 1

    4. ใน Description ให้พิมพ์ Closes the incident being discussed in the space.

    5. เลือกเปิดกล่องโต้ตอบ

    6. คลิกเสร็จ ระบบจะลงทะเบียนคำสั่งเครื่องหมายทับและแสดงไว้ในรายการ

  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. หากต้องการยุติการตอบสนองต่อเหตุการณ์และเริ่มกระบวนการแก้ไข ให้พิมพ์ /closeIncident ในพื้นที่ใน Chat กล่องโต้ตอบการจัดการเหตุการณ์ จะเปิดขึ้น

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

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

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

ล้างข้อมูล

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

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

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

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