-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjkweixin.py
50 lines (43 loc) · 1.52 KB
/
jkweixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import urllib.request
import json
#--------------------------------
# 获取企业微信token
#--------------------------------
def get_token(url, corpid, corpsecret):
token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret)
token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token']
return token
#--------------------------------
# 构建告警信息json
#--------------------------------
def messages(msg):
values = {
"touser": '@all',
"msgtype": 'text',
"agentid": 1000004, #偷懒没有使用变量了,注意修改为对应应用的agentid
"text": {'content': msg},
"safe": 0
}
msges=(bytes(json.dumps(values), 'utf-8'))
return msges
#--------------------------------
# 发送告警信息
#--------------------------------
def send_message(url,token, data):
send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token)
respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read()
x = json.loads(respone.decode())['errcode']
# print(x)
if x == 0:
print ('Succesfully')
else:
print ('Failed')
##############函数结束########################
corpid = 'wwbd9b074d49559d1e'
corpsecret = 'xehSTs-KHtUWU9GCEF3EXGVL0nWsLY7W2RLX4Sr5Irg'
url = 'https://qyapi.weixin.qq.com'
msg='jenkines监控测试'
#函数调用
test_token=get_token(url, corpid, corpsecret)
msg_data= messages(msg)
send_message(url,test_token, msg_data)