I have a chat space where I need to send daily alerts of different computations performed. The alert can be a simple table or a plot (matplotlib Figure). From some research, I think its not possible to send local image (buffer) directly to chat space. So I'm storing the alert image in a google cloud storage bucket (temporarily) and plan to send image by imageUrl as shown in the message_body below.
# Request Body
message_body = {
"cards": [
{
"header": {
"title": "Title",
"subtitle": "Subtitle",
},
"sections": [
{
"header": "TEST IMAGE",
"widgets": [
{
"image": {
"imageUrl": # Url of image inside bucket
}
}
]
}
]
}
]
}
In imageURL I have used both
- Authenticated url (format: "https://storage.cloud.google.com/project-id/test-image.png")
- Signed Url
What I have tried
Using Webhook:
webhook_url = "https://chat.googleapis.com/v1/spaces/..."
headers = {'Content-Type': "application/json; charset=UTF-8"}
response = requests.post(webhook_url, data=json.dumps(message_body), headers=headers)
Using REST API
After configuring Chat API with my app
request = chat.spaces().messages().create(parent=f'spaces/space_name',body=message_body)
result = request.execute()
Both method sends an empty card on Google chat space without image. But if I replace the cloud storage urls with
"imageUrl": "https://images.unsplash.com/photo-1660846194677-96299f2713f7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyOHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"
"imageUrl": "https://any/image/hosting/site"
Both method works perfectly (sends a card with image in it).
The Data might be sensitive so I don't want it to go out on any image hosting platform.