Python Example for Sending Text, Markdown, and News Messages via Enterprise WeChat Bot

This article presents a Python script that shows how to programmatically send text, markdown, and image‑text (news) messages through an Enterprise WeChat robot, enabling automated test report notifications that incorporate phone numbers and group mentions.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Example for Sending Text, Markdown, and News Messages via Enterprise WeChat Bot

This article provides a Python script that demonstrates how to send text, markdown, and news (image‑text) messages through an Enterprise WeChat robot, useful for automating test report notifications that rely on organizational data such as phone numbers and group mentions.

import requests
import json

class WeWork_Send_Msg():
    # 文本类型消息
    def send_txt(self):
        headers = {"Content-Type": "text/plain"}
        # 测试python发送消息-企业微信
        send_url = "企业微信群消息机器人"
        send_data = {
            "msgtype": "text",  # 消息类型
            "text": {
                "content": "深圳今日天气:32度,暴雨",  # 文本内容,最长不超过2048个字节,必须是utf8编码
                "mentioned_list": ["@all"],
                # userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
                "mentioned_mobile_list": ["手机号码"]  # 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
            }
        }
        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)

    # 发送Markdown类型消息
    def send_markdown(self):
        headers = {"Content-Type": "text/plain"}
        send_url = "企业微信群消息机器人"
        send_data = {
            "msgtype": "markdown",  # 消息类型,此时固定为markdown
            "markdown": {
                "content": "# 测试
"
                           "#### **测试!**
"
                           "> 类型:测试 
"
                           "> 普通:测试 
"
                           "> VIP:测试 
"
                           "[这是一个链接](https://www.baidu.com/)"
            }
        }
        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)

    # 发送图文类型消息
    def send_text_img(self):
        headers = {"Content-Type": "text/plain"}
        send_url = "企业微信群消息机器人"
        send_data = {
            "msgtype": "news",  # 消息类型,此时固定为news
            "news": {
                "articles": [  # 图文消息,一个图文消息支持1到8条图文
                    {
                        "title": "打开链接",  # 标题,不超过128个字节,超过会自动截断
                        "description": "打开链接",  # 描述,不超过512个字节,超过会自动截断
                        "url": "https://www.baidu.com/",  # 点击后跳转的链接。
                        "picurl": "/117wmy20201030225936-6.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    },
                    {
                        "title": "打开wpt测试环境",
                        "description": "这里是描述信息",
                        "url": "https://www.baidu.com/",
                        "picurl": "/wmy20201030225936-6.png"
                    }
                ]
            }
        }
        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)

if __name__ == '__main__':
    # WeWork_Send_Msg().send_txt()  # 文本类型消息
    WeWork_Send_Msg().send_markdown()  # 发送Markdown类型消息
    WeWork_Send_Msg().send_text_img()  # 发送图文类型消息

Replace the placeholder webhook URL and any required credentials with your actual Enterprise WeChat robot endpoint, customize the message content as needed, and run the script to deliver notifications.

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.

MessagingAPIWeChat
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.