
In Episode 31 of the Google Workspace Developer News, an interesting announcement removes a major administrative hurdle for Google Apps Script developers interacting with Google Chat.
“You no longer need to manually configure a Google Chat app in the Google Cloud console for integrations that only use the Chat API to read data on behalf of a user using OAuth. For read-only actions, such as getting a space or listing messages, you now only need to enable the Chat API and create an OAuth client.”
The simplified Chat API setup means developers no longer need to configure a full Google Chat App in a standard Google Cloud project just to perform read-only actions. Previously, reading space messages via the Chat Advanced Service required detaching your script from the default GCP project, linking a standard GCP project, configuring an OAuth consent screen and formally enabling the Google Chat API.
Now, retrieving data is immediate. To list your Chat Spaces and read messages, you can add the Chat Advanced Service in your script editor and declare the read-only scopes in your appsscript.json manifest:
{
"timeZone": "Europe/London",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Chat",
"serviceId": "chat",
"version": "v1"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/chat.messages.readonly",
"https://www.googleapis.com/auth/chat.spaces.readonly"
]
}
Once your manifest is configured, you can use the Chat service to list the spaces you are a member of and the most recent messages in a specified space. Here are two example functions you can use:
/**
* Lists the Google Chat spaces the authenticated user is a member of.
* * Prerequisites:
* 1. Enable the Google Chat API in Apps Script Advanced Services.
* 2. Add the following OAuth scope to your appsscript.json manifest:
* "https://www.googleapis.com/auth/chat.spaces.readonly"
*/
function listMySpaces() {
try {
let pageToken = null;
do {
// Call the Chat Advanced Service to list spaces
const response = Chat.Spaces.list({
pageSize: 50, // Adjust to retrieve more or fewer spaces per page
pageToken: pageToken
});
if (!response.spaces || response.spaces.length === 0) {
console.log("No spaces found.");
break;
}
// Iterate through and log the retrieved spaces
for (const space of response.spaces) {
console.log(`Space Name: ${space.displayName || 'Unnamed Space'}`);
console.log(`Resource ID: ${space.name}`);
console.log(`Type: ${space.spaceType}`);
console.log("-----------------------------------------");
}
// Update the page token to retrieve the next page of results
pageToken = response.nextPageToken;
} while (pageToken);
} catch (err) {
// Log any errors that occur during the API call
console.error("Failed to fetch spaces with error: %s", err.message);
}
}
/**
* Lists the most recent messages in a Google Chat space.
* * Prerequisites:
* 1. Enable the Google Chat API in Apps Script Advanced Services.
* 2. Add the following OAuth scope to your appsscript.json manifest:
* "https://www.googleapis.com/auth/chat.messages.readonly"
* * @param {string} spaceName The resource name of the space (e.g., "spaces/AAAAAAAAAAA").
*/
function listSpaceMessages(spaceName = "spaces/YOUR_SPACE_ID") {
try {
// Call the Chat Advanced Service to list messages
const response = Chat.Spaces.Messages.list(spaceName, {
pageSize: 10 // Adjust to retrieve more or fewer messages
});
// Check if the space has any messages
if (!response.messages || response.messages.length === 0) {
console.log("No messages found in this space.");
return;
}
// Iterate through and log the retrieved messages
for (const message of response.messages) {
// Note: When authenticating as a user rather than a Chat app,
// the API only returns the sender's 'name' (resource ID) and 'type'.
// Cross-reference this ID using the Admin SDK Directory or People API to retrieve display names.
console.log(`Sender ID: ${message.sender ? message.sender.name : 'Unknown'}`);
console.log(`Text: ${message.text}`);
console.log(`Created At: ${message.createTime}`);
console.log("-----------------------------------------");
}
} catch (err) {
// Log any errors that occur during the API call
console.error("Failed to fetch space messages with error: %s", err.message);
}
}
This change drastically reduces the setup time for getting personal access to your Space data and messages. Happy scripting!!!










