การส่งข้อความ

คำแนะนำนี้อธิบายวิธีการต่างๆ ที่แอป Google Chat สามารถส่งได้ ข้อความ:

  • ส่งข้อความ SMS และการ์ดแบบเรียลไทม์โดยตอบกลับผู้ใช้ การโต้ตอบ
  • ส่ง SMS และข้อความการ์ดแบบไม่พร้อมกันโดยเรียกใช้เมธอด create ใน ทรัพยากร Message
  • เริ่มหรือตอบกลับชุดข้อความ
  • ส่งและตั้งชื่อข้อความ

แหล่งข้อมูล Message รายการ แสดงถึง ข้อความ หรือ การ์ด ใน Google Chat คุณสามารถ create, get, update หรือ delete ข้อความใน Google Chat API โดยการเรียกใช้ ที่สัมพันธ์กัน หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับข้อความ SMS และการ์ด โปรดดูที่ ภาพรวมข้อความ Google Chat

ขนาดสูงสุดของข้อความ (รวมถึงข้อความหรือการ์ดทั้งหมด) คือ 32,000 ไบต์ หากมีข้อความเกินขนาดนี้ หมายความว่าแอป Chat ของคุณ สามารถส่งหลายข้อความแทนได้

แทนที่จะเรียกใช้เมธอด create ในทรัพยากร Message ของ Google Chat API เพื่อส่ง SMS หรือข้อความการ์ดแบบไม่พร้อมกัน แอป Google Chat ยังสร้างข้อความเพื่อตอบกลับการโต้ตอบของผู้ใช้ได้ใน แบบเรียลไทม์ การตอบกลับการโต้ตอบของผู้ใช้ไม่ต้องมีการตรวจสอบสิทธิ์และ รองรับข้อความประเภทอื่นๆ รวมถึงกล่องโต้ตอบและลิงก์แบบอินเทอร์แอกทีฟ ตัวอย่าง โปรดดูรายละเอียดที่หัวข้อ รับและตอบกลับการโต้ตอบกับแอป Google Chat

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

Node.js

Python

  • Google Workspace บัญชีที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • PIP เครื่องมือจัดการแพ็กเกจ
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง

    pip3 install --upgrade google-api-python-client google-auth
    
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนในหัวข้อ สร้างแอป Google Chat
  • กำหนดค่าการให้สิทธิ์สำหรับแอป Chat ในการส่ง ข้อความแบบอะซิงโครนัส ไม่ต้องมีการกำหนดค่าการให้สิทธิ์เพื่อส่ง ข้อความแบบเรียลไทม์

Apps Script

ส่งข้อความ

ส่วนนี้จะอธิบายวิธีการส่งข้อความด้วย 2 วิธีต่อไปนี้

  • ส่งข้อความแบบเรียลไทม์โดยตอบกลับการโต้ตอบของผู้ใช้
  • ส่งข้อความด้วยการเรียกใช้ Google Chat API แบบไม่พร้อมกัน

ส่งข้อความแบบเรียลไทม์

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

วิธีส่งข้อความเมื่อผู้ใช้เพิ่มแอป Chat พื้นที่ทำงาน ในแอป Chat ตอบสนองต่อ ADDED_TO_SPACE เหตุการณ์การโต้ตอบ เพื่อตอบกลับ ADDED_TO_SPACE เหตุการณ์การโต้ตอบที่มี SMS ใช้รหัสต่อไปนี้

Node.js

/**
 * Sends an onboarding message when the Chat app is added to a space.
 *
 * @param {Object} event The event object from Chat API.
 * @return {Object} Response from the Chat app. An onboarding message that
 * introduces the app and helps people get started with it.
 */
exports.onMessage = function onMessage(req, res) {
  if (req.method === 'GET' || !req.body.message) {
    res.send(
      'Hello! This function is meant to be used in a Google Chat space.');
  }

  // Send an onboarding message when added to a Chat space
  if (req.body.type === 'ADDED_TO_SPACE') {
    res.json({
      'text': 'Hi, Cymbal at your service. I help you manage your calendar
      from Google Chat. Take a look at your schedule today by typing
      `/checkCalendar`, or schedule a meeting with `/scheduleMeeting`. To
      learn what else I can do, type `/help`.'
    });
  }
};

