Skip to content

Commit d4199c0

Browse files
author
pierrick
committed
feat: add webhook chat app
1 parent 43971ae commit d4199c0

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Google Chat App Webhook
2+
3+
Please see related guide on how to
4+
[send messages to Google Chat with incoming webhooks](https://developers.google.com/workspace/chat/quickstart/webhooks).

solutions/webhook-chat-app/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2022 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<!-- [START chat_webhook_build] -->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>com.google.chat.webhook</groupId>
24+
<artifactId>webhook-app</artifactId>
25+
<version>0.1.0</version>
26+
<name>webhook-app</name>
27+
28+
<properties>
29+
<maven.compiler.target>11</maven.compiler.target>
30+
<maven.compiler.source>11</maven.compiler.source>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.google.code.gson</groupId>
36+
<artifactId>gson</artifactId>
37+
<version>2.9.1</version>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<pluginManagement>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>3.8.0</version>
47+
</plugin>
48+
</plugins>
49+
</pluginManagement>
50+
</build>
51+
</project>
52+
<!-- [END chat_webhook_build] -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2022 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+
* http://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+
package com.google.chat.webhook;
17+
18+
// [START chat_webhook]
19+
import com.google.gson.Gson;
20+
import java.net.http.HttpClient;
21+
import java.net.http.HttpRequest;
22+
import java.net.http.HttpResponse;
23+
import java.util.Map;
24+
import java.net.URI;
25+
26+
public class App {
27+
private static final String URL = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN";
28+
private static final Gson gson = new Gson();
29+
private static final HttpClient client = HttpClient.newHttpClient();
30+
31+
public static void main(String[] args) throws Exception {
32+
String message = gson.toJson(Map.of(
33+
"text", "Hello from Java!"
34+
));
35+
36+
HttpRequest request = HttpRequest.newBuilder(URI.create(URL))
37+
.header("accept", "application/json; charset=UTF-8")
38+
.POST(HttpRequest.BodyPublishers.ofString(message)).build();
39+
40+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
41+
42+
System.out.println(response.body());
43+
}
44+
}
45+
// [END chat_webhook]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
* http://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+
package com.google.chat.webhook;
17+
18+
// [START chat_webhook_thread]
19+
import com.google.gson.Gson;
20+
import java.net.http.HttpClient;
21+
import java.net.http.HttpRequest;
22+
import java.net.http.HttpResponse;
23+
import java.util.Map;
24+
import java.net.URI;
25+
26+
public class App {
27+
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";
28+
private static final Gson gson = new Gson();
29+
private static final HttpClient client = HttpClient.newHttpClient();
30+
31+
public static void main(String[] args) throws Exception {
32+
String message = gson.toJson(Map.of(
33+
"text", "Hello from Java!",
34+
"thread", Map.of(
35+
"threadKey", "THREAD_KEY_VALUE"
36+
)
37+
));
38+
39+
HttpRequest request = HttpRequest.newBuilder(URI.create(URL))
40+
.header("accept", "application/json; charset=UTF-8")
41+
.POST(HttpRequest.BodyPublishers.ofString(message)).build();
42+
43+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
44+
45+
System.out.println(response.body());
46+
}
47+
}
48+
// [END chat_webhook_thread]

0 commit comments

Comments
 (0)