קבלת פרטים על מצב קריאת השרשור של המשתמש

במדריך הזה מוסבר איך להשתמש בשיטה getThreadReadState המשאב ThreadReadState ב-Google Chat API לקבלת פרטים על את מצב הקריאה בשרשור הודעות. כדי לקבל את מצב הקריאה של הודעה מרחב, לראות פרטים על מצב הקריאה במרחב של המשתמש.

משאב אחד (ThreadReadState) הוא משאב סינגלטון שמייצג פרטים על ההודעה האחרונה שנקראה של משתמש מסוים בשרשור הודעות ב-Google Chat.

דרישות מוקדמות

Python

  • Python 3.6 ומעלה
  • הכלי לניהול חבילות pip
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 ומעלה
  • ה-npm כלי לניהול חבילות
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

איך בודקים את מצב הקריאה של השרשור של המשתמש שמתקשר

כדי לקבל פרטים על מצב הקריאה של משתמש בשרשור הודעות, צריך לכלול את הפונקציה הבא בבקשה שלך:

  • צריך לציין chat.users.readstate או chat.users.readstate.readonly את היקף ההרשאה.
  • קוראים לפונקציה אמצעי תשלום אחד (getThreadReadState) ב משאב ThreadReadState.
  • צריך להעביר את name של מצב הקריאה של השרשור כדי לקבל, כולל מזהה משתמש או כינוי ומזהה מרחב משותף. קבלת מצב קריאה של שרשורים תומכת רק בקבלת הקריאה של המשתמש ששלח את הקריאה, שמצוין על ידי הגדרה של אחד הבאים:
    • כתובת האימייל החלופית me. לדוגמה, users/me/spaces/SPACE/threads/THREAD/threadReadState.
    • כתובת האימייל של המשתמש שמתקשר ב-Workspace. לדוגמה, users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState.
    • מזהה המשתמש של המשתמש שמתקשר. לדוגמה, users/USER/spaces/SPACE/threads/THREAD/threadReadState.

בדוגמה הבאה רואים את מצב הקריאה של השרשור של המשתמש שמתקשר:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_threadReadState_get.py
  2. צריך לכלול את הקוד הבא ב-chat_threadReadState_get.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.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()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_threadReadState_get.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם chat_threadReadState_get.js
  2. צריך לכלול את הקוד הבא ב-chat_threadReadState_get:

    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);
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    node chat_threadReadState_get.js
    

Apps Script

בדוגמה הזו מתבצעת קריאה ל-Chat API באמצעות שירות Chat מתקדם.

  1. מוסיפים את היקף ההרשאה chat.users.readstate.readonly אל הקובץ appsscript.json של פרויקט Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. מוסיפים פונקציה כמו זו לפרויקט Apps Script קוד:

    /**
    * 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 מקבל את מצב הקריאה בשרשור שצוין ומחזיר מופע של משאב אחד (ThreadReadState).