Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ clasp
- [`clasp list-apis`](#apis)
- [`clasp enable-api<api>`](#apis)
- [`clasp disable-api <api>`](#apis)
- [`clasp chat [get | pull | push | update | open] [--open] [--file <file>] [--view <view>] [--updateMask <mask>]`](#chat)
- [`clasp run-function [function]`](#clasp-run)

## Guides
Expand Down Expand Up @@ -534,6 +535,27 @@ Clasp offers several commands to opens the current directory's `clasp` project a
- `clasp open-container`
- `clasp open-credentials-setup`

### Chat

Configure a Google Chat app built as a Google Workspace add-on using the [Google Workspace Add-ons API](https://developers.google.com/workspace/chat/configure-chat-api#configure-api).

Because Apps Script's `appsscript.json` manifest strictly validates against `ScriptManifest`, Chat app configuration (`ChatAppConfig`) is stored in a separate `chatapp.json` file alongside `appsscript.json` so `clasp push` is unaffected. When pushing `chatapp.json`, `clasp` automatically falls back to `addOns.common.name` and `addOns.common.logoUrl` from `appsscript.json` if `displayName` or `avatarUri` are omitted.

#### Subcommands & Options

- `clasp chat get [--view <view>]`: Retrieve the current Chat app configuration (`BASIC` or `FULL`, default: `FULL`).
- `clasp chat pull [-f, --file <file>] [--view <view>]`: Download the Chat app configuration to `chatapp.json` (or `--file`).
- `clasp chat push [-f, --file <file>] [--updateMask <mask>]` (alias `update`): Update the Chat app configuration from `chatapp.json` (or `--file`).
- `clasp chat open` / `clasp chat --open`: Open the Chat API Configuration page in the browser.

#### Examples

- `clasp chat get`
- `clasp chat pull`
- `clasp chat push`
- `clasp chat update --updateMask displayName,description`
- `clasp chat open`

### Deployments

List deployments of a script.
Expand Down
153 changes: 153 additions & 0 deletions src/commands/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file defines the 'chat' command for configuring a Google Chat app
// via the Google Workspace Add-ons API.

import {Command} from 'commander';
import type {ChatAppConfigView} from '../core/chat.js';
import {Clasp} from '../core/clasp.js';
import {INCLUDE_USER_HINT_IN_URL} from '../experiments.js';
import {intl} from '../intl.js';
import {GlobalOptions, assertGcpProjectConfigured, maybePromptForProjectId, openUrl, withSpinner} from './utils.js';

interface ChatCommandOptions extends GlobalOptions {
readonly open?: boolean;
readonly file?: string;
readonly view?: ChatAppConfigView;
readonly updateMask?: string;
}

async function openChatApiConsole(clasp: Clasp, json?: boolean) {
const projectId = await maybePromptForProjectId(clasp);
assertGcpProjectConfigured(clasp);

const url = new URL('https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat');
url.searchParams.set('project', projectId ?? '');
if (INCLUDE_USER_HINT_IN_URL) {
const userHint = await clasp.authorizedUser();
url.searchParams.set('authUser', userHint ?? '');
}
if (json) {
console.log(JSON.stringify({url: url.toString()}, null, 2));
}
await openUrl(url.toString());
}

const getSubCommand = new Command('get')
.description('Retrieve the current Google Chat app configuration.')
.option('--view <view>', 'View of ChatAppConfig to return (BASIC or FULL)', 'FULL')
.action(async function (this: Command) {
const options: ChatCommandOptions = this.optsWithGlobals();
const clasp: Clasp = options.clasp;

await maybePromptForProjectId(clasp);
assertGcpProjectConfigured(clasp);

const spinnerMsg = intl.formatMessage({
defaultMessage: 'Fetching Chat app configuration...',
});
const config = await withSpinner(spinnerMsg, async () => {
return await clasp.chat.getConfig(options.view ?? 'FULL');
});

console.log(JSON.stringify(config, null, 2));
});

const pullSubCommand = new Command('pull')
.description('Download the Google Chat app configuration to chatapp.json.')
.option('-f, --file <file>', 'Path to Chat app configuration JSON file (default: chatapp.json)')
.option('--view <view>', 'View of ChatAppConfig to return (BASIC or FULL)', 'FULL')
.action(async function (this: Command) {
const options: ChatCommandOptions = this.optsWithGlobals();
const clasp: Clasp = options.clasp;

await maybePromptForProjectId(clasp);
assertGcpProjectConfigured(clasp);

const spinnerMsg = intl.formatMessage({
defaultMessage: 'Pulling Chat app configuration...',
});
const result = await withSpinner(spinnerMsg, async () => {
return await clasp.chat.pullConfig(options.file, options.view ?? 'FULL');
});

if (options.json) {
console.log(JSON.stringify({filePath: result.filePath, config: result.config}, null, 2));
return;
}

const successMessage = intl.formatMessage(
{
defaultMessage: 'Pulled Chat app configuration to {filePath}.',
},
{
filePath: result.filePath,
},
);
console.log(successMessage);
});

const pushSubCommand = new Command('push')
.alias('update')
.description('Update the Google Chat app configuration from chatapp.json.')
.option('-f, --file <file>', 'Path to Chat app configuration JSON file (default: chatapp.json)')
.option('--updateMask <mask>', 'Comma-separated list of fields to update')
.action(async function (this: Command) {
const options: ChatCommandOptions = this.optsWithGlobals();
const clasp: Clasp = options.clasp;

await maybePromptForProjectId(clasp);
assertGcpProjectConfigured(clasp);

const spinnerMsg = intl.formatMessage({
defaultMessage: 'Updating Chat app configuration...',
});
const updated = await withSpinner(spinnerMsg, async () => {
return await clasp.chat.pushConfig(options.file, options.updateMask);
});

if (options.json) {
console.log(JSON.stringify(updated, null, 2));
return;
}

const successMessage = intl.formatMessage({
defaultMessage: 'Updated Chat app configuration.',
});
console.log(successMessage);
});

const openSubCommand = new Command('open')
.description('Open the Google Chat API Configuration page in the browser.')
.action(async function (this: Command) {
const options: ChatCommandOptions = this.optsWithGlobals();
await openChatApiConsole(options.clasp, options.json);
});

export const command = new Command('chat')
.description('Configure a Google Chat app via the Google Workspace Add-ons API.')
.option('--open', 'Open the Google Chat API Configuration page in the browser')
.addCommand(getSubCommand)
.addCommand(pullSubCommand)
.addCommand(pushSubCommand)
.addCommand(openSubCommand)
.action(async function (this: Command) {
const options: ChatCommandOptions = this.optsWithGlobals();
if (options.open) {
await openChatApiConsole(options.clasp, options.json);
return;
}
this.help();
});
5 changes: 5 additions & 0 deletions src/commands/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import {Command, CommanderError, Option} from 'commander';
import {PROJECT_NAME} from '../constants.js';

import {command as chatCommand} from './chat.js';
import {command as cloneCommand} from './clone-script.js';
import {command as createDeploymentCommand} from './create-deployment.js';
import {command as createCommand} from './create-script.js';
Expand Down Expand Up @@ -162,6 +163,7 @@ export function makeProgram(exitOverride?: (err: CommanderError) => void) {
enableApiCommand,
listApisCommand,
openApisConsoleCommand,
chatCommand,
authStatusCommand,
filesStatusCommand,
openLogsCommand,
Expand All @@ -180,6 +182,9 @@ export function makeProgram(exitOverride?: (err: CommanderError) => void) {
for (const cmd of commandsToAdd) {
program.addCommand(cmd);
cmd.copyInheritedSettings(program);
for (const sub of cmd.commands) {
sub.copyInheritedSettings(program);
}
}

program.on('command:*', function (this: Command, op) {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
export const PROJECT_NAME = 'clasp';
export const PROJECT_MANIFEST_BASENAME = 'appsscript';
export const PROJECT_MANIFEST_FILENAME = `${PROJECT_MANIFEST_BASENAME}.json`;
export const CHAT_APP_CONFIG_FILENAME = 'chatapp.json';
Loading
Loading