این راهنما نحوه استفاده از روش getThreadReadState
را در منبع ThreadReadState
در Google Chat API برای دریافت جزئیات در مورد وضعیت خواندن کاربر در یک رشته پیام توضیح می دهد. برای دریافت وضعیت خواندن پیام در یک فاصله، به دریافت جزئیات در مورد وضعیت خواندن فضای کاربر مراجعه کنید.
منبع ThreadReadState
یک منبع تکی است که جزئیات مربوط به آخرین پیام خوانده شده کاربر مشخص شده را در یک رشته پیام چت 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
را مشخص کنید. - متد
getThreadReadState
را در منبعThreadReadState
فراخوانی کنید. -
name
وضعیت خواندن رشته را برای دریافت ارسال کنید، که شامل شناسه کاربری یا نام مستعار و شناسه فاصله است. دریافت وضعیت خواندن رشته فقط از دریافت وضعیت خواندن کاربر تماس گیرنده پشتیبانی می کند که با تنظیم یکی از موارد زیر می توان آن را تعیین کرد:-
me
مستعار من به عنوان مثال،users/me/spaces/ SPACE /threads/ THREAD /threadReadState
. - آدرس ایمیل کاربر تماس گیرنده Workspace. برای مثال،
users/user@example.com/spaces/ SPACE threads/ THREAD /threadReadState
. - شناسه کاربری کاربر تماس گیرنده. برای مثال،
users/ USER /spaces/ SPACE /threads/ THREAD /threadReadState
.
-
مثال زیر وضعیت خواندن نخ کاربر فراخوان را دریافت می کند:
پایتون
- در پوشه کاری خود، فایلی با نام
chat_threadReadState_get.py
ایجاد کنید. کد زیر را در
chat_threadReadState_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 thread 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().threads().getThreadReadState( # The thread 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. # # Replace THREAD with a thread name. # Obtain the thread name from the messages resource of Chat API. name='users/me/spaces/SPACE/threads/THREAD/threadReadState' ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
در کد زیر را جایگزین کنید:
SPACE
: نام فضایی است که می توانید از روشspaces.list
در Chat API یا از URL یک فضا دریافت کنید.THREAD
: نام رشته ای که می توانید از متدspaces.messages.get
در Chat API بدست آورید.
در پوشه کاری خود، نمونه را بسازید و اجرا کنید:
python3 chat_threadReadState_get.py
Node.js
- در پوشه کاری خود، یک فایل با نام
chat_threadReadState_get.js
ایجاد کنید. کد زیر را در
chat_threadReadState_get
قرار دهید:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @return {!Promise<!Object>} */ async function getThreadReadState() { /** * 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.threads.getThreadReadState({ /** * The thread 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/threads/THREADS/threadReadState' }); } /** * Use the service endpoint to call Chat API. */ getThreadReadState().then(console.log);
در کد زیر را جایگزین کنید:
SPACE
: نام فضایی است که می توانید از روشspaces.list
در Chat API یا از URL یک فضا دریافت کنید.THREAD
: نام رشته ای که می توانید از متدspaces.messages.get
در Chat API بدست آورید.
در پوشه کاری خود، نمونه را بسازید و اجرا کنید:
node chat_threadReadState_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 thread read state for the calling user. * @param {string} threadReadStateName The resource name of the thread read state. */ function getThreadReadState(threadReadStateName) { try { Chat.Users.Spaces.Threads.getThreadReadState(threadReadStateName); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to get read state with error %s', err.message); } }
Google Chat API وضعیت خواندن رشته مشخص شده را دریافت می کند و نمونه ای از منبع ThreadReadState
را برمی گرداند.
موضوعات مرتبط
- وضعیت خواندن فضای کاربر تماس گیرنده را به روز کنید .
- وضعیت خواندن فضای کاربر تماس گیرنده را دریافت کنید .