Python enterprise WeChat notification is so simple

Last time I introduced how to make group notifications through DingTalk . Some students reported that they are more accustomed to using corporate WeChat or that their company has used corporate WeChat. They hope that they can also publish a Python-based corporate WeChat group notification tutorial, so there is this article.

In fact, the configuration and usage methods of the two are very similar, both of which are notified by robots. The following will teach you how to use Python to make group notifications to enterprise WeChat.

1. Prepare

Before starting, you need to make sure that Python and pip have been successfully installed on your computer. If not, you can visit this article: Super detailed Python installation guide  for installation.

(Optional 1)  If you use Python for data analysis, you can install Anaconda directly: Anaconda, a good helper for Python data analysis and mining , has built-in Python and pip.

(Optional 2)  In addition, it is recommended that you use the VSCode editor, which has many advantages: The best partner for Python programming—VSCode detailed guide .

Please choose one of the following ways to enter the command to install dependencies :
1. Open Cmd (Start-Run-CMD) in the Windows environment.
2. Open Terminal in the MacOS environment (command+space to enter Terminal).
3. If you are using VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.

pip install requests

2. Configure the enterprise WeChat robot

In order to be able to send corporate WeChat notifications through Python, first we need to add a group robot to the corporate WeChat group chat (there can be only you and the robot in this group):

1a6f1e7b7d4686bf0b898bd4ba158bf0.png

Then fill in the robot name:

94e07ada010de556a8483664ed7bbf62.png

Finally, a webhook address will be obtained:

e88788a49ac500b9057b6d73f7caffc0.png

Through this webhook address, we can send notifications to the enterprise WeChat through Python.

3. Python sends corporate WeChat notifications

Send a post request to the webhook address through the requests module to send a notification:

# 公众号:Python实用宝典
import requests
def send_weixin(content):
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的webhook密钥"  # 这里就是群机器人的Webhook地址
    headers = {"Content-Type": "application/json"} # http数据头,类型为json
    data = {
        "msgtype": "text",
        "text": {
            "content": content, # 让群机器人发送的消息内容。
            "mentioned_list": [],
        }
    }
    r = requests.post(url, headers=headers, json=data) # 利用requests库发送post请求
send_weixin("人工智能: 175")

The effect is as follows:

ed6efc27f70408c3165682fe48028ac3.png

In the menthoned_list parameter, you can also add the option of @everyone:

# 公众号:Python实用宝典
import requests
def send_weixin(content):
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的webhook密钥"  # 这里就是群机器人的Webhook地址
    headers = {"Content-Type": "application/json"} # http数据头,类型为json
    data = {
        "msgtype": "text",
        "text": {
            "content": content, # 让群机器人发送的消息内容。
            "mentioned_list": ["@all", ], # @全体成员
        }
    }
    r = requests.post(url, headers=headers, json=data) # 利用requests库发送post请求
send_weixin("人工智能: 175")

This will remind everyone to check the information at the same time as the message is sent.

In addition, the msgtype of the robot supports four message types: text, markdown, image, and news.

The sending method of Markdown is as follows:

# 公众号:Python实用宝典
import requests
def send_weixin_md(content):
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的webhook密钥"  # 这里就是群机器人的Webhook地址
    headers = {"Content-Type": "application/json"} # http数据头,类型为json
    data = {
        "msgtype": "markdown",
        "markdown": {
            "content": content,
            "mentioned_list": ["@all", ], # @全体成员
        }
    }
    r = requests.post(url, headers=headers, json=data) # 利用requests库发送post请求
send_weixin("实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
         >类型:<font color=\"comment\">用户反馈</font>
         >普通用户反馈:<font color=\"comment\">117例</font>
         >VIP用户反馈:<font color=\"comment\">15例</font>")
edbed1994470bef25c33704f78ffdc68.png

If you need to send a separate image, please use the image type, and pass the md5 and base64 of the image into the image parameter. The format of the complete data is as follows:

# 公众号:Python实用宝典
import requests
def send_weixin_images(MD5, base64data):
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的webhook密钥"  # 这里就是群机器人的Webhook地址
    headers = {"Content-Type": "application/json"} # http数据头,类型为json
    data = {
        "msgtype": "image",
        "image": {
            "base64": base64data,
            "md5": MD5
        }
    }
    r = requests.post(url, headers=headers, json=data) # 利用requests库发送post请求
send_weixin(MD5, base64data)

Note: The picture (before base64 encoding) cannot exceed 2M at most, and supports JPG and PNG formats. The effect is as follows:

143c5dded21e5635a378db4227763300.png

For graphic type, you only need to configure pictures, explanatory text and jump links, which is also very convenient:

# 公众号:Python实用宝典
import requests
def send_weixin_images(title, description, url, picurl):
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的webhook密钥"  # 这里就是群机器人的Webhook地址
    headers = {"Content-Type": "application/json"} # http数据头,类型为json
    data = {
        "msgtype": "news",
        "news": {
           "articles" : [
               {
                   "title" : title,
                   "description" : description,
                   "url" : url, 
                   "picurl" : picurl
               }
            ]
        }
    }
    r = requests.post(url, headers=headers, json=data) # 利用requests库发送post请求
send_weixin("中秋节礼品领取", "今年中秋节公司有豪礼相送", "www.qq.com", "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png")
6bc38f35fa618e9c6ecdb21f97d9bdbf.png

How about, do you need any of the above four notification types? If you have it, hurry up and use it!

This is the end of our article. If you like today's Python practical tutorial, please continue to pay attention to Python Practical Collection.

If you have any questions, you can reply in the background of the official account: join the group , answer the corresponding red letter verification information , and enter the mutual assistance group to ask.

Originality is not easy, I hope you can give me a thumbs up below and watch to support me to continue creating, thank you!

Click below to read the original text for a better reading experience

Python Practical Collection (pythondict.com)
is not just a collection.
Welcome to pay attention to the official account: Python Practical Collection

fa5d62ae15a2bc90a6f84d748465458f.jpeg

Guess you like

Origin blog.csdn.net/u010751000/article/details/128910722