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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
python2.x
注意:data变量里, agent_id为刚刚创建的应用
id
(可在web页面看到)
toparty即为目标部门,或者可以用touser,totag指定目标账户
比较简单的调用,已实测,可以使用。
#coding:utf-8
import
sys
import
requests
import
json
from
pymongo
import
MongoClient
reload
(sys)
sys.setdefaultencoding(
'utf-8'
)
class
Weixin(
object
):
def
__init__(
self
, corp_id, corp_secret):
self
.token_url
=
'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'
%
(corp_id, corp_secret)
self
.send_url
=
'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='
def
get_token(
self
):
try
:
r
=
requests.get(
self
.token_url, timeout
=
10
)
except
Exception as e:
print
e
sys.exit(
1
)
if
r.status_code
=
=
requests.codes.ok:
data
=
r.json()
if
data.get(
'errcode'
):
print
data[
'errmsg'
]
sys.exit(
1
)
return
data[
'access_token'
]
else
:
print
r.status_code
sys.exit(
1
)
def
send(
self
,message):
url
=
self
.send_url
+
self
.get_token()
data
=
{
"touser"
:
"hequan2011"
,
"msgtype"
:
"text"
,
"agentid"
:
"0"
,
"text"
: {
"content"
: message
},
"safe"
:
"0"
}
send_data
=
json.dumps(data,ensure_ascii
=
False
)
try
:
r
=
requests.post(url, send_data)
except
Exception, e:
print
e
sys.exit(
1
)
if
r.status_code
=
=
requests.codes.ok:
print
r.json()
else
:
print
r.code
sys.exit(
1
)
corpid
=
'xxxxxxxxxxx'
corpsecret
=
'xxxxxxxxxxxxxxxxx'
client
=
MongoClient(
'mongodb://user:password@127.0.0.1:27017/'
)
db
=
client.ku
collection
=
db.biao
a
=
[]
for
data
in
collection.find():
a.append(data)
l
=
a[
0
]
g
=
l
z
=
str
(g[
"name"
])
z1
=
int
(g[
"jg"
])
print
z
msg
=
"1:{0}\n 2:{1}\n"
.
format
(z,z1)
w
=
Weixin(corpid,corpsecret)
w.send(msg)
|
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1898946,如需转载请自行联系原作者