diff --git a/bot/extensions/weather/tests/test_weather_extension.py b/bot/extensions/weather/tests/test_weather_extension.py index cc9a481..ac43e57 100644 --- a/bot/extensions/weather/tests/test_weather_extension.py +++ b/bot/extensions/weather/tests/test_weather_extension.py @@ -85,16 +85,25 @@ def test_format_weather() -> None: }, ) - lines = message.splitlines() - - assert lines[0] == "### Weather for Paris" - assert lines[1] == "
Local time:    1970-01-01 00:00"
-    assert lines[2] == "Description:   Clear Sky"
-    assert lines[3] == "Temperature:   68.00°F | 20.00°C"
-    assert lines[4] == "- Min:         66.20°F | 19.00°C"
-    assert lines[5] == "- Max:         71.60°F | 22.00°C"
-    assert lines[6] == "Feels like:    69.80°F | 21.00°C"
-    assert lines[7] == "Humidity:      52%"
-    assert lines[8] == "Pressure:      1,014 hPa"
-    assert lines[9] == "Visibility:    10,000m | 32,808ft"
-    assert lines[10] == "
" + lines = message.render() + + assert "

Weather for Paris

" in lines + assert "" in lines + assert "Local time:" in lines + assert "1970-01-01 00:00" in lines + assert "Description:" in lines + assert "Clear Sky" in lines + assert "Temperature:" in lines + assert "68.00°F | 20.00°C" in lines + assert "- Min:" in lines + assert "66.20°F | 19.00°C" in lines + assert "- Max:" in lines + assert "71.60°F | 22.00°C" in lines + assert "Feels like:" in lines + assert "69.80°F | 21.00°C" in lines + assert "Humidity:" in lines + assert "52%" in lines + assert "Pressure:" in lines + assert "1,014 hPa" in lines + assert "Visibility:" in lines + assert "10,000m | 32,808ft" in lines diff --git a/bot/extensions/weather/weather_extension.py b/bot/extensions/weather/weather_extension.py index 31f50cd..85617a6 100644 --- a/bot/extensions/weather/weather_extension.py +++ b/bot/extensions/weather/weather_extension.py @@ -37,7 +37,7 @@ async def weather(ctx: Context, *city: str) -> None: case WeatherError.UNAVAILABLE: await ctx.reply("Weather service is temporarily unavailable.") case _: - await ctx.reply(format_weather(city_name, result)) + await ctx.reply(component=format_weather(city_name, result)) @weather.error(CheckError) diff --git a/bot/extensions/weather/weather_helper.py b/bot/extensions/weather/weather_helper.py index 54aca5e..5c7057f 100644 --- a/bot/extensions/weather/weather_helper.py +++ b/bot/extensions/weather/weather_helper.py @@ -1,8 +1,9 @@ from datetime import UTC, datetime +from matrix import Table from .openweather_service import WeatherPayload -def format_weather(city: str, data: WeatherPayload) -> str: +def format_weather(city: str, data: WeatherPayload) -> Table: current_time = _city_time(data["timestamp"]).strftime("%Y-%m-%d %H:%M") weather = data["weather"][0] main = data["main"] @@ -25,20 +26,19 @@ def format_weather(city: str, data: WeatherPayload) -> str: pressure = main["pressure"] visibility = _format_visibility(visibility_m, visibility_ft) - return ( - f"### Weather for {city}\n" - f"
"
-        f"{'Local time:':<14} {current_time}\n"
-        f"{'Description:':<14} {description}\n"
-        f"{'Temperature:':<14} {temperature}\n"
-        f"{'- Min:':<14} {temp_min}\n"
-        f"{'- Max:':<14} {temp_max}\n"
-        f"{'Feels like:':<14} {feels_like}\n"
-        f"{'Humidity:':<14} {humidity}%\n"
-        f"{'Pressure:':<14} {pressure:,} hPa\n"
-        f"{'Visibility:':<14} {visibility}\n"
-        f"
" - ) + table = Table(title=f"Weather for {city}") + + table.add_field("Local time:", current_time) + table.add_field("Description:", description) + table.add_field("Temperature:", temperature) + table.add_field("Feels like:", feels_like) + table.add_field("- Min:", temp_min) + table.add_field("- Max:", temp_max) + table.add_field("Humidity:", f"{humidity}%") + table.add_field("Pressure:", f"{pressure:,} hPa") + table.add_field("Visibility:", visibility) + + return table def _city_time(timestamp: int) -> datetime: diff --git a/pyproject.toml b/pyproject.toml index 7c0328f..357efc0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ description = "Ada is a Matrix bot built with matrix.py framework. It provides v readme = "README.md" requires-python = ">=3.12" dependencies = [ - "matrix-python>=1.4.9a0", + "matrix-python>=1.0.1", "coloredlogs==15.0.1", "click==8.3.2", "sqlmodel-toolkit @ git+https://github.com/Code-Society-Lab/sqlmodel-toolkit.git",