1
2
|
[root@monitor weixin]# curl "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx026430a7e676a190&corpsecret=tbb9lAJRS-tY96qzH0q8hcecCx563GceEKXTWmrvUQYXb52v90AeVBNxBP1O2dq1"
{"access_token":"-z6APpKnSGFgPHNjPgX24Nu9ph7uexsFQOjj2-I8YWZ0F-9_CJ5CgF2WnkX901Lu","expires_in":7200}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@monitor weixin]
# cat port_data.py
#!/usr/bin/env python
import
requests
url
=
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=WkLoCGsyUk7ItDtwG2P5KqX6EPvhIXlzgc4BbUAROAfjAJBD1ZRebeuIOG-_ylUF"
data
=
{
"touser"
:
"xiaoluo"
,
"msgtype"
:
"text"
,
"agentid"
:
1
,
"text"
: {
"content"
:
"hello ,xiaoluoge"
},
"safe"
:
"0"
}
r
=
requests.post(url,json
=
data)
print
r.status_code
print
r.content
|
1
2
3
|
[root@monitor weixin]
# python port_data.py
200
{
"errcode"
:
0
,
"errmsg"
:
"ok"
}
|
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
|
[root@monitor alertscripts]
# cat post_data.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
requests
import
json
import
sys
class
weChat:
def
__init__(
self
,Corpid,Secret):
url
=
'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'
%
(Corpid,Secret)
res
=
self
.url_req(url)
self
.token
=
res[
"access_token"
]
def
url_req(
self
,url):
req
=
requests.get(url)
res
=
json.loads(req.text)
return
res
def
send_message(
self
,user,content):
url
=
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
%
self
.token
data
=
{
"touser"
: user,
"msgtype"
:
"text"
,
"agentid"
:
1
,
"text"
: {
"content"
: content
},
"safe"
:
"0"
}
res
=
requests.post(url,json
=
data)
if
json.loads(res.content)[
'errmsg'
]
=
=
'ok'
:
return
"send message sucessed"
else
:
return
res
if
__name__
=
=
'__main__'
:
user
=
sys.argv[
1
]
content
=
sys.argv[
2
]
get_token
=
weChat(
id
,Secret)
print
get_token.send_message(user,content)
|