-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (58 loc) · 2.47 KB
/
main.py
File metadata and controls
78 lines (58 loc) · 2.47 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
68
69
70
71
72
73
74
75
76
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
"""
This is a echo bot.
It echoes any incoming text messages.
"""
import logging
import requests
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = '8124755868:AAEo_GxjkU0TVyshrJvNeozSx61NqgS0kxs'
OPENWEATHER_API_KEY="5eae9707f20f9f36c6552dc72a797b7a"
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/start` or `/help` command
"""
await message.reply("Йоу!\nТы видал погоду в Самаре??!?\nТак, подсказать?")
@dp.message_handler(commands=['weather'])
async def send_weather(message: types.Message):
weather_info = get_weather_samara()
await message.reply(weather_info)
def get_weather_samara():
city = "Самара"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric&lang=ru"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
temp = data['main']['temp']
feels_like = data['main']['feels_like']
weather_desc = data['weather'][0]['description'].capitalize()
humidity = data['main']['humidity']
wind_speed = data['wind']['speed']
city_name = data['name']
country = data['sys']['country']
return (
f"🌤 Погода в {city_name}, {country}:\n"
f"Температура: {temp}°C (ощущается как {feels_like}°C)\n"
f"Описание: {weather_desc}\n"
f"Влажность: {humidity}%\n"
f"Скорость ветра: {wind_speed} м/с"
)
else:
return '❌ Не удалось получить данные о погоде.'
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)