import datetime
from aiohttp import ClientSession
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
bot = Bot(token="")
dp = Dispatcher(bot)
@dp.message_handler(commands=['Weather'])
async def start_wether(message: types.Message):
await message.answer('Введите названия города.')
@dp.message_handler()
async def get_wether(message: types.Message):
code_to_smile = {
'Claer': 'Ясно \U00002600',
"Clouds": "Облачно \U00002601",
"Rain": "Дождь \U00002614",
"Drizzle": "Дождь \U00002614",
"Thunderstorm": "Гроза \U000026A1",
"Snow": "Снег \U0001F328",
"Mist": "Туман \U0001F32B"
}
try:
open_weather_token = '****************************'
url = f"https://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
async with ClientSession() as session:
async with session.get(url=url) as resp:
data = await resp.json()
city = data['name']
temp = data['main']['temp']
weather_description = data['weather'][0]['main']
if weather_description in code_to_smile:
wd = code_to_smile[weather_description]
else:
wd = '\U0001F600'
humidity = data['main']['humidity']
pressure = data['main']['pressure']
wind_speed = data['wind']['speed']
sunsire_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
sunset_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
length_of_the_day = datetime.datetime.fromtimestamp(data["sys"]["sunset"]) - datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
await message.reply(f'***{datetime.datetime.now().strftime("%Y-%m-%d %H:%M")}***\n'
f'Погода в городе: {city}\nТемпература:{temp}С {wd}\nВлажность: {humidity}\n'
f'Давление: {pressure} мм.рт.ст\nСкорость ветра: { wind_speed} м.с\n'
f'Восход: {sunsire_timestamp}\nЗакат: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n'
f'***Хорошего дня!***')
except:
await message.reply('\U00002620 Проверте название города \U00002620')
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True, on_startup=print('Bot is running!'))