Skip to content

Commit 0394995

Browse files
authored
feat: [Avatar app] Quick command samples (#355)
1 parent 910bf04 commit 0394995

File tree

7 files changed

+337
-233
lines changed

7 files changed

+337
-233
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
// use the same ID as set in the Google Chat API configuration.
2121
const ABOUT_COMMAND_ID = null;
2222

23+
// The ID of the quick command "Help".
24+
// It's not enabled by default, set to the actual ID to enable it. You need to
25+
// use the same ID as set in the Google Chat API configuration.
26+
const HELP_COMMAND_ID = null;
27+
2328
/**
2429
* Responds to a MESSAGE event in Google Chat.
2530
*
@@ -64,13 +69,38 @@ function createMessage(displayName, avatarUrl) {
6469
header: {
6570
title: `Hello ${displayName}!`,
6671
},
67-
sections: [{ widgets: [{
68-
textParagraph: { text: 'Your avatar picture: ' }
69-
}, {
70-
image: { imageUrl: avatarUrl }
71-
}]}]
72+
sections: [{
73+
widgets: [{
74+
textParagraph: {text: 'Your avatar picture: '}
75+
}, {
76+
image: {imageUrl: avatarUrl}
77+
}]
78+
}]
7279
}
7380
}]
7481
};
7582
}
83+
84+
// [START chat_avatar_quick_command]
85+
/**
86+
* Handles the APP_COMMAND event type. This function is triggered when a user
87+
* interacts with a quick command within the Google Chat app. It responds
88+
* based on the command ID.
89+
*
90+
* @param {Object} event The event object from Google Chat, containing details
91+
* about the app command interaction. It includes information like the
92+
* command ID and the user who triggered it.
93+
*/
94+
function onAppCommand(event) {
95+
// Executes the quick command logic based on its ID.
96+
// Command IDs are set in the Google Chat API configuration.
97+
switch (event.appCommandMetadata.appCommandId) {
98+
case HELP_COMMAND_ID:
99+
return {
100+
privateMessageViewer: event.user,
101+
text: 'The Avatar app replies to Google Chat messages.'
102+
};
103+
}
104+
}
105+
// [END chat_avatar_quick_command]
76106
// [END chat_avatar_app]

java/avatar-app/pom.xml

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,41 @@ limitations under the License.
1717

1818
<!-- [START chat_avatar_app_build] -->
1919
<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">
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/maven-v4_0_0.xsd">
2222
<modelVersion>4.0.0</modelVersion>
2323

24-
<groupId>com.google.chat</groupId>
24+
<groupId>gcfv2</groupId>
2525
<artifactId>avatar-app</artifactId>
26-
<version>1.0-SNAPSHOT</version>
26+
<version>0.0.1</version>
27+
<name>Avatar App</name>
2728

2829
<properties>
29-
<maven.compiler.target>17</maven.compiler.target>
30-
<maven.compiler.source>17</maven.compiler.source>
30+
<maven.compiler.release>21</maven.compiler.release>
3131
</properties>
3232

3333
<dependencies>
3434
<dependency>
3535
<groupId>com.google.cloud.functions</groupId>
3636
<artifactId>functions-framework-api</artifactId>
37-
<version>1.0.1</version>
37+
<version>1.1.4</version>
3838
</dependency>
3939

4040
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
4141
<dependency>
42-
<groupId>com.google.code.gson</groupId>
43-
<artifactId>gson</artifactId>
44-
<version>2.9.1</version>
42+
<groupId>com.google.code.gson</groupId>
43+
<artifactId>gson</artifactId>
44+
<version>2.12.1</version>
4545
</dependency>
4646

4747
<!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-chat -->
4848
<dependency>
4949
<groupId>com.google.apis</groupId>
5050
<artifactId>google-api-services-chat</artifactId>
51-
<version>v1-rev20241008-2.0.0</version>
51+
<version>v1-rev20250116-2.0.0</version>
5252
</dependency>
53+
5354
</dependencies>
5455

55-
<!-- Required for Java 11 functions in the inline editor -->
56-
<build>
57-
<plugins>
58-
<plugin>
59-
<groupId>org.apache.maven.plugins</groupId>
60-
<artifactId>maven-compiler-plugin</artifactId>
61-
<version>3.8.1</version>
62-
<configuration>
63-
<excludes>
64-
<exclude>.google/</exclude>
65-
</excludes>
66-
</configuration>
67-
</plugin>
68-
</plugins>
69-
</build>
7056
</project>
7157
<!-- [END chat_avatar_app_build] -->

java/avatar-app/src/main/java/App.java

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
17+
// [START chat_avatar_app]
18+
import com.google.api.services.chat.v1.model.CardWithId;
19+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Card;
20+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1CardHeader;
21+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Image;
22+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Section;
23+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1TextParagraph;
24+
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Widget;
25+
import com.google.api.services.chat.v1.model.Message;
26+
import com.google.api.services.chat.v1.model.User;
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 AvatarApp implements HttpFunction {
35+
private static final Gson gson = new Gson();
36+
37+
// Command IDs (configure these in Google Chat API)
38+
private static final int ABOUT_COMMAND_ID = 1; // ID for the "/about" slash command
39+
private static final int HELP_COMMAND_ID = 2; // ID for the "Help" quick command
40+
41+
@Override
42+
public void service(HttpRequest request, HttpResponse response) throws Exception {
43+
JsonObject event = gson.fromJson(request.getReader(), JsonObject.class);
44+
45+
if (event.has("appCommandMetadata")) {
46+
handleAppCommands(event, response);
47+
} else {
48+
handleRegularMessage(event, response);
49+
}
50+
}
51+
52+
// [START chat_avatar_slash_command]
53+
/**
54+
* Handles slash and quick commands.
55+
*
56+
* @param event The Google Chat event.
57+
* @param response The HTTP response object.
58+
*/
59+
private void handleAppCommands(JsonObject event, HttpResponse response) throws Exception {
60+
int appCommandId = event.getAsJsonObject("appCommandMetadata").get("appCommandId").getAsInt();
61+
62+
switch (appCommandId) {
63+
case ABOUT_COMMAND_ID:
64+
Message aboutMessage = new Message();
65+
aboutMessage.setText("The Avatar app replies to Google Chat messages.");
66+
aboutMessage.setPrivateMessageViewer(new User()
67+
.setName(event.getAsJsonObject("user").get("name").getAsString()));
68+
response.getWriter().write(gson.toJson(aboutMessage));
69+
return;
70+
case HELP_COMMAND_ID:
71+
Message helpMessage = new Message();
72+
helpMessage.setText("The Avatar app replies to Google Chat messages.");
73+
helpMessage.setPrivateMessageViewer(new User()
74+
.setName(event.getAsJsonObject("user").get("name").getAsString()));
75+
response.getWriter().write(gson.toJson(helpMessage));
76+
return;
77+
}
78+
}
79+
// [END chat_avatar_slash_command]
80+
81+
/**
82+
* Handles regular messages (not commands).
83+
*
84+
* @param event The Google Chat event.
85+
* @param response The HTTP response object.
86+
*/
87+
private void handleRegularMessage(JsonObject event, HttpResponse response) throws Exception {
88+
89+
if (!event.has("user")) {
90+
response.getWriter().write("Invalid request.");
91+
return;
92+
}
93+
94+
JsonObject user = event.getAsJsonObject("user");
95+
String displayName = user.has("displayName") ? user.get("displayName").getAsString() : "";
96+
String avatarUrl = user.has("avatarUrl") ? user.get("avatarUrl").getAsString() : "";
97+
Message message = createMessage(displayName, avatarUrl);
98+
response.getWriter().write(gson.toJson(message));
99+
}
100+
101+
/**
102+
* Creates a card message with the user's avatar.
103+
*
104+
* @param displayName The user's display name.
105+
* @param avatarUrl The URL of the user's avatar.
106+
* @return The card message object.
107+
*/
108+
private Message createMessage(String displayName, String avatarUrl) {
109+
return new Message()
110+
.setText("Here's your avatar")
111+
.setCardsV2(List.of(new CardWithId()
112+
.setCardId("avatarCard")
113+
.setCard(new GoogleAppsCardV1Card()
114+
.setName("Avatar Card")
115+
.setHeader(new GoogleAppsCardV1CardHeader()
116+
.setTitle(String.format("Hello %s!", displayName)))
117+
.setSections(List.of(new GoogleAppsCardV1Section().setWidgets(List.of(
118+
new GoogleAppsCardV1Widget()
119+
.setTextParagraph(new GoogleAppsCardV1TextParagraph()
120+
.setText("Your avatar picture:")),
121+
new GoogleAppsCardV1Widget()
122+
.setImage(new GoogleAppsCardV1Image().setImageUrl(avatarUrl)))))))));
123+
}
124+
}
125+
// [END chat_avatar_app]

0 commit comments

Comments
 (0)