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

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

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

  • צ'אטים ישירים הם שיחות בין שני משתמשים או משתמש, אפליקציה ל-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
    

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

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

  • מציינים את היקף ההרשאה chat.spaces.
  • קוראים לפונקציה שיטת 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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.

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

    node update-space.js
    

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