目录
  • 一、概述
  • 二、python脚本
  • 三、企业微信设置
    • 1. 注册企业微信
    • 2. 点击进入管理后台
    • 3. 创建应用完成后
    • 4. 查看企业id
    • 5. 查看部门id
  • 四、测试脚本

    一、概述

    本文将介绍如何使用python3给企业微信发送消息。我的环境是linux + python3.6.10。

    二、python脚本

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time    : 2020/8/20 上午12:42
    # @author  : wangying
    # @site    : 
    # @file    : 企业微信.py
    # @software: pycharm
     
    #!/root/.virtualenvs/wechat/bin/python
    # usage: send message via wechat
     
    import requests, sys, json
    import urllib3
    urllib3.disable_warnings()
     
     
    ###填写参数###
     
    # corpid是企业号的标识
    corpid = "ww32a580fceb30f350"
     
    # secret是管理组凭证密钥
    secret = "qybtbg1ql9lmnwaerqv1dmv0y_g3lzre268e0hptdqk"
     
    # 应用id
    agentid = "1000004"
     
    # token_config文件放置路径
    token_config = r'/tmp/zabbix_wechat_config.json'
     
    ###下面的代码都不需要动###
     
     
    def gettokenfromserver(corpid, secret):
        """获取access_token"""
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        data = {
            "corpid": corpid,
            "corpsecret": secret
        }
        r = requests.get(url=url, params=data, verify=false)
        print(r.json())
        if r.json()['errcode'] != 0:
            return false
        else:
            token = r.json()['access_token']
            file = open(token_config, 'w')
            file.write(r.text)
            file.close()
            return token
     
     
    def sendmessage(partyid, subject, content):
        """发送消息"""
        # 获取token信息
        try:
            file = open(token_config, 'r')
            token = json.load(file)['access_token']
            file.close()
        except:
            token = gettokenfromserver(corpid, secret)
     
        # 发送消息
        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % token
        data = {
            "toparty": partyid,
            "msgtype": "text",
            "agentid": agentid,
            "text": {"content": subject + '\n' + content},
            "safe": "0"
        }
        r = requests.post(url=url, data=json.dumps(data), verify=false)
     
        # 如果发送失败,将重试三次
        n = 1
        while r.json()['errcode'] != 0 and n < 4:
            n = n + 1
            token = gettokenfromserver(corpid, secret)
            if token:
                url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % token
                r = requests.post(url=url, data=json.dumps(data), verify=false)
                print(r.json())
     
        return r.json()
     
     
    if __name__ == '__main__':
        # 部门id
        partyid = '20'
        # 消息标题
        subject = '自应用程序代码测试'
        # 消息内容
        content = 'str(sys.argv[3])'
        status = sendmessage(partyid, subject, content)
        print(status)

    需要修改的地方,其中corpid,secret,agentid我会在后面的截图指出来要填的值。

    下行是linux环境下python3的可执行路径,如果是windows,那么这行就不需要,只要将python的路径加入到path即可

    #!/root/.virtualenvs/wechat/bin/python

    # corpid是企业id

    corpid = “ww34d7fed41a8d5dxx”

    # secret是管理组凭证密钥

    secret = “8qwzoi0xcvbp1zncbnqj1d6ueiv-lrbcjp93urhfxxx”

    # agentid是应用id

    agentid = “1000002”

    # token_config里面存放token信息,这个自己随便填就行,只要自己有写的权限

    token_config = r’/tmp/zabbix_wechat_config.json’

    三、企业微信设置

    1. 注册企业微信

    https://qy.weixin.qq.com/

    2. 点击进入管理后台

    创建应用

    3. 创建应用完成后

    agentid,secret就出来了

    4. 查看企业id

    这个就是脚本中的corpid

    5. 查看部门id

    我这个python脚本是通过部门id进行群体发送,所以这个部门id到后面会有用。

    四、测试脚本

    直接运行:python manage.py runserver

    以上就是使用python实现给企业微信发送消息功能的详细内容,更多关于python企业微信发送消息的资料请关注www.887551.com其它相关文章!