이 가이드에서는 Google Chat API의 ThreadReadState 리소스에서 update 메서드를 사용하여 메일 대화목록을 읽음 또는 읽지않음으로 표시하는 방법을 설명합니다.
ThreadReadState 리소스는 Google Chat 메시지 대화목록에서 지정된 사용자의 마지막으로 읽은 메시지에 관한 세부정보를 나타내는 싱글톤 리소스입니다.
기본 요건
Node.js
- Google Chat에 액세스할 수 있는 Business 또는 Enterprise Google Workspace 계정
- 환경을 설정합니다.
- Google Cloud 프로젝트를 만듭니다.
- OAuth 동의 화면 구성
- Chat 앱의 이름, 아이콘, 설명으로 Google Chat API를 사용 설정하고 구성합니다.
- Node.js Cloud 클라이언트 라이브러리를 설치합니다.
- 데스크톱 애플리케이션의 OAuth 클라이언트 ID 사용자 인증 정보를 만듭니다. 이 가이드의 샘플을 실행하려면 사용자 인증 정보를
credentials.json이라는 JSON 파일로 로컬 디렉터리에 저장합니다.
- 사용자 인증을 지원하는 승인 범위를 선택합니다.
Python
- Google Chat에 액세스할 수 있는 Business 또는 Enterprise Google Workspace 계정
- 환경을 설정합니다.
- Google Cloud 프로젝트를 만듭니다.
- OAuth 동의 화면 구성
- Chat 앱의 이름, 아이콘, 설명으로 Google Chat API를 사용 설정하고 구성합니다.
- Python Cloud 클라이언트 라이브러리를 설치합니다.
- 데스크톱 애플리케이션의 OAuth 클라이언트 ID 사용자 인증 정보를 만듭니다. 이 가이드의 샘플을 실행하려면 사용자 인증 정보를
credentials.json이라는 JSON 파일로 로컬 디렉터리에 저장합니다.
- 사용자 인증을 지원하는 승인 범위를 선택합니다.
호출 사용자의 대화목록 읽기 상태를 업데이트합니다.
메일 대화목록 내에서 사용자의 읽음 상태를 업데이트하려면 요청에 다음을 포함합니다.
chat.users.readstate승인 범위를 지정합니다.UpdateThreadReadState메서드를 호출합니다.updateMask을lastReadTime값으로 전달합니다.threadReadState를ThreadReadState의 인스턴스로 전달합니다.- 업데이트할 대화목록 읽기 상태로 설정된
name필드입니다. 여기에는 사용자 ID 또는 별칭, 스페이스 ID, 대화목록 ID가 포함됩니다. 대화목록 읽기 상태 업데이트는 호출 사용자의 읽기 상태 업데이트만 지원하며, 다음 중 하나를 설정하여 지정할 수 있습니다.me별칭입니다. 예를 들면users/me/spaces/SPACE/threads/THREAD/threadReadState입니다.- 호출하는 사용자의 Workspace 이메일 주소입니다. 예를 들면 다음과 같습니다.
users/user@example.com/spaces/SPACE/threads/THREAD/threadReadState - 호출하는 사용자의 사용자 ID입니다. 예를 들면 다음과 같습니다.
users/USER/spaces/SPACE/threads/THREAD/threadReadState
lastReadTime필드는 대화목록에서 호출 사용자의 마지막 읽기 위치를 표시하는 타임스탬프(예: 마지막으로 읽은 메시지의 타임스탬프 또는 현재 시간)로 설정됩니다.- 읽지 않은 상태로 표시:
lastReadTime를 대화목록의 최신 메시지 생성 시간보다 이전 타임스탬프로 설정합니다. Chat에서 대화목록은 해당 타임스탬프부터 읽지 않은 상태로 표시되어 읽지 않은 배지를 업데이트하고 대화목록을 굵게 표시하며 홈에 표시합니다. - 읽음으로 표시:
lastReadTime를 대화목록의 최신 메시지 생성 시간과 같거나 이후의 타임스탬프로 설정합니다. 미래의 타임스탬프를 지정하면 API는lastReadTime를 현재 요청 시간으로 강제 변환합니다. - 최소 타임스탬프:
lastReadTime필드는 유닉스 시간(1970-01-01T00:00:00Z) 이후여야 합니다. 음수 타임스탬프(1970-01-01T00:00:00Z이전)를 설정하면INVALID_ARGUMENT(400) 오류가 반환됩니다.
- 읽지 않은 상태로 표시:
- 업데이트할 대화목록 읽기 상태로 설정된
다음 예시에서는 호출 사용자의 대화목록 읽기 상태를 업데이트합니다.
Node.js
/**
* This sample shows how to update the calling user's thread read state.
*
* It relies on the @google-apps/chat npm package.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState
const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');
async function main() {
// Create a client
const chatClient = new ChatServiceClient({
authClient: await auth.getClient({
scopes: ['https://www.googleapis.com/auth/chat.users.readstate']
})
});
// Initialize request arguments.
const request = {
threadReadState: {
name: 'users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState',
lastReadTime: {
seconds: Math.floor(Date.now() / 1000),
nanos: 0
}
},
updateMask: {
paths: ['last_read_time']
}
};
// Make the request
const response = await chatClient.updateThreadReadState(request);
console.log(response);
}
main().catch(console.error);
Python
"""
This sample shows how to update the calling user's thread read state.
"""
from google.apps import chat_v1
import google.auth
from google.protobuf import timestamp_pb2
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState
def update_thread_read_state():
# Authenticate with Chat API using user credentials.
credentials, _ = google.auth.default(
scopes=["https://www.googleapis.com/auth/chat.users.readstate"]
)
client = chat_v1.ChatServiceClient(credentials=credentials)
# Set timestamp for last read time.
now = timestamp_pb2.Timestamp()
now.GetCurrentTime()
thread_read_state = chat_v1.ThreadReadState(
name="users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState",
last_read_time=now,
)
request = chat_v1.UpdateThreadReadStateRequest(
thread_read_state=thread_read_state,
update_mask={"paths": ["last_read_time"]},
)
response = client.update_thread_read_state(request=request)
print(response)
if __name__ == "__main__":
update_thread_read_state()
이 샘플을 실행하려면 다음을 바꿉니다.
SPACE_NAME: 스페이스의nameID입니다.ListSpaces메서드를 호출하거나 스페이스의 URL에서 ID를 가져올 수 있습니다.THREAD_NAME: 대화목록의nameID입니다. Chat API를 사용하여 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 ID를 가져오거나 생성 시 메시지에 할당된 커스텀 이름을 사용하여 ID를 가져올 수 있습니다.
Chat API는 지정된 대화목록 읽기 상태를 업데이트하고 ThreadReadState 인스턴스를 반환합니다.