במדריך הזה מוסבר איך להשתמש בשיטה updateSpaceReadState
משאב SpaceReadState
ב-Google Chat API לסימון מרחבים ככאלה ש'כבר קראתי' או 'עוד לא קראתי'.
משאב אחד (SpaceReadState
)
הוא משאב סינגלטון שמייצג פרטים על
ההודעה האחרונה שנקראה של משתמש מסוים במרחב ב-Google Chat.
דרישות מוקדמות
Python
- עסק או ארגון חשבון Google Workspace עם גישה אל Google Chat.
- מגדירים את הסביבה:
- יוצרים פרויקט ב-Google Cloud.
- מגדירים את מסך ההסכמה של OAuth.
- מפעילים ומגדירים את Google Chat API עם שם, הסמל והתיאור של אפליקציית Chat.
- להתקין את Python ספריית הלקוח של Google API.
-
יוצרים פרטי כניסה למזהה הלקוח ב-OAuth לאפליקציה למחשב. כדי להריץ את הדוגמה
מדריך, שומרים את פרטי הכניסה כקובץ JSON בשם
client_secrets.json
הספרייה המקומית.
- בוחרים את היקף ההרשאה שתומך באימות משתמש.
Node.js
- עסק או ארגון חשבון Google Workspace עם גישה אל Google Chat.
- מגדירים את הסביבה:
- יוצרים פרויקט ב-Google Cloud.
- מגדירים את מסך ההסכמה של OAuth.
- מפעילים ומגדירים את Google Chat API עם שם, הסמל והתיאור של אפליקציית Chat.
- להתקין את Node.js ספריית הלקוח של Google API.
-
יוצרים פרטי כניסה למזהה הלקוח ב-OAuth לאפליקציה למחשב. כדי להריץ את הדוגמה
מדריך, שומרים את פרטי הכניסה כקובץ JSON בשם
client_secrets.json
הספרייה המקומית.
- בוחרים את היקף ההרשאה שתומך באימות משתמש.
Apps Script
- עסק או ארגון חשבון Google Workspace עם גישה אל Google Chat.
- מגדירים את הסביבה:
- יוצרים פרויקט ב-Google Cloud.
- מגדירים את מסך ההסכמה של OAuth.
- מפעילים ומגדירים את Google Chat API עם שם, הסמל והתיאור של אפליקציית Chat.
- יוצרים פרויקט נפרד ב-Apps Script. ומפעילים את שירות Chat המתקדם.
- בוחרים את היקף ההרשאה שתומך באימות משתמש.
עדכון מצב קריאת המרחב המשותף של המשתמש שמתקשר
כדי לעדכן את מצב הקריאה של משתמש במרחב משותף, צריך לכלול את הפקודה הבאה: הבקשה שלך:
- מציינים את היקף ההרשאה
chat.users.readstate
. - קוראים לפונקציה
שיטת
updateSpaceReadState
ב משאבSpaceReadState
. - צריך להעביר את
name
של מצב הקריאה של המרחב כדי לקבל, כולל מזהה משתמש או כינוי ומזהה מרחב משותף. קבלת מצב קריאת רווח תומכת רק בקבלת הקריאה של המשתמש ששלח את הקריאה, שמצוין על ידי הגדרה של אחד הבאים:- כתובת האימייל החלופית
me
. לדוגמה,users/me/spaces/SPACE/spaceReadState
. - כתובת האימייל של המשתמש שמתקשר ב-Workspace. לדוגמה,
users/user@example.com/spaces/SPACE/spaceReadState
. - מזהה המשתמש של המשתמש שמתקשר. לדוגמה,
users/USER/spaces/SPACE/spaceReadState
.
- כתובת האימייל החלופית
- מעבירים את
updateMask
, שמציין את ההיבטים של מצב הקריאה של המרחב שתומך בנתיבי השדות הבאים:lastReadTime
: השעה שבה מצב הקריאה במרחב של המשתמש עודכן. בדרך כלל היא תואמת לחותמת הזמן של ההודעה האחרונה שנקראה, או חותמת זמן שציין המשתמש כדי לסמן את מיקום הקריאה האחרון המרחב המשותף. כשה-lastReadTime
הוא לפני זמן היצירה של ההודעה האחרונה, הפרמטר המרחב המשותף מופיע בתור 'עוד לא קראתי' בממשק המשתמש. כדי לסמן שהמרחב המשותף נקראו, צריך להגדירlastReadTime
לכל ערך מאוחר יותר (גדול יותר) מיצירת ההודעה האחרונה בזמן האימון. הפונקציהlastReadTime
מאלצת להתאים לשעת היצירה של ההודעה האחרונה. חשוב לזכור שמצב הקריאה של המרחב משפיע רק על מצב הקריאה של הודעות גלויים בשיחה שבמרחב המשותף ברמה העליונה. התשובות בשרשורים לא מושפעים מחותמת הזמן הזו, ומסתמכים במקום זאת על מצב הקריאה של השרשור.
בדוגמה הבאה מעדכנים את מצב קריאת המרחב של המשתמש שמתקשר:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_spaceReadState_update.py
צריך לכלול את הקוד הבא ב-
chat_spaceReadState_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.users.readstate"] def main(): ''' Authenticates with Chat API via user credentials, then updates 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().updateSpaceReadState( # The space read state to update. # # 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', updateMask='lastReadTime', body={'lastReadTime': f'{datetime.datetime(2000, 1, 3).isoformat()}Z'} ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: השם של המרחב המשותף שאפשר לקבל אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_spaceReadState_update.py
Node.js
- בספריית העבודה, יוצרים קובץ בשם
chat_spaceReadState_update.js
צריך לכלול את הקוד הבא ב-
chat_spaceReadState_update
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @return {!Promise<!Object>} */ async function updateSpaceReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate', ]; 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.updateSpaceReadState({ /** * The space read state to update. * * 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', updateMask: 'lastReadTime', requestBody: { lastReadTime: '{datetime.datetime(2000, 1, 3).isoformat()}Z' } }); } /** * Use the service endpoint to call Chat API. */ getSpaceReadState().then(console.log);
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: השם של המרחב המשותף שאפשר לקבל אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
node chat_spaceReadState_update.js
Apps Script
בדוגמה הזו מתבצעת קריאה ל-Chat API באמצעות שירות Chat מתקדם.
מוסיפים את היקף ההרשאה
chat.users.readstate
אל הקובץappsscript.json
של פרויקט Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate" ]
מוסיפים פונקציה כמו זו לפרויקט Apps Script קוד:
/** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @param {string} spaceReadStateName The resource name of the space read state. */ function updateSpaceReadState(spaceReadStateName) { try { const time = new Date('January 1, 2000')).toJSON(); const body = {'lastReadTime': time}; Chat.Users.Spaces.updateSpaceReadState(spaceReadStateName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to update read state with error %s', err.message); } }
Google Chat API מעדכן את מצב קריאת המרחב שצוין ומחזיר
מופע של
משאב אחד (SpaceReadState
).