이 페이지에서는 Google Chat 앱으로 비공개 메시지를 만들고 보내는 방법을 설명합니다.
비공개 메시지는 지정된 채팅 사용자에게만 표시되는 채팅 앱 메시지입니다. 스페이스에서 여러 사용자와 비공개 메시지를 사용하여 채팅 앱과 비공개로 상호작용할 수 있습니다. 예를 들어 채팅 앱은 비공개로 메시지를 전송하여 다음 중 하나를 수행할 수 있습니다.
- 슬래시 명령어에 응답합니다. 예를 들어 사용자가 스페이스에서 채팅 앱의
/about
슬래시 명령어를 호출하면 채팅 앱에서 채팅 앱의 기능과 사용 방법을 설명하는 비공개 메시지로 응답할 수 있습니다. - 사용자 한 명에게만 관련된 정보를 알림 또는 전송합니다. 예를 들어 사용자에게 작업이 할당되었음을 알리거나 작업을 완료하도록 알립니다.
- 오류 메시지를 보냅니다. 예를 들어 사용자가 슬래시 명령어의 필수 인수 텍스트를 생략하면 채팅 앱에서 비공개 메시지를 보내 오류를 설명하고 사용자가 명령어의 형식을 지정하는 데 도움을 줄 수 있습니다.
채팅 앱이 비공개 메시지를 보내면 메시지가 본인만 볼 수 있음을 알리는 라벨이 메시지에 표시됩니다.
기본 요건
Node.js
- Google Chat API가 사용 설정 및 구성된 Google Cloud 프로젝트 단계는 Google Chat 앱 빌드를 참고하세요.
- 슬래시 명령어에 비공개로 응답하려면 채팅 앱에 구성된 슬래시 명령어가 필요합니다. 슬래시 명령어를 빌드하려면 슬래시 명령어에 응답을 참고하세요.
messages.create()
메서드를 사용하여 비공개 메시지를 보내려면 앱 인증을 사용해야 합니다.
참고: 이 가이드의 Node.js 코드 샘플은 Google Cloud 함수로 실행되도록 작성되었습니다.
Python
- Google Chat API가 사용 설정 및 구성된 Google Cloud 프로젝트 단계는 Google Chat 앱 빌드를 참고하세요.
- 슬래시 명령어에 비공개로 응답하려면 채팅 앱에 구성된 슬래시 명령어가 필요합니다. 슬래시 명령어를 빌드하려면 슬래시 명령어에 응답을 참고하세요.
messages.create()
메서드를 사용하여 비공개 메시지를 보내려면 앱 인증을 사용해야 합니다.
참고: 이 가이드의 Python 코드 샘플은 Python 3.10을 사용하여 Google Cloud 함수로 실행되도록 작성되었습니다.
Apps Script
- 채팅 앱. 채팅 앱을 빌드하려면 이 quickstart을 따르세요.
- 슬래시 명령어에 비공개로 응답하려면 채팅 앱에 구성된 슬래시 명령어가 필요합니다. 슬래시 명령어를 빌드하려면 슬래시 명령어에 응답을 참고하세요.
messages.create()
메서드를 사용하여 비공개 메시지를 보내려면 앱 인증을 사용해야 합니다.
비공개 메시지 보내기
채팅 앱으로 비공개로 메시지를 보내려면 메시지를 만들 때 메시지에서 privateMessageViewer
필드를 지정합니다. 메시지를 만들 때와 마찬가지로, 사용자 상호작용에 응답하거나 Message
리소스에서 Google Chat API의 create()
메서드를 비동기식으로 호출하여 비공개 메시지를 만듭니다. 문자 메시지 또는 카드 메시지를 보내는 단계는 메시지 보내기를 참조하세요.
다음 예는 Hello private world!
라는 비공개 문자 메시지의 JSON을 보여줍니다.
{
"text": "Hello private world!",
"privateMessageViewer": "USER"
}
이 예에서 USER
는 User
리소스 형식으로 된 메시지를 볼 수 있는 Chat 사용자를 나타냅니다. 사용자 상호작용에 응답하는 경우 상호작용 이벤트에서 User
객체를 지정할 수 있습니다.
예시는 다음 슬래시 명령어에 비공개로 응답 섹션을 참고하세요.
비공개 메시지 뷰어를 지정하려면 User
리소스의 name
필드를 사용하면 됩니다.
{
"text": "Hello private world!",
"privateMessageViewer": {
"name": "users/USER_ID"
}
}
이 예시에서는 name
필드를 사용하여 Google Chat에서 뷰어의 User
리소스 이름을 지정합니다. USER_ID
를 사용자의 고유 ID(예: 12345678987654321
또는 hao@cymbalgroup.com
)로 바꿉니다.
사용자 지정에 관한 자세한 내용은 Google Chat 사용자 식별 및 지정을 참고하세요.
슬래시 명령어에 비공개로 응답
다음 코드는 슬래시 명령어에 비공개 메시지로 응답하는 채팅 앱의 예를 보여줍니다.
채팅 앱은 MESSAGE
상호작용 이벤트를 처리하고 사용 방법을 설명하는 비공개 텍스트 메시지로 /help
슬래시 명령어에 응답합니다.
Node.js
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {!Object} req Request sent from Google Chat app
* @param {!Object} res Response to send back
*
* @return {!Object} respond to slash command
*/
exports.onMessage = function onMessage(req, res) {
if (req.method === 'GET' || !req.body.message) {
return res.send('Hello! This function is meant to be used in Google Chat app.');
}
const event = req.body;
// Checks for the presence of event.message.slashCommand.
// If the slash command is "/help", responds with a private text message.
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case '1': // /help
return res.json({
privateMessageViewer: event.user,
text: 'This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>.'
});
}
}
// If the Chat app doesn't detect a slash command, it responds
// with a private text message
return res.json({
privateMessageViewer: event.user,
text: 'Try a slash command.'
});
};
Apps Script
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onMessage(event) {
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case 1: // Responds to /help
return {
"privateMessageViewer": event.user,
"text": "This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>."
};
}
}
else {
return { "text": "Try a slash command.", "privateMessageViewer": event.user };
}
}
Python
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
if req.method == 'GET':
return 'Hello! This function must be called from Google Chat.'
request = req.get_json(silent=True)
# Checks for the presence of event.message.slashCommand.
# If the slash command is "/help", responds with a private text message.
if request.get('message', {}).get('slashCommand'):
command_id = request.get('message', {}).get('slashCommand').get('commandId')
if command_id == '1': # /help
return {
'privateMessageViewer': request.get('user'),
'text': (
'This Chat app was created by Cymbal Labs. To get help with this'
' app, <https://cymbalgroup.com/docs|see our documentation> or'
' <https://cymbalgroup.com/support|contact our support team>.'
),
}
return {
'privateMessageViewer': request.get('user'),
'text': 'Try a slash command.',
}
제한사항
비공개 메시지를 보내려면 메시지에 다음을 포함하거나 사용할 수 없습니다.
- 첨부파일.
- 액세서리 작업.
- 일부 비공개 메시지 예를 들어 채팅 앱은 텍스트와 카드가 포함된 메시지를 보낼 수 없습니다. 이 경우 텍스트는 한 명의 사용자에게만 표시되지만 카드는 스페이스의 모든 사용자에게 표시됩니다.
- 사용자 인증. 채팅 앱만 비공개 메시지를 보낼 수 있으므로 채팅 앱은 비공개로 메시지를 보내는 사용자로 인증할 수 없습니다.
비공개 메시지 업데이트 또는 삭제
Google Chat 메시지를 업데이트하거나 삭제하려면 Chat API를 호출해야 합니다. 비공개 메시지의 뷰어를 변경하거나 메시지를 공개로 설정할 수 없습니다. 따라서 비공개 메시지를 업데이트할 때 API 호출에서 privateMessageViewer
필드를 생략해야 합니다 (이 필드는 출력 전용입니다).
비공개 메시지를 업데이트하려면 메시지 업데이트를 참조하세요. 비공개 메시지를 삭제하려면 메시지 삭제를 참조하세요.