傳送私人訊息給 Google Chat 使用者

本頁說明如何按照 Google Chat 應用程式。

私人訊息是指只有 Chat 應用程式的訊息 只有指定 Chat 使用者看得見。你可以在以下位置使用私人訊息: 與多人一起透過聊天室進行私人互動 Chat 擴充應用程式。舉例來說, Chat 擴充應用程式可以在私密模式下傳送訊息,以便執行下列任一操作, 包括:

  • 回應斜線指令。舉例來說 使用者叫用 Chat 應用程式的 /about 斜線 指令時,Chat 應用程式可以使用 私人訊息來說明 Chat 應用程式 以及使用方式
  • 通知或傳送只與一位使用者相關的資訊。適用對象 例如通知使用者他們獲派工作,或提醒使用者 以完成工作
  • 傳送錯誤訊息。例如,如果使用者省略必要的引數文字 輸入斜線指令,Chat 應用程式便可傳送 私人訊息,用於說明錯誤並協助使用者設定指令格式。
  • 在使用者加入的聊天室時,傳送私人歡迎訊息 說明 Chat 應用程式的相關指南或使用方式。

Chat 應用程式傳送私人訊息時 會顯示標籤,告知使用者該訊息僅供檢視:

私人訊息:
  Cymbal Labs 即時通訊應用程式。這則訊息會顯示
  Chat 擴充應用程式是由 Cymbal Labs 所製作,並分享連結
  說明文件,以及與支援團隊聯絡的連結。
圖 1:當 Chat 應用程式傳送 私人訊息時,使用者會看到附有標籤的訊息 上面寫著 Only visible to you

必要條件

Node.js

Python

Apps Script

發送私人訊息

如要以 Chat 應用程式的私人訊息傳送私人訊息,請指定 這個 privateMessageViewer敬上 欄位。建立私人訊息的方式和 您建立任何訊息:回應使用者互動方式,或 以非同步方式呼叫 Google Chat API create() 方法 Message 資源。如需傳送簡訊或卡片訊息的步驟,請參閱 傳送訊息

以下範例顯示私人簡訊的 JSON,會顯示 Hello private world!

{
    "text": "Hello private world!",
    "privateMessageViewer": "USER"
}

在此範例中,USER 代表 Chat 使用者 誰能檢視此訊息 (格式為 User 資源。如果有回應 但可以在互動事件中指定 User 物件。 如需範例,請參閱下列章節 透過私密方式回應斜線指令

否則,如要為私人訊息指定檢視者,您可以使用 Username 欄位 資源:

{
    "text": "Hello private world!",
    "privateMessageViewer": {
      "name": "users/USER_ID"
    }
}

在此範例中,請使用 name 欄位指定檢視器的 User Google Chat 中的資源名稱。取代 USER_ID 或使用者專屬 ID,例如 12345678987654321hao@cymbalgroup.com

如要進一步瞭解如何指定使用者,請參閱 識別並指定 Google Chat 使用者

以私密方式回應斜線指令

以下程式碼為 Chat 應用程式範例 會回應含有私人訊息的斜線指令。

Chat 應用程式會處理 MESSAGE 互動事件 然後透過私人簡訊回覆 /help 斜線指令 以瞭解使用方式:

Node.js

/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {!Object} req Request sent from Google Chat app
* @param {!Object} res Response to send back
*
* @return {!Object} respond to slash command
*/
exports.onMessage = function onMessage(req, res) {
  if (req.method === 'GET' || !req.body.message) {
    return res.send('Hello! This function is meant to be used in Google Chat app.');
  }

  const event = req.body;

  // Checks for the presence of event.message.slashCommand.
  // If the slash command is "/help", responds with a private text message.
  if (event.message.slashCommand) {
    switch (event.message.slashCommand.commandId) {
      case '1':  // /help
        return res.json({
          privateMessageViewer: event.user,
          text: 'This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>.'
        });
    }
  }

  // If the Chat app doesn't detect a slash command, it responds
  // with a private text message
  return res.json({
    privateMessageViewer: event.user,
    text: 'Try a slash command.'
  });
};

Apps Script

/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onMessage(event) {
  if (event.message.slashCommand) {
    switch (event.message.slashCommand.commandId) {
      case 1: // Responds to /help
        return {
          "privateMessageViewer": event.user,
          "text": "This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>."
        };
    }
  }
  else {
    return { "text": "Try a slash command.", "privateMessageViewer": event.user };
  }
}

Python

from typing import Any, Mapping

import flask
import functions_framework

@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
  """Responds to a MESSAGE event in Google Chat.

  Args:
      req (flask.Request): the event object from Chat API.

  Returns:
      Mapping[str, Any]: open a Dialog in response to a card's button click.
  """
  if req.method == 'GET':
    return 'Hello! This function must be called from Google Chat.'

  request = req.get_json(silent=True)

  # Checks for the presence of event.message.slashCommand.
  # If the slash command is "/help", responds with a private text message.
  if request.get('message', {}).get('slashCommand'):
    command_id = request.get('message', {}).get('slashCommand').get('commandId')
    if command_id == '1':  # /help
      return {
          'privateMessageViewer': request.get('user'),
          'text': (
              'This Chat app was created by Cymbal Labs. To get help with this'
              ' app, <https://cymbalgroup.com/docs|see our documentation> or'
              ' <https://cymbalgroup.com/support|contact our support team>.'
          ),
      }

  return {
      'privateMessageViewer': request.get('user'),
      'text': 'Try a slash command.',
  }

限制

如要傳送私人訊息,訊息不得含有或使用 包括:

  • 附件
  • 配件動作
  • 部分私人訊息。例如 Chat 擴充應用程式 無法傳送含有文字和資訊卡的訊息,訊息內容僅限本人查看 1 位使用者,但聊天室中的所有人都看得到這張資訊卡。
  • 使用者驗證。 只有 Chat 應用程式可以傳送私人訊息, Chat 擴充應用程式無法以使用者身分進行驗證,因此無法傳送 傳送私人訊息。

更新或刪除私人訊息

如要更新或刪除 Google Chat 訊息,你必須呼叫 Chat API。你無法變更私人訊息的檢視者,也無法將訊息標示為 訊息的公開。因此,更新私人訊息時,您必須省略 API 呼叫中的 privateMessageViewer 欄位 (該欄位僅供輸出)。

如要更新私人訊息,請參閱 更新訊息。 如要刪除私人訊息,請參閱 刪除訊息