weather_report/main.py
2024-02-25 22:52:18 +08:00

33 lines
No EOL
961 B
Python

import json
from telethon import TelegramClient
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
from pytz import timezone
from weather import get_weather_report
with open('config.json') as f:
config = json.loads(f.read())
api_id = config["api_id"]
api_hash = config["api_hash"]
bot_token = config["bot_token"]
bot = TelegramClient('weather_bot', api_id, api_hash).start(bot_token=bot_token)
async def send_weather_report():
weather_report = get_weather_report()
print(weather_report)
await bot.send_message("hiqianxue", weather_report)
# Define your target time in UTC+8
target_time = datetime.now(timezone('Asia/Shanghai')).replace(hour=8, minute=0, second=0)
target_time_utc = target_time.astimezone(timezone('UTC'))
scheduler = AsyncIOScheduler()
scheduler.add_job(send_weather_report, 'interval', days=1, next_run_time=target_time_utc)
scheduler.start()
with bot:
bot.loop.run_forever()