#Импорты
import requests
import telebot
import datetime
import pprint
#Конфиги
bot = telebot.TeleBot("token")
open_weather_token="OExjK1NnRGOXVJ6nTdR0V7of5v0SG6AD"
#Старт
@bot.message_handler(commands=["start"])
def start(m, res=False):
bot.send_message(m.chat.id, 'test123 ')
#weather
@bot.message_handler(content_types=["text"])
def get_text_messages(message):
if(message.text=='/weather'):
bot.send_message(message.from_user.id,"Погода в городе...")
bot.register_next_step_handler(message,get_weather)
else:
bot.send_message(message,"Введите /help для помощи" )
def get_weather(message):
city=message.text
print(city)
try:
r = requests.get(
f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={open_weather_token}&units=metric"
)
data = r.json()
cur_weather = data["main"]["temp"]
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"])
bot.send_message(message,f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
f"Погода в городе: {city}\nТемпература: {cur_weather}C°\n"
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\nВетер: {wind} м/с\n"
f"Восход солнца: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n"
f"***Хорошего дня!***" )
pprint(data)
except Exception as ex:
print("Ошибка...")
print(ex)
#Пошло поехало
bot.infinity_polling()