Skip to content

Commit da5506a

Browse files
PierrickVouletpierrick
andauthored
feat: add avatar chat app sample (#270)
Co-authored-by: pierrick <pierrick@google.com>
1 parent 8f0ae3e commit da5506a

14 files changed

Lines changed: 2142 additions & 83 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 chat_avatar_app_slash_command]
18+
// The ID of the slash command "/about".
19+
// You must use the same ID in the Google Chat API configuration.
20+
const ABOUT_COMMAND_ID = 1;
21+
22+
/**
23+
* Responds to an APP_COMMAND event in Google Chat.
24+
*
25+
* @param {Object} event the event object from Google Chat
26+
*/
27+
function onAppCommand(event) {
28+
// Executes the app command logic based on ID.
29+
switch (event.chat.appCommandPayload.appCommandMetadata.appCommandId) {
30+
case ABOUT_COMMAND_ID:
31+
return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
32+
text: 'The Avatar app replies to Google Chat messages.'
33+
}}}}};
34+
}
35+
}
36+
// [END chat_avatar_app_slash_command]
37+
38+
/**
39+
* Responds to a MESSAGE event in Google Chat.
40+
*
41+
* @param {Object} event the event object from Google Chat
42+
*/
43+
function onMessage(event) {
44+
// Stores the Google Chat user as a variable.
45+
const chatUser = event.chat.messagePayload.message.sender;
46+
const displayName = chatUser.displayName;
47+
const avatarUrl = chatUser.avatarUrl;
48+
return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
49+
text: 'Here\'s your avatar',
50+
cardsV2: [{
51+
cardId: 'avatarCard',
52+
card: {
53+
name: 'Avatar Card',
54+
header: {
55+
title: `Hello ${displayName}!`,
56+
},
57+
sections: [{ widgets: [{
58+
textParagraph: { text: 'Your avatar picture: ' }
59+
}, {
60+
image: { imageUrl: avatarUrl }
61+
}]}]
62+
}
63+
}]
64+
}}}}};
65+
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
# Avatar app as Google Workspace add-on
1+
# Google Chat avatar app as Google Workspace add-on
22

3-
This code sample creates a simple Google Chat app as Google Workspace add-on
4-
that responds to a simple message with the user's avatar or more information about
3+
This code sample creates a simple Google Chat app as Google Workspace add-on that
4+
responds to a simple message with the user's avatar or more information about
55
the app with a slash command.
66

7-
## Run the sample in Google Chat
7+
## Deploy the sample
88

