From a54d7567f0d937ddeab3049986cf1a9e7970601b Mon Sep 17 00:00:00 2001 From: pierrick Date: Tue, 9 Dec 2025 03:55:37 +0000 Subject: [PATCH 1/2] feat: add webhook chat app --- solutions/webhook-chat-app/README.md | 4 ++ solutions/webhook-chat-app/index.js | 34 ++++++++++++ solutions/webhook-chat-app/package-lock.json | 54 ++++++++++++++++++++ solutions/webhook-chat-app/package.json | 12 +++++ solutions/webhook-chat-app/thread-reply.js | 37 ++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 solutions/webhook-chat-app/README.md create mode 100644 solutions/webhook-chat-app/index.js create mode 100644 solutions/webhook-chat-app/package-lock.json create mode 100644 solutions/webhook-chat-app/package.json create mode 100644 solutions/webhook-chat-app/thread-reply.js 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/index.js b/solutions/webhook-chat-app/index.js new file mode 100644 index 00000000..8f8e7172 --- /dev/null +++ b/solutions/webhook-chat-app/index.js @@ -0,0 +1,34 @@ +/** + * 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] +/** + * Sends asynchronous message to Google Chat + * @return {Object} response + */ +async function webhook() { + const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages" + const res = await fetch(url, { + method: "POST", + headers: {"Content-Type": "application/json; charset=UTF-8"}, + body: JSON.stringify({text: "Hello from a Node script!"}) + }); + return await res.json(); +} + +webhook().then(res => console.log(res)); +// [END chat_webhook] diff --git a/solutions/webhook-chat-app/package-lock.json b/solutions/webhook-chat-app/package-lock.json new file mode 100644 index 00000000..888f652f --- /dev/null +++ b/solutions/webhook-chat-app/package-lock.json @@ -0,0 +1,54 @@ +{ + "name": "webhook-chat-app", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "webhook-chat-app", + "version": "1.0.0", + "license": "Apache-2.0", + "dependencies": { + "node-fetch": "^2.6.0" + } + }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } +} diff --git a/solutions/webhook-chat-app/package.json b/solutions/webhook-chat-app/package.json new file mode 100644 index 00000000..fb008159 --- /dev/null +++ b/solutions/webhook-chat-app/package.json @@ -0,0 +1,12 @@ +{ + "name": "webhook-chat-app", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "node-fetch": "^2.6.0" + }, + "license": "Apache-2.0" +} diff --git a/solutions/webhook-chat-app/thread-reply.js b/solutions/webhook-chat-app/thread-reply.js new file mode 100644 index 00000000..e5cb52ab --- /dev/null +++ b/solutions/webhook-chat-app/thread-reply.js @@ -0,0 +1,37 @@ +/** + * Copyright 2023 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. + */ + + +// [START chat_webhook_thread] +/** + * Sends asynchronous message to Google Chat + * @return {Object} response + */ +async function webhook() { + const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" + const res = await fetch(url, { + method: "POST", + headers: {"Content-Type": "application/json; charset=UTF-8"}, + body: JSON.stringify({ + text: "Hello from a Node script!", + thread: {threadKey: "THREAD_KEY_VALUE"} + }) + }); + return await res.json(); +} + +webhook().then(res => console.log(res)); +// [END chat_webhook_thread] From efb894a43b17dc823e9e6bff32decc0b0bbebda9 Mon Sep 17 00:00:00 2001 From: pierrick Date: Tue, 9 Dec 2025 04:35:45 +0000 Subject: [PATCH 2/2] improve indentation --- solutions/webhook-chat-app/index.js | 6 ++++-- solutions/webhook-chat-app/thread-reply.js | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/solutions/webhook-chat-app/index.js b/solutions/webhook-chat-app/index.js index 8f8e7172..ac66ce89 100644 --- a/solutions/webhook-chat-app/index.js +++ b/solutions/webhook-chat-app/index.js @@ -21,11 +21,13 @@ * @return {Object} response */ async function webhook() { - const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages" + const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN" const res = await fetch(url, { method: "POST", headers: {"Content-Type": "application/json; charset=UTF-8"}, - body: JSON.stringify({text: "Hello from a Node script!"}) + body: JSON.stringify({ + text: "Hello from a Node script!" + }) }); return await res.json(); } diff --git a/solutions/webhook-chat-app/thread-reply.js b/solutions/webhook-chat-app/thread-reply.js index e5cb52ab..81ff1216 100644 --- a/solutions/webhook-chat-app/thread-reply.js +++ b/solutions/webhook-chat-app/thread-reply.js @@ -27,7 +27,9 @@ async function webhook() { headers: {"Content-Type": "application/json; charset=UTF-8"}, body: JSON.stringify({ text: "Hello from a Node script!", - thread: {threadKey: "THREAD_KEY_VALUE"} + thread: { + threadKey: "THREAD_KEY_VALUE" + } }) }); return await res.json();