Skip to content

Commit dc39b4f

Browse files
PierrickVouletpierrick
andauthored
feat: Webhook chat (#1217)
* feat: add webhook chat app * improve indentation --------- Co-authored-by: pierrick <[email protected]>
1 parent e75e371 commit dc39b4f

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-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).
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
* https://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+
17+
18+
// [START chat_webhook]
19+
/**
20+
* Sends asynchronous message to Google Chat
21+
* @return {Object} response
22+
*/
23+
async function webhook() {
24+
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"
25+
const res = await fetch(url, {
26+
method: "POST",
27+
headers: {"Content-Type": "application/json; charset=UTF-8"},
28+
body: JSON.stringify({
29+
text: "Hello from a Node script!"
30+
})
31+
});
32+
return await res.json();
33+
}
34+
35+
webhook().then(res => console.log(res));
36+
// [END chat_webhook]

solutions/webhook-chat-app/package-lock.json

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "webhook-chat-app",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"dependencies": {
9+
"node-fetch": "^2.6.0"
10+
},
11+
"license": "Apache-2.0"
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2023 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+
17+
18+
// [START chat_webhook_thread]
19+
/**
20+
* Sends asynchronous message to Google Chat
21+
* @return {Object} response
22+
*/
23+
async function webhook() {
24+
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
25+
const res = await fetch(url, {
26+
method: "POST",
27+
headers: {"Content-Type": "application/json; charset=UTF-8"},
28+
body: JSON.stringify({
29+
text: "Hello from a Node script!",
30+
thread: {
31+
threadKey: "THREAD_KEY_VALUE"
32+
}
33+
})
34+
});
35+
return await res.json();
36+
}
37+
38+
webhook().then(res => console.log(res));
39+
// [END chat_webhook_thread]

0 commit comments

Comments
 (0)