Skip to content

Commit 2d21e67

Browse files
authored
Update wechat_qiye_alert.py
完善注释
1 parent e9f1a8c commit 2d21e67

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

wechat_qiye_alert.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import requests
1313

1414
'''
15-
##########################################################
16-
# 微信企业号推送消息 #
17-
# #
18-
# 作者: AnJia <[email protected]> #
19-
# 作者博客: https://anjia.ml/ #
20-
# Github: https://github.com/anjia0532/weixin-qiye-alert #
21-
# #
22-
##########################################################
15+
#################################################################
16+
# 微信企业号推送消息 #
17+
# #
18+
# 作者: AnJia <[email protected]> #
19+
# 作者博客: https://anjia.ml/ #
20+
# Github: https://github.com/anjia0532/elastalert-wechat-plugin #
21+
# #
22+
#################################################################
2323
'''
2424
class WeChatAlerter(Alerter):
2525

@@ -44,20 +44,22 @@ def create_default_title(self, matches):
4444
return subject
4545

4646
def alert(self, matches):
47+
48+
# 参考elastalert的写法
4749
# https://github.com/Yelp/elastalert/blob/master/elastalert/alerts.py#L236-L243
50+
4851
body = self.create_alert_body(matches)
4952

53+
# 微信企业号获取Token文档
5054
# http://qydev.weixin.qq.com/wiki/index.php?title=AccessToken
5155
self.get_token()
5256

53-
#print self.access_token
54-
#print self.expires_in
55-
5657
self.senddata(body)
57-
elastalert_logger.info("发送消息给 %s" % (self.corp_id))
58+
elastalert_logger.info("send message to %s" % (self.corp_id))
5859

5960
def get_token(self):
6061

62+
#获取token是有次数限制的,本想本地缓存过期时间和token,但是elastalert每次调用都是一次性的,不能全局缓存
6163
#if self.expires_in >= datetime.datetime.now() and not self.access_token:
6264
# return self.access_token
6365

@@ -74,7 +76,11 @@ def get_token(self):
7476
token_data = token_file.read().decode('utf-8')
7577
token_json = json.loads(token_data)
7678
token_json.keys()
77-
79+
80+
if token_json['access_token'] is None :
81+
print token_data
82+
sys.exit()
83+
7884
#获取access_token和expires_in
7985
self.access_token = token_json['access_token']
8086
#self.expires_in = datetime.datetime.now() + datetime.timedelta(seconds=token_json['expires_in'])
@@ -87,15 +93,16 @@ def senddata(self, content):
8793

8894
if len(content) > 2000:
8995
content = content[:1997] + "..."
90-
96+
# 微信发送消息文档
97+
# http://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F
9198
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.access_token
9299

93100
headers = {'content-type': 'application/json'}
94101

95102
payload = {
96-
"touser": self.user_id,
97-
"toparty": self.party_id,
98-
"totag": self.tag_id,
103+
"touser": self.user_id, #用户账户,建议使用tag
104+
"toparty": self.party_id, #部门id,建议使用tag
105+
"totag": self.tag_id, #tag可以很灵活的控制发送群体细粒度。比较理想的推送应该是,在heartbeat或者其他elastic工具自定义字段,添加标签id。这边根据自定义的标签id,进行推送
99106
'msgtype': "text",
100107
"agentid": self.agent_id,
101108
"text":{
@@ -105,6 +112,7 @@ def senddata(self, content):
105112
}
106113

107114
# set https proxy, if it was provided
115+
# 如果需要设置代理,可修改此参数并传入requests
108116
# proxies = {'https': self.pagerduty_proxy} if self.pagerduty_proxy else None
109117
try:
110118
response = requests.post(send_url, data=json.dumps(payload, ensure_ascii=False), headers=headers)
@@ -118,4 +126,4 @@ def senddata(self, content):
118126

119127

120128
def get_info(self):
121-
return {'type': 'WeChatAlerter'}
129+
return {'type': 'WeChatAlerter'}

0 commit comments

Comments
 (0)