Answer the question
In order to leave comments, you need to log in
Telegram bot question?
Telegram weather bot, how to make it so that when openweathermap did not find a country or city, instead of an error, a message like: City not found. Help plz
Here is the code:
import pyowm
from pyowm.commons.enums import SubscriptionTypeEnum
from pyowm.utils.measurables import kelvin_to_celsius
import telebot
config = {
'subscription_type': SubscriptionTypeEnum.FREE,
'language': 'ru',
'connection': {
' use_ssl': True,
'verify_ssl_certs': True,
'use_proxy': False,
'timeout_secs': 5
},
'proxies': {
'http': '
'https': ' socks5://user:[email protected]:port '
}
}
owm = pyowm.OWM('66d228e4647a94aff7bfd6112de11ab7', config=config)
mgr = owm.weather_manager()
bot = telebot.TeleBot("1151683680:AAHHvAcJ4DmT69DwQ2 ", parse_mode=None)
@bot.message_handler(content_types=['text'])
def send_echo(message):
observation = mgr.weather_at_place( message.text )
w = observation.weather
temp = w.temperature('celsius') ['temp']
answer = " City/country " + message.text + " current temperature: " + str(temp) + " Celsius." + "\n"
answer += '
answer += "Dubak outside, dress like a tank!"
elif temp < 7:
answer += "It's cold now, put on a jacket!"
elif temp < 15:
answer += "It's cold now, dress warmly!"
elif temp > 30:
answer += "It's hot outside, dress lightly)"
elif temp > 20:
answer += "It's warm outside, you can go out without outerwear))"
elif temp > 15:
answer += "Now, more or less heat XD"
bot.send_message(message.chat.id, answer)
bot.polling(none_stop = True)
Answer the question
In order to leave comments, you need to log in
First you need to find where it throws an error, and then use
try:
the place where it throws an error
except:
bot.send_message(message.chat.id, "City not found")
to find where it throws an error, add several bot.send_message to the code to test, if the bot did not send some message, then the error is higher
And I see my last message helped, mark in the topic "Mark solutions"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question