本文借鉴众多大佬的文章,纯粹小白外加无聊搞得,钉钉机器人使用token+加签发送,除库文件需要安装外代码完整,添加代码中的urltoken和secret即可使用!使用scheduler框架实现定时任务!如侵权请联系作者删掉!
import requests
import time
import datetime
import hmac
import hashlib
import base64
import urllib.parse
from colorama import init, Fore
from zhdate import ZhDate
from apscheduler.schedulers.blocking import BlockingScheduler
# 时间计算的一堆
def clock ():
inclock=time.localtime()
#上午
forenoon_value='{c0}-{c1}-{c2} 11-00-00'.format(c0=inclock.tm_year,c1=inclock.tm_mon,c2=inclock.tm_mday)
forenoon_clock = datetime.datetime.strptime(forenoon_value, '%Y-%m-%d %H-%M-%S')
#中午
high_noon='{c0}-{c1}-{c2} 13-00-00'.format(c0=inclock.tm_year,c1=inclock.tm_mon,c2=inclock.tm_mday)
high_noon_clock = datetime.datetime.strptime(high_noon, '%Y-%m-%d %H-%M-%S')
#下午
afternoon_value='{c0}-{c1}-{c2} 18-00-00'.format(c0=inclock.tm_year,c1=inclock.tm_mon,c2=inclock.tm_mday)
afternoon_clock = datetime.datetime.strptime(afternoon_value, '%Y-%m-%d %H-%M-%S')
#固定时间
example = '{c0}-{c1}-{c2} 17-30-00'.format(c0=inclock.tm_year,c1=inclock.tm_mon,c2=inclock.tm_mday)
datetime_emp = datetime.datetime.strptime(example, '%Y-%m-%d %H-%M-%S')
#实时时间
datetime_clock = time.strftime('%Y-%m-%d %H-%M-%S',inclock)
datetime_now = datetime.datetime.strptime(datetime_clock, '%Y-%m-%d %H-%M-%S')
difference_value=datetime_emp-datetime_now
#计算
if datetime_now<forenoon_clock:
clocktime='上午好! '
elif forenoon_clock<=datetime_now<high_noon_clock:
clocktime='中午好! '
elif high_noon_clock<=datetime_now<afternoon_clock:
clocktime='下午好! '
else:
clocktime='晚上好! '
return clocktime,difference_value,datetime_clock
# DingTalk群机器人计算加签
timestamp = str(round(time.time() * 1000))
urlToken=''
secret = ''
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
Dingtalkurl=urlToken+'×tamp='+timestamp+'&sign='+sign
# 计算距节假日日期
def get_week_day(date):
week_day_dict = {
0: '星期一',
1: '星期二',
2: '星期三',
3: '星期四',
4: '星期五',
5: '星期六',
6: '星期天',
}
day = date.weekday()
return week_day_dict[day]
def time_parse(today):
distance_year = (datetime.datetime.strptime(f"{today.year}-01-01", "%Y-%m-%d").date() - today).days
distance_year = distance_year if distance_year > 0 else (
datetime.datetime.strptime(f"{today.year + 1}-01-01", "%Y-%m-%d").date() - today).days
distance_big_year = (ZhDate(today.year, 1, 1).to_datetime().date() - today).days
distance_big_year = distance_big_year if distance_big_year > 0 else (
ZhDate(today.year + 1, 1, 1).to_datetime().date() - today).days
distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05", "%Y-%m-%d").date() - today).days
distance_4_5 = distance_4_5 if distance_4_5 > 0 else (
datetime.datetime.strptime(f"{today.year + 1}-04-05", "%Y-%m-%d").date() - today).days
distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01", "%Y-%m-%d").date() - today).days
distance_5_1 = distance_5_1 if distance_5_1 > 0 else (
datetime.datetime.strptime(f"{today.year + 1}-05-01", "%Y-%m-%d").date() - today).days
distance_5_5 = (ZhDate(today.year, 5, 5).to_datetime().date() - today).days
distance_5_5 = distance_5_5 if distance_5_5 > 0 else (
ZhDate(today.year + 1, 5, 5).to_datetime().date() - today).days
distance_8_15 = (ZhDate(today.year, 8, 15).to_datetime().date() - today).days
distance_8_15 = distance_8_15 if distance_8_15 > 0 else (
ZhDate(today.year + 1, 8, 15).to_datetime().date() - today).days
distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01", "%Y-%m-%d").date() - today).days
distance_10_1 = distance_10_1 if distance_10_1 > 0 else (
datetime.datetime.strptime(f"{today.year + 1}-10-01", "%Y-%m-%d").date() - today).days
distance_25 = (datetime.datetime.strptime(f"{today.year}-{today.month}-25", "%Y-%m-%d").date() - today).days
distance_25 = distance_25 if distance_25 > 0 else (
datetime.datetime.strptime(f"{today.year}-{today.month+1}-25", "%Y-%m-%d").date() - today).days
weekday=5-today.weekday()
return weekday,distance_year,distance_big_year,distance_4_5,distance_5_1,distance_5_5,distance_8_15,distance_10_1,distance_25
def today_ ():
today = datetime.date.today()
now_ = f"{today.year}年{today.month}月{today.day}日"
week_day_ = get_week_day(today)
weekday,distance_year,distance_big_year,distance_4_5,distance_5_1,distance_5_5,distance_8_15,distance_10_1,distance_25=time_parse(today)
return now_,week_day_,weekday,distance_year,distance_big_year,distance_4_5,distance_5_1,distance_5_5,distance_8_15,distance_10_1,distance_25
# 编码utf-8
headers = {"Content-Type": "application/json;charset=utf-8"}
# txt格式/内容
def v():
content = {
"msgtype": "text",
"text": {
"content":
'【摸鱼办】温馨提示您:\n'
'\n'
'{clock_time} 摸鱼人!工作再累,一定不要忘记摸鱼哦!\n'
'今天是:{today}\n'
'当前时间:{clock}\n'
'• 距离下班:{clock_value}\n'
'• 距离周末:{weekday}天\n'
'• 距发工资:{distance_25}天\n'
'• 距离元旦:{distance_year}天\n'
'• 距离过年:{distance_big_year}天\n'
'• 距离清明:{distance_4_5}天\n'
'• 距离劳动:{distance_5_1}天\n'
'• 距离端午:{distance_5_5}天\n'
'• 距离中秋:{distance_8_15}天\n'
'• 距离国庆:{distance_10_1}天\n'
.format(clock=clock()[2],
clock_value=clock()[1],clock_time=clock()[0],today=today_()[0]+' | '+today_()[1],weekday=today_()[2],
distance_year=today_()[3],distance_big_year=today_()[4],distance_4_5=today_()[5],
distance_5_1=today_()[6],distance_5_5=today_()[7],distance_8_15=today_()[8],
distance_10_1=today_()[9],distance_25=today_()[10])
},
"at": {
#@电话某人
"atMobiles": [''],
#是否@所有人
"isAtAll": False
}
}
return content
# 请求开始
def myfunction():
today_()
clock()
content=v()
requests.post(url=Dingtalkurl,headers=headers,json=content)
# 实例化
scheduler=BlockingScheduler()
scheduler.add_job(myfunction,'cron',hour=9,minute=59)
scheduler.start()