python⾃动发_python3实现每⽇定时发送⽇报和
图⽚
本⽂实例为⼤家分享了python3实现每⽇定时发送的具体代码,供⼤家参考,具体内容如下
步骤是这样:先申请,到接⼝⽂件。看了之后发现主要是通过corpid(企业秘钥)和corpsecret(应⽤秘钥)获得登录token,通过这个token进⼊各个url操作。
我这个⽤的是企业,所以有部门。其他也类似。结果如下:
# -*- coding:utf-8 -*-
import requests
import json
import time
url0 = 'qyapi.weixin.qq/cgi-bin/gettoken'
def get_group_id(): #查看部门与成员
python新手代码useridvalues_address = {'corpid': '你的corpid',
'corpsecret': 通讯录corpsecret',
}
req = requests.post(url0, params=values_address)
data = json.)
token = data["access_token"]
url_department="qyapi.weixin.qq/cgi-bin/department/list?access_token="+token #部门
r_(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 = "qyapi.weixin.qq/cgi-bin/user/simplelist?
access_token=%s&department_id=%s&fetch_child=FETCH_CHILD" % (token, i) # 成员
r_member = (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.)
token = data["access_token"]
print("***已获取token.")
url_upimg="qyapi.weixin.qq/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()
media_id=data['media_id']
print("***已获取素材所需id.")
return token,media_id
def send_msg(token,media_id): #发送图⽚
url="qyapi.weixin.qq/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("返回结果:", )
return req
#打印返回信息
while True:
current_time = time.localtime(time.time())
if ((_hour == 8) and (_min == 13) and (_sec == 50)): (result_department_no, result_member_no) = get_group_id()
(token, media_id) = upload_img()
send_msg(token,media_id)
time.sleep(1)
效果是这样:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持脚本之家。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。