Answer the question
In order to leave comments, you need to log in
Error when calling a bot function in python, what needs to be changed in the code?
The condition that calls the function
The telebot and requests library is used
elif message.text == 'Погода':
markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
back = types.KeyboardButton('Назад')
markup.add(back)
bot.send_message(message.chat.id, 'Введите ваш город')
bot.register_next_step_handler(message, open_weather_token, get_weather)
def get_weather(message,*args):
try:
r = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
)
data = r.json()
city = data['name']
cur_weather = data['main']['temp']
humidity = data['main']['humidity']
pressure = data['main']['pressure']
wind_speed = data['wind']['speed']
bot.send_message(message.chat.id, str(f'Погода в городе: {city}\nТемпература : {cur_weather}C°\nВлажность
{humidity}%\nДавление {pressure} мм.рт.ст\nВетер: {wind_speed} м/с'))
except Exception as ex:
bot.send_message(message.chat.id, ex)
bot.send_message(message.chat.id, 'Проверьте название города')
Answer the question
In order to leave comments, you need to log in
@weather.message_handler(content_types='text')
def get_weather(message):
try:
r = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
)
data = r.json()
gorod = data['name']
cur_weather = data['main']['temp']
humidity = data['main']['humidity']
pressure = data['main']['pressure']
wind_speed = data['wind']['speed']
print(gorod)
bot.send_message(message.chat.id, str(f'Погода в городе: {gorod}\nТемпература : {cur_weather}C°\nВлажность: {humidity}%\nДавление {pressure} мм.рт.ст\nВетер: {wind_speed} м/с'))
except Exception as ex:
bot.send_message(message.chat.id, (ex))
bot.send_message(message.chat.id, 'Проверьте название города')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question