E
E
expp2020-05-04 18:13:27
Python
expp, 2020-05-04 18:13:27

Telegram bot on TelegramBotApi. How to solve this error?

Good afternoon. I wrote a bot in Python for Telegram and ran into this error:

raise api_response_error.NotFoundError('Unable to find the resource')
pyowm.exceptions.api_response_error.NotFoundError: The searched item was not found.
Reason: Unable to find the resource.

...
elif message.text == " Температура":
      bot.send_message(message.chat.id, "Введите город: ")
      def weather():
        owm = pyowm.OWM('...')
        city = message.text
        observation = owm.weather_at_place( city )
        w = observation.get_weather()
      weather()
      bot.send_message(message.chat.id, f"Температура в {city} сейчас: {temperature}")
...

As I managed to understand, the essence of this error is that the wrong city is entered, but this is very strange, because the city variable takes the value from the entered message, and it is entered immediately before this function. Of course, I wrote adequate values, and the problem is not that I enter a stupid city name that I invented.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-05-04
@expp

Your piece of code is not very informative.
After the line 'city = message.text' add 'print(city)' and see the output in the console.
Here is the simplest code, check with yours:

import telebot
import pyowm

owm = pyowm.OWM('')
bot = telebot.TeleBot('')

def getWeather(city):
    observation = owm.weather_at_place(city)
    w = observation.get_weather()
    temp = w.get_temperature('celsius')["temp"]
    
    return temp

@bot.message_handler(content_types=['text'])
def repeat_all_message(message):
  try:
    temp = getWeather(message.text)
    bot.send_message(message.chat.id,f'Температура в {message.text} - {temp}')
    
  except pyowm.exceptions.api_response_error.NotFoundError:
    bot.send_message(message.chat.id,'Ошибка! Город не найден!')

if __name__ == '__main__':
    bot.polling(none_stop=True)

And it is desirable to move the code into separate functions, it will be easier when debugging.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question