Skip to content

Commit b2d7fab

Browse files
PierrickVouletpierrick
andauthored
feat: add contact form app (#273)
Co-authored-by: pierrick <pierrick@google.com>
1 parent b19f7c5 commit b2d7fab

24 files changed

Lines changed: 1977 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"addOns": {
7+
"common": {
8+
"name": "Avatar app",
9+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
10+
},
11+
"chat": {}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"addOns": {
7+
"common": {
8+
"name": "Basic app",
9+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
10+
},
11+
"chat": {}
12+
}
13+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START subsequent_steps]
18+
/**
19+
* Responds to a message in Google Chat.
20+
*
21+
* @param {Object} event The event object from the Google Workspace add-on.
22+
* @return {Object} response that handles dialogs.
23+
*/
24+
function onMessage(event) {
25+
// Reply with a message that contains a button to open the initial dialog
26+
return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
27+
text: "To add a contact, use the `ADD CONTACT` button below.",
28+
accessoryWidgets: [
29+
// [START open_dialog_from_button]
30+
{ buttonList: { buttons: [{
31+
text: "ADD CONTACT",
32+
onClick: { action: {
33+
function: "openInitialDialog",
34+
interaction: "OPEN_DIALOG"
35+
}}
36+
}]}}
37+
// [END open_dialog_from_button]
38+
]
39+
}}}}};
40+
}
41+
42+
// [START open_initial_dialog]
43+
/**
44+
* Opens the initial step of the dialog that lets users add contact details.
45+
*
46+
* @param {Object} event The event object from the Google Workspace add-on.
47+
* @return {Object} open the dialog.
48+
*/
49+
function openInitialDialog(event) {
50+
return { action: { navigations: [{ pushCard: { sections: [{ widgets: [
51+
{ textInput: {
52+
name: "contactName",
53+
label: "First and last name",
54+
type: "SINGLE_LINE"
55+
}},
56+
{ dateTimePicker: {
57+
name: "contactBirthdate",
58+
label: "Birthdate",
59+
type: "DATE_ONLY"
60+
}},
61+
{ selectionInput: {
62+
name: "contactType",
63+
label: "Contact type",
64+
type: "RADIO_BUTTON",
65+
items: [
66+
{ text: "Work", value: "Work", selected: false },
67+
{ text: "Personal", value: "Personal", selected: false }
68+
]
69+
}},
70+
{ buttonList: { buttons: [{
71+
text: "NEXT",
72+
onClick: { action: { function : "openConfirmationDialog" }}
73+
}]}}
74+
]}]}}]}};
75+
}
76+
// [END open_initial_dialog]
77+
78+
/**
79+
* Opens the second step of the dialog that lets users confirm details.
80+
*
81+
* @param {Object} event The event object from the Google Workspace add-on.
82+
* @return {Object} update the dialog.
83+
*/
84+
function openConfirmationDialog(event) {
85+
// Retrieve the form input values
86+
const name = event.commonEventObject.formInputs["contactName"].stringInputs.value[0];
87+
const birthdate = event.commonEventObject.formInputs["contactBirthdate"].dateInput.msSinceEpoch;
88+
const type = event.commonEventObject.formInputs["contactType"].stringInputs.value[0];
89+
// Display the input values for confirmation
90+
return { action: { navigations: [{ pushCard: { sections: [{ widgets: [
91+
{ textParagraph: { text: "Confirm contact information and submit:" }},
92+
{ textParagraph: { text: "<b>Name:</b> " + name }},
93+
{ textParagraph: { text: "<b>Birthday:</b> " + new Date(birthdate) }},
94+
{ textParagraph: { text: "<b>Type:</b> " + type }},
95+
// [START set_parameters]
96+
{ buttonList: { buttons: [{
97+
text: "SUBMIT",
98+
onClick: { action: {
99+
function: "submitDialog",
100+
// Pass input values as parameters for last dialog step (submission)
101+
parameters: [
102+
{ key: "contactName", value: name },
103+
{ key: "contactBirthdate", value: birthdate },
104+
{ key: "contactType", value: type }
105+
]
106+
}}
107+
}]}}
108+
// [END set_parameters]
109+
]}]}}]}};
110+
}
111+
// [END subsequent_steps]
112+
113+
/**
114+
* Handles submission and closes the dialog.
115+
*
116+
* @param {Object} event The event object from the Google Workspace add-on.
117+
* @return {Object} close the dialog with a status in text notification or message.
118+
*/
119+
function submitDialog(event) {
120+
// [START status_notification]
121+
// Validate the parameters.
122+
if (!event.commonEventObject.parameters["contactName"]) {
123+
return { action: {
124+
navigations: [{ endNavigation: { action: "CLOSE_DIALOG"}}],
125+
notification: { text: "Failure, the contact name was missing!" }
126+
}};
127+
}
128+
// [END status_notification]
129+
130+
// [START status_message]
131+
return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
132+
text: "✅ " + event.commonEventObject.parameters["contactName"] + " has been added to your contacts."
133+
}}}}};
134+
// [END status_message]
135+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Google Chat contact form app as Google Workspace add-on
2+
3+
This code sample is a Google Chat app that uses cards, dialogs, form inputs, and action parameters.
4+
5+
Please see related guides
6+
[Build interactive dialogs](https://developers.google.com/workspace/add-ons/chat/dialogs)
7+
and [Collect and process information from Google Chat users](http://developers.google.com/workspace/add-ons/chat/collect-information).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"addOns": {
7+
"common": {
8+
"name": "Contact Form app",
9+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/contact-icon.png"
10+
},
11+
"chat": {}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"addOns": {
7+
"common": {
8+
"name": "Preview Link app",
9+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
10+
},
11+
"chat": {}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"addOns": {
7+
"common": {
8+
"name": "Selection Input app",
9+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
10+
},
11+
"chat": {}
12+
}
13+
}
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+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# Target directory for maven builds
17+
target/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Google Chat contact form app as Google Workspace add-on
2+
3+
This code sample is a Google Chat app that uses cards, dialogs, form inputs, and action parameters.
4+
5+
Please see related guides
6+
[Build interactive dialogs](https://developers.google.com/workspace/add-ons/chat/dialogs)
7+
and [Collect and process information from Google Chat users](http://developers.google.com/workspace/add-ons/chat/collect-information).

java/chat/contact-form-app/pom.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2024 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+
http://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>contact-form-app</name>
22+
23+
<groupId>com.google.chat</groupId>
24+
<artifactId>contact-form-app</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<properties>
28+
<maven.compiler.target>17</maven.compiler.target>
29+
<maven.compiler.source>17</maven.compiler.source>
30+
</properties>
31+
32+
<dependencyManagement>
33+
<dependencies>
34+
<dependency>
35+
<!-- Import dependency management from Spring Boot -->
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-dependencies</artifactId>
38+
<version>3.2.3</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.cloud</groupId>
45+
<artifactId>spring-cloud-dependencies</artifactId>
46+
<version>Greenwich.SR1</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.fasterxml.jackson</groupId>
53+
<artifactId>jackson-bom</artifactId>
54+
<version>2.17.2</version> <type>pom</type>
55+
<scope>import</scope>
56+
</dependency>
57+
</dependencies>
58+
</dependencyManagement>
59+
60+
<dependencies>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-web</artifactId>
64+
<version>3.2.3</version>
65+
<exclusions>
66+
<!-- Exclude the Tomcat dependency -->
67+
<exclusion>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-tomcat</artifactId>
70+
</exclusion>
71+
</exclusions>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-starter-jetty</artifactId>
77+
<version>3.2.3</version>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>com.google.apis</groupId>
82+
<artifactId>google-api-services-chat</artifactId>
83+
<version>v1-rev20241008-2.0.0</version>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>com.fasterxml.jackson.core</groupId>
88+
<artifactId>jackson-databind</artifactId>
89+
</dependency>
90+
</dependencies>
91+
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
97+
<version>3.2.3</version>
98+
<executions>
99+
<execution>
100+
<goals>
101+
<goal>repackage</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
107+
<plugin>
108+
<groupId>com.google.cloud.tools</groupId>
109+
<artifactId>appengine-maven-plugin</artifactId>
110+
<version>2.7.0</version>
111+
<configuration>
112+
<version>GCLOUD_CONFIG</version>
113+
</configuration>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</project>

0 commit comments

Comments
 (0)