این راهنما نحوه استفاده از روش getSpaceReadState
را در منبع SpaceReadState
در Google Chat API برای دریافت جزئیات در مورد وضعیت خواندن کاربر در یک فضا توضیح میدهد. برای دریافت وضعیت خواندن یک پیام در یک رشته پیام، به دریافت جزئیات در مورد وضعیت خواندن رشته کاربر مراجعه کنید.
منبع SpaceReadState
یک منبع تکی است که جزئیات مربوط به آخرین پیام خوانده شده کاربر مشخص شده را در فضای چت Google نشان می دهد.
پیش نیازها
پایتون
- یک حساب Google Workspace تجاری یا سازمانی با دسترسی به Google Chat .
- محیط خود را تنظیم کنید:
- یک پروژه Google Cloud ایجاد کنید .
- صفحه رضایت OAuth را پیکربندی کنید .
- Google Chat API را با نام، نماد و توضیحات برای برنامه Chat خود فعال و پیکربندی کنید .
- کتابخانه Google API Client Python را نصب کنید.
- شناسه مشتری OAuth را برای یک برنامه دسکتاپ ایجاد کنید . برای اجرای نمونه در این راهنما، اعتبارنامه ها را به عنوان یک فایل JSON با نام
client_secrets.json
در فهرست محلی خود ذخیره کنید.
- محدوده مجوزی را انتخاب کنید که از احراز هویت کاربر پشتیبانی می کند.
Node.js
- یک حساب Google Workspace تجاری یا سازمانی با دسترسی به Google Chat .
- محیط خود را تنظیم کنید:
- یک پروژه Google Cloud ایجاد کنید .
- صفحه رضایت OAuth را پیکربندی کنید .
- Google Chat API را با نام، نماد و توضیحات برای برنامه Chat خود فعال و پیکربندی کنید .
- Node.js Google API Client Library را نصب کنید.
- شناسه مشتری OAuth را برای یک برنامه دسکتاپ ایجاد کنید . برای اجرای نمونه در این راهنما، اعتبارنامه ها را به عنوان یک فایل JSON با نام
client_secrets.json
در فهرست محلی خود ذخیره کنید.
- محدوده مجوزی را انتخاب کنید که از احراز هویت کاربر پشتیبانی می کند.
اسکریپت برنامه ها
- یک حساب Google Workspace تجاری یا سازمانی با دسترسی به Google Chat .
- محیط خود را تنظیم کنید:
- یک پروژه Google Cloud ایجاد کنید .
- صفحه رضایت OAuth را پیکربندی کنید .
- Google Chat API را با نام، نماد و توضیحات برای برنامه Chat خود فعال و پیکربندی کنید .
- یک پروژه Apps Script مستقل ایجاد کنید و سرویس گفتگوی پیشرفته را روشن کنید.
- محدوده مجوزی را انتخاب کنید که از احراز هویت کاربر پشتیبانی می کند.
وضعیت خواندن فضای کاربر تماس گیرنده را دریافت کنید
برای دریافت جزئیات در مورد وضعیت خواندن کاربر در یک فاصله، موارد زیر را در درخواست خود بگنجانید:
- محدوده مجوز
chat.users.readstate
یاchat.users.readstate.readonly
را مشخص کنید. - متد
getSpaceReadState
را در منبعSpaceReadState
فراخوانی کنید. -
name
وضعیت خواندن فاصله را برای دریافت ارسال کنید، که شامل شناسه کاربری یا نام مستعار و شناسه فاصله است. دریافت وضعیت خواندن فضا فقط از دریافت وضعیت خواندن کاربر تماسگیرنده پشتیبانی میکند که با تنظیم یکی از موارد زیر میتوان آن را تعیین کرد:-
me
مستعار من برای مثال،users/me/spaces/ SPACE /spaceReadState
. - آدرس ایمیل کاربر تماس گیرنده Workspace. به عنوان مثال،
users/user@example.com/spaces/ SPACE /spaceReadState
. - شناسه کاربری کاربر تماس گیرنده. به عنوان مثال،
users/ USER /spaces/ SPACE /spaceReadState
.
-
مثال زیر وضعیت خواندن فضای کاربر فراخوان را دریافت می کند:
پایتون
- در پوشه کاری خود، فایلی به نام
chat_spaceReadState_get.py
ایجاد کنید. کد زیر را در
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()
در کد زیر را جایگزین کنید:
-
SPACE
: نام فضایی است که می توانید از روشspaces.list
در Chat API یا از URL یک فضا دریافت کنید.
-
در پوشه کاری خود، نمونه را بسازید و اجرا کنید:
python3 chat_spaceReadState_get.py
Node.js
- در پوشه کاری خود، یک فایل با نام
chat_spaceReadState_get.js
ایجاد کنید. کد زیر را در
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);
در کد زیر را جایگزین کنید:
-
SPACE
: نام فضایی است که می توانید از روشspaces.list
در Chat API یا از URL یک فضا دریافت کنید.
-
در پوشه کاری خود، نمونه را بسازید و اجرا کنید:
node chat_spaceReadState_get.js
اسکریپت برنامه ها
این مثال Chat API را با استفاده از Advanced Chat Service فراخوانی میکند.
دامنه مجوز
chat.users.readstate.readonly
را به فایلappsscript.json
پروژه Apps Script اضافه کنید:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
تابعی مانند این را به کد پروژه 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
را برمی گرداند.
موضوعات مرتبط
- وضعیت خواندن فضای کاربر تماس گیرنده را به روز کنید .
- وضعیت خواندن رشته کاربر تماسگیرنده را دریافت کنید .