企业微信应用推送消息 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) != 5: 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
##微信消息推送测试
python weixin_send.py corpId SecretString AgentId "微信消息推送测试" @all
###zabbix 报警媒介类型 设置
chmod a+x weixin_send.py \cp weixin_send.py $(find / -name ‘alertscripts‘)/