D
D
daMpoff2022-01-16 02:14:32
Python
daMpoff, 2022-01-16 02:14:32

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)

61e352045df2a966752288.jpeg
function code
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 error

output (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\dMp\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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
daMpoff, 2022-01-16
@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, 'Проверьте название города')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question