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..ac66ce89 --- /dev/null +++ b/solutions/webhook-chat-app/index.js @@ -0,0 +1,36 @@ +/** + * 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?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!" + }) + }); + 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..81ff1216 --- /dev/null +++ b/solutions/webhook-chat-app/thread-reply.js @@ -0,0 +1,39 @@ +/** + * 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]