AI-generated Key Takeaways
-  This guide focuses on utilizing the findDirectMessage()method within the Google Chat API to retrieve information about direct messages (DMs).
-  Authentication can be performed using either app authentication (for Chat app access to DMs) or user authentication (for authenticated user access to DMs). 
-  To find a DM, you need to call the FindDirectMessage()method, providing the user's name, and utilize the appropriate authorization scope based on the authentication method.
-  The response from the Chat API will contain a Spaceobject providing detailed information about the specified DM.
This guide explains how to use the  findDirectMessage() 
 
method on the Space 
resource of the Google Chat API to get details about a
direct message (DM) space.
The  Space 
resource 
represents a place where people and Chat apps can send messages,
share files, and collaborate. There are several types of spaces:
- Direct messages (DMs) are conversations between two users or a user and a Chat app.
- Group chats are conversations between three or more users and Chat apps.
- Named spaces are persistent places where people send messages, share files, and collaborate.
When a Google Workspace administrator installs a Chat app for their entire Google Workspace organization , Google Chat creates a DM between the installed Chat app and each user in the organization.
Authenticating with app authentication lets a Chat app get DMs that the Chat app has access to in Google Chat (for example, DMs it's a member of). Authenticating with user authentication returns DMs that the authenticated user has access to.
Prerequisites
Node.js
- A Business or Enterprise Google Workspace account with access to Google Chat .
- Set up your environment: - Create a Google Cloud project .
- Configure the OAuth consent screen .
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Node.js Cloud Client Library .
- Create access credentials based on how you want to authenticate in your Google Chat API
        request: - To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named credentials.jsonto your local directory.
- To authenticate as the Chat app, create service account
            credentials 
and save the credentials as a JSON file named credentials.json.
 
- To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named 
 
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Find a direct message
To find a direct message in Google Chat, pass the following in your request:
- With app authentication 
,
specify the chat.botauthorization scope. With user authentication , specify thechat.spaces.readonlyorchat.spacesauthorization scope.
- Call the  FindDirectMessage(), method passing thenameof the other user in the DM to return. With user authentication , this method returns a DM between the calling user and the specified user. With app authentication , this method returns a DM between the calling app and the specified user.
- To add a human user as a space member, specify users/{user}, where{user}is either the{person_id}for thepersonfrom the People API, or the ID of auserin the Directory API. For example, if the People API personresourceNameispeople/123456789, you can add the user to the space by including a membership withusers/123456789as themember.name.
Find a direct message with user authentication
Here's how to find a direct message with user authentication :
Node.js
To run this sample, replace  USER_NAME 
 
with the ID from the
user's  name 
 
field.
The Chat API returns an instance of  Space 
 
that details the specified DM.
Find a direct message with app authentication
Here's how to find a direct message with app authentication :
Node.js
To run this sample, replace  USER_NAME 
 
with the ID from the
user's  name 
 
field.
The Chat API returns an instance of  Space 
 
that details the specified DM.

