Panduan ini menjelaskan cara menggunakan metode updateSpaceReadState
pada
Resource SpaceReadState
Google Chat API untuk menandai ruang sebagai telah dibaca atau belum dibaca.
Tujuan
Referensi SpaceReadState
adalah resource singleton yang mewakili detail tentang
pesan yang terakhir dibaca pengguna tertentu di ruang Google Chat.
Prasyarat
Python
- Sebuah Business atau Enterprise Akun Google Workspace yang memiliki akses ke Google Chat.
- Menyiapkan lingkungan Anda:
- Buat project Google Cloud.
- Konfigurasi layar izin OAuth.
- Aktifkan dan konfigurasikan Google Chat API dengan nama, ikon, dan deskripsi untuk aplikasi Chat Anda.
- Instal Python Library Klien Google API.
-
Membuat kredensial client ID OAuth untuk aplikasi desktop. Untuk menjalankan
sampel dalam
panduan, simpan kredensial sebagai file JSON bernama
client_secrets.json
ke direktori lokal.
- Pilih cakupan otorisasi yang mendukung autentikasi pengguna.
Node.js
- Sebuah Business atau Enterprise Akun Google Workspace yang memiliki akses ke Google Chat.
- Menyiapkan lingkungan Anda:
- Buat project Google Cloud.
- Konfigurasi layar izin OAuth.
- Aktifkan dan konfigurasikan Google Chat API dengan nama, ikon, dan deskripsi untuk aplikasi Chat Anda.
- Instal Node.js Library Klien Google API.
-
Membuat kredensial client ID OAuth untuk aplikasi desktop. Untuk menjalankan
sampel dalam
panduan, simpan kredensial sebagai file JSON bernama
client_secrets.json
ke direktori lokal.
- Pilih cakupan otorisasi yang mendukung autentikasi pengguna.
Apps Script
- Sebuah Business atau Enterprise Akun Google Workspace yang memiliki akses ke Google Chat.
- Menyiapkan lingkungan Anda:
- Buat project Google Cloud.
- Konfigurasi layar izin OAuth.
- Aktifkan dan konfigurasikan Google Chat API dengan nama, ikon, dan deskripsi untuk aplikasi Chat Anda.
- Membuat project Apps Script mandiri, dan aktifkan Layanan Chat Lanjutan.
- Pilih cakupan otorisasi yang mendukung autentikasi pengguna.
Memperbarui status pembacaan ruang pengguna yang melakukan panggilan
Untuk memperbarui status baca pengguna dalam ruang, sertakan baris berikut di permintaan Anda:
- Tentukan cakupan otorisasi
chat.users.readstate
. - Panggil
Metode
updateSpaceReadState
di ResourceSpaceReadState
. - Teruskan
name
status baca ruang yang akan didapatkan, yang mencakup ID pengguna atau alias dan ID ruang. Mendapatkan status baca ruang hanya mendukung mendapatkan operasi baca pengguna yang menelepon, yang bisa ditetapkan dengan mengatur salah satu status berikut ini:- Alias
me
. Misalnya,users/me/spaces/SPACE/spaceReadState
. - Alamat email Workspace pengguna yang menelepon. Misalnya,
users/user@example.com/spaces/SPACE/spaceReadState
. - ID pengguna pengguna yang menelepon. Misalnya,
users/USER/spaces/SPACE/spaceReadState
.
- Alias
- Teruskan
updateMask
, yang menentukan aspek status operasi baca ruang ke yang mendukung jalur kolom berikut:lastReadTime
: Waktu saat status baca ruang pengguna diperbarui. Biasanya ini berkaitan dengan stempel waktu pesan yang terakhir dibaca, atau stempel waktu yang ditentukan oleh pengguna untuk menandai posisi terakhir dibaca spasi. JikalastReadTime
jatuh sebelum waktu pembuatan pesan terbaru, muncul sebagai belum dibaca di UI. Untuk menandai ruang sebagai telah dibaca, setellastReadTime
ke nilai apa pun setelahnya (lebih besar) dari pesan terbaru yang dibuat baik.lastReadTime
dikonversi agar cocok dengan waktu pembuatan pesan terbaru. Perhatikan bahwa status baca ruang hanya memengaruhi status dibaca pesan yang terlihat dalam percakapan tingkat teratas ruang tersebut. Balasan dalam rangkaian pesan tidak terpengaruh oleh stempel waktu ini, dan sebagai gantinya bergantung pada status pembacaan thread.
Contoh berikut memperbarui status pembacaan ruang pengguna yang melakukan panggilan:
Python
- Di direktori kerja, buat file bernama
chat_spaceReadState_update.py
. Sertakan kode berikut di
chat_spaceReadState_update.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # 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.users.readstate"] def main(): ''' Authenticates with Chat API via user credentials, then updates the space read state for the calling user. ''' # 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.users().spaces().updateSpaceReadState( # The space read state to update. # # Replace USER with the calling user's ID, Workspace email, # or the alias me. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='users/me/spaces/SPACE/spaceReadState', updateMask='lastReadTime', body={'lastReadTime': f'{datetime.datetime(2000, 1, 3).isoformat()}Z'} ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
Dalam kode, ganti kode berikut:
SPACE
: nama ruang, yang yang dapat diperoleh dari Metodespaces.list
di Chat API, atau dari URL ruang.
Dalam direktori kerja, build dan jalankan contoh:
python3 chat_spaceReadState_update.py
Node.js
- Di direktori kerja, buat file bernama
chat_spaceReadState_update.js
. Sertakan kode berikut di
chat_spaceReadState_update
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @return {!Promise<!Object>} */ async function updateSpaceReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.users.spaces.updateSpaceReadState({ /** * The space read state to update. * * Replace USER with the calling user's ID, Workspace email, * or the alias me. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. */ name: 'users/me/spaces/SPACE/spaceReadState', updateMask: 'lastReadTime', requestBody: { lastReadTime: '{datetime.datetime(2000, 1, 3).isoformat()}Z' } }); } /** * Use the service endpoint to call Chat API. */ getSpaceReadState().then(console.log);
Dalam kode, ganti kode berikut:
SPACE
: nama ruang, yang yang dapat diperoleh dari Metodespaces.list
di Chat API, atau dari URL ruang.
Dalam direktori kerja, build dan jalankan contoh:
node chat_spaceReadState_update.js
Apps Script
Contoh ini memanggil Chat API menggunakan Layanan Chat Lanjutan.
Tambahkan cakupan otorisasi
chat.users.readstate
ke Fileappsscript.json
project Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate" ]
Tambahkan fungsi seperti ini ke project Apps Script kode:
/** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @param {string} spaceReadStateName The resource name of the space read state. */ function updateSpaceReadState(spaceReadStateName) { try { const time = new Date('January 1, 2000')).toJSON(); const body = {'lastReadTime': time}; Chat.Users.Spaces.updateSpaceReadState(spaceReadStateName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to update read state with error %s', err.message); } }
Google Chat API mengupdate status pembacaan ruang yang ditentukan dan menampilkan
contoh dari
Referensi SpaceReadState
.
Topik terkait
- Mendapatkan status pembacaan ruang pengguna yang melakukan panggilan.
- Mendapatkan status pembacaan thread pengguna yang pemanggil.