עדכון מרחב משותף

במדריך הזה מוסבר איך משתמשים ב-method patch במשאב Space של Google Chat API כדי לעדכן מרחבים משותפים. אתם יכולים לעדכן את המרחב המשותף כדי לשנות את המאפיינים שלו, כמו השם המוצג, התיאור וההנחיות שלו.

המשאב Space מייצג מקום שבו אנשים ואפליקציות ל-Chat יכולים לשלוח הודעות, לשתף קבצים ולעבוד יחד. יש כמה סוגים של מרחבים משותפים:

  • צ'אטים אישיים (DM) הם שיחות בין שני משתמשים או משתמש ואפליקציית 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.spaces.

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.spaces.

איך משנים את המרחב המשותף

כדי לעדכן מרחב משותף קיים ב-Google Chat, מעבירים את הבקשה הבאה:

  • מציינים את היקף ההרשאה chat.spaces.
  • קוראים ל-method patch במשאב Space. בבקשה מציינים את השדה name של המרחב המשותף, את השדה updateMask עם שדה אחד או יותר לעדכון, ואת השדה body עם פרטי המרחב המשותף.

אפשר לעדכן פרטים כמו השם המוצג, סוג המרחב, מצב ההיסטוריה ועוד. כדי לראות את כל השדות שאפשר לעדכן, עיינו במאמרי העזרה.

כך מעדכנים את השדה spaceDetails במרחב משותף קיים:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_space_update.py.
  2. צריך לכלול את הקוד הבא ב-chat_space_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.spaces"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates the specified space description and guidelines.
        '''
    
        # 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.spaces().patch(
    
          # The space to update, and the updated space details.
          #
          # Replace {space} with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          name='spaces/SPACE',
          updateMask='spaceDetails',
          body={
    
            'spaceDetails': {
              'description': 'This description was updated with Chat API!',
              'guidelines': 'These guidelines were updated with Chat API!'
            }
    
          }
    
        ).execute()
    
        # Prints details about the updated space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאפשר לקבל באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.

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

    python3 chat_space_update.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a Chat space with the description and guidelines.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.patch({
        name: 'spaces/SPACE',
        updateMask: 'spaceDetails',
        requestBody: {
          spaceDetails: {
            description: 'This description was updated with Chat API!',
            guidelines: 'These guidelines were updated with Chat API!'
          },
        }
      });
    }
    
    updateSpace().then(console.log);
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאפשר לקבל באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.

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

    node update-space.js
    

ב-Google Chat API מוחזר מופע של המשאב Space שמשקף את העדכונים.