'''
在微信企业版的群聊里,可以添加群聊机器人,群聊机器人会开放一个https接口
通过https接口就可以用代码控制群聊机器人推送消息
本脚本实现一个番茄工作法的闹钟提醒功能
作者: 林新发
'''
import requests
import time
import time,datetime
import json
# 法定节假日信息
holiday_info = {}
# 今年的年份,用来获取法定节假日信息
CUR_YEAR = '2020'
# 机器人的webhook地址
WORK_WX_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxxxxxxx'
# 法定节假日信息
def init_holiday_info():
global holiday_info
rep = requests.get('http://tool.bitefu.net/jiari/?d=' + CUR_YEAR)
info_txt = rep.content.decode()
holiday_info = json.loads(info_txt)
# 判断今天是否是工作日(过滤法定节假日和周末)
def check_if_is_work_day():
day_info = time.strftime("%m%d",time.localtime(time.time()))
if day_info in holiday_info[CUR_YEAR]:
return False
week = datetime.datetime.now().weekday()
if 0 <= week and 4 >= week:
return True
return False
# 检测现在是否为上班时间:早上9:00 到 12:00,下午13:30到18:30
def check_if_is_work_time():
now = datetime.datetime.now()
if now.hour >= 9 and now.hour <= 11:
return True
elif now.hour >= 13 and now.hour < 14 and now.minute >= 30:
return True
elif now.hour >= 14 and now.hour <= 17:
return True
elif now.hour >= 18 and now.hour < 19 and now.minute <= 30:
return True
return False
# 执行hppts的post请求给微信企业版的群聊机器人
def post_to_wx(content):
data = {
"msgtype": "text",
"text": {
"content": content
}
}
headers = {'Content-Type':'application/object'}
jdata = json.dumps(data)
jdata = bytes(jdata, encoding="utf8")
rep = requests.post(url=WORK_WX_URL, data=jdata, headers=headers)
def work():
print('work')
post_to_wx('#番茄工作法#\n25分钟工作时间\n专注工作')
def rest():
print('rest')
post_to_wx('#番茄工作法#\n5分钟休息时间\n休息一下眼睛,喝喝水,扭扭腰')
#按分钟计时
def run(workTime,interval):
while True:
try:
if check_if_is_work_day() and check_if_is_work_time():
work();
time.sleep(workTime*60)
rest()
time.sleep(interval*60)
else:
time.sleep(1)
print('sleep')
except Exception as e:
print(e)
if __name__ == "__main__":
# 工作时间,25分钟
workTime = 25
# 休息时间,5分钟
interval = 5
init_holiday_info()
run(workTime,interval)
来源:CSDN
作者:林新发
链接:https://blog.csdn.net/linxinfa/article/details/104258936