nonebot2/docs/guide/code/awesome-bot-2/awesome/plugins/weather.py

44 lines
2.1 KiB
Python
Raw Normal View History

2018-12-27 12:23:45 +00:00
from nonebot import on_command, CommandSession
2018-08-18 15:24:57 +00:00
# on_command 装饰器将函数声明为一个命令处理器
# 这里 weather 为命令的名字,同时允许使用别名「天气」「天气预报」「查天气」
@on_command('weather', aliases=('天气', '天气预报', '查天气'))
async def weather(session: CommandSession):
2019-01-26 14:21:51 +00:00
# 从会话状态session.state中获取城市名称city如果当前不存在则询问用户
2018-08-18 15:24:57 +00:00
city = session.get('city', prompt='你想查询哪个城市的天气呢?')
# 获取城市的天气预报
weather_report = await get_weather_of_city(city)
# 向用户发送天气预报
await session.send(weather_report)
# weather.args_parser 装饰器将函数声明为 weather 命令的参数解析器
# 命令解析器用于将用户输入的参数解析成命令真正需要的数据
@weather.args_parser
async def _(session: CommandSession):
# 去掉消息首尾的空白符
stripped_arg = session.current_arg_text.strip()
2019-01-26 14:21:51 +00:00
if session.is_first_run:
# 该命令第一次运行(第一次进入命令会话)
if stripped_arg:
# 第一次运行参数不为空,意味着用户直接将城市名跟在命令名后面,作为参数传入
# 例如用户可能发送了:天气 南京
session.state['city'] = stripped_arg
return
if not stripped_arg:
# 用户没有发送有效的城市名称(而是发送了空白字符),则提示重新输入
# 这里 session.pause() 将会发送消息并暂停当前会话(该行后面的代码不会被运行)
session.pause('要查询的城市名称不能为空呢,请重新输入')
# 如果当前正在向用户询问更多信息(例如本例中的要查询的城市),且用户输入有效,则放入会话状态
session.state[session.current_key] = stripped_arg
2018-08-18 15:24:57 +00:00
async def get_weather_of_city(city: str) -> str:
# 这里简单返回一个字符串
# 实际应用中,这里应该调用返回真实数据的天气 API并拼接成天气预报内容
return f'{city}的天气是……'