運用 Google Chat、Vertex AI 與 Apps Script 針對事件做出回應

本教學課程說明如何建立可即時回應事件的 Google Chat 應用程式。回應事件時,應用程式會建立 Chat 聊天室並填入資料,利用訊息、斜線指令和對話方塊協助解決事件,並在 Google 文件中運用 AI 總結事件回應結果。

「事件」是指需要馬上引起團隊關注並解決的事件。事件的例子包括:

  • 有時效性的情況下是在客戶關係管理 (CRM) 平台中建立,需要服務團隊才能協作解決方式。
  • 系統離線後,會通知一群網站可靠性工程師 (SRE) 供他們合作,讓軟體重新上線。
  • 地震發生規模較大的地震,因此緊急應變人員需要協調緊急應變措施。

為配合本教學課程,當使用者透過網頁點選按鈕回報事件時,事件快訊就會啟動。網頁會要求使用者輸入基本事件資訊,例如回應者的標題、說明和電子郵件地址,藉此模擬事件。

查看事件管理 Chat 應用程式的實際運作情形:

  • 發生事件的網站。
    圖 1.可供使用者檢舉事件的網站。
  • 說明 Chat 聊天室已建立事件的通知。
    圖 2.說明 Chat 聊天室已建立事件的通知。
  • 事件回應 Chat 聊天室。
    圖 3.事件回應 Chat 聊天室。
  • 透過斜線指令解決事件。
    圖 4.透過斜線指令解決事件。
  • 事件解決對話方塊。
    圖 5.事件解決對話方塊。
  • 解決在聊天室中共用的 Google 文件事件。
    圖 6.解決在聊天室中共用的 Google 文件事件。
  • AI 摘要事件解決 Google 文件
    圖 7.AI 摘要事件解決 Google 文件。

必要條件

如果您需要為貴機構啟用上述任何一項先決條件,請要求 Google Workspace 管理員開啟:

  • 擁有可存取 Google ChatGoogle Workspace 帳戶。
  • 如要為 Google Workspace 啟用目錄 (聯絡人共用功能),請按照下列步驟操作。事件應用程式會使用目錄來查詢事件回應者的聯絡資訊,例如姓名和電子郵件地址。事件回應者必須是在 Google Workspace 機構中擁有 Google Chat 帳戶的使用者。

目標

  • 建構可回應事件的 Chat 應用程式。
  • 執行下列操作,協助使用者回應事件:
    • 建立事件回應空間。
    • 張貼提供事件和回應摘要的訊息。
    • 利用互動式 Chat 應用程式功能支援協同合作。
  • 使用 Vertex AI 產生對話與解決方案的摘要。

架構

下圖顯示事件回應 Google Chat 應用程式使用的 Google Workspace 和 Google Cloud 資源架構。

Google Chat 應用程式事件回應架構

此架構說明 Google Chat 應用程式如何處理事件回應和解決方案。

  1. 使用者從透過 Apps Script 託管的外部網站啟動事件。

  2. 網站會傳送非同步 HTTP 要求到 Google Chat 應用程式,也由 Apps Script 託管。

  3. 事件回應 Google Chat 應用程式會處理這項要求:

    1. Apps Script Admin SDK 服務會取得團隊成員資訊,例如使用者 ID 和電子郵件地址。

    2. 透過 Apps Script Advanced Chat 服務向 Chat API 發出一組 HTTP 要求,事件回應 Google Chat 應用程式便會建立事件 Chat 聊天室,並在其中填入團隊成員,然後將訊息傳送至聊天室。

  4. 團隊成員在 Chat 聊天室中討論事件。

  5. 團隊成員會叫用斜線指令,向事件發出解決方案通知。

    1. 使用 Apps Script Advanced Chat 服務對 Chat API 發出的 HTTP 呼叫,會列出所有 Chat 聊天室的訊息。

    2. Vertex AI 會接收列出的訊息並產生摘要。

    3. Apps Script DocumentApp 服務會建立文件文件,並將 Vertex AI 摘要加入文件。

    4. 事件回應 Google Chat 應用程式會呼叫 Chat API,將摘要文件連結的訊息分享連結傳送給應用程式。

準備環境

本節說明如何為 Chat 應用程式建立及設定 Google Cloud 專案。

建立 Google Cloud 專案

Google Cloud 控制台

  1. 在 Google Cloud 控制台中,依序點選「選單」圖示 >「IAM 與管理」>「建立專案」

    前往「Create a Project」(建立專案)

  2. 在「Project Name」欄位中,輸入專案的描述性名稱。

    選用:如要編輯專案 ID,請按一下「Edit」。專案建立後即無法變更專案 ID,因此請在專案生命週期內選擇符合您需求的 ID。

  3. 在「位置」欄位中,按一下「瀏覽」,查看專案的潛在位置。然後按一下「選取」
  4. 點選「建立」。Google Cloud 控制台會前往「資訊主頁」頁面,並在幾分鐘內建立專案。

