-
Notifications
You must be signed in to change notification settings - Fork 927
feat: Webhook chat #2535
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 #2535
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,44 @@ | ||
| # Copyright 2020 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. | ||
| # A sample script for using an incoming webhook for Google Chat rooms. | ||
|
|
||
|
|
||
| # [START chat_webhook] | ||
| from json import dumps | ||
| from httplib2 import Http | ||
|
|
||
| # Copy the webhook URL from the Chat space where the webhook is registered. | ||
| # The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included | ||
| # when you copy the webhook URL. | ||
|
|
||
| def main(): | ||
| """Google Chat incoming webhook quickstart.""" | ||
| url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN" | ||
| app_message = { | ||
| "text": "Hello from a Python script!" | ||
| } | ||
| message_headers = {"Content-Type": "application/json; charset=UTF-8"} | ||
| http_obj = Http() | ||
| response = http_obj.request( | ||
| uri=url, | ||
| method="POST", | ||
| headers=message_headers, | ||
| body=dumps(app_message), | ||
| ) | ||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| # [END chat_webhook] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| httplib2>=0.17.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # 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] | ||
| from json import dumps | ||
| from httplib2 import Http | ||
|
|
||
| # Copy the webhook URL from the Chat space where the webhook is registered. | ||
| # The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included | ||
| # when you copy the webhook URL. | ||
| # | ||
| # Then, append messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD to the | ||
| # webhook URL. | ||
|
|
||
|
|
||
| def main(): | ||
| """Google Chat incoming webhook that starts or replies to a message thread.""" | ||
| 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 secrets, such as webhook URLs, is a critical security risk. It can lead to accidental exposure of credentials if the code is committed to version control. It is strongly recommended to load sensitive information from environment variables. You will need to add url = os.environ.get("GOOGLE_CHAT_WEBHOOK_URL")
if not url:
raise SystemExit("GOOGLE_CHAT_WEBHOOK_URL environment variable must be set.") |
||
| app_message = { | ||
| "text": "Hello from a Python script!", | ||
| # To start a thread, set threadKey to an arbitratry string. | ||
| # To reply to a thread, specify that thread's threadKey value. | ||
| "thread": { | ||
| "threadKey": "THREAD_KEY_VALUE" | ||
| }, | ||
| } | ||
| message_headers = {"Content-Type": "application/json; charset=UTF-8"} | ||
| http_obj = Http() | ||
| response = http_obj.request( | ||
| uri=url, | ||
| method="POST", | ||
| headers=message_headers, | ||
| body=dumps(app_message), | ||
| ) | ||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| # [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 secrets, such as webhook URLs, is a critical security risk. It can lead to accidental exposure of credentials if the code is committed to version control. It is strongly recommended to load sensitive information from environment variables. You will need to add
import osat the top of your file for this suggestion to work.