ส่งข้อความส่วนตัวถึงผู้ใช้ Google Chat

หน้านี้จะอธิบายวิธีการสร้างและส่งข้อความส่วนตัวในฐานะ แอป Google Chat

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

  • ตอบกลับคําสั่งเครื่องหมายทับ ตัวอย่างเช่น หาก ผู้ใช้เรียกเครื่องหมายทับ /about ของแอป Chat ของคุณ ในพื้นที่ทำงาน แอป 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 จากเหตุการณ์การโต้ตอบได้ ดูตัวอย่างได้ที่ส่วนต่อไปนี้ ตอบกลับโดยใช้คําสั่งเครื่องหมายทับ

หรือหากต้องการระบุผู้ดูสำหรับข้อความส่วนตัว คุณสามารถใช้ ช่อง name ของ User แหล่งข้อมูล:

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

ในตัวอย่างนี้ คุณใช้ช่อง name เพื่อระบุ User ของผู้ชม ชื่อทรัพยากรใน Google Chat แทนที่ USER_ID ที่มีรหัสที่ไม่ซ้ำกันสำหรับผู้ใช้ เช่น 12345678987654321 หรือ hao@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 คุณไม่สามารถเปลี่ยนผู้ดูข้อความส่วนตัวหรือ ข้อความเป็นแบบสาธารณะ ดังนั้น เมื่อคุณอัปเดตข้อความส่วนตัว คุณไม่จำเป็นต้อง ช่อง privateMessageViewer ในการเรียก API (ช่องนี้เป็นเอาต์พุตเท่านั้น)

หากต้องการอัปเดตข้อความส่วนตัว โปรดดูที่ อัปเดตข้อความ หากต้องการลบข้อความส่วนตัว โปรดดู ลบข้อความ