ตอบสนองต่อเหตุการณ์ต่างๆ ด้วย 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 เอกสารเกี่ยวกับการแก้ไขเหตุการณ์ที่แชร์ในอวกาศ
  • เอกสารเกี่ยวกับการแก้ปัญหาเหตุการณ์สรุปเหตุการณ์โดย AI ใน Google เอกสาร
    รูปที่ 7 เอกสารเกี่ยวกับการแก้ไขเหตุการณ์โดยสรุปของ AI ใน Google เอกสาร

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

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

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

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

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

    1. การเรียก HTTP ไปยัง Chat API โดยใช้ Apps Script บริการ Chat ขั้นสูงจะแสดงรายการ 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 ให้ไปที่เมนู > IAM และ ผู้ดูแลระบบ > สร้างโครงการ

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

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

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

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

gcloud CLI

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

  • Cloud Shell: วิธีใช้เทอร์มินัลออนไลน์กับ gcloud CLI ที่ตั้งค่าแล้ว ให้เปิดใช้งาน Cloud Shell
    เปิดใช้งาน Cloud Shell
  • Local Shell: หากต้องการใช้สภาพแวดล้อมการพัฒนาในเครื่อง ติดตั้งและ เริ่มต้น gcloud CLI นี้
    หากต้องการสร้างโปรเจ็กต์ระบบคลาวด์ ให้ใช้คำสั่ง "gcloud projects 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. หากจำเป็น ให้ตั้งค่าโปรเจ็กต์ระบบคลาวด์ปัจจุบันเป็นโปรเจ็กต์ที่คุณสร้าง ด้วยคำสั่ง 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

กำหนดค่าคงที่ที่อ้างอิงโดยไฟล์โค้ดอื่นๆ ซึ่งรวมถึงรหัสโปรเจ็กต์ที่อยู่ในระบบคลาวด์, รหัสตำแหน่ง 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. คลิก "การตั้งค่าและยูทิลิตี" &gt; การตั้งค่าโปรเจ็กต์

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

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

วิธีสร้างโครงการ Apps Script และเชื่อมต่อกับ โปรเจ็กต์ที่อยู่ในระบบคลาวด์:

  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 ในหมายเลขโปรเจ็กต์ GCP
  4. คลิกตั้งค่าโปรเจ็กต์ โปรเจ็กต์ที่อยู่ในระบบคลาวด์และ 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 ให้คลิกเมนู &gt; ผลิตภัณฑ์อื่นๆ &gt; Google Workspace &gt; คลังผลิตภัณฑ์ &gt; Google Chat API &gt; จัดการ &gt; การกำหนดค่า

    ไปที่การกำหนดค่า 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. ใน ชื่อ ให้พิมพ์ /closeIncident

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

    4. ในคำอธิบาย ให้พิมพ์ 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 แล้วคลิก Allow

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

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

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

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

ล้างข้อมูล

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

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

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

  2. ในรายการโปรเจ็กต์ ให้เลือกโปรเจ็กต์ที่ต้องการลบ แล้วคลิก ลบ
  3. ในกล่องโต้ตอบ ให้พิมพ์รหัสโปรเจ็กต์แล้วคลิกปิดเครื่องเพื่อลบ ให้กับโครงการ
  • ตอบคำถามตามการสนทนาใน Chat ด้วยแอป Gemini AI Chat
  • จัดการโปรเจ็กต์ด้วย Google Chat, Vertex AI และ Firestore
  • แอปตอบสนองต่อเหตุการณ์ตรวจสอบสิทธิ์ด้วยข้อมูลเข้าสู่ระบบของผู้ใช้เพื่อเรียกใช้ API และเรียกใช้บริการ Google Cloud เช่น Chat API และ Vertex AI API ซึ่งหมายความว่าผู้ใช้ที่เริ่มต้นเหตุการณ์ต้องมีสิทธิ์เข้าถึงเหตุการณ์เหล่านี้ บริการต่างๆ ลองเรียกใช้ Vertex AI API เพื่อให้แอปมีประสิทธิภาพยิ่งขึ้น ด้วยบัญชีบริการ แทนที่จะเป็นข้อมูลเข้าสู่ระบบของผู้ใช้ที่เรียกใช้ คำสั่งเครื่องหมายทับ /closeIncident สำหรับความช่วยเหลือในการกำหนดค่าบัญชีบริการ สำหรับ Vertex AI API โปรดดู ใช้บัญชีบริการที่กำหนดเอง