前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【微信告警脚本】python3企业微信告

【微信告警脚本】python3企业微信告

作者头像
py3study
发布2020-01-03 13:10:53
2.6K0
发布2020-01-03 13:10:53
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
#!/usr/bin/python
# --*-- encoding=utf-8 --*--

import urllib.request
import json
import sys
import simplejson

def gettoken(corpid, corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    #print  gettoken_url
    try:
        token_file = urllib.request.urlopen(gettoken_url)
    except urllib.request.HTTPError as e:
        print(e.code)
        print(e.read().decode("utf8"))
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token

def senddata(content,accesstoken):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
    send_values = {
        "touser": '@all',  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。@all 发送所有人
        #"touser": "HX0011055|HX0011027",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        #"touser": "HX0010933",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        #"touser": "HX0011055",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty": "1",  # 企业号中的部门id。
        "msgtype": "text",  # 消息类型。
        "agentid": "1000003",  # 企业号中的应用id。
        "text": {
            "content": content
        },
        "safe": "0"
    }
    send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
    send_request = urllib.request.Request(send_url, send_data)
    response = json.loads(urllib.request.urlopen(send_request).read())
    print(str(response))

def deal(content):
    corpid = 'ww861ef6bad293d02311'  # CorpID是企业号的标识
    corpsecret = 'Pn3YA4H0d87He8ef-hpf0tGB5HzYUvsEtoLwUKsAHWott'  # corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid, corpsecret)
    senddata(content,accesstoken)

#通过deal 函数直接发送信息
message = '测试'
deal(message)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档