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

คู่มือนี้จะอธิบายวิธีต่างๆ ที่แอป Google Chat ใช้ในการส่งข้อความได้

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

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

แทนที่จะเรียกใช้เมธอด create ในทรัพยากร Message ของ Google Chat API เพื่อส่งข้อความหรือข้อความในการ์ดแบบไม่พร้อมกัน แต่แอป 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 วิธีต่อไปนี้

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

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

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

หากต้องการส่ง SMS เมื่อผู้ใช้เพิ่มแอป 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`.'
  }
}

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

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

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

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

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

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

ส่ง SMS ด้วยการตรวจสอบสิทธิ์แอป

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

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 ซึ่งแสดงรายละเอียดของข้อความที่ส่ง

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

วิธีส่ง SMS ที่มีการตรวจสอบสิทธิ์ผู้ใช้มีดังนี้

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 membership.
        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 และแอป 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

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 อย่างน้อย 1 รายการที่คุณต้องการรวมไว้ ทุกคนในพื้นที่ทำงานต้องเห็นข้อความดังกล่าว (คุณจะเพิ่มวิดเจ็ตอุปกรณ์เสริมลงในข้อความส่วนตัวไม่ได้)

รูปภาพต่อไปนี้แสดงแอป 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",
             }
           }
         }
       ]
     }
   }
 ]

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

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

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

แก้ปัญหา

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

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