-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (47 loc) · 2.11 KB
/
main.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
60
61
import requests
from datetime import datetime
from config import OPEN_TOKEN, BOT_TOKEN
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
emojis_code = {
'clear sky': 'Clear sky \U0001F30C',
'few clouds': 'Few clouds \U0001F324',
'scattered clouds': 'Scattered clouds \U0001F325',
'broken clouds': 'Broken clouds \U0001F325',
'overcast clouds': 'Broken clouds \U0001F325',
'shower rain': 'Shower rain \U0001F326',
'rain': 'Rain \U0001F327',
'thunderstorm': 'Thunderstorm \U0001F329',
'snow': 'Snow \U0001F328',
'mist': 'Mist \U0001F32B',
}
bot = Bot(token=BOT_TOKEN)
dispatcher = Dispatcher(bot)
@dispatcher.message_handler(commands=['start'])
async def start_command(message: types.Message):
await message.reply('Hello! I am Fast Weather Bot \U0001F31A \U0001F308 . \nTo find out the weather in your '
'city - enter the name of the city: ')
@dispatcher.message_handler(commands=['help'])
async def help_command(message: types.Message):
await message.reply("Hi!\nIf you want know a weather in your city - just type the city in English.")
@dispatcher.message_handler()
async def get_weather(message: types.Message, token=OPEN_TOKEN):
try:
info = requests.get(
f'http://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={token}&units=metric'
)
data = info.json()
weather = data['weather'][0]['description']
if weather in emojis_code:
weather = emojis_code[weather]
answer = f"Hello! Actual weather for city {data['name']} now - at {datetime.now().strftime('%Y-%m-%d %H:%M')} is: \n" \
f"temperature: {data['main']['temp']} \U0001F321 \n" \
f"it feels like: {data['main']['feels_like']} \U0001F92B \n" \
f"by the way the weather is - {weather}. \n" \
f"Have a nice day! \U0001F917"
await message.reply(answer)
except:
await message.reply('Can not find such city! Try another one.')
if __name__ == '__main__':
executor.start_polling(dispatcher)