Apps Script

/**
 * Sends an onboarding message when the Chat app is added to a space.
 *
 * @param {Object} event The event object from Chat API.
 * @return {Object} Response from the Chat app. An onboarding message that
 * introduces the app and helps people get started with it.
 */
function onAddToSpace(event) {

  return {
    'text': 'Hi, Cymbal at your service. I help you manage your calendar
    from Google Chat. Take a look at your schedule today by typing
    `/checkCalendar`, or schedule a meeting with `/scheduleMeeting`. To learn
    what else I can do, type `/help`.'
  }
}

ตัวอย่างโค้ดแสดงข้อความต่อไปนี้

ตัวอย่างข้อความสำหรับการเริ่มต้นใช้งาน

ส่งข้อความแบบไม่พร้อมกัน

ส่วนต่อไปนี้จะอธิบายวิธีส่งข้อความแบบไม่พร้อมกันด้วย การตรวจสอบสิทธิ์แอปและการตรวจสอบสิทธิ์ผู้ใช้

หากต้องการส่ง SMS ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์ chat.bot ด้วย การตรวจสอบสิทธิ์ผู้ใช้ ให้ระบุขอบเขตการให้สิทธิ์ chat.messages.create
  • เรียกใช้ create วิธี ใน แหล่งข้อมูล Message

ส่งข้อความที่มีการตรวจสอบสิทธิ์แอป

