N
N
Nikikez2020-08-13 12:45:06
Python
Nikikez, 2020-08-13 12:45:06

How to make a telegram bot workable?

Errors: 5f350f4031932188456082.jpegWhen I run the code through the command line, I go to Tg and write the city, after which a sea of ​​​​errors pops up. Thanks in advance.

<code lang="python">
from pyowm import OWM
from pyowm.utils.config import get_default_config
config_dict = get_default_config()
config_dict['language'] = 'RU'
import telebot

owm = OWM('a91bee44d09a57842353573df6d4e01a')
bot = telebot.TeleBot("1058623246:AAEbGu0nyd681DjRmpWx217WRx2VFi1VJsw")
mgr = owm.weather_manager()

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

    answer = " В городе " + message.text + " cейчас " + w.detailed_status() + "\n"
    answer += " Температура в выбранном городе соответствует " + str(temp) + " градусам по шкале Цельсия" +"\n"

    if temp < 15 :
        answer+=  " На улице холодно, надевай валенки"
    elif 20 > temp > 14 :
        answer+=  " Ну такое"

    elif 19 < temp < 27 :
        answer+=  " Комфортно, сер"
    else :
        answer+=  " Жарко чет"
    bot.send_message(message.chat.id, answer)

bot.polling(none_stop=True)

</code>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SKEPTIC, 2020-08-13
@Nikikez

from pyowm import OWM
from pyowm.utils.config import get_default_config
config_dict = get_default_config()
config_dict['language'] = 'RU'
import telebot

owm = OWM('a91bee44d09a57842353573df6d4e01a')
bot = telebot.TeleBot("1058623246:AAEbGu0nyd681DjRmpWx217WRx2VFi1VJsw")
mgr = owm.weather_manager()

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

    answer = " В городе " + message.text + " cейчас " + w.detailed_status + "\n"
    answer += " Температура в выбранном городе соответствует " + str(temp) + " градусам по шкале Цельсия" +"\n"

    if temp < 15 :
        answer+=  " На улице холодно, надевай валенки"
    elif 20 > temp > 14 :
        answer+=  " Ну такое"

    elif 19 < temp < 27 :
        answer+=  " Комфортно, сер"
    else :
        answer+=  " Жарко чет"
    bot.send_message(message.chat.id, answer)

bot.polling(none_stop=True)

Found differences?
And here it is:
You wrote it like this:
answer = " В городе " + message.text + " cейчас " + w.detailed_status() + "\n"

And I have this:
answer = " В городе " + message.text + " cейчас " + w.detailed_status + "\n"

You sort of understand the differences between calling a method on an object and getting its property.

S
Sergey Karbivnichy, 2020-08-13
@hottabxp

Of course it doesn't work, since a very small percentage of code copied from the Internet without basic knowledge of how this code works is working.
There are several steps to follow:
1) Get a basic knowledge of Python with the help of book(s);
2) We take hello world for the pyTelegramBotAPI library and documentation for it. And we read the docks and along the way change something in the code until you understand how the bots work;
3) We take hello world for the pyowm library and documentation for it. And we read the docks and along the way we change something in the code until you understand how this library works;
4) You can combine these 2 libraries.
See how simple it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question