P
P
papapython2021-03-10 22:03:15
Python
papapython, 2021-03-10 22:03:15

What to write in the telegram bot code so that it does not close when entering non-existent data?

I have a program that takes data from the site about the weather and gives out with different prints, and so the bot starts up / start writes enter the name of the city. With the names of cities and countries, it can work indefinitely, but when I want to make any set of letters, I also want to make some kind of answer, but it just gives an error, because it only answers the name of cities. It's all because of the pyowm module

import telebot
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils.config import get_default_config
from telebot import types
owm = OWM("")
bot = telebot.TeleBot("")
config_dict = get_default_config()
config_dict['language'] = 'ru' 

@bot.message_handler(commands=['start'])
def send_welcome(message):
  #клавиши
  markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
  item1 = types.KeyboardButton(" /start ")
  markup.add(item1)
  bot.reply_to(message, "Здравствуйте! Введите название города, в котором хотите узнать погоду: ", reply_markup=markup)

@bot.message_handler(content_types=['text'])
def weather_text(message):
  mgr = owm.weather_manager()
  observation = mgr.weather_at_place(message.text)
  w = observation.weather
  temp = w.temperature('celsius')["temp"]
  wind = w.wind()["speed"]
  answer = 'В городе ' + message.text + ' сейчас ' + w.detailed_status + '\n'
  answer += 'Температура воздуха в среднем ' + str(temp) + ' градусов Цельсия' + '\n'
  answer += 'Скорость ветра достигает ' + str(wind) + ' м/c' + '\n'
  if temp < -10:
    answer += 'Вам следует оставаться дома.' + '\n'
  elif temp < 0:
    answer += 'Вам следует одеваться потеплее.' + '\n'
  elif temp < 15:
    answer += 'Это не лучшая погода для прогулок'
  elif temp < 30:
    answer += 'Погода отличная, хорошего дня!'	
  elif temp > 30:
    answer += 'На улице жарко, возьмите с собой бутылку воды.' + '\n'
  else: 
    answer += 'Хорошего дня!'
  bot.send_message(message.chat.id, answer)
bot.polling(none_stop=True, interval=0)

If else will not work here, because there is no specific variable, so I want to know how to simply restart it
spoiler
Traceback (most recent call last):
File "D:\Codes\погода.py", line 42, in
bot.polling()
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 427, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 451, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 118, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\six.py", line 703, in reraise
raise value
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 69, in run
task(*args, **kwargs)
File "D:\Codes\погода.py", line 22, in weather_text
observation = mgr.weather_at_place(message.text)
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\pyowm\weatherapi25\weather_manager.py", line 53, in weather_at_place
_, json_data = self.http_client.get_json(OBSERVATION_URI, params=params)
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\pyowm\commons\http_client.py", line 143, in get_json
HttpClient.check_status_code(resp.status_code, resp.text)
File "C:\Users\tortik\AppData\Local\Programs\Python\Python39\lib\site-packages\pyowm\commons\http_client.py", line 288, in check_status_code
raise exceptions.NotFoundError('Unable to find the resource')
pyowm.commons.exceptions.NotFoundError: Unable to find the resource

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-03-10
@papapython

I have already answered a similar question .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question