Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14360

Send image to MS Teams Channel via Webhook url using Python?

$
0
0

I simply just want to send an image in Python via a webhook url to my MS Teams channel. I already found some posts regarding my question. This one is similar to my question, but I can not find a sample to do it in Python. There are also different opinions about if it is possible to send an image via python to ms teams channel looking at this post and this one.

So I tried my best to send an image file to my teams channel.

import cv2import base64import jsonimport requests# Load an color image in grayscaleimg = cv2.imread('./frame.png')retval, buffer = cv2.imencode('.png', img)my_string = base64.b64encode(buffer).decode()my_string = "data:image/png;base64," + str(my_string)x = { "@type": "MessageCard", "text": "Test image","sections": [        {"images": [                {"image": my_string                }            ]        }    ]}y = json.dumps(x)webhook = "<my-webhook-url>"z = requests.post(webhook, json=y)print(z)requests.post(                        webhook,                        json={"text": "test"},                    )

However, I always get a <Response [400]> as a result for z. So my image is not posted in my channel. I checked my webhook with the second requests.post with the test message and this one came through.

Just my image is not posted in my teams channel. Does anybody know where I am making a mistake? Maybe some of you has a better solution to do so?

########################################################################Update

I found another way to create the payload for ms teams. Here is my code now:

import requestsfrom base64 import b64encodewebhook_url = '<webhook-url>'with open('./frame.png', 'rb') as f:    img_data = f.read()message = 'This is an image message.'payload = {"@type": "MessageCard","@context": "http://schema.org/extensions","themeColor": "0072C6","summary": message,"sections": [        {"activityTitle": message,"activityImage": "data:image/png;base64," + str(b64encode(img_data).decode()),        }    ]}response = requests.post(webhook_url, json=payload)print(response)requests.post(webhook_url, json={"text": "test"})

Now I do get a response of 200, which means that my image was sent successfully. Regrettably, I still can't see the image in my channel.

Does anybody know, why it was sent successfully but I still can't see it? Do I need to have special settings in my ms teams channel for it?


Viewing all articles
Browse latest Browse all 14360

Trending Articles