9-
Please see [instructions](https://developers.google.com/workspace/add-ons/chat/quickstart-http).
9+
1. Follow the steps in
10+
[Build a Google Chat app with Google Apps Script](https://developers.google.com/workspace/add-ons/chat/quickstart-apps-script)
11+
to set up your environment, set up the script, and publish the Chat app.
12+
13+
1. Replace the script `Code.gs` with the one in this project.
14+
15+
1. Follow the steps in
16+
[Respond to commands](https://developers.google.com/workspace/add-ons/chat/commands)
17+
to configure the Chat app with the slash command.

apps-script/chat/avatar-app/avatar-app.gs

Lines changed: 0 additions & 76 deletions
This file was deleted.

java/chat/avatar-app/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Google Chat avatar app as Google Workspace add-on
3+
4+
This code sample creates a simple Google Chat app as Google Workspace add-on that
5+
responds to a simple message with the user's avatar or more information about
6+
the app with a slash command.
7+
8+
## Deploy the sample
9+
10+
1. Follow the steps in
11+
[Build a Google Chat app with HTTP service](https://developers.google.com/workspace/add-ons/chat/quickstart-http)
12+
to set up your environment, set up the script, and publish the Chat app.
13+
14+
1. Follow the steps in
15+
[Respond to commands](https://developers.google.com/workspace/add-ons/chat/commands)
16+
to configure the Chat app with the slash command.

java/chat/avatar-app/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2022 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+
18+
<!-- [START chat_avatar_app_build] -->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<groupId>com.google.chat</groupId>
25+
<artifactId>avatar-app</artifactId>
26+
<version>1.0-SNAPSHOT</version>
27+
28+
<properties>
29+
<maven.compiler.target>17</maven.compiler.target>
30+
<maven.compiler.source>17</maven.compiler.source>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.google.cloud.functions</groupId>
36+
<artifactId>functions-framework-api</artifactId>
37+
<version>1.1.4</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.google.code.gson</groupId>
41+
<artifactId>gson</artifactId>
42+
<version>2.9.1</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.google.apis</groupId>
46+
<artifactId>google-api-services-chat</artifactId>
47+
<version>v1-rev20230115-2.0.0</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.8.1</version>
57+
<configuration>
58+
<excludes>
59+
<exclude>.google/</exclude>
60+
</excludes>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
66+
<!-- [END chat_avatar_app_build] -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
* https://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+
// [START chat_avatar_app]
17+
package com.google.chat.avatar;
18+
19+
import com.google.api.services.chat.v1.model.CardWithId;
20+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Card;
21+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1CardHeader;
22+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Image;
23+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Section;
24+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1TextParagraph;
25+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Widget;
26+
import com.google.api.services.chat.v1.model.Message;
27+
import com.google.cloud.functions.HttpFunction;
28+
import com.google.cloud.functions.HttpRequest;
29+
import com.google.cloud.functions.HttpResponse;
30+
import com.google.gson.Gson;
31+
import com.google.gson.JsonObject;
32+
import java.util.List;
33+
34+
public class App implements HttpFunction {
35+
// [START chat_avatar_app_slash_command]
36+
// The ID of the slash command "/about".
37+
// You must use the same ID in the Google Chat API configuration.
38+
private static final int ABOUT_COMMAND_ID = 1;
39+
40+
private static final Gson gson = new Gson();
41+
42+
/**
43+
* Handle requests from Google Workspace add on
44+
*
45+
* @param request the request sent by Google Chat
46+
* @param response the response to be sent back to Google Chat
47+
*/
48+
@Override
49+
public void service(HttpRequest request, HttpResponse response) throws Exception {
50+
JsonObject event = gson.fromJson(request.getReader(), JsonObject.class);
51+
JsonObject chatEvent = event.getAsJsonObject("chat");
52+
Message message;
53+
if (chatEvent.has("appCommandPayload")) {
54+
message = handleAppCommand(chatEvent);
55+
} else {
56+
message = handleMessage(chatEvent);
57+
}
58+
JsonObject createMessageAction = new JsonObject();
59+
createMessageAction.add("message", gson.fromJson(gson.toJson(message), JsonObject.class));
60+
JsonObject chatDataAction = new JsonObject();
61+
chatDataAction.add("createMessageAction", createMessageAction);
62+
JsonObject hostAppDataAction = new JsonObject();
63+
hostAppDataAction.add("chatDataAction", chatDataAction);
64+
JsonObject dataActions = new JsonObject();
65+
dataActions.add("hostAppDataAction", hostAppDataAction);
66+
response.getWriter().write(gson.toJson(dataActions));
67+
}
68+
69+
/**
70+
* Handles an APP_COMMAND event in Google Chat.
71+
*
72+
* @param event the event object from Google Chat
73+
* @return the response message object.
74+
*/
75+
private Message handleAppCommand(JsonObject event) throws Exception {
76+
switch (event.getAsJsonObject("appCommandPayload")
77+
.getAsJsonObject("appCommandMetadata").get("appCommandId").getAsInt()) {
78+
case ABOUT_COMMAND_ID:
79+
return new Message()
80+
.setText("The Avatar app replies to Google Chat messages.");
81+
default:
82+
return null;
83+
}
84+
}
85+
// [END chat_avatar_app_slash_command]
86+
87+
/**
88+
* Handles a MESSAGE event in Google Chat.
89+
*
90+
* @param event the event object from Google Chat
91+
* @return the response message object.
92+
*/
93+
private Message handleMessage(JsonObject event) throws Exception {
94+
// Stores the Google Chat user as a variable.
95+
JsonObject chatUser = event.getAsJsonObject("messagePayload").getAsJsonObject("message").getAsJsonObject("sender");
96+
String displayName = chatUser.has("displayName") ? chatUser.get("displayName").getAsString() : "";
97+
String avatarUrl = chatUser.has("avatarUrl") ? chatUser.get("avatarUrl").getAsString() : "";
98+
return new Message()
99+
.setText("Here's your avatar")
100+
.setCardsV2(List.of(new CardWithId()
101+
.setCardId("avatarCard")
102+
.setCard(new GoogleAppsCardV1Card()
103+
.setName("Avatar Card")
104+
.setHeader(new GoogleAppsCardV1CardHeader()
105+
.setTitle(String.format("Hello %s!", displayName)))
106+
.setSections(List.of(new GoogleAppsCardV1Section().setWidgets(List.of(
107+
new GoogleAppsCardV1Widget().setTextParagraph(new GoogleAppsCardV1TextParagraph()
108+
.setText("Your avatar picture:")),
109+
new GoogleAppsCardV1Widget()
110+
.setImage(new GoogleAppsCardV1Image().setImageUrl(avatarUrl)))))))));
111+
}
112+
}
113+
// [END chat_avatar_app]

java/chat/preview-link/src/main/java/com/google/chat/previewLink/App.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import com.google.api.services.chat.v1.model.GoogleAppsCardV1OnClick;
3535
import com.google.api.services.chat.v1.model.GoogleAppsCardV1OpenLink;
3636
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Section;
37-
import com.google.api.services.chat.v1.model.GoogleAppsCardV1SelectionInput;
38-
import com.google.api.services.chat.v1.model.GoogleAppsCardV1SelectionItem;
3937
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Widget;
4038
import com.google.api.services.chat.v1.model.Message;
4139

node/chat/avatar-app/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Google Chat avatar app as Google Workspace add-on
2+
3+
This code sample creates a simple Google Chat app as Google Workspace add-on that
4+
responds to a simple message with the user's avatar or more information about
5+
the app with a slash command.
6+
7+
## Deploy the sample
8+
9+
1. Follow the steps in
10+
[Build a Google Chat app with HTTP service](https://developers.google.com/workspace/add-ons/chat/quickstart-http)
11+
to set up your environment, set up the script, and publish the Chat app.
12+
13+
1. Follow the steps in
14+
[Respond to commands](https://developers.google.com/workspace/add-ons/chat/commands)
15+
to configure the Chat app with the slash command.

0 commit comments

Comments
 (0)