gcloud CLI

在下列其中一種開發環境中,存取 Google Cloud CLI (「gcloud):

  • Cloud Shell:如要在已設定 gcloud CLI 中使用線上終端機,請啟用 Cloud Shell。
    啟用 Cloud Shell
  • 本機殼層:如要使用本機開發環境,請安裝initialize gcloud CLI。
    如要建立 Cloud 專案,請使用「gcloud projects create」指令:
    gcloud projects create PROJECT_ID
    取代 PROJECT_ID,為要建立的專案設定 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 專案的專案 ID
    • BILLING_ACCOUNT_ID 是要與 Google Cloud 專案連結的帳單帳戶 ID

啟用 API

Google Cloud 控制台

  1. 在 Google Cloud 控制台中,啟用 Google Chat API、Google 文件 API、Admin SDK API 和 Vertex AI API。

    啟用 API

  2. 確認您在正確的 Cloud 專案中啟用 API,然後點選「Next」

  3. 確認您啟用的是正確的 API,然後按一下「啟用」

gcloud CLI

  1. 如有需要,請將目前的 Cloud 專案設為您使用 gcloud config set project 指令建立的專案:

    gcloud config set project PROJECT_ID
    

    PROJECT_ID 替換成您建立的 Cloud 專案的專案 ID

  2. 使用 gcloud services enable 指令啟用 Google Chat API、Google Document API、Admin SDK API 和 Vertex AI API:

    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. 在「User support email」(使用者支援電子郵件) 中,選取您的電子郵件地址或適當的 Google 群組。

  5. 在「開發人員聯絡資訊」下方,輸入您的電子郵件地址。

  6. 點選「Save and Continue」

  7. 按一下「新增或移除範圍」。畫面上會出現一個面板,列出您在 Cloud 專案中啟用的各個 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. 按一下「Add to Table」

  10. 按一下「更新」

  11. 點選「Save and Continue」

  12. 檢查應用程式註冊摘要,然後按一下「Back to Dashboard」(返回資訊主頁)

建立及部署 Chat 應用程式

在下一節中,您將複製並更新整個 Apps Script 專案,其中包含 Chat 應用程式所需的所有應用程式程式碼,因此無需複製及貼上每個檔案。

部分函式會在名稱結尾加上底線,例如 ChatApp.gsprocessSlashCommand_()。在瀏覽器中開啟事件初始化頁面時,底線會隱藏函式。詳情請參閱私人函式一節。

Apps Script 支援兩種檔案類型:.gs 指令碼和 .html 檔案。為配合這項支援功能,應用程式的用戶端 JavaScript 會納入 <script /> 標記內,且其 CSS 會納入 HTML 檔案的 <style /> 標記內。

或者,您可以在 GitHub 上查看整個專案。

前往 GitHub 查看

以下是每個檔案的總覽:

Consts.gs

定義其他程式碼檔案參照的常數,包括您的 Cloud 專案 ID、Vertex AI 位置 ID,以及用來關閉事件的斜線指令 ID。

查看 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 斜線指令。在 Chat API 中呼叫 spaces.messages.list 方法,讀取聊天室中的訊息。使用 Apps Script 中的 Admin SDK Directory 服務取得使用者 ID。

查看 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 Document 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

產生 使用 Vertex AI 中 Chat 聊天室的對話摘要此摘要會張貼在 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

處理事件初始化網站的表單行為,包括提交、錯誤和清除內容。且由 WebController.gs 中的自訂 include 函式包含在 Index.html 中。

查看 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。會由 WebController.gs 中的自訂 include 函式包含在 Index.html 中。

查看 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>

找出 Cloud 專案編號和 ID

  1. 在 Google Cloud 控制台中,前往您的 Cloud 專案。

    前往 Google Cloud 控制台

  2. 依序按一下「設定」與「公用程式」圖示 >「專案設定」

  3. 請記下「專案編號」和「專案 ID」欄位中的值。您將在後續章節中使用。

建立 Apps Script 專案

如何建立 Apps Script 專案,並連結至 Cloud 專案:

  1. 點選下方按鈕,開啟「透過 Google Chat 回應事件」Apps Script 專案。
    開啟專案
  2. 按一下 「Overview」(總覽)
  3. 在總覽頁面上,按一下「用於建立副本的圖示 建立副本」
  4. 為 Apps Script 專案的副本命名:

    1. 按一下「使用 Google Chat 回覆事件的副本」

    2. 在「專案標題」中輸入 Incident Management Chat app

    3. 按一下 [Rename] (重新命名)

  5. 在 Apps Script 專案副本中,前往 Consts.gs 檔案,然後將 YOUR_PROJECT_ID 替換成您的 Cloud 專案 ID。

