How to solve this error : AttributeError: 'NoneType' object has no attribute 'reply_text'?
Asked Answered
P

2

5

I've a button , it's supposed to return the ask_wikipedia function , so I used CallbackQueryHandler but when i want to call ask_wikipedia function i recive an attribute error ! Why? How can i fix it ?

def Click_Button(update, context) :
    query = update.callback_query
    if query.data == "Research":
        ask_wikipedia(update, context)

query_handler = CallbackQueryHandler(Click_Button)

dispatcher.add_handler(query_handler)



def ask_wikipedia(update, context)  :
    update.message.reply_text('What do you want to know about ? πŸ”Ž')
    return About


When I click on the button I get this error

AttributeError: 'NoneType' object has no attribute 'reply_text'

How can I fix it ?

Parallelize answered 10/3, 2021 at 12:17 Comment(7)
Do you have a return statement in the CallbackQueryHandler function? – Venus
no i don't think so ... – Parallelize
query_handler is going to be a NoneType then. You should add a return statement for whatever you want to be saved in the query_handler variable. – Venus
sorry I'm literally a noob , How can i add a return statement ? – Parallelize
No worries haha. "return About" is an example of a return statement in your "ask_wikipedia" function. So just add "return" followed by the variable you want as the output of the function as the last line of the function. Watch the indentation too. It should only be one level in from the edge. – Venus
do you mean , I should return the update "return update " , what am i going to return? – Parallelize
You don't have the CallbackQueryHandler function definition in your code so I can't say what you should return. But whatever you put after "return" in the CallbackQueryHandler function is what query_handler is going to have as a value. – Venus
I
5

When replying to a text message (from a MessageHandler) is fine to use update.message.reply_text, but in your case the incoming message is a managed by the CallbackHandler which receives a different object.
You can reply using

update.callback_query.message.edit_text(message)
Immiscible answered 10/3, 2021 at 17:5 Comment(0)
K
1

Faced AttributeError: 'NoneType' object has no attribute 'reply_text' error in my app. After investigation found that it raises when user edit original message, so in update I get edited_message instead of message. So I decided to use update.effective_chat.send_message instead of update.message.reply_text to avoid this problem. Even if it not solution for you, keep in mind that it might be edited_message in update instead of message.
(Wrote here because it is first in google for this Error)

Kleper answered 30/11, 2023 at 12:20 Comment(0)

© 2022 - 2025 β€” McMap. All rights reserved.