Skip to content

Commit 09ddde4

Browse files
author
pierrick
committed
feat: add webhook chat app
1 parent 6df54e5 commit 09ddde4

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Google Chat App Webhook
2+
3+
Please see related guide on how to
4+
[send messages to Google Chat with incoming webhooks](https://developers.google.com/workspace/chat/quickstart/webhooks).
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# A sample script for using an incoming webhook for Google Chat rooms.
15+
16+
17+
# [START chat_webhook]
18+
from json import dumps
19+
from httplib2 import Http
20+
21+
# Copy the webhook URL from the Chat space where the webhook is registered.
22+
# The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included
23+
# when you copy the webhook URL.
24+
25+
def main():
26+
"""Google Chat incoming webhook quickstart."""
27+
url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"
28+
app_message = {"text": "Hello from a Python script!"}
29+
message_headers = {"Content-Type": "application/json; charset=UTF-8"}
30+
http_obj = Http()
31+
response = http_obj.request(
32+
uri=url,
33+
method="POST",
34+
headers=message_headers,
35+
body=dumps(app_message),
36+
)
37+
print(response)
38+
39+
40+
if __name__ == "__main__":
41+
main()
42+
# [END chat_webhook]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
httplib2>=0.17.0
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START chat_webhook_thread]
17+
from json import dumps
18+
from httplib2 import Http
19+
20+
# Copy the webhook URL from the Chat space where the webhook is registered.
21+
# The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included
22+
# when you copy the webhook URL.
23+
#
24+
# Then, append messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD to the
25+
# webhook URL.
26+
27+
28+
def main():
29+
"""Google Chat incoming webhook that starts or replies to a message thread."""
30+
url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
31+
app_message = {
32+
"text": "Hello from a Python script!",
33+
# To start a thread, set threadKey to an arbitratry string.
34+
# To reply to a thread, specify that thread's threadKey value.
35+
"thread": {"threadKey": "THREAD_KEY_VALUE"},
36+
}
37+
message_headers = {"Content-Type": "application/json; charset=UTF-8"}
38+
http_obj = Http()
39+
response = http_obj.request(
40+
uri=url,
41+
method="POST",
42+
headers=message_headers,
43+
body=dumps(app_message),
44+
)
45+
print(response)
46+
47+
48+
if __name__ == "__main__":
49+
main()
50+
# [END chat_webhook_thread]

0 commit comments

Comments
 (0)