V
V
Viktor2021-06-29 22:20:00
Python
Viktor, 2021-06-29 22:20:00

I wrote the first bot, but it gives errors, can you help?

I wrote and wrote a bot, but in the end it doesn’t work, I googled and Yandex errors, but everything went by.
Everything worked in the terminal until I tried to make a bot. The bot starts, but when you enter a trigger, it falls off and gives out a bunch of red letters.
Language: Python
Libraries: telebot, pyowm, translate

Errors:

Traceback (most recent call last):
File "C:\Users\Badmajor\PycharmProjects\test_FCS\main.py", line 36, in
bot.polling(none_stop=True)
File "D:\Python\lib\site-packages \telebot\__init__.py", line 619, in polling
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 678, in __threaded_polling
raise e
File "D:\Python\lib\site-packages\telebot\__init__.py", line 641, in __threaded_polling
self.worker_pool.raise_exceptions()
File "D:\Python\lib\site-packages\telebot \util.py", line 130, in raise_exceptions
raise self.exception_info
File "D:\Python\lib\site-packages\telebot\util.py", line 82, in run
task(*args, **kwargs)
File "C:\Users\Badmajor\PycharmProjects\test_FCS\main.py ", line 33, in handle_text
bot.reply_to(message.from_chat.id, answer)
AttributeError: 'Message' object has no attribute 'from_chat'

Process finished with exit code 1


The code itself:
import telebot
import pyowm
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps
import translate
from translate import Translator

translator = Translator(to_lang='ru')

owm = OWM('7a64c349fa71ba8cff904ea78d46e6aa')
mgr = owm.weather_manager()
bot = telebot.TeleBot('1839512085:AAEBe_-Hyfvf0u2ZWg1hbhktnoEEZnQdJxM')


@bot.message_handler(content_types=['text'])
def handle_text(message):
    if message.text == "что надеть?" or "Что надеть?":

        observation = mgr.weather_at_place('Набережные Челны')

        w = observation.weather
        temp = w.temperature('celsius')['temp']

        tranlation = translator.translate(w.detailed_status)
        answer = 'Сейчас ' + str(temp) + ' и ' + tranlation + '\n'
        if temp > 30:
            answer += 'На улице Ташкент, лучше ничего не одевать, тебе это очень идет'
        elif temp > 20:
            answer += 'Теплынь же надевай короткую юбку и топик,' + '\n' + 'но и сарафанчик подойдет'
        elif temp > 10:
            answer += 'Ну погодка такая себе, джинсы и толстовка ' + '\n' + 'подойдут,курточку прихвати на всякий'
        elif temp > 0:
            answer += 'Холотун на улице, сиди дома под пледом'
        else:
            answer += 'я умею определять только плюсовую температуру'
        bot.reply_to(message.from_chat.id, answer)


bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-06-29
@Badmajor

Have you even tried to read what is written in the errors?

bot.reply_to(message.from_chat.id, answer)
AttributeError: 'Message' object has no attribute 'from_chat'

The message object does not have a from_chat attribute.
Try like this:
bot.reply_to(message.chat.id, answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question