mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-11-03 16:36:44 +00:00 
			
		
		
		
	🔖 pre release 2.0.0a1
This commit is contained in:
		
							
								
								
									
										24
									
								
								tests/test_plugins/test_weather.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								tests/test_plugins/test_weather.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
from nonebot import on_command
 | 
			
		||||
from nonebot.rule import to_me
 | 
			
		||||
from nonebot.typing import Bot, Event
 | 
			
		||||
 | 
			
		||||
weather = on_command("天气", rule=to_me(), priority=1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@weather.handle()
 | 
			
		||||
async def handle_first_receive(bot: Bot, event: Event, state: dict):
 | 
			
		||||
    args = str(event.message).strip()  # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海
 | 
			
		||||
    print(f"==={args}===")
 | 
			
		||||
    if args:
 | 
			
		||||
        state["city"] = args  # 如果用户发送了参数则直接赋值
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@weather.got("city", prompt="你想查询哪个城市的天气呢?")
 | 
			
		||||
async def handle_city(bot: Bot, event: Event, state: dict):
 | 
			
		||||
    city = state["city"]
 | 
			
		||||
    if city not in ["上海", "北京"]:
 | 
			
		||||
        await weather.reject("你想查询的城市暂不支持,请重新输入!")
 | 
			
		||||
    # weather = await get_weather_from_xxx(city)
 | 
			
		||||
    city_weather = "晴天"
 | 
			
		||||
    # await bot.send(city_weather)
 | 
			
		||||
    await weather.finish(city_weather)
 | 
			
		||||
		Reference in New Issue
	
	Block a user