Skip to content

Commit

Permalink
feat: [Avatar app] Quick command samples (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-google authored Feb 1, 2025
1 parent 910bf04 commit 0394995
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 233 deletions.
40 changes: 35 additions & 5 deletions apps-script/avatar-app/avatar-app.gs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
// use the same ID as set in the Google Chat API configuration.
const ABOUT_COMMAND_ID = null;

// The ID of the quick command "Help".
// It's not enabled by default, set to the actual ID to enable it. You need to
// use the same ID as set in the Google Chat API configuration.
const HELP_COMMAND_ID = null;

/**
* Responds to a MESSAGE event in Google Chat.
*
Expand Down Expand Up @@ -64,13 +69,38 @@ function createMessage(displayName, avatarUrl) {
header: {
title: `Hello ${displayName}!`,
},
sections: [{ widgets: [{
textParagraph: { text: 'Your avatar picture: ' }
}, {
image: { imageUrl: avatarUrl }
}]}]
sections: [{
widgets: [{
textParagraph: {text: 'Your avatar picture: '}
}, {
image: {imageUrl: avatarUrl}
}]
}]
}
}]
};
}

// [START chat_avatar_quick_command]
/**
* Handles the APP_COMMAND event type. This function is triggered when a user
* interacts with a quick command within the Google Chat app. It responds
* based on the command ID.
*
* @param {Object} event The event object from Google Chat, containing details
* about the app command interaction. It includes information like the
* command ID and the user who triggered it.
*/
function onAppCommand(event) {
// Executes the quick command logic based on its ID.
// Command IDs are set in the Google Chat API configuration.
switch (event.appCommandMetadata.appCommandId) {
case HELP_COMMAND_ID:
return {
privateMessageViewer: event.user,
text: 'The Avatar app replies to Google Chat messages.'
};
}
}
// [END chat_avatar_quick_command]
// [END chat_avatar_app]
38 changes: 12 additions & 26 deletions java/avatar-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,41 @@ limitations under the License.

<!-- [START chat_avatar_app_build] -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.google.chat</groupId>
<groupId>gcfv2</groupId>
<artifactId>avatar-app</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.1</version>
<name>Avatar App</name>

<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.release>21</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.1</version>
<version>1.1.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.12.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-chat -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-chat</artifactId>
<version>v1-rev20241008-2.0.0</version>
<version>v1-rev20250116-2.0.0</version>
</dependency>

</dependencies>

<!-- Required for Java 11 functions in the inline editor -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<exclude>.google/</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
<!-- [END chat_avatar_app_build] -->
94 changes: 0 additions & 94 deletions java/avatar-app/src/main/java/App.java

This file was deleted.

125 changes: 125 additions & 0 deletions java/avatar-app/src/main/java/AvatarApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
 * 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.
 */

// [START chat_avatar_app]
import com.google.api.services.chat.v1.model.CardWithId;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Card;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1CardHeader;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Image;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Section;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1TextParagraph;
import com.google.api.services.chat.v1.model.GoogleAppsCardV1Widget;
import com.google.api.services.chat.v1.model.Message;
import com.google.api.services.chat.v1.model.User;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.List;

public class AvatarApp implements HttpFunction {
private static final Gson gson = new Gson();

// Command IDs (configure these in Google Chat API)
private static final int ABOUT_COMMAND_ID = 1; // ID for the "/about" slash command
private static final int HELP_COMMAND_ID = 2; // ID for the "Help" quick command

@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject event = gson.fromJson(request.getReader(), JsonObject.class);

if (event.has("appCommandMetadata")) {
handleAppCommands(event, response);
} else {
handleRegularMessage(event, response);
}
}

// [START chat_avatar_slash_command]
/**
* Handles slash and quick commands.
*
* @param event The Google Chat event.
* @param response The HTTP response object.
*/
private void handleAppCommands(JsonObject event, HttpResponse response) throws Exception {
int appCommandId = event.getAsJsonObject("appCommandMetadata").get("appCommandId").getAsInt();

switch (appCommandId) {
case ABOUT_COMMAND_ID:
Message aboutMessage = new Message();
aboutMessage.setText("The Avatar app replies to Google Chat messages.");
aboutMessage.setPrivateMessageViewer(new User()
.setName(event.getAsJsonObject("user").get("name").getAsString()));
response.getWriter().write(gson.toJson(aboutMessage));
return;
case HELP_COMMAND_ID:
Message helpMessage = new Message();
helpMessage.setText("The Avatar app replies to Google Chat messages.");
helpMessage.setPrivateMessageViewer(new User()
.setName(event.getAsJsonObject("user").get("name").getAsString()));
response.getWriter().write(gson.toJson(helpMessage));
return;
}
}
// [END chat_avatar_slash_command]

/**
* Handles regular messages (not commands).
*
* @param event The Google Chat event.
* @param response The HTTP response object.
*/
private void handleRegularMessage(JsonObject event, HttpResponse response) throws Exception {

if (!event.has("user")) {
response.getWriter().write("Invalid request.");
return;
}

JsonObject user = event.getAsJsonObject("user");
String displayName = user.has("displayName") ? user.get("displayName").getAsString() : "";
String avatarUrl = user.has("avatarUrl") ? user.get("avatarUrl").getAsString() : "";
Message message = createMessage(displayName, avatarUrl);
response.getWriter().write(gson.toJson(message));
}

/**
* Creates a card message with the user's avatar.
*
* @param displayName The user's display name.
* @param avatarUrl The URL of the user's avatar.
* @return The card message object.
*/
private Message createMessage(String displayName, String avatarUrl) {
return new Message()
.setText("Here's your avatar")
.setCardsV2(List.of(new CardWithId()
.setCardId("avatarCard")
.setCard(new GoogleAppsCardV1Card()
.setName("Avatar Card")
.setHeader(new GoogleAppsCardV1CardHeader()
.setTitle(String.format("Hello %s!", displayName)))
.setSections(List.of(new GoogleAppsCardV1Section().setWidgets(List.of(
new GoogleAppsCardV1Widget()
.setTextParagraph(new GoogleAppsCardV1TextParagraph()
.setText("Your avatar picture:")),
new GoogleAppsCardV1Widget()
.setImage(new GoogleAppsCardV1Image().setImageUrl(avatarUrl)))))))));
}
}
// [END chat_avatar_app]
Loading

0 comments on commit 0394995

Please sign in to comment.