diff --git a/.github/workflows/send_love.yml b/.github/workflows/send_love.yml index 09648dd..125b14a 100644 --- a/.github/workflows/send_love.yml +++ b/.github/workflows/send_love.yml @@ -1,22 +1,48 @@ name: CI on: schedule: - - cron: "5 0 * * *" + # 每天早上7:40运行 + - cron: "40 7 * * *" + # 每天下午5:00运行 + - cron: "0 17 * * *" push: branches: [ "main" ] jobs: - build: + send-morning-message: runs-on: ubuntu-latest - steps: + steps: + - name: Checkout repository - uses: actions/checkout@v3 + - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 + - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - - name: send msg + + - name: Run morning message script run: | - python send_love_msg.py + python send_morning_message.py + send-evening-message: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run evening message script + run: | + python send_evening_message.py diff --git a/config.dev.yaml b/config.dev.yaml deleted file mode 100644 index fcf15cf..0000000 --- a/config.dev.yaml +++ /dev/null @@ -1,17 +0,0 @@ -wxPusher: - appToken: "xxxxxxxxxxxxxxxxxxxxxx" # wxPusher的appToken - topicIds: - - xxxx # wxPusher的topicIds,支持多个ID - -wechatWork: - webhookKey: "xxxxxx" # 企业微信的webhook key - -weather: - apiKey: "xxxxxxxxxxxxx" # 高德天气接口的key - city: 420100 # https://a.amap.com/lbs/static/code_resource/AMap_adcode_citycode.zip - -lover: - expressLoveTimestamp: 1136185445 - monthOfBirthday: 1 - dayOfBirthday: 1 - meetingTimestamp: 1136185445 diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..9b37656 --- /dev/null +++ b/config.yaml @@ -0,0 +1,17 @@ +wxPusher: + appToken: "AT_penx9gyzxwP6g7dwd4lb6LoWHskuGPiE" # wxPusher的appToken + topicIds: + - 34432 # wxPusher的topicIds,支持多个ID + +wechatWork: + webhookKey: "xxxxxx" # 企业微信的webhook key + +weather: + apiKey: "fd14b9ca913160e65eb47b1edba5d48c" # 高德天气接口的key + city: 610118 # https://a.amap.com/lbs/static/code_resource/AMap_adcode_citycode.zip + +lover: + expressLoveTimestamp: 1594378800 + monthOfBirthday: 8 + dayOfBirthday: 19 + meetingTimestamp: 1504233600 diff --git a/send_evening_message.py b/send_evening_message.py new file mode 100644 index 0000000..2fb1257 --- /dev/null +++ b/send_evening_message.py @@ -0,0 +1,71 @@ +import json +from datetime import datetime, timedelta +import pytz +import requests +from config import loadConfig + +# Load configuration +config = loadConfig("config.yaml") +qywxWebhookKey = config.weChatWork.webhookKey +wxpushAppToken = config.wxPusher.appToken +wxpushTopicIds = config.wxPusher.topicIds + +def getDaysUntil(date_str): + target_date = datetime.strptime(date_str, "%Y-%m-%d") + tz = pytz.timezone("Asia/Shanghai") + now = datetime.now(tz) + return (target_date - now).days + +def sendAlarmMsg(mdTex): + wechatwork(mdTex) + +def wechatwork(tex): + webhook = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={qywxWebhookKey}" + header = {"Content-Type": "application/json", "Charset": "UTF-8"} + message = {"msgtype": "markdown", "markdown": {"content": tex}} + message_json = json.dumps(message) + try: + requests.post(url=webhook, data=message_json, headers=header) + except requests.exceptions.RequestException as e: + print("unable to connect to wechat server, err:", e) + except Exception as e2: + print("send message to wechat server, err:", e2) + sendAlarmMsg(str(e2)) + +def wxPusher(tex): + url = "http://wxpusher.zjiecode.com/api/send/message" + header = {"Content-Type": "application/json", "Charset": "UTF-8"} + message = { + "appToken": wxpushAppToken, + "content": tex, + "summary": "按时吃药提醒", + "contentType": 2, + "topicIds": wxpushTopicIds, + "url": "http://wxpusher.zjiecode.com", + } + message_json = json.dumps(message) + try: + info = requests.post(url=url, data=message_json, headers=header) + print(info.text) + except requests.exceptions.RequestException as e: + print("unable to connect to wx, err:", e) + sendAlarmMsg(str(e)) + except Exception as e: + print("send message to wx, err:", e) + sendAlarmMsg(str(e)) + +if __name__ == "__main__": + days_until_end = getDaysUntil("2024-10-27") + + # 新增的提醒内容 + medication_reminder = ( + '宝宝快要下班了呢,记得按时吃药哦:' + '饭前半小时:雷贝拉唑 * 1,枸酸秘钾 * 2;' + '饭后半小时:阿莫西林 * 4,克拉霉素 * 2。' + ) + + # 发送提醒内容到企业微信 + wechatwork(medication_reminder) + + # 发送提醒内容到WxPusher + wxPusher(medication_reminder) diff --git a/send_morning_message.py b/send_morning_message.py new file mode 100644 index 0000000..9cf7355 --- /dev/null +++ b/send_morning_message.py @@ -0,0 +1,332 @@ +import json +from datetime import datetime + +import pytz +import requests + +from config import loadConfig + +# Load configuration +config = loadConfig("config.yaml") +qywxWebhookKey = config.weChatWork.webhookKey +wxpushAppToken = config.wxPusher.appToken +wxpushTopicIds = config.wxPusher.topicIds +city = config.weather.city +monthOfBirthday = config.lover.monthOfBirthday +dayOfBirthday = config.lover.dayOfBirthday +expressLoveTimestamp = config.lover.expressLoveTimestamp +meetingTimestamp = config.lover.meetingTimestamp +weatherApiKey = config.weather.apiKey + + +def getMsgHeader(): + tz = pytz.timezone("Asia/Shanghai") + dt = datetime.now(tz) + h = '今天是 {}'.format(dt.strftime("%Y-%m-%d %A")) + return h + + +def getMsgHeaderToWechat(): + tz = pytz.timezone("Asia/Shanghai") + dt = datetime.now(tz) + h = '今天是 {}'.format(dt.strftime("%Y-%m-%d %A")) + return h + + +class Weather: + def __init__(self): + self.city = "" + self.adcode = "" + self.province = "" + self.reporttime = "" + self.date = "" + self.week = "" + self.dayweather = "" + self.nightweather = "" + self.daytemp = "" + self.nighttemp = "" + self.daywind = "" + self.nightwind = "" + self.daypower = "" + self.nightpower = "" + + def isValide(self) -> bool: + return self.city != "" + + def jsonDecode(self, jsonTex): + self.city = jsonTex["city"] + self.adcode = jsonTex["adcode"] + self.province = jsonTex["province"] + self.reporttime = jsonTex["reporttime"] + casts = jsonTex["casts"][0] + self.date = casts["date"] + self.week = casts["week"] + self.dayweather = casts["dayweather"] + self.nightweather = casts["nightweather"] + self.daytemp = casts["daytemp"] + self.nighttemp = casts["nighttemp"] + self.daywind = casts["daywind"] + self.nightwind = casts["nightwind"] + self.daypower = casts["daypower"] + self.nightpower = casts["nightpower"] + + def getWeatherTextToWechatWork(self): + tex = '西安天气\n > {}, 白天温度: {} ~ 晚上温度: {}\n白天风力:{}-{},晚上风力:{}-{}。'.format( + self.dayweather, + self.daytemp, + self.nighttemp, + self.daypower, + self.daywind, + self.nightpower, + self.nightwind, + ) + return tex + + def getWeatherTextToWechat(self): + tex = '