با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این راهنما نحوه استفاده از متد findDirectMessage() را در منبع Space Google Chat API برای دریافت جزئیات در مورد فضای پیام مستقیم (DM) توضیح میدهد.
منبع Space مکانی را نشان میدهد که در آن افراد و برنامههای چت میتوانند پیام ارسال کنند، فایلها را به اشتراک بگذارند و با هم همکاری کنند. چند نوع فضا وجود دارد:
پیامهای مستقیم (DM) مکالمات بین دو کاربر یا یک کاربر و یک برنامه چت هستند.
چت گروهی مکالمه بین سه یا چند کاربر و برنامه های چت است.
فضاهای نامگذاری شده مکانهایی هستند که افراد در آن پیام میفرستند، فایلها را به اشتراک میگذارند و همکاری میکنند.
احراز هویت با احراز هویت برنامه به برنامه Chat امکان میدهد DMهایی را که برنامه Chat در Google Chat به آنها دسترسی دارد دریافت کند (برای مثال، پیامهایی که عضو آن است). احراز هویت با احراز هویت کاربر ، DM هایی را که کاربر احراز هویت شده به آنها دسترسی دارد، برمی گرداند.
اعتبارنامه دسترسی را بر اساس نحوه احراز هویت در درخواست Google Chat API خود ایجاد کنید:
برای احراز هویت به عنوان کاربر چت، اعتبار شناسه مشتری OAuth را ایجاد کنید و اعتبارنامه ها را به عنوان یک فایل JSON با نام credentials.json در فهرست محلی خود ذخیره کنید.
برای احراز هویت بهعنوان برنامه چت، اعتبار حساب سرویس ایجاد کنید و اعتبارنامهها را بهعنوان فایل JSON با نام credentials.json ذخیره کنید.
برای پیدا کردن یک پیام مستقیم در Google Chat، موارد زیر را در درخواست خود ارسال کنید:
با احراز هویت برنامه ، محدوده مجوز chat.bot را مشخص کنید. با احراز هویت کاربر ، محدوده مجوز chat.spaces.readonly یا chat.spaces را مشخص کنید.
متد FindDirectMessage() را فراخوانی کنید و name کاربر دیگر را در DM ارسال کنید تا برگردد. با احراز هویت کاربر ، این روش یک DM بین کاربر تماسگیرنده و کاربر مشخص شده برمیگرداند. با احراز هویت برنامه ، این روش یک DM بین برنامه تماس گیرنده و کاربر مشخص شده برمی گرداند.
برای افزودن یک کاربر انسانی بهعنوان عضو فضایی، users/{user} را مشخص کنید، جایی که {user} یا {person_id} برای person از People API یا شناسه یک user در Directory API است. برای مثال، اگر People API person resourceNamepeople/123456789 باشد، میتوانید کاربر را با عضویت با users/123456789 بهعنوان member.name .name به فضا اضافه کنید.
import{createClientWithUserCredentials}from'./authentication-utils.js';constUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.spaces.readonly'];// This sample shows how to find a Direct Message space with user credentialasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// Initialize request argument(s)constrequest={// Replace USER_NAME herename:'users/USER_NAME'};// Make the requestconstresponse=awaitchatClient.findDirectMessage(request);// Handle the responseconsole.log(response);}main().catch(console.error);
برای اجرای این نمونه، شناسه فیلد name کاربر را جایگزین USER_NAME کنید.
Chat API نمونهای از Space را برمیگرداند که DM مشخصشده را جزئیات میدهد.
import{createClientWithAppCredentials}from'./authentication-utils.js';// This sample shows how to find a Direct Message space with app credentialasyncfunctionmain(){// Create a clientconstchatClient=createClientWithAppCredentials();// Initialize request argument(s)constrequest={// Replace USER_NAME herename:'users/USER_NAME'};// Make the requestconstresponse=awaitchatClient.findDirectMessage(request);// Handle the responseconsole.log(response);}main().catch(console.error);
برای اجرای این نمونه، شناسه فیلد name کاربر را جایگزین USER_NAME کنید.
Chat API نمونهای از Space را برمیگرداند که DM مشخصشده را جزئیات میدهد.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis guide focuses on utilizing the \u003ccode\u003efindDirectMessage()\u003c/code\u003e method within the Google Chat API to retrieve information about direct messages (DMs).\u003c/p\u003e\n"],["\u003cp\u003eAuthentication can be performed using either app authentication (for Chat app access to DMs) or user authentication (for authenticated user access to DMs).\u003c/p\u003e\n"],["\u003cp\u003eTo find a DM, you need to call the \u003ccode\u003eFindDirectMessage()\u003c/code\u003e method, providing the user's name, and utilize the appropriate authorization scope based on the authentication method.\u003c/p\u003e\n"],["\u003cp\u003eThe response from the Chat API will contain a \u003ccode\u003eSpace\u003c/code\u003e object providing detailed information about the specified DM.\u003c/p\u003e\n"]]],["The guide details using the `findDirectMessage()` method within the Google Chat API to retrieve information about a direct message space. This involves specifying the target user's ID in the request. Authentication can be done via user or app methods, each requiring distinct scopes (`chat.spaces.readonly` or `chat.spaces` for user, and `chat.bot` for app). The process includes creating the appropriate client and sending a request with the user's name; the API returns `Space` details of the DM.\n"],null,["# Find a direct message (DM) space\n\nThis guide explains how to use the\n[`findDirectMessage()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindDirectMessage)\nmethod on the `Space` resource of the Google Chat API to get details about a\ndirect message (DM) space.\n\nThe\n[`Space` resource](/workspace/chat/api/reference/rest/v1/spaces)\nrepresents a place where people and Chat apps can send messages,\nshare files, and collaborate. There are several types of spaces:\n\n- Direct messages (DMs) are conversations between two users or a user and a Chat app.\n- Group chats are conversations between three or more users and Chat apps.\n- Named spaces are persistent places where people send messages, share files, and collaborate.\n\nWhen a Google Workspace administrator\n[installs a Chat app for their entire\nGoogle Workspace organization](https://support.google.com/a/answer/172482),\nGoogle Chat creates a DM between the installed\nChat app and each user in the organization.\n\nAuthenticating with\n[app authentication](/workspace/chat/authenticate-authorize-chat-app)\nlets a Chat app get DMs that the\nChat app has access to in Google Chat\n(for example, DMs it's a member of). Authenticating with\n[user authentication](/workspace/chat/authenticate-authorize-chat-user) returns\nDMs that the authenticated user has access to.\n\nPrerequisites\n-------------\n\n\n### Node.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - Create access credentials based on how you want to authenticate in your Google Chat API request:\n - To authenticate as a Chat user, [create OAuth client ID\n credentials](/workspace/chat/authenticate-authorize-chat-user) and save the credentials as a JSON file named `credentials.json` to your local directory.\n - To authenticate as the Chat app, [create service account\n credentials](/workspace/chat/authenticate-authorize-chat-app) and save the credentials as a JSON file named `credentials.json`.\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) based on whether you want to authenticate as a user or the Chat app.\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nFind a direct message\n---------------------\n\nTo find a direct message in Google Chat, pass the following in\nyour request:\n\n- With [app authentication](/workspace/chat/authenticate-authorize-chat-app), specify the `chat.bot` authorization scope. With [user authentication](/workspace/chat/authenticate-authorize-chat-user), specify the `chat.spaces.readonly` or `chat.spaces` authorization scope.\n- Call the [`FindDirectMessage()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindDirectMessage), method passing the `name` of the other user in the DM to return. With [user authentication](/workspace/chat/authenticate-authorize-chat-user), this method returns a DM between the calling user and the specified user. With [app authentication](/workspace/chat/authenticate-authorize-chat-app), this method returns a DM between the calling app and the specified user.\n- To add a human user as a space member, specify `users/{user}`, where `{user}` is either the `{person_id}` for the [`person`](/people/api/rest/v1/people) from the People API, or the ID of a [`user`](/workspace/admin/directory/reference/rest/v1/users) in the Directory API. For example, if the People API person `resourceName` is `people/123456789`, you can add the user to the space by including a membership with `users/123456789` as the `member.name`.\n\n### Find a direct message with user authentication\n\nHere's how to find a direct message with\n[user authentication](/workspace/chat/authenticate-authorize-chat-user): \n\n### Node.js\n\nchat/client-libraries/cloud/find-dm-space-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/find-dm-space-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\n\nconst USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces.readonly'];\n\n// This sample shows how to find a Direct Message space with user credential\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // Initialize request argument(s)\n const request = {\n // Replace USER_NAME here\n name: 'users/USER_NAME'\n };\n\n // Make the request\n const response = await chatClient.findDirectMessage(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e with the ID from the\nuser's\n[`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.User.FIELDS.string.google.chat.v1.User.name)\nfield.\n\nThe Chat API returns an instance of\n[`Space`](/workspace/chat/api/reference/rpc/google.chat.v1#space)\nthat details the specified DM.\n\n### Find a direct message with app authentication\n\nHere's how to find a direct message with\n[app authentication](/workspace/chat/authenticate-authorize-chat-app): \n\n### Node.js\n\nchat/client-libraries/cloud/find-dm-space-app-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/find-dm-space-app-cred.js) \n\n```javascript\nimport {createClientWithAppCredentials} from './authentication-utils.js';\n\n// This sample shows how to find a Direct Message space with app credential\nasync function main() {\n // Create a client\n const chatClient = createClientWithAppCredentials();\n\n // Initialize request argument(s)\n const request = {\n // Replace USER_NAME here\n name: 'users/USER_NAME'\n };\n\n // Make the request\n const response = await chatClient.findDirectMessage(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e with the ID from the\nuser's\n[`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.User.FIELDS.string.google.chat.v1.User.name)\nfield.\n\nThe Chat API returns an instance of\n[`Space`](/workspace/chat/api/reference/rpc/google.chat.v1#space)\nthat details the specified DM.\n\nRelated topics\n--------------\n\n- [Create a space](/workspace/chat/create-spaces).\n- [Set up a space](/workspace/chat/set-up-spaces).\n- [Get details about a space](/workspace/chat/get-spaces).\n- [List spaces](/workspace/chat/list-spaces).\n- [Update a space](/workspace/chat/update-spaces).\n- [Delete a space](/workspace/chat/delete-spaces)."]]