我只是想通过 webhook url 将 Python 中的图像发送到我的 MS Teams 频道。我已经找到了一些关于我的问题的帖子。 这个与我的问题类似,但我找不到在Python中执行此操作的示例。关于是否可以通过Python将图像发送到MS团队频道也有不同的意见查看这篇文章和这个。
所以我尽力将图像文件发送到我的团队频道。
import cv2
import base64
import json
import requests
# Load an color image in grayscale
img = 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"},
)
但是,我总是得到
只是我的图像没有发布在我的团队频道中。有人知道我在哪里犯了错误吗?也许你们中的一些人有更好的解决方案?
################################################## ####################### 更新
我找到了另一种为女士团队创建有效负载的方法。现在这是我的代码:
import requests
from base64 import b64encode
webhook_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"})
现在我确实得到了 200 的响应,这意味着我的图像已成功发送。遗憾的是,我仍然看不到我频道中的图像。
有人知道为什么发送成功但我还是看不到吗?我需要在我的 MS 团队频道中进行特殊设置吗?
你有没有解决这个问题,因为我也有同样的问题?
尼克