E
E
Emin7772020-08-05 23:20:04
Python
Emin777, 2020-08-05 23:20:04

How to fix 'NoneType' object has no attribute 'chat' error?

@bot.message_handler(content_types=["text"])
def handle_text(message):
    keyboardmain = types.InlineKeyboardMarkup(row_width=2)
    first_button = types.InlineKeyboardButton(text="Youtube", callback_data="first")
    second_button = types.InlineKeyboardButton(text="Twitter", callback_data="second")
    keyboardmain.add(first_button, second_button)
    bot.send_message(message.chat.id, "Select ", reply_markup=keyboardmain)


@bot.callback_query_handler(func=lambda call: True)
def handle_text(call, message=None):
if call.data == "first":
        keyboard = types.InlineKeyboardMarkup()
        rele1 = types.InlineKeyboardButton(text="Оставьте отзыв", callback_data="3")
        backbutton = types.InlineKeyboardButton(text="back", callback_data="a")
        keyboard.add(rele1, backbutton)
        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
                              text="Текст",
                              reply_markup=keyboard)
        msg = "Пользователь {} написал \"{}\".".format(message.chat.id, message.messag_id)
        bot.send_message('-100000000000', msg)


Gives an error message:
File "C:/Users/99455/PycharmProjects/untitled3/main.py", line 67, in handle_text
    msg = "Пользователь {} написал \"{}\".".format(message.chat.id, message.messag_id)
AttributeError: 'NoneType' object has no attribute 'chat'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PavelMos, 2020-08-06
@PavelMos

the message object must have a chat attribute, but the object is empty. The problem is either in the previous code, or you need to check and fill in some default values.

S
soremix, 2020-08-06
@SoreMix

1. Remove the message parameter from the callback processing function. Why is he there?
2. Stop referring to this mythical message when you have a call.message that you have already used in addition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question