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

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

המשאב SpaceReadState הוא משאב בודד ב-Google Chat, שמייצג פרטים על ההודעה האחרונה שהמשתמש קרא במרחב המשותף ב-Google Chat.

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

Python

  • Python 3.6 ואילך
  • הכלי pip לניהול חבילות
  • ספריות הלקוח העדכניות של Google ל-Python. כדי להתקין או לעדכן אותן, מריצים את הפקודה הבאה בממשק שורת הפקודה:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • פרויקט ב-Google Cloud עם ממשק Google Chat API פעיל ומוגדר. במאמר איך יוצרים אפליקציה ל-Google Chat מוסבר איך עושים זאת.
  • הוגדרה הרשאה לאפליקציית Chat. כדי לקבל פרטים על מצב הקריאה של משתמש במרחב משותף מסוים, צריך אימות משתמש באמצעות היקף ההרשאות chat.users.readstate או chat.users.readstate.readonly.

Node.js

  • Node.js ו-npm
  • ספריות הלקוח העדכניות של Google ל-Node.js. כדי להתקין אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • פרויקט ב-Google Cloud עם ממשק Google Chat API פעיל ומוגדר. במאמר איך יוצרים אפליקציה ל-Google Chat מוסבר איך עושים זאת.
  • הוגדרה הרשאה לאפליקציית Chat. כדי לקבל פרטים על מצב הקריאה של משתמש במרחב משותף מסוים, צריך אימות משתמש באמצעות היקף ההרשאות chat.users.readstate או chat.users.readstate.readonly.

Apps Script

  • חשבון Google Workspace עם גישה ל-Google Chat.
  • אפליקציה שפורסמה ב-Chat. כדי ליצור אפליקציה ל-Chat, תוכלו להיעזר בquickstart.
  • הוגדרה הרשאה לאפליקציית Chat. כדי לקבל פרטים על מצב הקריאה של משתמש במרחב משותף מסוים, צריך אימות משתמש באמצעות היקף ההרשאות chat.users.readstate או chat.users.readstate.readonly.

בדיקת מצב הקריאה במרחב המשותף של המשתמש המתקשר

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

  • מציינים את היקף ההרשאה chat.users.readstate או chat.users.readstate.readonly.
  • קוראים ל-method getSpaceReadState במשאב SpaceReadState.
  • מעבירים את name של מצב הקריאה של המרחב המשותף, כולל מזהה משתמש או כינוי ומזהה מרחב משותף. קבלת מצב קריאת מרחב תומכת רק בקבלת מצב הקריאה של המשתמש שמתקשר. אפשר לבחור את אחת מהאפשרויות הבאות:
    • כתובת האימייל החלופית של me. לדוגמה, users/me/spaces/SPACE/spaceReadState.
    • כתובת האימייל של המשתמש המתקשר ב-Workspace. לדוגמה, users/user@example.com/spaces/SPACE/spaceReadState.
    • מזהה המשתמש של המשתמש שמתקשר. לדוגמה, users/USER/spaces/SPACE/spaceReadState.

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

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_spaceReadState_get.py.
  2. יש לכלול את הקוד הבא ב-chat_spaceReadState_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 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().getSpaceReadState(
    
            # The space 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/spaceReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם של מרחב משותף, שאפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_spaceReadState_get.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the space read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getSpaceReadState() {
    
      /**
      * 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.getSpaceReadState({
    
        /**
        * The space 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/spaceReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getSpaceReadState().then(console.log);
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם של מרחב משותף, שאפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    node chat_spaceReadState_get.js
    

Apps Script

הדוגמה הזו מפעילה את Chat API באמצעות Advanced Chat Service.

  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 space read state for the calling user.
    * @param {string} spaceReadStateName The resource name of the space read state.
    */
    function getSpaceReadState(spaceReadStateName) {
      try {
        Chat.Users.Spaces.getSpaceReadState(spaceReadStateName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get read state with error %s', err.message);
      }
    }
    

ה-Google Chat API מקבל את מצב הקריאה שצוין במרחב המשותף ומחזיר מופע של משאב SpaceReadState.