K
K
kurle2021-08-23 17:30:10
Python
kurle, 2021-08-23 17:30:10

What to do if the telegrambot that shows the weather crashes after entering the wrong city?

The bot just crashes after an incorrectly entered city. HERE IS THE CODE i
mport telebot
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps
from pyowm.utils import config as cfg

config = cfg.get_default_config()
config['language'] = 'ru'

owm = OWM( '035f2c3fc8a6883ffd824b56f29c2740', config)
mgr = owm.weather_manager ()

bot = telebot.TeleBot ( "1956104990: AAEV_dzCP9HWEcLff47UhOhb0V3ysMFnU48")

@ bot.message_handler (content_types = [ "text"])
def send_echo (message):
observation mgr.weather_at_place = ( message.text)
w = observation.weather
temp = w.temperature('celsius')["

otvet = "In the city " + message.text + " now " + w.detailed_status + "\n"
otvet += "Temperature now " + str(temp) + "\n\n"

bot.send_message(message.chat. id,answer)

bot.polling(none_stop = True)

I understand that you need to check for what the bot entered, but how?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stefan, 2021-08-23
@MEDIOFF

Make sure the user has entered a valid city

V
Vindicar, 2021-08-23
@Vindicar

Most likely, with the wrong city, calling
observation = mgr.weather_at_place(message.text) will
throw an exception. Based on the docs , this will either be a ParseResponseException or an APICallException. If you bothered to give the full error message, you could say for sure.
In any case, read about exception handling . You need to wrap the call in a try block, and if an error occurs, inform the user.

I understand it. I did not write the bot myself, this is a video from YouTube.

If you understand, learn the basics of python the old fashioned way - tutorial and exercises. On YouTube you will only learn to repeat without understanding the meaning. First - the basics, then rush to write bots.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question