#! /bin/bash userid=$1 content=$2 python /data/zabbix/alertscripts/weixin.py $userid $content
#!/usr/bin/env python # _*_ coding:utf8 _*_ import urllib2 import simplejson as json import sys class weChat: def __init__(self,url,Corpid,Secret): url = ‘%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (url,Corpid,Secret) res = self.url_req(url) self.token = res[‘access_token‘] def url_req(self,url,method=‘get‘,data={}): if method == ‘get‘: req = urllib2.Request(url) res = json.loads(urllib2.urlopen(req).read()) elif method == ‘post‘: req = urllib2.Request(url,data) res = json.loads(urllib2.urlopen(req).read()) else: print ‘error request method...exit‘ sys.exit() return res def send_message(self,userlist,content,agentid=1000002): self.userlist = userlist self.content = content url = ‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s‘ % self.token data = { "touser": "", "toparty": "", "totag": "", "msgtype": "text", "agentid": "0", "text": { "content": "" }, "safe":"0" } data[‘touser‘] = userlist data[‘agentid‘] = agentid data[‘text‘][‘content‘] = content data = json.dumps(data,ensure_ascii=False) # print data res = self.url_req(url,method=‘post‘,data=data) if res[‘errmsg‘] == ‘ok‘: print ‘send sucessed!!!‘ else: print ‘send failed!!‘ print res if __name__ == ‘__main__‘: userlist = sys.argv[1] # userlist = ‘ZhangSen‘ content = sys.argv[2:] content = ‘\n‘.join(content) # content = ‘test1‘ Corpid = ‘wwdc52e74bcaa0dfc2‘ #此处对应修改 Secret = ‘Y1Xh5eQmlc7QSJ1RblNXEjppUVYfqwvWhiKJmBncOBk‘ #此处对应修改 url = ‘https://qyapi.weixin.qq.com‘ wechat = weChat(url,Corpid,Secret) wechat.send_message(userlist,content)
时间: 2024-10-29 19:07:20