JustUndertaker / ComWeChatBotClient

基于COM通信的微信pc hook应用端,支持Onebot12协议
https://justundertaker.github.io/ComWeChatBotClient/
GNU Affero General Public License v3.0
281 stars 60 forks source link

定时任务没有执行 #33

Open koking0 opened 1 year ago

koking0 commented 1 year ago

ComWeChatBotClient搭建的微信机器人,反向WebSocket,起了一个Nonebot,然后用plugin-apscheduler创建了一个每天定时推送天气预报的任务,启动的显示插件加载成功了,但是到时间后没有任何反应。

import nonebot
import requests
from nonebot import require
from nonebot.adapters.onebot.v12 import Adapter

# 初始化 NoneBot
nonebot.init()
app = nonebot.get_asgi()

# 注册适配器
driver = nonebot.get_driver()
driver.register_adapter(Adapter)

nonebot.load_plugins("plugins/chatgpt")  # ChatGPT

require("nonebot_plugin_apscheduler")

from nonebot_plugin_apscheduler import scheduler

def get_daily_weather_msg():
    params = {"key": "288a5858cf8e924665f6eafb18e88bcb", "city": "110108"}
    res = requests.get(url="http://api.tianapi.com/tianqi/index", params=params)
    data = res.json()
    print(f"data: {data}")
    date = data["newslist"][0]["date"]
    week = data["newslist"][0]["week"]
    area = data["newslist"][0]["area"]
    weather = data["newslist"][0]["weather"]
    lowest = data["newslist"][0]["lowest"]
    highest = data["newslist"][0]["highest"]
    wind = data["newslist"][0]["wind"]
    windsc = data["newslist"][0]["windsc"]
    tips = data["newslist"][0]["tips"]
    msg = f"今天是{date},{week},小麦为您播报{area}的天气。\n\n"
    msg += f"今天{weather},最低温度:{lowest},最高温度:{highest},风向:{wind}{windsc}\n\n"
    msg += f"小麦提醒你,{tips}"
    return msg

# 每日天气
@scheduler.scheduled_job("cron", hour="17", minute="21", id="daily_weather")
async def daily_weather():
    nonebot.logger.info("每日天气推送")
    bot, = nonebot.get_bots().values()
    msg = get_daily_weather_msg()

    vip_user_list = ["wxid_u7s0skwtellf22"]
    print(f"vip_user_list: {vip_user_list}")
    for user_id in vip_user_list:
        await bot.send_private_msg(user_id=user_id, message=msg)

scheduler.add_job(daily_weather, "interval", days=1, id="daily_weather")

if __name__ == "__main__":
    nonebot.logger.warning("小麦微信版启动")
    nonebot.run()

按理来说应该在下午五点21分的时候推送消息,但是启动后到时间没有任何反应。

image

JustUndertaker commented 1 year ago

大概率原因是你nb代码问题,自己在nb侧加点日志调试,