設定 Apps Script 專案的 Cloud 專案

  1. 在 Apps Script 專案中,按一下 專案設定圖示「Project Settings」
  2. 按一下「Google Cloud Platform (GCP) 專案」下方的「變更專案」
  3. GCP 專案編號中,貼上 Cloud 專案的專案編號。
  4. 按一下「設定專案」。Cloud 專案與 Apps Script 專案現已連結。

建立 Apps Script 部署作業

現在所有程式碼都已就緒,即可部署 Apps Script 專案。您在 Google Cloud 中設定 Chat 應用程式時會使用部署 ID。

  1. 在 Apps Script 中,開啟事件回應應用程式的專案。

    前往 Apps Script

  2. 依序點選「部署」 >「新增部署作業」

  3. 如果尚未選取「外掛程式」和「網頁應用程式」,請按一下「Select type」旁的部署類型 專案設定圖示,然後選取「Add-on」和「Web app」

  4. 在「Description」中輸入這個版本的說明,例如 Complete version of incident management app

  5. 在「執行身分」中,選取「存取網頁應用程式的使用者」

  6. 在「擁有存取權」中,選取「您 Workspace 機構中的任何人」,其中「您的 Workspace 機構」是您 Google Workspace 機構的名稱。

  7. 按一下「部署」。Apps Script 會回報成功部署,並提供部署 ID 和事件初始化網頁的網址。

  8. 記下「網頁應用程式」網址,以便在事件發生時日後造訪。複製「Deployment ID」。您會在 Google Cloud 控制台中設定 Chat 應用程式時使用這個 ID。

  9. 按一下 [完成]。

在 Google Cloud 控制台中設定 Chat 應用程式

本節說明如何在 Google Cloud 控制台中設定 Chat 應用程式的相關資訊,包括您剛從 Apps Script 專案建立的部署作業 ID。

  1. 在 Google Cloud 控制台中,依序點選「選單」圖示 >「更多產品」 >「Google Workspace」 >「產品資料庫」 >Google Chat API>「管理」 >「設定」

    前往 Chat API 設定

  2. 在「應用程式名稱」中輸入 Incident Management

  3. 在「Avatar URL」(顯示圖片網址) 中輸入 https://developers.google.com/chat/images/quickstart-app-avatar.png

  4. 在「說明」中輸入 Responds to incidents.

  5. 按一下「啟用互動功能」切換鈕,切換至開啟位置。

  6. 在「功能」下方,依序選取「接收 1:1 訊息」和「加入聊天室和群組對話」

  7. 在「連線設定」下方,選取「Apps Script 專案」

  8. 在「Deployment ID」中,貼上您先前從 Apps Script 專案部署作業複製的 Apps Script 部署 ID。

  9. 註冊完整實作 Chat 應用程式使用的斜線指令

    1. 在「斜線指令」下方,點選「新增斜線指令」

    2. 在「名稱」中輸入 /closeIncident

    3. 在「指令 ID」中輸入 1

    4. 在「說明」中輸入 Closes the incident being discussed in the space.

    5. 選取「開啟對話方塊」

    6. 按一下 [完成]。系統會註冊並列出斜線指令。

  10. 在「Visibility」(顯示設定) 下方,選取「Make this Chat app is available for yourWorkspace Domain」(將這個即時通訊應用程式提供給您 Workspace 網域中的特定使用者和群組),然後輸入您的電子郵件地址。

  11. 在「記錄檔」下方,選取「將錯誤記錄到 Logging」

  12. 按一下「儲存」。畫面上會顯示設定儲存的訊息,代表應用程式已準備就緒,可以進行測試。

測試 Chat 應用程式

如要測試事件管理 Chat 應用程式,請從網頁啟動事件,並確認 Chat 應用程式是否正常運作:

  1. 前往 Apps Script 部署網頁應用程式網址。

  2. 當 Apps Script 要求存取您的資料時,請按一下「Review permissions」,使用 Google Workspace 網域中的適當 Google 帳戶登入,然後按一下「Allow」

  3. 事件初始化網頁隨即會開啟。輸入測試資訊:

    1. 在「Incident title」(事件名稱) 中輸入 The First Incident
    2. 或者,在「事件回應者」中,輸入其他事件回應者的電子郵件地址。這些使用者必須是 Google Workspace 機構或聊天室中的 Google Chat 帳戶使用者,因此無法建立聊天室。請勿輸入您自己的電子郵件地址,因為系統會自動納入該地址。
    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. 按一下「關閉事件」

Incident Management 應用程式會列出聊天室中的訊息,使用 Vertex AI 產生訊息摘要、將摘要貼到 Google 文件文件中,然後在聊天室中共用文件。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的費用,建議您刪除 Cloud 專案。

  1. 在 Google Cloud 控制台中,前往「Manage resources」(管理資源) 頁面。依序點選「Menu」圖示 >「IAM & Admin」>「管理資源」

    前往 Resource Manager

  2. 在專案清單中選取要刪除的專案,然後按一下「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下「Shut down」(關閉) 來刪除專案。