Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question