1.4 Добавлен модуль погоды
This commit is contained in:
@@ -6,6 +6,7 @@ from aiogram import Router
|
||||
from .start_cmd import start_cmd
|
||||
from .start_time_cmd import start_time_cmd
|
||||
from .help_cmd import help_cmd
|
||||
from .weather_cmd import weather_cmd
|
||||
|
||||
# Объявление роутера и настройка экспорта модулей
|
||||
__all__ = ("router",)
|
||||
@@ -16,6 +17,7 @@ router = Router(name="user_cmd_router")
|
||||
router.include_routers(
|
||||
help_cmd.router,
|
||||
start_time_cmd.router,
|
||||
weather_cmd.router,
|
||||
)
|
||||
|
||||
router.include_routers(start_cmd.router)
|
||||
|
||||
16
BotCode/routers/commands/user_cmd/weather_cmd.py
Normal file
16
BotCode/routers/commands/user_cmd/weather_cmd.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# BotCode/routers/commands/user_cmd/start_time_cmd.py
|
||||
|
||||
from aiogram import types
|
||||
from BotLibrary import CommandHandler
|
||||
from BotCode.utils import get_weather
|
||||
|
||||
__all__ = ("weather_cmd",)
|
||||
|
||||
weather_cmd = CommandHandler(
|
||||
name="weather",
|
||||
description="Погода",
|
||||
keywords=["weather", "gjujlf", "цуферук", "погода"],
|
||||
callbackdata=["keywords"],
|
||||
media="command",
|
||||
func=[get_weather],
|
||||
)
|
||||
@@ -2,9 +2,10 @@
|
||||
# Инициализация пакета utils, для работы с механиками
|
||||
|
||||
from aiogram import Router
|
||||
from .weather_api import get_weather
|
||||
|
||||
# Объявление роутера и настройка экспорта модулей
|
||||
__all__ = ("router",)
|
||||
__all__ = ("router", "get_weather")
|
||||
router = Router(name="utils_head_router")
|
||||
|
||||
# Идет самым последним, если другие роутеры не сработали
|
||||
|
||||
41
BotCode/utils/weather_api.py
Normal file
41
BotCode/utils/weather_api.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import aiohttp
|
||||
from ProjectsFiles import weather_api_key
|
||||
|
||||
|
||||
async def get_weather(message, *args) -> str:
|
||||
# Извлекаем город из сообщения
|
||||
command_parts = message.text.split(maxsplit=1)
|
||||
print(command_parts[1])
|
||||
if len(command_parts) > 1:
|
||||
city = command_parts[1]
|
||||
else:
|
||||
return "Пожалуйста, укажите город."
|
||||
|
||||
# Обработка города
|
||||
city = city.lower().capitalize()
|
||||
|
||||
# URL для запроса к API
|
||||
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=metric&lang=ru"
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as response:
|
||||
data = await response.json()
|
||||
|
||||
if data["cod"] != 200:
|
||||
return "Город не найден. Попробуйте еще раз."
|
||||
|
||||
weather = data["weather"][0]["description"]
|
||||
temp = data["main"]["temp"]
|
||||
humidity = data["main"]["humidity"]
|
||||
wind = data["wind"]["speed"]
|
||||
|
||||
weather_today: str = (f"Погода <b>{city}</b>\n"
|
||||
f"☁️Погода: <b>{weather}</b>\n"
|
||||
f"🌡Температура: <b>{temp}°C</b>\n"
|
||||
f"💧Влажность: <b>{humidity}%</b>\n"
|
||||
f"💨Скорость ветра: <b>{wind} м/с</b>")
|
||||
await message.answer(weather_today)
|
||||
return weather_today
|
||||
except Exception as e:
|
||||
return f"Произошла ошибка при получении данных о погоде: {str(e)}"
|
||||
Reference in New Issue
Block a user