Repository navigation
Expand file tree
/
Copy pathmain_bot.py
More file actions
67 lines (52 loc) · 2.52 KB
/
Copy pathmain_bot.py
File metadata and controls
67 lines (52 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import datetime
from config import tg_bot_token, open_weather_token
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
bot = Bot(token=tg_bot_token)
dp = Dispatcher(bot)
@dp.message_handler(commands=["start"])
async def start_command(message: types.Message):
await message.reply("Напишите название города и я покажу вам погоду!")
@dp.message_handler()
async def get_weather(message: types.Message):
code_to_smile = {
"Clear": "Ясно \U00002600",
"Clouds": "Облачно \U00002601",
"Rain": "Дождь \U00002614",
"Drizzle": "Небольшой дождь \U00002614",
"Thunderstorm": "Гроза \U000026A1",
"Snow": "Снег \U0001F328",
"Mist": "Туман \U0001F32B"
}
try:
r = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
)
data = r.json()
city = data["name"]
cur_weather = data["main"]["temp"]
weather_description = data["weather"][0]["main"]
if weather_description in code_to_smile:
wd = code_to_smile[weather_description]
else:
wd = "Ошибка о погоде!"
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"])
await message.reply(f"**{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}**\n"
f"Погода в городе: {city}\nТемпература: {cur_weather}°C {wd}\n"
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.столба\n"
f"Скорость ветра: {wind}М/С\nВосход: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\n"
f"Продолжителность светового дня: {length_of_the_day}\n"
f"***Одевайтесь по погоде***!"
)
except:
await message.reply("Проверьте название города")
if __name__ == '__main__':
executor.start_polling(dp)