架构图
通过数据库查询并推送值班人员信息,其中allday字段代表24小时值班,24小时值班时8点和17点均进行推送,不填写此字段时只在17点推送。
预览
值班人员提醒
人员未维护提醒
数据库维护,按此格式维护
renyuan表
zhibanbiao表
系统组成
zhibantouch.py 初始化数据库
zhiban.py 主程序
初始化数据库
首先手动创建库和用户,通过zhibantouch.py
初始化,生成表
import mysql.connector
mqdb = mysql.connector.connect(
host="127.0.0.1",
user="zhiban",
passwd="passwd",
database="zhiban"
)
mqcursor = mqdb.cursor()
mqcursor.execute("CREATE TABLE renyuan (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255), phone VARCHAR(255))charset=utf8mb4")
mqcursor.execute("CREATE TABLE zhibanbiao (id INT AUTO_INCREMENT PRIMARY KEY, time VARCHAR(255), name VARCHAR(255), allday VARCHAR(255))charset=utf8mb4")
mqdb.commit()
mqcursor.close()
初始化后的数据库
监控主程序
通过定时任务运行zhiban.py
主程序
定时任务,每天的8:01和17:01执行脚本
1 8,17 * * * python3 /root/zhibancheck/zhiban.py
程序代码
import requests,time
import mysql.connector
def check_user():
nowtime = time.strftime("%Y-%m-%d", time.localtime())
mqdb = mysql.connector.connect(
host="127.0.0.1",
user="zhiban",
passwd="passwd",
database="zhiban"
)
mqcursor = mqdb.cursor()
mqcursor.execute("SELECT * FROM zhibanbiao WHERE time = '%s'" % (nowtime))
if mqcursor.fetchall() == []:
return None
else:
mqcursor.execute(
"select renyuan.name,renyuan.phone,zhibanbiao.allday from zhibanbiao inner join renyuan on zhibanbiao.name=renyuan.name where zhibanbiao.time='%s'" % (
nowtime))
info = mqcursor.fetchall()
return info
def post_weixin(data):
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=用你自己的'
body = {
"msgtype": "news",
"news": {
"articles": [
{
"title": "值班信息",
"description": data,
"url": "90apt.com",
"picurl": "用你自己的图片"
}
]
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=body)
print(response.text)
print(response.status_code)
checkuser = check_user()
if time.strftime("%H", time.localtime()) == "8":
if checkuser == None:
post_weixin("没有维护值班人员信息")
elif checkuser[0][2] == "1":
post_weixin("今日值班人员 "+checkuser[0][0]+"\n电话:"+checkuser[0][1])
else:
None
elif time.strftime("%H", time.localtime()) == "17":
if checkuser == None:
post_weixin("没有维护值班人员信息")
elif checkuser[0][2] == "1":
None
else:
post_weixin("今晚值班人员 " + checkuser[0][0] + "\n电话:" + checkuser[0][1])
else:
print("禁止推送时间")
总结
简单
评论