[Python] 纯文本查看 复制代码
# coding=utf-8
import time
import requests
class WX:
def __init__(self):
self.corpid = ""#企业ID
self.corpsecret = ""#汇报应用Secret
self.access_url = " https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(
self.corpid, self.corpsecret
)
self.webhook = ""#群Webhook地址
def GET_ACCESS_Token(self):
response = requests.get(self.access_url).json()
print(response)
access_token = response['access_token']
return access_token
def get_record_list(self, cursor=0):
starttime = str(time.mktime(time.strptime(str(time.strftime("%Y-%m-%d") + " 00:00:00"), '%Y-%m-%d %H:%M:%S')))[
:-2]
endtime = str(time.mktime(time.strptime(str(time.strftime("%Y-%m-%d") + " 23:59:59"), '%Y-%m-%d %H:%M:%S')))[
:-2]
print(int((time.time()-60)))
url = "https://qyapi.weixin.qq.com/cgi-bin/oa/journal/get_record_list?access_token={}".format(self.GET_ACCESS_Token())
data = {
#"starttime": int(time.time()), #正式使用的时间戳
"starttime": int((time.time()-60)), #测试使用的时间戳
#"endtime": int(time.time()+86400), #正式使用的时间戳
"endtime": int((time.time()+60)), #测试使用的时间戳
"cursor": cursor,
"limit": 100,
"filters": [
{
"key": "template_id",
"value": "3TjzDqVEqLJFs6GrC7T5WNXiU1LiGmLY1guJ7hs4"#订单号,日报的
}
]
}
response = requests.post(url=url, json=data).json()
# print(response)
# print(len(response['journaluuid_list']))
print(response)
result = [[response['next_cursor']], response['journaluuid_list']]
print('1111',result)
return result
def GET_details(self, journaluuid):
edg = 1
user_list = [["张三", "******"], ["李四", '********'], ["王五", '18*****191']]#这个地方无法获取用户名,所以做了一个手动列表,前面用户名,后面是企业微信用户ID
url = "https://qyapi.weixin.qq.com/cgi-bin/oa/journal/get_record_detail?access_token={}".format(str(self.GET_ACCESS_Token()))
data = {
"journaluuid": journaluuid,
}
response = requests.post(url=url, json=data).json()['info']
print(response)
template_name = response['template_name']
report_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(response['report_time']))
submitter = response['submitter']['userid']
for user in user_list:
if submitter in user:
submitter = user[0]
receiverss = []
number = 0
while True:
try:
receivers = response['receivers'][number]['userid']
for user in user_list:
if receivers in user:
receiver = user[0]
receiverss.append(receiver)
number += 1
except Exception:
break
try:
receivers = ','.join(receiverss)
Product = response['apply_data']['contents'][0]['value']['text']
Phone = response['apply_data']['contents'][1]['value']['text']
Customer_Name = response['apply_data']['contents'][2]['value']['text']
Customer_Company = response['apply_data']['contents'][3]['value']['text']
Province = response['apply_data']['contents'][4]['value']['text']
Company_category = response['apply_data']['contents'][5]['value']['selector']['options'][0]['value'][0][
'text']
MarkDownText = " ### %s\r %s\r\n> ### 今日工作:\n<font color=\"warning\"> %s\r</font>\n" "> ### 明日计划:\n <font color=\"info\"> %s\r</font>\n 提交时间:%s" %(
submitter,template_name, Product, Phone, report_time
)
print(MarkDownText)
return MarkDownText,edg
except Exception as e:
print(e)
edg = 0
receivers = ','.join(receiverss)
Product = response['apply_data']['contents'][0]['value']['text']
Phone = response['apply_data']['contents'][1]['value']['text']
Customer_Name = response['apply_data']['contents'][2]['value']['text']
MarkDownText = " ### %s\r %s\r\n> ### 今日工作:\n<font color=\"warning\"> %s\r</font>\n" "> ### 明日计划:\n <font color=\"info\"> %s\r</font>\n 提交时间:%s" %(
submitter,template_name, Product, Phone, report_time
)
print(MarkDownText)
return MarkDownText,edg
def Run(self):
try:
next_cursor = [0]
record_list = self.get_record_list(next_cursor[0])
print(record_list)
next_cursor = record_list[0]
record_list = record_list[1]
details_list = record_list[-1]
"模板不一样所以暂时把单号固定"
# details = "8WBtd3jNAAfiJtjHRHm8x64LiRx4oJFLX92kvvVnQ4SRaFbqsEYp4Jji981BwAexjP"
Text, edg = self.GET_details(journaluuid=details_list)
headers = {"Content-Type": "text/plain"}
data = {
"msgtype": "markdown",
"markdown": {
"content": Text,
}
}
if edg == 0:
response = requests.post(url=self.webhook, headers=headers, json=data).text
print("pooo")
print(response)
else:
url = ''#机器人地址
response = requests.post(url=url, headers=headers, json=data).text
print("pooo2")
print(response)
except:
print("还没更新")
# for details in details_list:
# Text = self.GET_details(journaluuid=details)
# headers = {"Content-Type": "text/plain"}
# data = {
# "msgtype": "markdown",
# "markdown": {
# "content": Text,
# }
# }
# response = requests.post(url=self.webhook, headers=headers, json=data).text
# print("pooo")
# print(response)
if __name__ == '__main__':
while 1:
app = WX()
app.Run()
time.sleep(60)