本文实例为大家分享了python3实现公众号每日定时发送的具体代码,供大家参考,具体内容如下

步骤是这样:先申请公众号,找到接口文件。看了之后发现主要是通过corpid(企业秘钥)和corpsecret(应用秘钥)获得登录token,通过这个token进入各个url操作。

我这个用的是企业微信,所以有部门。其他公众号也类似。结果如下:

python3实现公众号每日定时发送日报和图片

# -*- 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"+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" % (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"%(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"+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)

效果是这样:

python3实现公众号每日定时发送日报和图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python3公众号每日定时发送,python公众号定时发送图片,python公众号定时发送

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?