import
time
import
requests
class
WX:
def
__init__(
self
):
self
.corpid
=
""
self
.corpsecret
=
""
self
.access_url
=
" https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}"
.
format
(
self
.corpid,
self
.corpsecret
)
self
.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()
-
60
)),
"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)
result
=
[[response[
'next_cursor'
]], response[
'journaluuid_list'
]]
print
(
'1111'
,result)
return
result
def
GET_details(
self
, journaluuid):
edg
=
1
user_list
=
[[
"张三"
,
"******"
], [
"李四"
,
'********'
], [
"王五"
,
'18*****191'
]]
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
]
"模板不一样所以暂时把单号固定"
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
(
"还没更新"
)
if
__name__
=
=
'__main__'
:
while
1
:
app
=
WX()
app.Run()
time.sleep(
60
)