Commit d4b7de9

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-01-22 16:47:49
feat: add support for daily messages
1 parent c1a96fa
Changed files (2)
src/config.py
@@ -16,6 +16,7 @@ TEXT_LENGTH = int(os.getenv("TEXT_LENGTH", "4096"))  # Maximum length of text me
 CAPTION_LENGTH = int(os.getenv("CAPTION_LENGTH", "1024"))  # 4096 for Premium user
 MAX_FILE_BYTES = int(os.getenv("MAX_FILE_BYTES", "2000")) * 1024 * 1024  # 4000 MB for Premium user
 ASR_MAX_DURATION = int(os.getenv("ASR_MAX_DURATION", "600"))
+DAILY_MESSAGES = os.getenv("DAILY_MESSAGES", "{}")  # Useful for daily checkin for some services. Should be a json string: '{"chat-1": "msg-1", "chat-2": "msg-2"}'
 
 
 class ENABLE:
src/main.py
@@ -2,10 +2,13 @@
 # -*- coding: utf-8 -*-
 import argparse
 import asyncio
+import contextlib
+import json
 import logging
 import os
 import platform
 import sys
+from json import JSONDecodeError
 from urllib.parse import urlparse
 
 from apscheduler.schedulers.asyncio import AsyncIOScheduler
@@ -17,11 +20,11 @@ from pyrogram.types import LinkPreviewOptions, Message
 
 from bridge.ocr import forward_ocr_results
 from bridge.social import forward_social_media_results
-from config import DEVICE_NAME, ENABLE, PROXY, TID, TOKEN, cache
+from config import DAILY_MESSAGES, DEVICE_NAME, ENABLE, PROXY, TID, TOKEN, TZ, cache
 from handler import handle_social_media, handle_utilities
 from message_utils import parse_msg
 from others.raw_img_file import convert_raw_img_file
-from utils import cleanup_old_files
+from utils import cleanup_old_files, nowdt
 
 # ruff: noqa: RUF001
 
@@ -110,17 +113,23 @@ async def main():
     await app.stop()
 
 
-async def scheduling(client: Client):  # noqa: ARG001
+async def scheduling(client: Client):
     cache.evict()  # delete expired cache
     cleanup_old_files()
 
     # custom crontab jobs
-    # now = nowdt()
-    # if now.minute == 0:
-    #     await client.send_message(TID.ADMIN_GROUP, "I'm still alive.")
-    # if now.hour == 8 and now.minute == 0:
-    #     weather = await get_weather(location="shanghai")
-    #     await client.send_message(TID.ADMIN, weather)
+    now = nowdt(TZ)
+    if now.hour == 8 and now.minute == 0:  # daliy messages
+        daliy = {}
+        try:
+            daliy = json.loads(DAILY_MESSAGES)
+        except (JSONDecodeError, TypeError):
+            logger.warning(f"Invalid DAILY_MESSAGES: {DAILY_MESSAGES}")
+        with contextlib.suppress(Exception):
+            for chat_id, msg in daliy.items():
+                cid = int(chat_id)
+                logger.info(f"Sending daily message to {cid}: {msg}")
+                await client.send_message(cid, msg)
 
 
 if __name__ == "__main__":