|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.google.chat.selectionInput; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.springframework.boot.SpringApplication; |
| 19 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 20 | +import org.springframework.web.bind.annotation.PostMapping; |
| 21 | +import org.springframework.web.bind.annotation.RequestBody; |
| 22 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 23 | +import org.springframework.web.bind.annotation.RestController; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.google.api.client.json.GenericJson; |
| 27 | +import com.google.api.services.chat.v1.model.CardWithId; |
| 28 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1Action; |
| 29 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1Card; |
| 30 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1Section; |
| 31 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1SelectionInput; |
| 32 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1SelectionItem; |
| 33 | +import com.google.api.services.chat.v1.model.GoogleAppsCardV1Widget; |
| 34 | +import com.google.api.services.chat.v1.model.Message; |
| 35 | + |
| 36 | +// [START selection_input] |
| 37 | +@SpringBootApplication |
| 38 | +@RestController |
| 39 | +// Web app that responds to events sent from a Google Chat space. |
| 40 | +public class App { |
| 41 | + private static final String FUNCTION_URL = "your-function-url"; |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + SpringApplication.run(App.class, args); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Handle requests from Google Chat |
| 49 | + * |
| 50 | + * @param event the event object sent by Google Chat |
| 51 | + * @return The response to be sent back to Google Chat |
| 52 | + */ |
| 53 | + @PostMapping("/") |
| 54 | + @ResponseBody |
| 55 | + public GenericJson onEvent(@RequestBody JsonNode event) throws Exception { |
| 56 | + // Stores the Google Chat event |
| 57 | + JsonNode chatEvent = event.at("/chat"); |
| 58 | + |
| 59 | + // Handle user interaction with multiselect. |
| 60 | + if (!chatEvent.at("/widgetUpdatedPayload").isEmpty()) { |
| 61 | + return queryContacts(event); |
| 62 | + } |
| 63 | + |
| 64 | + // Replies with a card that contains the multiselect menu. |
| 65 | + Message message = new Message().setCardsV2(List.of(new CardWithId() |
| 66 | + .setCardId("contactSelector") |
| 67 | + .setCard(new GoogleAppsCardV1Card() |
| 68 | + .setSections(List.of(new GoogleAppsCardV1Section().setWidgets(List.of(new GoogleAppsCardV1Widget() |
| 69 | + .setSelectionInput(new GoogleAppsCardV1SelectionInput() |
| 70 | + .setName("contacts") |
| 71 | + .setType("MULTI_SELECT") |
| 72 | + .setLabel("Selected contacts") |
| 73 | + .setMultiSelectMaxSelectedItems(3) |
| 74 | + .setMultiSelectMinQueryLength(1) |
| 75 | + .setExternalDataSource(new GoogleAppsCardV1Action().setFunction(FUNCTION_URL)) |
| 76 | + // Suggested items loaded by default. |
| 77 | + // The list is static here but it could be dynamic. |
| 78 | + .setItems(List.of(getSuggestedContact("3"))))))))))); |
| 79 | + |
| 80 | + GenericJson createMessageAction = new GenericJson(); |
| 81 | + createMessageAction.set("message", message); |
| 82 | + |
| 83 | + GenericJson chatDataAction = new GenericJson(); |
| 84 | + chatDataAction.set("createMessageAction", createMessageAction); |
| 85 | + |
| 86 | + GenericJson hostAppDataAction = new GenericJson(); |
| 87 | + hostAppDataAction.set("chatDataAction", chatDataAction); |
| 88 | + |
| 89 | + GenericJson renderActions = new GenericJson(); |
| 90 | + renderActions.set("hostAppDataAction", hostAppDataAction); |
| 91 | + return renderActions; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get contact suggestions based on text typed by users. |
| 96 | + * |
| 97 | + * @param event the event object that contains the user's query. |
| 98 | + * @return The response with contact suggestions. |
| 99 | + */ |
| 100 | + GenericJson queryContacts(JsonNode event) throws Exception { |
| 101 | + String query = event.at("/commonEventObject/parameters/autocomplete_widget_query").asText(); |
| 102 | + List<GoogleAppsCardV1SelectionItem> suggestions = List.of( |
| 103 | + // The list is static here but it could be dynamic. |
| 104 | + getSuggestedContact("1"), getSuggestedContact("2"), getSuggestedContact("3"), getSuggestedContact("4"), getSuggestedContact("5") |
| 105 | + // Only return items based on the query from the user |
| 106 | + ).stream().filter(e -> query == null || e.getText().indexOf(query) > -1).toList(); |
| 107 | + |
| 108 | + GenericJson selectionInputWidgetSuggestions = new GenericJson(); |
| 109 | + selectionInputWidgetSuggestions.set("suggestions", suggestions); |
| 110 | + |
| 111 | + GenericJson updateWidget = new GenericJson(); |
| 112 | + updateWidget.set("selectionInputWidgetSuggestions", selectionInputWidgetSuggestions); |
| 113 | + |
| 114 | + GenericJson modifyOperation = new GenericJson(); |
| 115 | + modifyOperation.set("updateWidget", updateWidget); |
| 116 | + |
| 117 | + GenericJson action = new GenericJson(); |
| 118 | + action.set("modifyOperations", List.of(modifyOperation)); |
| 119 | + |
| 120 | + GenericJson renderActions = new GenericJson(); |
| 121 | + renderActions.set("action", action); |
| 122 | + return renderActions; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generate a suggested contact given an ID. |
| 127 | + * |
| 128 | + * @param id The ID of the contact to return. |
| 129 | + * @return The contact formatted as a selection item in the menu. |
| 130 | + */ |
| 131 | + GoogleAppsCardV1SelectionItem getSuggestedContact(String id) { |
| 132 | + return new GoogleAppsCardV1SelectionItem() |
| 133 | + .setValue(id) |
| 134 | + .setStartIconUri("https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png") |
| 135 | + .setText("Contact " + id); |
| 136 | + } |
| 137 | +} |
| 138 | +// [END selection_input] |
0 commit comments