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

คำแนะนำนี้จะอธิบายวิธีต่างๆ ที่แอป 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 แบบไม่พร้อมกันด้วยการตรวจสอบสิทธิ์แอปและการตรวจสอบสิทธิ์ผู้ใช้

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

รูปภาพต่อไปนี้แสดงแอปใน Chat ที่ต่อท้าย SMS ที่มีวิดเจ็ตอุปกรณ์เสริมเพื่อให้ผู้ใช้ให้คะแนนประสบการณ์การใช้งานแอป 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 ในข้อความ มีเพียง แอปใน Chat เท่านั้นที่ส่งข้อความส่วนตัวได้ หากต้องการส่งข้อความส่วนตัวแบบไม่พร้อมกัน คุณต้องใช้การตรวจสอบสิทธิ์แอป

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

แก้ปัญหา

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

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