이 가이드에서는 Google Chat 앱에서 메시지를 전송할 수 있는 다양한 방법을 설명합니다. 메시지:
- 사용자에게 응답하여 실시간으로 문자 및 카드 메시지를 보내세요 상호작용하지 않습니다.
create
메서드를 호출하여 텍스트 및 카드 메시지를 비동기식으로 전송합니다.Message
리소스- 메시지 대화목록을 시작하거나 답장을 보냅니다.
- 메시지를 보내고 이름을 지정합니다.
이
Message
리소스
는
텍스트
또는
카드
메시지가 표시됩니다. 다음과 같은 작업을 할 수 있습니다.
다음을 호출하여 Google Chat API의 create
, get
, update
또는 delete
메시지
사용할 수 있습니다. 문자 및 카드 메시지에 대한 자세한 내용은 다음을 참고하세요.
Google Chat 메시지 개요
최대 메시지 크기 (텍스트 또는 카드 포함)는 32,000바이트입니다. 메시지가 이 크기를 초과하면 채팅 앱에서 여러 개의 메시지를 보낼 수 있습니다.
다음의 Message
리소스에서 create
메서드를 호출하는 대신
Google Chat API를 사용하여 문자 또는 카드 메시지를 비동기식으로 전송
또한 Google Chat 앱은 사용자 상호작용에 응답하기 위해
있습니다. 사용자 상호작용에 대한 응답에는 인증이 필요하지 않으며
대화형 대화상자, 링크 등 다른 유형의 메시지 지원
미리 볼 수 있습니다 자세한 내용은
Google Chat 앱과의 상호작용을 수신하고 응답하세요.
기본 요건
Node.js
- Google Chat 앱 채팅 앱을 만들려면 다음 단계를 따르세요. HTTP 서비스를 사용하는 경우 이 빠른 시작:
- 채팅 앱에서 전송하도록 구성된 승인 비동기 메시지를 표시합니다. 전송하는 데 승인 구성이 필요하지 않습니다. 채팅 메시지를 실시간으로 전송합니다.
Python
- Google Chat 앱 채팅 앱을 만들려면 다음 단계를 따르세요. HTTP 서비스를 사용하는 경우 이 빠른 시작:
- 채팅 앱에서 전송하도록 구성된 승인 비동기 메시지를 표시합니다. 전송하는 데 승인 구성이 필요하지 않습니다. 채팅 메시지를 실시간으로 전송합니다.
Apps Script
- Google Chat 앱 채팅 앱을 만들려면 다음 단계를 따르세요. 을 사용하려면 다음 단계를 따르세요 빠른 시작:
- 채팅 앱에서 전송하도록 구성된 승인 비동기 메시지를 표시합니다. 전송하는 데 승인 구성이 필요하지 않습니다. 채팅 메시지를 실시간으로 전송합니다.
문자 메시지 보내기
이 섹션에서는 다음 두 가지 방법으로 문자 메시지를 보내는 방법을 설명합니다.
- 사용자 상호작용에 응답하여 실시간으로 문자 메시지를 보낼 수 있습니다.
- Google Chat API를 비동기식으로 호출하여 문자 메시지를 보냅니다.
실시간으로 문자 메시지 보내기
이 예시에서는 채팅 앱이 메시지가 표시됩니다. 온라인 여행 포털에 대한 자세한 내용은 유용한 온보딩으로 사용자와 스페이스를 시작하세요.
사용자가 채팅 앱을 추가할 때 문자 메시지를 보내기 위해
채팅 앱 스페이스에
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
리소스.
앱 인증이 포함된 문자 메시지 보내기
다음을 사용하여 문자 메시지를 보내는 방법은 다음과 같습니다. 앱 인증:
Python
- 작업 디렉터리에서
chat_create_text_message_app.py
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)
코드에서
SPACE
을 공백 이름으로 바꿉니다. GCP 콘솔에서spaces.list()
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_create_text_message_app.py
Chat API는
Message
전송된 메시지를 자세히 설명합니다.
사용자 인증을 포함한 문자 메시지 전송
다음을 사용하여 문자 메시지를 보내는 방법은 다음과 같습니다. 사용자 인증:
Python
- 작업 디렉터리에서
chat_create_text_message_user.py
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()
코드에서
SPACE
을 공백 이름으로 바꿉니다. GCP 콘솔에서spaces.list()
메서드를 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_create_text_message_user.py
Chat API는
Message
전송된 메시지를 자세히 설명합니다.
카드 메시지 보내기
이 섹션에서는 다음 두 가지 방법으로 카드 메시지를 보내는 방법을 설명합니다.
- 사용자 상호작용에 응답하여 실시간으로 카드 메시지를 보냅니다.
- Google Chat API를 비동기식으로 호출하여 카드 메시지를 전송합니다.
실시간으로 카드 메시지 전송
채팅 앱에서 카드 메시지를 만들어 사용자에게 응답할 수 있습니다. 예를 들어 사용자가 채팅 앱에 메시지를 보내거나 스페이스에 채팅 앱을 추가합니다. 자세히 알아보기 사용자 상호작용에 응답하는 방법은 사용자 상호작용에 대한 채팅 앱 상호작용 이벤트를 사용하세요.
이 예시에서는 사용자가 채팅 앱에 메시지를 보냅니다. 채팅 앱이 카드 메시지를 전송하여 응답합니다. 사용자 이름과 아바타 이미지를 표시합니다.
비동기식으로 카드 메시지 전송
카드 메시지, 요청에 다음을 전달합니다.
- 앱 인증의 경우
chat.bot
승인 범위를 지정합니다. 할 수 없습니다. 사용자 인증이 포함된 카드 메시지를 보냅니다. - 먼저
create
메서드 에Message
리소스.
다음은 카드 메시지의 예입니다.
앱 인증을 통해 카드 메시지를 보내는 방법은 다음과 같습니다.
Python
- 작업 디렉터리에서
chat_create_card_message.py
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)
코드에서
SPACE
을 공백 이름으로 바꿉니다. GCP 콘솔에서spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_create_card_message.py
메시지 대화목록 시작 또는 답장
메시지 대화목록을 시작하려면 메시지를 보내고
thread.name
비어 있습니다. Google Chat에서 대화목록을 만들 때 이 입력란을 채웁니다. 원하는 경우
스레드의 이름을 맞춤설정하고
thread.threadKey
필드를 확인합니다.
메시지 대화목록에 답장하려면 대화목록의
threadKey
또는 name
필드 대화목록이 다른 사람 또는 다른 사람이 만든 경우
채팅 앱에서는 thread.name
필드를 사용해야 합니다.
일치하는 대화목록이 없으면
메시지가 새 대화목록을 시작하는지 아니면 게시 실패인지 여부를 지정합니다.
messageReplyOption
필드를 확인합니다.
messageReplyOption
인 경우
이 설정되면 thread.name
또는 thread.threadKey
도 설정해야 합니다.
다음과 같이 정의된 threadKey
필드로 스레드를 시작하거나 스레드에 답장하는 방법은 다음과 같습니다.
nameOfThread
:
Python
- 작업 디렉터리에서
chat_create_message_thread.py
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)
코드에서
SPACE
을 공백 이름으로 바꿉니다. GCP 콘솔에서spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_create_message_thread.py
Chat API는
Message
전송된 메시지를 자세히 설명합니다.
메시지 이름 지정
이 섹션에서는
메시지가 표시됩니다. 맞춤 ID를 사용하여 메일을 가져오거나 업데이트하거나 삭제할 수 있습니다. 맞춤 ID
시스템 할당 ID를 저장하지 않고도 메시지를 지정할 수 있습니다.
메시지의 리소스 이름입니다 (name
필드에 표시됨). 리소스
생성된 포드의 이름은
응답 본문
설정할 수 있습니다.
예를 들어 get()
메서드를 사용하여 메시지를 검색하려면 다음을 사용합니다.
리소스 이름을 사용하여 검색할 메시지를 지정합니다. 리소스 이름은
spaces/{space}/messages/{message}
형식으로 표시되며, 여기서 {message}
는 다음을 나타냅니다.
시스템에서 할당한 ID를 찾습니다. 메일 이름을 지정한 경우
{message}
의 값을 커스텀 ID로 바꿉니다.
메일 이름을 지정하려면 맞춤 ID를 지정합니다.
messageId
필드를 설정할 수 없습니다. messageId
필드는
clientAssignedMessageId
Message
리소스의 필드입니다.
메시지를 만들 때만 메시지 이름을 지정할 수 있습니다. 이름을 지정하거나 기존 메일의 맞춤 ID를 수정할 수 있습니다. 커스텀 ID는 다음을 충족해야 합니다. 요구사항:
client-
로 시작합니다. 예를 들어client-custom-name
는 유효한 커스텀입니다. ID이지만custom-name
는 아닙니다.- 최대 63자(영문 기준)까지 소문자, 숫자, 하이픈이 있습니다.
- 스페이스 내에서 고유합니다. 채팅 앱에서는 여러 메시지에 대해 동일한 맞춤 ID를 사용할 수 있습니다.
맞춤 ID를 사용하여 메시지를 보내는 방법은 다음과 같습니다.
Python
- 작업 디렉터리에서
chat_create_named_message.py
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)
코드에서 다음을 바꿉니다.
SPACE
: 추가하려는 스페이스의 ID입니다. 메시지를 게시하세요.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.NAME
: 메시지의 맞춤 이름입니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_create_named_message.py
Chat API는
Message
메시지 하단에 대화형 위젯 추가
원하는 경우 액세서리 위젯을 사용하여 메시지를 추가할 수 있습니다. 액세서리 위젯은 메시지의 텍스트 또는 카드 뒤에 표시됩니다. 이러한 다음과 같은 다양한 방법으로 사용자가 메시지와 상호작용하도록 메시지를 표시하는 위젯 다음과 같습니다.
- 메시지의 정확성 또는 만족도를 평가합니다.
- 메시지 또는 채팅 앱 문제 신고
- 문서와 같은 관련 콘텐츠의 링크를 엽니다.
- Chat 앱에서 비슷한 메시지 닫기 또는 다시 알림 확인할 수 있습니다.
액세서리 위젯을 추가하려면
accessoryWidgets[]
객체를 하나 이상 지정하고
AccessoryWidgets
포함할 수 있습니다. 메시지는 스페이스에 있는 모든 사용자에게 표시되어야 합니다.
비공개 메시지에는 액세서리 위젯을 추가할 수 없습니다.
다음 이미지는 다음을 추가하는 채팅 앱을 보여줍니다. 액세서리 위젯이 포함된 문자 메시지로 사용자가 사용 환경을 평가할 수 있음 채팅 앱
다음 코드 샘플은 이 메시지의 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",
}
}
}
]
}
}
]
비공개로 메시지 보내기
채팅 앱에서 문자 및 카드 메시지를 비공개로 전송하여
메시지는 스페이스의 사용자 한 명에게만 표시됩니다. 비공개로 메시지를 보내려면 다음 안내를 따르세요.
메시지에서 privateMessageViewer
필드를 지정합니다. 단
채팅 앱에서 비공개 메시지를 보낼 수 있습니다. 비공개 메시지 보내기
비동기식으로 사용하려면 앱 인증을 사용해야 합니다.
자세한 내용은 Google Chat에 비공개 메시지 보내기를 참고하세요. 사용자가 누구인지 알아보세요.
문제 해결
Google Chat 앱 또는 card가 오류를 반환하는 경우 Chat 인터페이스에 '문제가 발생했습니다'라는 메시지가 표시됨 또는 '요청을 처리할 수 없습니다'와 같은 메시지가 표시됩니다. 채팅 UI가 오류 메시지가 표시되지 않지만 채팅 앱 또는 카드에서 예기치 않은 결과가 발생합니다. 예를 들어 카드 메시지가 나타납니다.
채팅 UI에 오류 메시지가 표시되지 않을 수도 있지만 오류 해결에 도움이 되는 오류 메시지 및 로그 데이터를 사용할 수 있음 채팅 앱의 오류 로깅이 사용 설정된 경우 보는 데 도움이 필요한 경우 오류를 수정하는 방법에 대한 자세한 내용은 Google Chat 오류 문제 해결하기
관련 주제
- 메일 서식 지정
- 메시지 세부정보 확인하기
- 스페이스의 메시지 나열하기
- 메시지 업데이트하기
- 메시지 삭제
- Google Chat 메시지에서 사용자 식별하기.
- 수신 웹훅을 사용하여 Google Chat에 메시지 보내기