diff --git a/solutions/webhook-chat-app/README.md b/solutions/webhook-chat-app/README.md new file mode 100644 index 00000000..59643860 --- /dev/null +++ b/solutions/webhook-chat-app/README.md @@ -0,0 +1,4 @@ +# Google Chat App Webhook + +Please see related guide on how to +[send messages to Google Chat with incoming webhooks](https://developers.google.com/workspace/chat/quickstart/webhooks). diff --git a/solutions/webhook-chat-app/pom.xml b/solutions/webhook-chat-app/pom.xml new file mode 100644 index 00000000..be47addd --- /dev/null +++ b/solutions/webhook-chat-app/pom.xml @@ -0,0 +1,52 @@ + + + + + + 4.0.0 + + com.google.chat.webhook + webhook-app + 0.1.0 + webhook-app + + + 11 + 11 + + + + + com.google.code.gson + gson + 2.9.1 + + + + + + + + maven-compiler-plugin + 3.8.0 + + + + + + diff --git a/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/App.java b/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/App.java new file mode 100644 index 00000000..3f9f4032 --- /dev/null +++ b/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/App.java @@ -0,0 +1,45 @@ +/** + * Copyright 2022 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 + * + * http://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. + */ +package com.google.chat.webhook; + +// [START chat_webhook] +import com.google.gson.Gson; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Map; +import java.net.URI; + +public class App { + private static final String URL = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"; + private static final Gson gson = new Gson(); + private static final HttpClient client = HttpClient.newHttpClient(); + + public static void main(String[] args) throws Exception { + String message = gson.toJson(Map.of( + "text", "Hello from Java!" + )); + + HttpRequest request = HttpRequest.newBuilder(URI.create(URL)) + .header("accept", "application/json; charset=UTF-8") + .POST(HttpRequest.BodyPublishers.ofString(message)).build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println(response.body()); + } +} +// [END chat_webhook] diff --git a/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/AppThread.java b/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/AppThread.java new file mode 100644 index 00000000..d5c5fb52 --- /dev/null +++ b/solutions/webhook-chat-app/src/main/java/com/google/chat/webhook/AppThread.java @@ -0,0 +1,48 @@ +/** + * 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 + * + * http://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. + */ +package com.google.chat.webhook; + +// [START chat_webhook_thread] +import com.google.gson.Gson; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Map; +import java.net.URI; + +public class App { + private static final String URL = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"; + private static final Gson gson = new Gson(); + private static final HttpClient client = HttpClient.newHttpClient(); + + public static void main(String[] args) throws Exception { + String message = gson.toJson(Map.of( + "text", "Hello from Java!", + "thread", Map.of( + "threadKey", "THREAD_KEY_VALUE" + ) + )); + + HttpRequest request = HttpRequest.newBuilder(URI.create(URL)) + .header("accept", "application/json; charset=UTF-8") + .POST(HttpRequest.BodyPublishers.ofString(message)).build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println(response.body()); + } +} +// [END chat_webhook_thread]