feat: Add optional location name to weather report
All checks were successful
Docker CI/CD / Docker Build and Push to Docker Hub (push) Successful in 9m33s

This update allows users to provide a location name for their weather reports, which can be useful when requesting weather information for specific locations.
This commit is contained in:
Kumi 2024-05-10 18:31:24 +02:00
parent 9a4c250eb4
commit e46be65707
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -17,6 +17,10 @@ class Weather(BaseTool):
"type": "string",
"description": "The longitude of the location.",
},
"name": {
"type": "string",
"description": "A location name to include in the report. This is optional, latitude and longitude are always required."
}
},
"required": ["latitude", "longitude"],
}
@ -26,6 +30,8 @@ class Weather(BaseTool):
if not (latitude := self.kwargs.get("latitude")) or not (longitude := self.kwargs.get("longitude")):
raise Exception('No location provided.')
name = self.kwargs.get("name")
weather_api_key = self.bot.config.get("OpenWeatherMap", "APIKey")
if not weather_api_key:
@ -37,7 +43,7 @@ class Weather(BaseTool):
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
return f"""**Weather report**
return f"""**Weather report{f" for {name}" if name else ""}**
Current: {data['current']['temp']}°C, {data['current']['weather'][0]['description']}
Feels like: {data['current']['feels_like']}°C
Humidity: {data['current']['humidity']}%