-
Notifications
You must be signed in to change notification settings - Fork 410
feat: add webhook chat app #1715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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 | ||
|
|
||
| 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_webhook_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"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.google.chat.webhook</groupId> | ||
| <artifactId>webhook-app</artifactId> | ||
| <version>0.1.0</version> | ||
| <name>webhook-app</name> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.code.gson</groupId> | ||
| <artifactId>gson</artifactId> | ||
| <version>2.9.1</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <pluginManagement> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.8.0</version> | ||
| </plugin> | ||
| </plugins> | ||
| </pluginManagement> | ||
| </build> | ||
| </project> | ||
| <!-- [END chat_webhook_build] --> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding URLs with sensitive information like API keys and tokens is a major security risk. These credentials can be easily exposed if the source code is compromised or made public. It's highly recommended to externalize this configuration. For this example, you could pass the full URL as a command-line argument and read it in the |
||||||
| private static final Gson gson = new Gson(); | ||||||
| private static final HttpClient client = HttpClient.newHttpClient(); | ||||||
|
|
||||||
| public static void main(String[] args) throws Exception { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| 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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||||||
|
|
||||||
| System.out.println(response.body()); | ||||||
| } | ||||||
| } | ||||||
| // [END chat_webhook] | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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 { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| 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"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding URLs with sensitive information like API keys and tokens is a major security risk. These credentials can be easily exposed if the source code is compromised or made public. It's highly recommended to externalize this configuration. For this example, you could pass the full URL as a command-line argument and read it in the |
||||||
| private static final Gson gson = new Gson(); | ||||||
| private static final HttpClient client = HttpClient.newHttpClient(); | ||||||
|
|
||||||
| public static void main(String[] args) throws Exception { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| 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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||||||
|
|
||||||
| System.out.println(response.body()); | ||||||
| } | ||||||
| } | ||||||
| // [END chat_webhook_thread] | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
gsondependency version2.9.1is outdated and has a known Denial of Service (DoS) vulnerability (CVE-2022-25647). It's crucial to use updated dependencies to avoid security risks. Please update to version2.10.1or later, which contains the fix.