调整天气触发词

This commit is contained in:
liuwei
2025-05-20 15:39:10 +08:00
parent f19d7f954f
commit d22eecc28a

View File

@@ -55,7 +55,7 @@ class WeatherPlugin(MessagePluginInterface):
城市名天气
城市名 天气""")
self.enable = self._config.get("Weather", {}).get("enable", True)
# 加载API配置
self.api_key = self._config.get("Weather", {}).get("API_KEY", "")
self.api_domain = self._config.get("Weather", {}).get("API_DOMAIN", "")
@@ -82,11 +82,11 @@ class WeatherPlugin(MessagePluginInterface):
content = str(message.get("content", "")).strip()
roomid = message.get("roomid", "")
# 只处理群消息且包含"天气"的消息
if not roomid or content.startswith("天气") or content.endswith("天气") :
return True
if not roomid and not (content.startswith("天气") or content.endswith("天气")):
return False
return True
@plugin_stats_decorator(plugin_name="天气查询")
@@ -106,14 +106,15 @@ class WeatherPlugin(MessagePluginInterface):
# 处理消息内容 - 不再使用jieba分词
city_name = self._extract_city_name(content)
if not city_name:
await bot.send_text_message((roomid if roomid else sender), f"\n{self.command_format}", sender)
return False, "命令格式错误"
# 配置密钥检查
if not self.api_key:
await bot.send_text_message((roomid if roomid else sender), "\n你还没配置天气API密钥请在config.toml中配置Weather.API_KEY", sender)
await bot.send_text_message((roomid if roomid else sender),
"\n你还没配置天气API密钥请在config.toml中配置Weather.API_KEY", sender)
return False, "API密钥未配置"
try:
@@ -122,7 +123,7 @@ class WeatherPlugin(MessagePluginInterface):
if not weather_info:
await bot.send_text_message((roomid if roomid else sender), "\n⚠️查询天气失败!", sender)
return False, "查询天气失败"
# 发送天气信息
await bot.send_text_message((roomid if roomid else sender), weather_info, sender)
return True, "发送成功"
@@ -131,12 +132,12 @@ class WeatherPlugin(MessagePluginInterface):
self.LOG.error(f"处理天气请求出错: {e}")
await bot.send_text_message((roomid if roomid else sender), f"\n⚠️处理出错: {str(e)}", sender)
return False, f"处理出错: {e}"
def _extract_city_name(self, content: str) -> str:
"""提取城市名称替代jieba分词"""
# 去除空格
content = content.replace(" ", "")
# 处理几种常见格式
if content.startswith("天气"):
# 格式: "天气北京"
@@ -165,7 +166,8 @@ class WeatherPlugin(MessagePluginInterface):
}
geo_api_url = f"{self.api_domain}/geo/v2/city/lookup"
conn_ssl = aiohttp.TCPConnector(ssl=False)
async with aiohttp.request('GET', url=geo_api_url, connector=conn_ssl, headers=headers, params=params) as response:
async with aiohttp.request('GET', url=geo_api_url, connector=conn_ssl, headers=headers,
params=params) as response:
geoapi_json = await response.json()
await conn_ssl.close()
@@ -235,4 +237,4 @@ class WeatherPlugin(MessagePluginInterface):
uv_index = day['uvIndex']
message += f'{date} {weather} 最高🌡️{max_temp}℃ 最低🌡️{min_temp}℃ ☀️紫外线:{uv_index}\n'
return message
return message