From 09ddde4424f85bdaa2c6ea7ad3201bec958f0036 Mon Sep 17 00:00:00 2001 From: pierrick Date: Tue, 9 Dec 2025 04:01:24 +0000 Subject: [PATCH 1/2] feat: add webhook chat app --- solutions/webhook-chat-app/README.md | 4 ++ solutions/webhook-chat-app/quickstart.py | 42 +++++++++++++++++ solutions/webhook-chat-app/requirements.txt | 1 + solutions/webhook-chat-app/thread-reply.py | 50 +++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 solutions/webhook-chat-app/README.md create mode 100644 solutions/webhook-chat-app/quickstart.py create mode 100644 solutions/webhook-chat-app/requirements.txt create mode 100644 solutions/webhook-chat-app/thread-reply.py 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/quickstart.py b/solutions/webhook-chat-app/quickstart.py new file mode 100644 index 00000000..7caba461 --- /dev/null +++ b/solutions/webhook-chat-app/quickstart.py @@ -0,0 +1,42 @@ +# 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] diff --git a/solutions/webhook-chat-app/requirements.txt b/solutions/webhook-chat-app/requirements.txt new file mode 100644 index 00000000..e75eb880 --- /dev/null +++ b/solutions/webhook-chat-app/requirements.txt @@ -0,0 +1 @@ +httplib2>=0.17.0 diff --git a/solutions/webhook-chat-app/thread-reply.py b/solutions/webhook-chat-app/thread-reply.py new file mode 100644 index 00000000..4ffcc10a --- /dev/null +++ b/solutions/webhook-chat-app/thread-reply.py @@ -0,0 +1,50 @@ +# 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" + 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] From ada533265b5f604f65c38d5711d30c4fcb42ab25 Mon Sep 17 00:00:00 2001 From: pierrick Date: Tue, 9 Dec 2025 04:35:06 +0000 Subject: [PATCH 2/2] improve indentations --- solutions/webhook-chat-app/quickstart.py | 4 +++- solutions/webhook-chat-app/thread-reply.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/solutions/webhook-chat-app/quickstart.py b/solutions/webhook-chat-app/quickstart.py index 7caba461..c7aba443 100644 --- a/solutions/webhook-chat-app/quickstart.py +++ b/solutions/webhook-chat-app/quickstart.py @@ -25,7 +25,9 @@ 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!"} + app_message = { + "text": "Hello from a Python script!" + } message_headers = {"Content-Type": "application/json; charset=UTF-8"} http_obj = Http() response = http_obj.request( diff --git a/solutions/webhook-chat-app/thread-reply.py b/solutions/webhook-chat-app/thread-reply.py index 4ffcc10a..ba8b9aee 100644 --- a/solutions/webhook-chat-app/thread-reply.py +++ b/solutions/webhook-chat-app/thread-reply.py @@ -32,7 +32,9 @@ def main(): "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"}, + "thread": { + "threadKey": "THREAD_KEY_VALUE" + }, } message_headers = {"Content-Type": "application/json; charset=UTF-8"} http_obj = Http()