@daMpoff

Ошибка при вызове функции бота на python, что нужно изменить в коде?

Условие которое вызывает функцию
Используется библиотека telebot и requests

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)

61e352045df2a966752288.jpeg
код функции
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, 'Проверьте название города')


Вывод ошибки

Traceback (most recent call last):
File "d:\protonic_bot\bot.py", line 189, in
bot.polling()
File "C:\Users\daMp\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 664, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "C:\Users\daMp\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 726, in __threaded_polling
raise e
File "C:\Users\daMp\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 686, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\daMp\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 136, in raise_exceptions
raise self.exception_info
File "C:\Users\daMp\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 88, in run
task(*args, **kwargs)
TypeError: 'str' object is not callable
  • Вопрос задан
  • 120 просмотров
Решения вопроса 1
@daMpoff Автор вопроса
@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, 'Проверьте название города')
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы