-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
57 lines (41 loc) · 1.62 KB
/
bot.py
File metadata and controls
57 lines (41 loc) · 1.62 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
import os
import requests
from telebot import TeleBot
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('TOKEN')
APPID = os.getenv('APPID')
bot = TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def start(msg):
bot.send_message(msg.chat.id, 'Привет! Я бот для получения погоды. Напиши /weather [город]')
@bot.message_handler(commands=['weather'])
def weather(msg):
try:
city = ' '.join(msg.text.split()[1:])
if not city:
raise ValueError("Название города не указано")
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {
"q": city,
"units": "metric",
"lang": "ru",
"appid": APPID
}
response = requests.get(url, params=params)
data = response.json()
if data['cod'] != 200:
raise Exception(data['message'])
info = (
f"🌤 Погода в {data['name']}:\n"
f"Температура: {data['main']['temp']}°C\n"
f"Ощущается как: {data['main']['feels_like']}°C\n"
f"Влажность: {data['main']['humidity']}%\n"
f"Ветер: {data['wind']['speed']} м/с\n"
f"Описание: {data['weather'][0]['description'].capitalize()}"
)
bot.reply_to(msg, info)
except Exception as e:
error = f"Ошибка: {str(e)}. Попробуйте позже или проверьте название города."
bot.reply_to(msg, error)
bot.polling(none_stop=True, interval=0)