Как привязать парсер к меню кнопки погода
Проблема, написал парсер погоды привязываю его к кнопке "Погода" и он только выводит то что в except. Помогите пожалуйста
import telebot
import requests
import datetime
from telebot import types
from config import token_bot, token_weather
bot = telebot.TeleBot(token_bot)
@bot.message_handler(commands=['start'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('⛅Погода')
item2 = types.KeyboardButton('?Курсы валют')
item3 = types.KeyboardButton('?Новости')
item4 = types.KeyboardButton('?Календарь')
markup.add(item1, item2, item3, item4)
bot.send_message(message.chat.id, 'Привет, {0.first_name}! Выбери в меню, то что тебя интересует'
.format(message.from_user), reply_markup=markup)
@bot.message_handler(content_types=['text'])
def bot_message(message):
if message.text == '⛅Погода':
weather(message)
elif message.text == '?Курсы валют':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('?Курс Доллара')
item2 = types.KeyboardButton('? Курс Евро')
back = types.KeyboardButton('⬅Назад')
markup.add(item1, item2, back)
bot.send_message(message.chat.id, '?Курсы валют'.format(message.from_user), reply_markup=markup)
elif message.text == '?Новости':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
back = types.KeyboardButton('⬅Назад')
markup.add(back)
bot.send_message(message.chat.id, '?Новости'.format(message.from_user), reply_markup=markup)
elif message.text == '?Календарь':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
back = types.KeyboardButton('⬅Назад')
markup.add(back)
bot.send_message(message.chat.id, '?Календарь'.format(message.from_user), reply_markup=markup)
elif message.text == '⬅Назад':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('⛅Погода')
item2 = types.KeyboardButton('?Курсы валют')
item3 = types.KeyboardButton('?Новости')
item4 = types.KeyboardButton('?Календарь')
markup.add(item1, item2, item3, item4)
bot.send_message(message.chat.id, '⬅Назад'.format(message.from_user), reply_markup=markup)
def weather(message):
code_to_smile = {
"Clear": "Ясно \U00002600",
"Clouds": "Облачно \U00002601",
"Rain": "Дождь \U00002614",
"Drizzle": "Дождь \U00002614",
"Thunderstorm": "Гроза \U000026A1",
"Snow": "Снег \U0001F328",
"Mist": "Туман \U0001F32B"
}
try:
r = requests.get(
f'https://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={token_weather}&units=metric'
)
data = r.json()
city = data["name"]
cur_weather = data["main"]["temp"]
weather_description = data["weather"][0]["main"]
if weather_description in code_to_smile:
wd = code_to_smile[weather_description]
else:
wd = "Посмотри в окно, не пойму что там за погода!"
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.reply_to(message,
f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
f"Погода в городе: {city}\nТемпература: {cur_weather}C° {wd}\n"
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\nВетер: {wind} м/с\n"
f"Восход солнца: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\nПродолжительность "
f"дня: {length_of_the_day}\n "
f"***Хорошего дня!***")
except:
bot.reply_to(message, "\U00002620 Проверьте название города \U00002620")
if __name__ == '__main__':
bot.polling(none_stop=True)