commit 938d5e714a57726d128ff0f0b022871d1a0b07d1 Author: ClovertaTheTrilobita Date: Sat Jan 4 22:38:51 2025 +0800 初始化 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/SanYeCao-Nonebot.iml b/.idea/SanYeCao-Nonebot.iml new file mode 100644 index 0000000..59094ca --- /dev/null +++ b/.idea/SanYeCao-Nonebot.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8210dbc --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,39 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72440b1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6fb409e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5c1302f --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# SanYeCao-Nonebot + +## How to start + +1. generate project using `nb create` . +2. install plugins using `nb plugin install` . +3. run your bot using `nb run` . + +## Documentation + +See [Docs](https://nonebot.dev/) diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..a233c37 --- /dev/null +++ b/bot.py @@ -0,0 +1,14 @@ +import nonebot +from nonebot.adapters.qq import Adapter as QQAdapter + +nonebot.init() + +driver = nonebot.get_driver() +driver.register_adapter(QQAdapter) + +nonebot.load_builtin_plugins('echo', 'single_session') +nonebot.load_from_toml("pyproject.toml") + + +if __name__ == "__main__": + nonebot.run() \ No newline at end of file diff --git a/example.env.prod b/example.env.prod new file mode 100644 index 0000000..62dabab --- /dev/null +++ b/example.env.prod @@ -0,0 +1,15 @@ +DRIVER=~fastapi+~httpx+~websockets + +QQ_BOTS=' +[ + { + "id": "xxx", + "token": "xxx", + "secret": "xxx", + "intent": { + "c2c_group_at_messages": true + }, + "use_websocket": false + } +] +' \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..982f3e7 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[project] +name = "SanYeCao-Nonebot" +version = "0.1.0" +description = "SanYeCao-Nonebot" +readme = "README.md" +requires-python = ">=3.9, <4.0" + +[tool.nonebot] +adapters = [ + { name = "QQ", module_name = "nonebot.adapters.qq" } +] +plugins = [] +plugin_dirs = ["src/plugins"] +builtin_plugins = ["echo"] diff --git a/src/plugins/weather.py b/src/plugins/weather.py new file mode 100644 index 0000000..1b3f76e --- /dev/null +++ b/src/plugins/weather.py @@ -0,0 +1,53 @@ +from nonebot.rule import to_me +from nonebot.plugin import on_command +import requests + +weather = on_command("天气", rule=to_me(), aliases={"weather", "查天气"}, priority=10, block=True) + +@weather.handle() +async def handle_function(): + # await weather.send("天气是...") + city_name = "海口" + result = format_weather(city_name) + await weather.finish("天气是...\n" + result) + + + + +def get_weather(city_name): + # 设置请求的URL和参数 + url = f'https://apis.juhe.cn/simpleWeather/query?key=50a3bd415158e186903d6e6994157589&city={city_name}' + # 发送GET请求 + response = requests.get(url) + # 检查请求是否成功 + if response.status_code == 200: + # 解析返回的JSON数据 + data = response.json() + + # 检查是否查询成功 + if data['reason'] == '查询成功!': + # 返回天气数据 + return data['result'] + else: + return {"error": "查询失败: " + data['reason']} + else: + return {"error": "请求失败,状态码: " + str(response.status_code)} + + +def format_weather(city_name): + # 假设这里你已经有了城市的URL编码,这里用'%E9%87%8D%E5%BA%86'作为示例 + city_encoded = city_name # 重庆的URL编码 + weather_data = get_weather(city_encoded) + + # 检查是否返回了错误 + if 'error' in weather_data: + return weather_data['error'] + else: + # 实时天气 + realtime_weather = weather_data['realtime'] + result = f"实时天气:" + "\n" + f"{realtime_weather['info']}, 温度: {realtime_weather['temperature']}℃, 湿度: {realtime_weather['humidity']}%, 风向: {realtime_weather['direct']}, 风力: {realtime_weather['power']}级, AQI: {realtime_weather['aqi']}" + # 未来几天的天气 + result = result + "\n" + "未来几天的天气:" + for day in weather_data['future']: + result = result + "\n" + f"日期: {day['date']}, 天气: {day['weather']}, 温度: {day['temperature']}, 风向: {day['direct']}" + return result