使用Python发送企业微信Webhook请求

学习笔记作者:admin日期:2025-07-30点击:6

摘要:介绍如何用Python发送企业微信Webhook请求,包括请求URL、请求头、JSON数据及完整代码示例。

使用Python发送企业微信Webhook请求

背景

      本文介绍了如何通过Python向企业微信发送Webhook请求,用于发送文本消息。

解决方案

import requests
import json

# Webhook URL(替换为你自己的 key)
webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa"

# 请求头:指定内容类型为 JSON
headers = {
    "Content-Type": "application/json"
}

# 请求体:要发送的消息内容
data = {
    "msgtype": "text",
    "text": {
        "content": "hello world"
    }
}

# 发送 POST 请求
response = requests.post(webhook_url, data=json.dumps(data), headers=headers)

# 输出响应结果
print("Status Code:", response.status_code)
print("Response Body:", response.text)

依赖安装

      若尚未安装 requests,请执行以下命令:

pip install requests

预期结果

      运行成功后,将返回企业微信的消息推送,并打印如下内容:

{"errcode": 0, "errmsg": "ok"}

关键词

      Python, Webhook, 企业微信, requests, JSON

上一篇      下一篇