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