import os
import datetime
import requests
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import math
bot = Bot(token='my token')
dp = Dispatcher(bot)
@dp.message_handler(commands=["start"])
async def start_command(message: types.Message):
await message.reply("Привет!\nВ каком городе мне посмотреть погоду?")
@dp.message_handler()
async def get_weather(message: types.Message):
try:
response = requests.get("http://api.openweathermap.org/data/2.5/weather?&lang=ru&units=metric&appid=my token")
data = response.json()
city = data["name"]
cur_temp = data["main"]["temp"]
humidity = data["main"]["humidity"]
pressure = data["main"]["pressure"]
wind = data["wind"]["speed"]
sunrise_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"])
except:
await message.answer(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}\n"
f"Погода в городе: {city}\nТемпература: {cur_temp}°C\n"
f"Влажность: {humidity}%\nДавление: {math.ceil(pressure / 1.333)} мм.рт.ст\nВетер: {wind} м/с \n"
f"Восход солнца: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n"
f"Хорошего дня!"
)
if __name__ == "__main__":
executor.start_polling(dp)