Microsoft Teams Webhook发送Connector卡片返回400状态码问题
问题原因
返回400状态码是两个代码错误共同导致的:
- 传参格式错误:你手动赋值的
payload是预序列化的JSON字符串,但send()方法内部会再次执行json.dumps()对payload做序列化,最终发给服务端的请求体是被二次转义的非法JSON,无法被解析。 - 卡片结构错误:你使用的外层包裹
contentType、content字段的结构是Bot框架接口的传参规范,Teams入站Webhook要求直接传递MessageCard的根结构,不需要额外嵌套外层字段。
修复方法
把主函数中payload的赋值逻辑修改为字典类型,去掉多余的外层嵌套结构即可,修正后的主函数代码如下:
if __name__ == "__main__": myTeamsMessage = ConnectorCard("你的Teams Webhook地址") # 直接传入字典格式的标准MessageCard结构,无需外层嵌套 myTeamsMessage.payload = { "@type": "MessageCard", "@context": "http://schema.org/extensions", "summary": "John Doe commented on Trello", "title": "Project Tango", "sections": [ { "activityTitle": "John Doe commented", "activitySubtitle": "On Project Tango", "activityText": "Here are the designs", "activityImage": "示例图片地址" }, { "title": "Details", "facts": [ { "name": "Labels", "value": "Designs, redlines" }, { "name": "Due date", "value": "Dec 7, 2016" }, { "name": "Attachments", "value": "附件内容" } ] }, { "title": "Images", "images": [ {"image": "示例图片地址1"}, {"image": "示例图片地址2"}, {"image": "示例图片地址3"} ] } ], "potentialAction": [ { "@context": "http://schema.org", "@type": "ViewAction", "name": "View in Trello", "target": ["跳转地址"] } ] } myTeamsMessage.send()
补充说明:如果只需要发送纯文本消息,直接调用封装好的
.text("消息内容")方法即可,不需要手动覆盖payload属性,避免出现结构冲突。
内容的提问来源于stack exchange,提问作者Gaurang Shah
相关产品推荐
相关产品推荐

