G
G
ganyamede2021-06-14 18:25:40
Python
ganyamede, 2021-06-14 18:25:40

Error AttributeError: 'NoneType' object has no attribute 'from_user', how to fix?

If we write !hug without replying to the message, then I get an error
File "/home/ganymede1/main.py", line 1734, in get_text_messages
bot.send_message(message.chat.id, ~f'@{message.from_user. username} Embraced @{message.reply_to_message.from_user.username}')
AttributeError: 'NoneType' object has no attribute 'from_user'
and if the message is answered then everything is fine, how to fix it? What if you write !hug without replying to the message then nothing happens

if message.text.lower() == '!hug':
bot.send_message(message.chat.id, ~f'@{message.from_user.username} Hugs @{message.reply_to_message.from_user.username}')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-06-14
@o5a

So check in the condition that the answer

if message.reply_to_message:
    bot.send_message(message.chat.id, ~f'@{message.from_user.username} Обнял @{message.reply_to_message.from_user.username}')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question