ต่อไปนี้คือวิธีการส่งข้อความด้วย การตรวจสอบสิทธิ์แอป:

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_create_text_message_app.py
  2. รวมรหัสต่อไปนี้ใน chat_create_text_message_app.py:

    from apiclient.discovery import build
    from google.oauth2 import service_account
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = service_account.Credentials.from_service_account_file(
        'credentials.json', scopes=SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Create a Chat message.
    result = chat.spaces().messages().create(
    
        # The space to create the message in.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        parent='spaces/SPACE',
    
        # The message to create.
        body={'text': 'Hello, world!'}
    
    ).execute()
    
    print(result)
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจาก spaces.list() วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_create_text_message_app.py
    

Chat API จะแสดงผลอินสแตนซ์ Message ซึ่งระบุรายละเอียดของข้อความที่ส่ง

ส่งข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้

ต่อไปนี้คือวิธีการส่งข้อความด้วย การตรวจสอบสิทธิ์ผู้ใช้:

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_create_text_message_user.py
  2. รวมรหัสต่อไปนี้ใน chat_create_text_message_user.py:

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then creates a text message in a Chat space.
        '''
    
        # Start with no credentials.
        creds = None
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                        'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().create(
    
            # The space to create the message in.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The message to create.
            body={'text': 'Hello, world!'}
    
        ).execute()
    
        # Prints details about the created message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจาก spaces.list() เมธอดใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_create_text_message_user.py
    

Chat API จะแสดงผลอินสแตนซ์ Message ซึ่งระบุรายละเอียดของข้อความที่ส่ง

ส่งข้อความการ์ด

ส่วนนี้จะอธิบายวิธีส่งข้อความการ์ดได้ 2 วิธีดังนี้

  • ส่งข้อความการ์ดแบบเรียลไทม์โดยตอบกลับการโต้ตอบของผู้ใช้
  • ส่งข้อความการ์ดโดยเรียกใช้ Google Chat API แบบไม่พร้อมกัน

ส่งข้อความการ์ดแบบเรียลไทม์

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

ในตัวอย่างนี้ ผู้ใช้ส่งข้อความไปยังแอป Chat และแอป Chat ตอบสนองด้วยการส่งข้อความการ์ด ที่แสดงชื่อและรูปโปรไฟล์ของผู้ใช้

แอปใน Chat ตอบกลับด้วยการ์ดที่มีชื่อที่แสดงและรูปโปรไฟล์ของผู้ส่ง
รูปภาพ

Node.js

node/avatar-app/index.js
/**
 * Google Cloud Function that responds to messages sent from a
 * Google Chat room.
 *
 * @param {Object} req Request sent from Google Chat room
 * @param {Object} res Response to send back
 */
exports.helloChat = function helloChat(req, res) {
  if (req.method === 'GET' || !req.body.message) {
    res.send('Hello! This function is meant to be used in a Google Chat '  
      'Room.');
  }

  const sender = req.body.message.sender.displayName;
  const image = req.body.message.sender.avatarUrl;

  const data = createMessage(sender, image);

  res.send(data);
};

/**
 * Creates a card with two widgets.
 * @param {string} displayName the sender's display name
 * @param {string} imageUrl the URL for the sender's avatar
 * @return {Object} a card with the user's avatar.
 */
function createMessage(displayName, imageUrl) {
  const cardHeader = {
    title: `Hello ${displayName}!`,
  };

  const avatarWidget = {
    textParagraph: {text: 'Your avatar picture: '},
  };

  const avatarImageWidget = {
    image: {imageUrl},
  };

  const avatarSection = {
    widgets: [
      avatarWidget,
      avatarImageWidget,
    ],
  };

  return {
    text: 'Here\'s your avatar',
    cardsV2: [{
      cardId: 'avatarCard',
      card: {
        name: 'Avatar Card',
        header: cardHeader,
        sections: [avatarSection],
      }
    }],
  };
}

Python

python/avatar-app/main.py
from typing import Any, Mapping

import flask
import functions_framework


# Google Cloud Function that responds to messages sent in
# Google Chat.
#
# @param {Object} req Request sent from Google Chat.
# @param {Object} res Response to send back.
@functions_framework.http
def hello_chat(req: flask.Request) -> Mapping[str, Any]:
  if req.method == "GET":
    return "Hello! This function must be called from Google Chat."

  request_json = req.get_json(silent=True)

  display_name = request_json["message"]["sender"]["displayName"]
  avatar = request_json["message"]["sender"]["avatarUrl"]

  response = create_message(name=display_name, image_url=avatar)

  return response


# Creates a card with two widgets.
# @param {string} name the sender's display name.
# @param {string} image_url the URL for the sender's avatar.
# @return {Object} a card with the user's avatar.
def create_message(name: str, image_url: str) -> Mapping[str, Any]:
  avatar_image_widget = {"image": {"imageUrl": image_url}}
  avatar_text_widget = {"textParagraph": {"text": "Your avatar picture:"}}
  avatar_section = {"widgets": [avatar_text_widget, avatar_image_widget]}

  header = {"title": f"Hello {name}!"}

  cards = {
      "text": "Here's your avatar",
      "cardsV2": [
          {
              "cardId": "avatarCard",
              "card": {
                  "name": "Avatar Card",
                  "header": header,
                  "sections": [avatar_section],
              },
          }
      ]
  }

  return cards

Apps Script

ตัวอย่างนี้ส่งข้อความการ์ดโดยการส่งคืน JSON ของการ์ด คุณยังสามารถใช้ บริการการ์ด Apps Script

apps-script/avatar-app/hello-chat.gs
/**
 * Responds to a MESSAGE event in Google Chat.
 *
 * @param {Object} event the event object from Google Chat
 */
function onMessage(event) {
  const displayName = event.message.sender.displayName;
  const avatarUrl = event.message.sender.avatarUrl;

  return createMessage(displayName, avatarUrl);
}

/**
 * Creates a card with two widgets.
 * @param {string} displayName the sender's display name
 * @param {string} avatarUrl the URL for the sender's avatar
 * @return {Object} a card with the sender's avatar.
 */
function createMessage(displayName, avatarUrl) {
  const cardHeader = {
    title: `Hello ${displayName}!`
  };

  const avatarWidget = {
    textParagraph: {text: 'Your avatar picture: '}
  };

  const avatarImageWidget = {
    image: {imageUrl: avatarUrl}
  };

  const avatarSection = {
    widgets: [
      avatarWidget,
      avatarImageWidget
    ],
  };

  return {
    text: 'Here\'s your avatar',
    cardsV2: [{
      cardId: 'avatarCard',
      card: {
        name: 'Avatar Card',
        header: cardHeader,
        sections: [avatarSection],
      }
    }],
  };
}

ส่งข้อความการ์ดแบบไม่พร้อมกัน

หากต้องการส่ง ข้อความในการ์ด ส่งต่อสิ่งต่อไปนี้ในคำขอของคุณ

  • เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์ chat.bot เธอทำไม่ได้หรอก ส่งข้อความการ์ดที่มีการตรวจสอบสิทธิ์ผู้ใช้
  • เรียกใช้ create วิธี ใน แหล่งข้อมูล Message

ตัวอย่างข้อความในการ์ดมีดังนี้

ข้อความในการ์ดที่ส่งด้วย Chat API

วิธีส่งข้อความการ์ดที่มีการตรวจสอบสิทธิ์แอปมีดังนี้

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_create_card_message.py
  2. รวมรหัสต่อไปนี้ใน chat_create_card_message.py:

    from apiclient.discovery import build
    from google.oauth2 import service_account
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = service_account.Credentials.from_service_account_file(
        'credentials.json', scopes=SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Create a Chat message.
    result = chat.spaces().messages().create(
    
        # The space to create the message in.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        parent='spaces/SPACE',
    
        # The message to create.
        body=
        {
          'cardsV2': [{
            'cardId': 'createCardMessage',
            'card': {
              'header': {
                'title': 'A card message!',
                'subtitle': 'Created with the Chat API',
                'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
                'imageType': 'CIRCLE'
              },
              'sections': [
                {
                  'widgets': [
                    {
                      'buttonList': {
                        'buttons': [
                          {
                            'text': 'Read the docs!',
                            'onClick': {
                              'openLink': {
                                'url': 'https://developers.google.com/chat'
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          }]
        }
    
    ).execute()
    
    print(result)
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_create_card_message.py
    

เริ่มหรือตอบกลับชุดข้อความ

หากต้องการเริ่มชุดข้อความ ให้ส่งข้อความแล้วออก thread.name ว่างเปล่า Google Chat จะป้อนข้อมูลนี้เมื่อสร้างชุดข้อความ หากต้องการ กำหนดชื่อชุดข้อความเอง ระบุ thread.threadKey ด้วย

หากต้องการตอบกลับชุดข้อความ ให้ส่งข้อความที่ระบุชุดข้อความ threadKey หรือ name หากชุดข้อความสร้างขึ้นโดยบุคคลหรือผู้อื่น แอป Chat คุณต้องใช้ช่อง thread.name

หากไม่พบชุดข้อความที่ตรงกัน คุณจะระบุได้ ข้อความควรขึ้นชุดข้อความใหม่หรือโพสต์ไม่สำเร็จจากการตั้งค่า messageReplyOption ด้วย

หากเป็น messageReplyOption ได้รับการตั้งค่าแล้ว คุณต้องตั้งค่าthread.name หรือthread.threadKey ด้วย

ต่อไปนี้เป็นวิธีเริ่มหรือตอบกลับชุดข้อความที่มีช่อง threadKey กำหนดไว้เป็น nameOfThread:

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_create_message_thread.py
  2. รวมรหัสต่อไปนี้ใน chat_create_message_thread.py:

    from apiclient.discovery import build
    from google.oauth2 import service_account
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = service_account.Credentials.from_service_account_file(
        'credentials.json', scopes=SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Create a Chat message.
    result = chat.spaces().messages().create(
    
        # The space to create the message in.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        parent='spaces/SPACE',
    
        # Whether to start a thread or reply to an existing one.
        #
        # Required when threading is enabled in a space unless starting a
        # thread.  Ignored in other space types. Threading is enabled when
        # space.spaceThreadingState is THREADED_MESSAGES.
        #
        # REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD replies to an existing thread
        # if one exists, otherwise it starts a new one.
        messageReplyOption='REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
    
        # The message body.
        body={
    
            # The message to create.
            'text': 'Start or reply to another message in a thread!',
    
            # The thread to start or reply to.
            'thread': {
                'threadKey': 'nameOfThread'
            }
        }
    
    ).execute()
    
    print(result)
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_create_message_thread.py
    

Chat API จะแสดงผลอินสแตนซ์ Message ซึ่งระบุรายละเอียดของข้อความที่ส่ง

ตั้งชื่อข้อความ

ส่วนนี้จะอธิบายวิธีตั้งชื่อข้อความโดยการตั้งค่ารหัสที่กำหนดเองสำหรับ คุณใช้รหัสที่กำหนดเองเพื่อรับ อัปเดต หรือลบข้อความได้ รหัสที่กำหนดเอง ช่วยให้คุณระบุข้อความได้โดยไม่ต้องจัดเก็บรหัสที่ระบบกำหนดจาก ชื่อทรัพยากรของข้อความ (แสดงในช่อง name) แหล่งข้อมูล จะสร้างขึ้นในส่วน เนื้อหาการตอบสนอง เมื่อคุณสร้างข้อความ

ตัวอย่างเช่น ในการเรียกข้อความโดยใช้เมธอด get() ให้ใช้เมธอด ชื่อทรัพยากรเพื่อระบุข้อความที่จะเรียก ชื่อทรัพยากรคือ มีรูปแบบเป็น spaces/{space}/messages/{message} โดยที่ {message} แสดงถึง รหัสที่ระบบกำหนด หากคุณตั้งชื่อข้อความไว้ คุณสามารถแทนที่ ค่าของ {message} ด้วยรหัสที่กำหนดเอง

หากต้องการตั้งชื่อข้อความ ให้ระบุรหัสที่กำหนดเองใน messageId เมื่อคุณสร้างข้อความ ฟิลด์ messageId จะตั้งค่าค่าของฟิลด์ clientAssignedMessageId ของทรัพยากร Message

คุณสามารถตั้งชื่อข้อความได้เฉพาะเมื่อสร้างข้อความเท่านั้น คุณไม่สามารถตั้งชื่อหรือ แก้ไขรหัสที่กำหนดเองสำหรับข้อความที่มีอยู่ รหัสที่กำหนดเองต้องเป็นไปตามข้อกำหนดต่อไปนี้ ข้อกำหนด

  • ขึ้นต้นด้วย client- เช่น client-custom-name เป็นที่กําหนดเองที่ถูกต้อง รหัส แต่ custom-name ไม่ใช่รหัส
  • มีอักขระได้สูงสุด 63 ตัว และมีเฉพาะตัวพิมพ์เล็ก ตัวเลข และ ขีดกลาง
  • ไม่ซ้ำกันภายในพื้นที่ทำงาน แอป Chat ไม่สามารถใช้ รหัสที่กำหนดเองเดียวกันสำหรับข้อความที่แตกต่างกัน

วิธีส่งข้อความด้วยรหัสที่กำหนดเองมีดังนี้

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_create_named_message.py
  2. รวมรหัสต่อไปนี้ใน chat_create_named_message.py:

    from apiclient.discovery import build
    from google.oauth2 import service_account
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = service_account.Credentials.from_service_account_file(
        'credentials.json', scopes=SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Create a Chat message with a custom name.
    result = chat.spaces().messages().create(
    
        # The space to create the message in.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        parent='spaces/SPACE',
    
        # Custom name for the message used to facilitate later operations.
        messageId='client-NAME',
    
        # The message to create.
        body={'text': 'Hello, world!'}
    
    ).execute()
    
    print(result)
    
  3. ในโค้ด ให้แทนที่

    • SPACE: รหัสพื้นที่ทำงานที่คุณต้องการ โพสต์ข้อความ ซึ่งคุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
    • NAME: ชื่อที่กำหนดเองของข้อความ
  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_create_named_message.py
    

Chat API จะแสดงผลอินสแตนซ์ Message

เพิ่มวิดเจ็ตแบบอินเทอร์แอกทีฟที่ด้านล่างของข้อความ

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

  • ให้คะแนนความถูกต้องหรือความพึงพอใจของข้อความ
  • รายงานปัญหาเกี่ยวกับข้อความหรือแอป Chat
  • เปิดลิงก์ไปยังเนื้อหาที่เกี่ยวข้อง เช่น เอกสารประกอบ
  • ปิดหรือเลื่อนการแจ้งเตือนข้อความที่คล้ายกันจากแอป Chat สำหรับระยะเวลาที่กำหนด

หากต้องการเพิ่มวิดเจ็ตอุปกรณ์เสริม ให้ใส่ accessoryWidgets[] ในข้อความและระบุ AccessoryWidgets ที่คุณต้องการรวมไว้ด้วย ทุกคนในพื้นที่ทำงานต้องเห็นข้อความ (คุณจะเพิ่มวิดเจ็ตอุปกรณ์เสริมในข้อความส่วนตัวไม่ได้)

รูปภาพต่อไปนี้แสดงแอปใน Chat ที่ต่อท้าย ข้อความที่มีวิดเจ็ตอุปกรณ์เสริมเพื่อให้ผู้ใช้ให้คะแนนประสบการณ์การใช้งาน ด้วยแอป Chat

ตัวอย่างวิดเจ็ตอุปกรณ์เสริม

ตัวอย่างโค้ดต่อไปนี้แสดง JSON สำหรับข้อความนี้ เมื่อผู้ใช้คลิก ปุ่มใดปุ่มหนึ่ง การโต้ตอบจะทริกเกอร์ฟังก์ชันที่เกี่ยวข้อง (เช่น doUpvote) ที่ประมวลผลการจัดประเภท


 "text": "Rate your experience with this Chat app.",
 "accessoryWidgets": [
   {
     "buttonList": {
       "buttons": [
         {
           "icon": {
             "material_icon": {
               "name": "thumb_up"
             }
           },
           "color": {
             "red": 0,
             "blue": 255,
             "green": 0
           },
           "onClick": {
             "action": {
               "function": "doUpvote",
             }
           }
         },
         {
           "icon": {
             "material_icon": {
               "name": "thumb_down"
             }
           },
           "color": {
             "red": 0,
             "blue": 255,
             "green": 0
           },
           "onClick": {
             "action": {
               "function": "doDownvote",
             }
           }
         }
       ]
     }
   }
 ]

ส่งข้อความแบบส่วนตัว

แอปแชทสามารถส่งข้อความ SMS และการ์ดแบบส่วนตัวเพื่อให้ ข้อความจะแสดงต่อผู้ใช้ 1 คนในพื้นที่ทำงาน ถ้าจะส่งข้อความแบบส่วนตัว คุณระบุช่อง privateMessageViewer ในข้อความ เฉพาะ แอปแชทจะส่งข้อความส่วนตัวได้ เมื่อต้องการส่งข้อความส่วนตัว แบบอะซิงโครนัส คุณต้องใช้การตรวจสอบสิทธิ์แอป

โปรดดูรายละเอียดที่หัวข้อส่งข้อความส่วนตัวไปยัง Google Chat ผู้ใช้

แก้ปัญหา

เมื่อแอป Google Chat หรือ card จะแสดงผลข้อผิดพลาด อินเทอร์เฟซ Chat แสดงข้อความว่า "เกิดข้อผิดพลาด" หรือ "ไม่สามารถดำเนินการตามคำขอของคุณ" บางครั้ง UI ของ Chat ไม่แสดงข้อความแสดงข้อผิดพลาดใดๆ แต่แอป Chat หรือ ทำให้เกิดผลลัพธ์ที่ไม่คาดคิด เช่น ข้อความในการ์ดอาจ ปรากฏขึ้น

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