Send Text, Markdown, and News Messages to WeChat Work with Python Webhooks

This guide shows how to use Python to post text, markdown, and image‑text (news) messages to a WeChat Work webhook, covering required headers, payload structures, and example code for each message type.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Send Text, Markdown, and News Messages to WeChat Work with Python Webhooks

When automated test reports need to be delivered quickly and accurately, many teams rely on the organization’s contact data (phone numbers, enterprise WeChat groups) and a webhook provided by WeChat Work. The following sections demonstrate how to send three common message types—plain text, markdown, and news (image‑text)—using Python's requests library.

Sending a Plain Text Message

def send_txt(self):
    headers = {"Content-Type": "text/plain"}
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=56fbb81e-f143-41bc-8095-08e61520c161"
    send_data = {
        "msgtype": "text",
        "text": {
            "content": "深圳今日天气:32度,暴雨",
            "mentioned_list": ["@all"],
            "mentioned_mobile_list": ["13306534323"]
        }
    }
    res = requests.post(url=send_url, headers=headers, json=send_data)
    print(res.text)

The payload uses msgtype": "text" and includes optional mentioned_list or mentioned_mobile_list to notify specific members or all participants.

Sending a Markdown Message

def send_markdown(self):
    headers = {"Content-Type": "text/plain"}
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=56fbb81e-f143-41bc-8095-08e61520c161"
    send_data = {
        "msgtype": "markdown",
        "markdown": {
            "content": "# 测试
"
                       "#### **测试!**
"
                       "> 类型:测试 
"
                       "> 普通:测试 
"
                       "> VIP:测试 
"
                       "[这是一个链接](https://www.baidu.com/)"
        }
    }
    res = requests.post(url=send_url, headers=headers, json=send_data)
    print(res.text)

Markdown messages support headings (levels 1‑6), bold text, block quotes, and limited color tags. The example builds a multi‑line string that combines these features and adds a hyperlink.

Sending a News (Image‑Text) Message

def send_text_img(self):
    headers = {"Content-Type": "text/plain"}
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=56fbb81e-f143-41bc-8095-08e61520c161"
    send_data = {
        "msgtype": "news",
        "news": {
            "articles": [
                {
                    "title": "打开wpt测试环境",
                    "description": "打开wpt测试环境",
                    "url": "https://t.weipaitang.com/",
                    "picurl": "/Users/huaan9527/Desktop/117wmy20201030225936-6.png"
                },
                {
                    "title": "打开wpt测试环境",
                    "description": "这里是描述信息",
                    "url": "https://t.weipaitang.com/",
                    "picurl": "/Users/huaan9527/Desktop/117wmy20201030225936-6.png"
                }
            ]
        }
    }
    res = requests.post(url=send_url, headers=headers, json=send_data)
    print(res.text)

News messages can contain up to eight articles, each with a title, description, link, and image URL. The example shows two articles with local image paths; in production, replace them with publicly accessible URLs.

All three functions share the same webhook URL, which includes a unique key parameter that authenticates the request. Adjust the key, headers, and payload as needed for your own environment.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationMessagingAPIwechat-workwebhook
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.