python给企业微信发送运维警告

第一步:获得token

def get_token():
    url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
    values = {'corpid': '×××××××××××', 'corpsecret': '××××××××××××'}
    r = requests.post(url, params=values)
    data = json.loads(r.text)
    return data["access_token"]

第二步:发送信息

def send_msg(msg):
    url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+get_token()
    values = {"touser": "×××", "toparty": "××", "msgtype": "text", "agentid": "×××", "text": {"content": msg}, "safe": 0}
    data = json.dumps(values)
    response = requests.post(url, data=data)
    errcode = json.loads(response.text)['errcode']
    if errcode == 0:
        print('Succesfully')
    else:
        print('Failed')
注意:×××部分需要根据企业微信获得
        "touser":"*******",   #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"2",        #企业号中的部门id。
        "msgtype":"text",     #消息类型。
        "agentid":"*******",  #企业号中的应用id。
 

企业微信登录管理员后台的页面
 
python调用企业微信接口发送报警信息
 
 
 
 
点击 "我的企业" 获取企业 ID (等一下代码中会用到)
 
python调用企业微信接口发送报警信息
 
 
 
 
点击 "应用与小程序" 创建应用 (报警信息将发送到应用中)
 
python调用企业微信接口发送报警信息
 
 
 
 
根据要求填写应用信息创建应用
 
python调用企业微信接口发送报警信息
 
 
 
 
获取 Agentid 和 Secret (等一下代码中会用到)
 
python调用企业微信接口发送报警信息

猜你喜欢

转载自blog.csdn.net/qq_24920947/article/details/85069603