You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python通过Webhook向MS Teams频道发送图片的问题排查

Python调用Teams Webhook发送图片失败问题排查与解决

问题背景

已验证Teams Webhook可正常发送文本消息,但发送图片时遇到以下问题:

  • 初始代码请求返回<Response [400]>,无法发送图片
  • 调整Payload后请求返回200,但频道内仍看不到图片

初始代码

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"})

问题原因

  1. 初始代码400错误:MessageCard格式中,sections.images数组内的图片字段应为imageUrl,而非image,字段名不匹配导致Teams接口校验失败。
  2. 更新后返回200但无图片:MessageCard的activityImage字段仅支持公开可访问的HTTP/HTTPS URL,不支持base64编码的内嵌图片数据。Teams无法解析该格式,因此即使请求成功,也不会显示图片。

解决方案

方案1:使用公开图片URL发送(MessageCard格式)

将图片上传至公开可访问的存储服务(如云存储、静态文件服务器),获取图片URL后修改Payload:

import requests

webhook_url = '<你的Teams Webhook URL>'
message = '这是一张图片消息'
public_image_url = 'https://xxx.yyy/frame.png'  # 替换为你的公开图片URL

payload = {
    "@type": "MessageCard",
    "@context": "http://schema.org/extensions",
    "themeColor": "0072C6",
    "summary": message,
    "sections": [
        {
            "activityTitle": message,
            "activityImage": public_image_url
        }
    ]
}

response = requests.post(webhook_url, json=payload)
print(response.status_code)

方案2:使用自适应卡片(Adaptive Card)内嵌base64图片

Adaptive Card是Teams推荐的新式卡片格式,支持内嵌base64图片,无需依赖外部存储:

import requests
from base64 import b64encode

webhook_url = '<你的Teams Webhook URL>'
with open('./frame.png', 'rb') as f:
    img_base64 = b64encode(f.read()).decode()

payload = {
    "type": "message",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.adaptive",
            "content": {
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                "type": "AdaptiveCard",
                "version": "1.0",
                "body": [
                    {
                        "type": "Image",
                        "url": f"data:image/png;base64,{img_base64}",
                        "altText": "测试图片"
                    }
                ]
            }
        }
    ]
}

response = requests.post(webhook_url, json=payload)
print(response.status_code)

注意事项

  • MessageCard格式仅支持外部图片URL,不支持base64内嵌;Adaptive Card支持两种方式。
  • 图片大小需符合Teams限制(单张图片建议不超过4MB)。
  • 确保Webhook URL未过期且权限正常。

内容的提问来源于stack exchange,提问作者Cori

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:38:09