Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions solutions/webhook-chat-app/README.md
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).
44 changes: 44 additions & 0 deletions solutions/webhook-chat-app/quickstart.py
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

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 os at the top of your file for this suggestion to work.

    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!"
}
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]
1 change: 1 addition & 0 deletions solutions/webhook-chat-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
httplib2>=0.17.0
52 changes: 52 additions & 0 deletions solutions/webhook-chat-app/thread-reply.py
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

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 os at the top of your file for this suggestion to work. The environment variable should contain the full webhook URL, including the messageReplyOption parameter.

    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]
Loading