Skip to content

Commit 00c77a4

Browse files
feat: add connectivity chat app for Java (#202)
1 parent 5f5a76e commit 00c77a4

File tree

10 files changed

+935
-0
lines changed

10 files changed

+935
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
README.md
11+
# If you would like to upload your .git directory, .gitignore file or files
12+
# from your .gitignore file, remove the corresponding line
13+
# below:
14+
.git
15+
.gitignore
16+
# Target directory for maven builds
17+
target/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/client_secrets.json
2+
target/
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Google Chat Connectivity App
2+
3+
This sample demonstrates how to create a Google Chat app that requests
4+
authorization from the user to make API calls on their behalf. The first
5+
time the user interacts with the app, it requests offline OAuth credentials for the
6+
user and saves them to a Firestore database. If the user interacts with the app
7+
again, the saved credentials are used so the app can make API calls on behalf of the
8+
user without asking for authorization again. Once saved, the OAuth credentials could
9+
even be used without any further user interactions.
10+
11+
This app is built using Java 21 and
12+
[Spring Boot](https://spring.io/projects/spring-boot) on Google App Engine
13+
(Standard Environment) and leverages Google's OAuth2 for authorization and
14+
Firestore for data storage. It replies with a Chat message that contains the
15+
link to a Meet space that was created calling the Google Meet API with their
16+
consent.
17+
18+
**Key Features:**
19+
20+
* **User Authorization:** Securely requests user consent to call Meet API with
21+
their credentials.
22+
* **Meet API Integration:** Calls Meet API to create a new Meet space on behalf
23+
of the user.
24+
* **Google Chat Integration:** Responds to DMs and @mentions in Google Chat. If
25+
necessary, request configuration to start an OAuth authorization flow.
26+
* **App Engine Deployment:** Provides step-by-step instructions for deploying
27+
to App Engine.
28+
* **Cloud Firestore:** Stores user credentials in a Firestore database.
29+
30+
## Prerequisites
31+
32+
* **JDK 21:** [Download](https://openjdk.org/projects/jdk/21/)
33+
* **Google Cloud SDK:** [Install](https://cloud.google.com/sdk/docs/install)
34+
* **Google Cloud Project:** [Create](https://console.cloud.google.com/projectcreate)
35+
36+
## Deployment Steps
37+
38+
1. **Set up your development environment:**
39+
40+
* Follow the steps in
41+
[Setting Up Your Development Environment](https://cloud.google.com/appengine/docs/standard/setting-up-environment?tab=java)
42+
to install Java and the Google Cloud SDK.
43+
44+
* Follow the steps in
45+
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/standard/java-gen2/using-maven)
46+
to install Maven.
47+
48+
1. **Enable APIs:**
49+
50+
* Enable the Cloud Firestore, Meet, and Google Chat APIs using the
51+
[console](https://console.cloud.google.com/apis/enableflow?apiid=firestore.googleapis.com,meet.googleapis.com,chat.googleapis.com)
52+
or gcloud:
53+
54+
```bash
55+
gcloud services enable firestore.googleapis.com meet.googleapis.com chat.googleapis.com
56+
```
57+
58+
1. **Initiate Deployment to App Engine:**
59+
60+
* Go to [App Engine](https://console.cloud.google.com/appengine) and
61+
initialize an application.
62+
63+
* Deploy the app to App Engine:
64+
65+
```bash
66+
mvn clean package appengine:deploy -Dapp.deploy.projectId=YOUR_PROJECT_ID
67+
```
68+
69+
Replace `YOUR_PROJECT_ID` with your Google Cloud Project ID.
70+
71+
1. **Create and Use OAuth Client ID:**
72+
73+
* Get the app hostname:
74+
75+
```bash
76+
gcloud app describe | grep defaultHostname
77+
```
78+
79+
* In your Google Cloud project, go to
80+
[APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials).
81+
* Click `Create Credentials > OAuth client ID`.
82+
* Select `Web application` as the application type.
83+
* Add `<hostname from the previous step>/oauth2` to `Authorized redirect URIs`.
84+
* Download the JSON file and rename it to `client_secrets.json` in the
85+
`src/main/resources/` subdirectory of your project directory.
86+
* Redeploy the app with the file `client_secrets.json`:
87+
88+
```bash
89+
mvn clean package appengine:deploy -Dapp.deploy.projectId=YOUR_PROJECT_ID
90+
```
91+
92+
Replace `YOUR_PROJECT_ID` with your Google Cloud Project ID.
93+
94+
1. **Create a Firestore Database:**
95+
96+
* Create a Firestore database in native mode named `auth-data` using the
97+
[console](https://console.cloud.google.com/firestore) or gcloud:
98+
99+
```bash
100+
gcloud firestore databases create \
101+
--database=auth-data \
102+
--location=REGION \
103+
--type=firestore-native
104+
```
105+
106+
Replace `REGION` with a
107+
[Firestore location](https://cloud.google.com/firestore/docs/locations#types)
108+
such as `nam5` or `eur3`.
109+
110+
## Create the Google Chat app
111+
112+
* Go to
113+
[Google Chat API](https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat)
114+
and click `Configuration`.
115+
* In **App name**, enter `Connectivity App`.
116+
* In **Avatar URL**, enter `https://developers.google.com/chat/images/quickstart-app-avatar.png`.
117+
* In **Description**, enter `Connectivity app`.
118+
* Under **Functionality**, select **Join spaces and group conversations**.
119+
* Under **Connection settings**, select **HTTP endpoint URL**
120+
* Under **Triggers**, select **Use a common HTTP endpoint URL for all triggers**.
121+
* In **HTTP endpoint URL** enter your App Engine app's URL (obtained in the previous
122+
deployment steps) without the trailing `/`.
123+
* Under **Commands**, click **Add a command**, and click **Done** after setting:
124+
* **Command Id** to `1`.
125+
* **Description** and **Quick command name** to `Logout`.
126+
* Under **Visibility**, select **Make this Google Chat app available to specific
127+
people and groups in your domain** and enter your email address.
128+
* Click **Save**.
129+
130+
## Interact with the App
131+
132+
* Message the app.
133+
* Follow the authorization link to grant the app access to your account.
134+
* Once authorization is complete, the app will reply with a link to the newly
135+
created Meet space.
136+
* Message the app again, it will reply without asking for authorization.
137+
* Execute the quick command `Logout`, it will deauthorizes the app.
138+
139+
## Related Topics
140+
141+
* [Authenticate and authorize Chat apps and Google Chat API requests](https://developers.google.com/workspace/chat/authenticate-authorize)
142+
* [Build Google Chat interfaces](https://developers.google.com/workspace/add-ons/chat/build)

java/chat/connectivity-app/pom.xml

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<name>connectivity-app</name>
22+
23+
<groupId>com.google.chat</groupId>
24+
<artifactId>connectivity-app</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<properties>
28+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29+
<maven.compiler.release>21</maven.compiler.release>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
<version>3.2.3</version>
37+
<exclusions>
38+
<!-- Exclude the Tomcat dependency -->
39+
<exclusion>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-tomcat</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-jetty</artifactId>
48+
<version>3.2.3</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-databind</artifactId>
53+
<version>2.17.2</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.guava</groupId>
57+
<artifactId>guava</artifactId>
58+
<version>33.4.0-jre</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.protobuf</groupId>
62+
<artifactId>protobuf-java</artifactId>
63+
<version>4.29.2</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.google.cloud</groupId>
67+
<artifactId>google-cloud-firestore</artifactId>
68+
<version>3.30.2</version>
69+
</dependency>
70+
71+
<!-- Google OAuth2 Client Library -->
72+
<dependency>
73+
<groupId>com.google.auth</groupId>
74+
<artifactId>google-auth-library-credentials</artifactId>
75+
<version>1.30.1</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>com.google.auth</groupId>
79+
<artifactId>google-auth-library-oauth2-http</artifactId>
80+
<version>1.30.1</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.google.oauth-client</groupId>
84+
<artifactId>google-oauth-client-jetty</artifactId>
85+
<version>1.37.0</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>com.google.api-client</groupId>
89+
<artifactId>google-api-client</artifactId>
90+
<version>2.7.1</version>
91+
</dependency>
92+
93+
<!-- Google Cloud Meet library -->
94+
<!-- Used for instantiating Google Meet API protos -->
95+
<dependency>
96+
<groupId>com.google.api.grpc</groupId>
97+
<artifactId>proto-google-cloud-meet-v2</artifactId>
98+
<version>0.41.0</version>
99+
</dependency>
100+
<!-- Used for creating Meet Service Client for making Google Meet API calls -->
101+
<dependency>
102+
<groupId>com.google.cloud</groupId>
103+
<artifactId>google-cloud-meet</artifactId>
104+
<version>0.41.0</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>com.google.api</groupId>
108+
<artifactId>gax-bom</artifactId>
109+
<version>2.70.1</version>
110+
<type>pom</type>
111+
</dependency>
112+
</dependencies>
113+
114+
<build>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-maven-plugin</artifactId>
119+
<version>3.2.3</version>
120+
<executions>
121+
<execution>
122+
<goals>
123+
<goal>repackage</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-compiler-plugin</artifactId>
132+
<version>3.13.0</version> <configuration>
133+
<compilerArgs>
134+
<arg>-parameters</arg>
135+
</compilerArgs>
136+
</configuration>
137+
</plugin>
138+
139+
<plugin>
140+
<groupId>com.google.cloud.tools</groupId>
141+
<artifactId>appengine-maven-plugin</artifactId>
142+
<version>2.8.1</version>
143+
<configuration>
144+
<version>GCLOUD_CONFIG</version>
145+
</configuration>
146+
</plugin>
147+
</plugins>
148+
</build>
149+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java21
16+
instance_class: F1
17+
18+
# Explicitly set the memory limit and maximum heap size for the Spring Boot app
19+
env_variables:
20+
JAVA_TOOL_OPTIONS: "-XX:MaxRAM=256m -XX:ActiveProcessorCount=2 -Xmx32m"

0 commit comments

Comments
 (0)