-
Notifications
You must be signed in to change notification settings - Fork 495
feat: Webhook chat #1217
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
feat: Webhook chat #1217
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,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(); | ||
|
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 current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
}
return res.json(); |
||
| } | ||
|
|
||
| webhook().then(res => console.log(res)); | ||
|
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 promise returned by webhook().then(res => console.log(res)).catch(console.error); |
||
| // [END chat_webhook] | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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" | ||||||
|
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 credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.
Suggested change
|
||||||
| 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(); | ||||||
|
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 current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
}
return res.json(); |
||||||
| } | ||||||
|
|
||||||
| webhook().then(res => console.log(res)); | ||||||
|
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 promise returned by webhook().then(res => console.log(res)).catch(console.error); |
||||||
| // [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.
Hardcoding credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.