-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_bot.py
59 lines (49 loc) · 2.24 KB
/
weather_bot.py
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
import requests
import datetime
from config import TOKEN, open_weather_token
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=["start"])
async def start_command(message: types.Message):
await message.reply("Hi! Write to me if you want to know the weather")
@dp.message_handler()
async def get_weather(message: types.Message):
code_to_smile = {
"Clear" : "Clear \U00002600",
"Clouds" : "Clouds \U00002601",
"Rain" : "Rain \U00002614",
"Drizzle" : "Rain \U00002614",
"Thunderstorm" : "Thunderstorm \U000026A1",
"Snow" : "Snow \U0001F328",
"Mist" : "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_discription = data["weather"][0]["main"]
if weather_discription in code_to_smile:
wd = code_to_smile[weather_discription]
else:
wd = "Look out the window, I don't understand what is happening"
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"Weather in the city: {city}\nTemp: {cur_weather}С° {wd}"
f"\nHumidity: {humidity}%\nPressure: {pressure}mmHg\nWind: {wind}m/s"
f"\nSunrise: {sunrise_timestamp}\nSunset: {sunset_timestamp}\nDuration of the day: {length_of_the_day}"
"\n***Good day!***")
except:
await message.reply("\U00002620 Check the name of the city \U00002620")
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)