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

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

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

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

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

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

Node.js

Python

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

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
    
  • แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้างและเผยแพร่ แอป Chat โปรดดูที่ สร้างแอป Google Chat

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

Apps Script

  • บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้าง แอป Chat ให้ทำตาม quickstartนี้

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

ส่วนนี้จะอธิบายวิธีส่งข้อความตัวอักษรใน 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 httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # 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 httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # 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

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

Python

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

    from httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # 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 ที่มีรายละเอียดข้อความที่ส่ง

ส่งและตั้งชื่อข้อความ

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

การกำหนดชื่อที่กำหนดเองจะไม่แทนที่ช่อง name ที่สร้างขึ้น ซึ่งเป็นชื่อทรัพยากรของข้อความ แต่จะตั้งชื่อที่กําหนดเองเป็นช่อง clientAssignedMessageId แทน ซึ่งคุณใช้อ้างอิงระหว่างการประมวลผลในภายหลังได้ เช่น อัปเดตหรือการลบข้อความ

ชื่อที่กำหนดเองมีข้อกำหนดต่อไปนี้

  • เริ่มต้นด้วย client- ตัวอย่างเช่น client-custom-name เป็นชื่อที่กำหนดเองที่ถูกต้อง แต่ custom-name ไม่ใช่
  • มีเฉพาะตัวอักษรพิมพ์เล็ก ตัวเลข และขีดกลาง
  • มีความยาวไม่เกิน 63 อักขระ
  • การระบุชื่อที่กำหนดเองที่ใช้แล้วขณะที่ส่งข้อความแสดงข้อผิดพลาด แต่วิธีการอื่นๆ เช่น update และ delete ทำงานได้ตามที่คาดไว้

วิธีส่งและตั้งชื่อข้อความมีดังนี้

Python

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

    from httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # 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-custom-name',
    
        # The message to create.
        body={'text': 'Hello, world!'}
    
    ).execute()
    
    print(result)
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่งดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

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

    python3 chat_create_named_message.py
    

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

แก้ปัญหา

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

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