Bu kılavuzda, bir kullanıcının mesaj dizisindeki okuma durumu hakkında ayrıntılı bilgi edinmek için Google Chat API'nin ThreadReadState
kaynağında getThreadReadState
yönteminin nasıl kullanılacağı açıklanmaktadır. Bir alandaki mesajın okunma durumunu öğrenmek için Bir kullanıcının alan okuma durumuyla ilgili ayrıntıları alma bölümüne bakın.
ThreadReadState
kaynağı, belirli bir kullanıcının Google Chat mesaj dizisinde son okunan mesajıyla ilgili ayrıntıları temsil eden bir tekil kaynaktır.
Ön koşullar
Python
- Python 3.6 veya sonraki sürümler
- pip paket yönetimi aracı
Python için en yeni Google istemci kitaplıkları. Bunları yüklemek veya güncellemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için
chat.users.readstate
veyachat.users.readstate.readonly
yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.
Node.js
- Node.js ve npm
Node.js için en yeni Google istemci kitaplıkları. Bunları yüklemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:
npm install @google-cloud/local-auth @googleapis/chat
- Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için
chat.users.readstate
veyachat.users.readstate.readonly
yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.
Apps Komut Dosyası
- Google Chat'e erişimi olan bir Google Workspace hesabı.
- Yayınlanmış bir Chat uygulamasıdır. Chat uygulaması oluşturmak için bu quickstart takip edin.
- Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için
chat.users.readstate
veyachat.users.readstate.readonly
yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.
Arayan kullanıcının ileti dizisi okunma durumunu alma
Bir mesaj dizisindeki bir kullanıcının okunma durumuyla ilgili ayrıntıları almak için isteğinize aşağıdakileri ekleyin:
chat.users.readstate
veyachat.users.readstate.readonly
yetkilendirme kapsamını belirtin.ThreadReadState
kaynağındagetThreadReadState
yöntemini çağırın.- Almak için iş parçacığı okuma durumunun
name
parametresini iletin. Bu, bir kullanıcı kimliği veya takma ad ve bir boşluk kimliği içerir. İş parçacığı okuma durumunun alınması yalnızca çağrı yapan kullanıcının okuma durumunun alınmasını destekler. Bu durum, aşağıdakilerden birini ayarlayarak belirtilebilir:me
takma adı. Örneğin,users/me/spaces/SPACE/threads/THREAD/threadReadState
.- Arayan kullanıcının Workspace e-posta adresi. Örneğin,
users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
. - Arayan kullanıcının kimliği. Örneğin,
users/USER/spaces/SPACE/threads/THREAD/threadReadState
.
Aşağıdaki örnekte, arayan kullanıcının ileti dizisi okuma durumu verilmiştir:
Python
- Çalışma dizininizde
chat_threadReadState_get.py
adında bir dosya oluşturun. chat_threadReadState_get.py
içine şu kodu ekleyin: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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets the thread 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().threads().getThreadReadState( # The thread read state to get. # # 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. # # Replace THREAD with a thread name. # Obtain the thread name from the messages resource of Chat API. name='users/me/spaces/SPACE/threads/THREAD/threadReadState' ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
Kodda aşağıdakileri değiştirin:
SPACE
: Chat API'dekispaces.list
yönteminden veya alanın URL'sinden edinebileceğiniz bir alan adıdır.THREAD
: Chat API'dekispaces.messages.get
yönteminden edinebileceğiniz bir ileti dizisi adıdır.
Çalışma dizininizde örneği derleyip çalıştırın:
python3 chat_threadReadState_get.py
Node.js
- Çalışma dizininizde
chat_threadReadState_get.js
adında bir dosya oluşturun. chat_threadReadState_get
içine şu kodu ekleyin:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @return {!Promise<!Object>} */ async function getThreadReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate.readonly', ]; 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.threads.getThreadReadState({ /** * The thread read state to get. * * 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/threads/THREADS/threadReadState' }); } /** * Use the service endpoint to call Chat API. */ getThreadReadState().then(console.log);
Kodda aşağıdakileri değiştirin:
SPACE
: Chat API'dekispaces.list
yönteminden veya alanın URL'sinden edinebileceğiniz bir alan adıdır.THREAD
: Chat API'dekispaces.messages.get
yönteminden edinebileceğiniz bir ileti dizisi adıdır.
Çalışma dizininizde örneği derleyip çalıştırın:
node chat_threadReadState_get.js
Apps Komut Dosyası
Bu örnek, Gelişmiş Sohbet Hizmeti'ni kullanan Chat API'yi çağırır.
chat.users.readstate.readonly
yetkilendirme kapsamını, Apps Komut Dosyası projesininappsscript.json
dosyasına ekleyin:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
Apps Komut Dosyası projesinin koduna şunun gibi bir işlev ekleyin:
/** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @param {string} threadReadStateName The resource name of the thread read state. */ function getThreadReadState(threadReadStateName) { try { Chat.Users.Spaces.Threads.getThreadReadState(threadReadStateName); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to get read state with error %s', err.message); } }
Google Chat API, belirtilen iş parçacığı okuma durumunu alır ve ThreadReadState
kaynağının bir örneğini döndürür.