优化天气插件

This commit is contained in:
liuwei
2025-05-22 11:38:46 +08:00
parent 208a062e18
commit 48a9491921

View File

@@ -110,7 +110,7 @@ class WeatherPlugin(MessagePluginInterface):
city_name = self._extract_city_name(content)
self.LOG.debug(f"城市名称:{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, "命令格式错误"
# 配置密钥检查
@@ -123,7 +123,7 @@ class WeatherPlugin(MessagePluginInterface):
# 获取天气信息
weather_info = await self._get_weather_info(city_name)
if not weather_info:
await bot.send_text_message((roomid if roomid else sender), "\n⚠️查询天气失败!", sender)
self.LOG.debug("\n⚠️查询天气失败!")
return False, "查询天气失败"
# 发送天气信息
@@ -132,7 +132,7 @@ class WeatherPlugin(MessagePluginInterface):
except Exception as 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}"
def _extract_city_name(self, content: str) -> str:
@@ -173,10 +173,14 @@ class WeatherPlugin(MessagePluginInterface):
geoapi_json = await response.json()
await conn_ssl.close()
self.LOG.debug(f"geoapi_json: {geoapi_json}")
if 'code' not in geoapi_json:
self.LOG.error(f"API响应格式错误缺少code字段")
return ""
if geoapi_json['code'] == '404':
return "\n⚠️查无此地!"
return ""
elif geoapi_json['code'] != '200':
return f"\n⚠️请求失败\n{geoapi_json}"
return ""
country = geoapi_json["location"][0]["country"]
adm1 = geoapi_json["location"][0]["adm1"]