七仔的博客

七仔的博客GithubPages分博

0%

企业微信应用每天60秒读懂世界

一个简单的python脚本,每天定时执行即可,基于运小站的每天60秒读懂世界API

企业微信应用每天60秒读懂世界

  • 一个简单的python3脚本,可以在服务用crond执行,或者使用宝塔面板做定时,执行一次发送一个每天60秒读懂世界的图片

  • 需要企业微信,然后新建应用,获取到corpid(企业ID)、agentid(企业应用ID)、corpsecret(应用的凭证密钥)

  • 脚本基于运小站的每天60秒读懂世界API,链接:http://bjb.yunwj.top/php/api/html.html

  • 如果因为该API失效导致脚本不可再用,我会第一时间修复,需要使用脚本的可以收藏我的博客地址或者添加我的微信或qq

脚本内容如下:

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
84
85
86
87
88
89
90
91
92
93
94
95
import logging
import requests
import json
import time


# http://bjb.yunwj.top/php/api/html.html # 接口文档地址
class CompanyWeixin:
__corp_id = 'xxxxxx' # 企业ID
__agent_id = 100001 # 企业应用ID(自己创建企业应用的ID)
__corp_secret = 'xxxxxxxxxxxx' # 应用的凭证密钥,此处应用(企业自己创建的应用):消息推送
__access_token = '' # 访问应用的临时凭证
__expires_in = 0 # 访问应用的临时凭证的有效时间(秒)

headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Pragma': 'no-cache',
'User-Agent': 'self-defind-user-agent',
'Cookie': 'name=self-define-cookies-in header',
'X-Requested-With': 'XMLHttpRequest'
}

@staticmethod
def send_post(url: str, json_data: dict):
try:
res = requests.post(url=url, data=json.dumps(json_data), headers=CompanyWeixin.headers)
return json.loads(res.content.decode("utf-8").encode("utf-8"))
except Exception as e:
logging.info('[send_get]Failed to json.load, {0}'.format(e))
return {}

@staticmethod
def send_get(url: str):
try:
res = requests.get(url=url)
return json.loads(res.content.decode("utf-8").encode("utf-8"))
except Exception as e:
logging.info('[send_get]Failed to json.load, {0}'.format(e))
return {}

def get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}'. \
format(CompanyWeixin.__corp_id, CompanyWeixin.__corp_secret)
result = CompanyWeixin.send_get(url)
self.__access_token = result['access_token']
return result

def upload_file(self, file_name):
try:
url = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type=image'. \
format(self.__access_token)
files = {"pic": (file_name, open(file_name, "rb"), "images/jpg")}
return requests.post(url=url, files=files).json()
except Exception as e:
logging.info('[send_get]Failed to json.load, {0}'.format(e))
return {}

def send_img(self, media_id, touser: str = '@all'):
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}'. \
format(self.__access_token)
data = {
"touser": touser,
"msgtype": "image",
"agentid": CompanyWeixin.__agent_id,
"image": {
"media_id": media_id
},
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
return CompanyWeixin.send_post(url, data)


def download_img(new_file_name):
tp1 = requests.get('http://bjb.yunwj.top/php/tp/lj.php').json()['tp1']
img = requests.get(url=tp1).content
with open(new_file_name, 'wb') as tmp:
tmp.write(img)


today_str = time.strftime("%Y-%m-%d", time.localtime())
file_name = today_str + '.jpg'
download_img(file_name)
weixin = CompanyWeixin()
weixin.get_access_token()
img_msg = weixin.upload_file(file_name)
img_msg_media_id = img_msg['media_id']
weixin.send_img(img_msg_media_id)

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/118.html

欢迎关注我的其它发布渠道