本文实例为大家分享了python3实现公众号每日定时发送的具体代码,供大家参考,具体内容如下
步骤是这样:先申请公众号,找到接口文件。看了之后发现主要是通过corpid(企业秘钥)和corpsecret(应用秘钥)获得登录token,通过这个token进入各个url操作。
我这个用的是企业微信,所以有部门。其他公众号也类似。结果如下:
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
81
82
83
|
# -*- coding:utf-8 -*-
import requests
import json
import time
url0 = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
def get_group_id(): #查看部门与成员
values_address = { 'corpid' : '你的corpid' ,
'corpsecret' : 通讯录corpsecret',
}
req = requests.post(url0, params = values_address)
data = json.loads(req.text)
token = data[ "access_token" ]
url_department = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=" + token #部门
r_department = requests.get(url_department)
result_department = r_department.json()
result_department_no = result_department[ 'department' ]
print ( "***已获取部门信息如下:" )
for item in result_department_no:
print ( "[部门]:" ,item[ 'id' ], " [部门名称]:" ,item[ 'name' ], " [父部门]:" ,item[ 'parentid' ], " [序号]:" ,item[ 'order' ])
print ( "***已获取成员信息如下:" )
for i in range ( len (result_department_no)):
i = i + 1
url_member = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=%s&fetch_child=fetch_child" % (token, i) # 成员
r_member = requests.get(url_member)
result_member = r_member.json()
result_member_no = result_member[ 'userlist' ]
for item in result_member_no:
print ( "[成员id]:" , item[ 'userid' ], " [成员名称]:" , item[ 'name' ], " [所属部门]:" , item[ 'department' ])
return result_department_no,result_member_no
def upload_img():
values_address = { 'corpid' : '你的corpsecret' ,
'corpsecret' : '应用corpsecret' ,
}
req = requests.post(url0, params = values_address)
data = json.loads(req.text)
token = data[ "access_token" ]
print ( "***已获取token." )
url_upimg = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (token, "image" )
files = { 'filename' : ( 'xn.jpg' , open ( "d:\docs\day\邮件/drjpg.jpg" , 'rb' ))
} # 显式的设置文件名
values_upimg = {
"content - type" : 'multipart/form-data; boundary="----webkitformboundaryn5uouhkhfu8g2xnp";' ,
"content - length" : '331698; boundary="----webkitformboundaryn5uouhkhfu8g2xnp";' ,
"content - disposition" : 'form-data; name="image"; boundary=----webkitformboundaryn5uouhkhfu8g2xnp;' ,
"content - type" : "application/octet-stream; boundary=----webkitformboundaryn5uouhkhfu8g2xnp;"
}
req_upimg = requests.post(url_upimg,files = files, data = values_upimg)
data = json.loads(req_upimg.text)
media_id = data[ 'media_id' ]
print ( "***已获取素材所需id." )
return token,media_id
def send_msg(token,media_id): #发送图片
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
values = {
"touser" : "@all" ,
"toparty" : "2" , #***************部门******************
"msgtype" : "image" ,
"agentid" : 1000003 ,
"image" : {
"media_id" : media_id
},
"safe" : 0
}
data = json.dumps(values)
req = requests.post(url, data)
print ( "返回结果:" , req.text)
return req
#打印返回信息
while true:
current_time = time.localtime(time.time())
if ((current_time.tm_hour = = 8 ) and (current_time.tm_min = = 13 ) and (current_time.tm_sec = = 50 )):
(result_department_no, result_member_no) = get_group_id()
(token, media_id) = upload_img()
send_msg(token,media_id)
time.sleep( 1 )
|
效果是这样:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_37408031/article/details/78616089