企业微信应用推送消息 python代码 zabbix设置

企业微信应用推送消息 python代码

 

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip -V
pip install requests

cat >weixin_send.py <<EOF
#!/usr/bin/env python
# coding=utf-8
import requests
import sys

def get_token_in_time(corp_id,secret):
    res = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (corp_id, secret))
    res = res.json()
    token = res.get('access_token', False)
    return token

def get_auth_code(code):
    rest = requests.get('https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s' % (get_token_in_time(),code))
    return rest.content

#https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE


if __name__ == '__main__':
    if len(sys.argv) != 6:
        print("Usage:%s corpId SecretString AgentId MsgTxt ToUser" % sys.argv[0])
    else:

        corp_str = sys.argv[1]
        secret_str = sys.argv[2]
        agentid_str = str(sys.argv[3])
        msgtxt = sys.argv[4:][0]
        touser_str = sys.argv[-1]


        datastr=""" {"touser":"%s","msgtype":"text","agentid":%d,"text":{"content":"%s"}}""" % (touser_str,int(agentid_str),msgtxt)
        ACCESS_TOKEN = get_token_in_time(corp_str,secret_str)
        print("ACCESS_TOKEN::%s"%ACCESS_TOKEN)
        r = requests.post(url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % ACCESS_TOKEN,data=datastr)
        print r.text
EOF

###https://work.weixin.qq.com/ 登陆企业信息 ,
获取corpID(企业id) :
在 我的企业---企业信息---企业ID
获取AgentID:在应用管理--应用---选择应用名称进入--AgentId Secret

##微信消息推送测试
python
weixin_send.py corpId SecretString AgentId "微信消息推送测试" @all


###zabbix 报警媒介类型 设置

chmod a+x weixin_send.py
\cp weixin_send.py $(find / -name 'alertscripts')/

 

 

posted @ 2019-10-18 11:34  些许记忆  阅读(849)  评论(0)    收藏  举报