Skip to content

[FEATURE] A function to automatically decide if update.message.reply_text or update.callback_query.edit_message_text will be used. #2826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
evakdev opened this issue Dec 24, 2021 · 14 comments
Labels
⚙️ documentation affected functionality: documentation

Comments

@evakdev
Copy link

evakdev commented Dec 24, 2021

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

In some situations, it may not be possible to know if you should use update.message.reply_text or update.callback_query.edit_message_text. This could happen if user can start a conversation from different stages of a conversation (where sometimes they are using an inline keyboard and sometimes sending text.) IF it happens, you'll face an exception.

Describe the solution you'd like

I think it would be useful to have a function in update, that decides automatically if it should use update.message.reply_text or update.callback_query.edit_message_text. This could even be a simple try except block such as this:

def send_message(update, text, reply_markup=None, parse_mode=None):
    """
    Sends a message to the user.
    This will use callback_query.edit_message_text as priority. if it doesn't work, will use message.reply_text instead.
    To be used when we're not sure which one will be needed.
    """
    try:
        return update.callback_query.edit_message_text(
            text, reply_markup=reply_markup, parse_mode=parse_mode
        )
    except:
        return update.message.reply_text(
            text, reply_markup=reply_markup, parse_mode=parse_mode
        )

Describe alternatives you've considered

No response

Additional context

This may also an issue for accessing from_user, but I haven't checked the docs to see if there is a solution for that or not.

@evakdev evakdev changed the title [FEATURE] [FEATURE] A function to automatically decide if update.message.reply_text or update.callback_query.edit_message_text will be used. Dec 24, 2021
@Poolitzer
Copy link
Member

Hi. I would vote to say this is the responsibility of the end user. Easy enough to do in code:

callback(update, context):
    text = "This is the text to get to the user"
    if update.effective_message:
        update.effective_message.reply_text(text)
    else:
        update.callback_query.edit_message_text(text)

Or even more if statements if you have more potentially callbacks. Thats easy to know because you register them with specific handlers, normally.

@evakdev
Copy link
Author

evakdev commented Dec 24, 2021

Sure, its easy enough to do. Just thought it can be a common issue, as I've had it several times :)

@Poolitzer
Copy link
Member

It might be, though I personally define one callback per handler type.

Anyway, I don't think the library should just switch methods for the user. This is not the responsibility of it.

@evakdev
Copy link
Author

evakdev commented Dec 24, 2021

Alright, should I close the issue then?

@Poolitzer
Copy link
Member

You can leave it up if you are still convinced its a good idea and see what the other maintainers think.

@Bibo-Joshi
Copy link
Member

Hi. The update.effective_message already covers update.callback_query.message, i.e. one doesn't need the if clauses in Pools example. IMO this already covers the requested use case.

Note that there is also update.effective_{user, chat}, which in particular covers the from_user case.

@evakdev
Copy link
Author

evakdev commented Dec 26, 2021

Ohh, I wasn't aware that covers them all! That makes things much easier. Thanks :)
Then perhaps maybe we could make the docs a bit clearer on what effective_{} do? it does say "no matter what kind of update this is", but maybe it could mention all the various things it checks? e.g whether it be message, edited message, callback_query, etc.

If you think that's a helpful addition, I could make that change. Otherwise I'll just close the ticket :)

@Poolitzer
Copy link
Member

We list the updates which set it here, where is your string from?

@evakdev
Copy link
Author

evakdev commented Dec 26, 2021

Yes, that's where I got it from. It says it will be none for this and that. It doesn't say that it will return whichever of message, edited_message, callback_query, channel_post, edited_channel_post that is present.

Copy link
Member

@evakdev Ohh you mean instead of saying when it is None, you would say when it is the message?

@evakdev
Copy link
Author

evakdev commented Dec 26, 2021

yes, exactly! it could say both too, actually

@Bibo-Joshi
Copy link
Member

If you like to, you're very welcome to open a PR against the doc-fixes branch to elaborate the docs :)

@Bibo-Joshi Bibo-Joshi added ⚙️ documentation affected functionality: documentation and removed enhancement labels Dec 27, 2021
@Bibo-Joshi
Copy link
Member

I added the doc-enhancement to #2633 and will close this thread in favor of #2633 :)

@Bibo-Joshi
Copy link
Member

FYI: I added a commit to #2822 that addresses the documentation. For effective_{user, chat}, I didn't list every possibility, but included exapmles that should get the idea across better :)

@github-actions github-actions bot locked and limited conversation to collaborators Jan 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⚙️ documentation affected functionality: documentation
Projects
None yet
Development

No branches or pull requests

3 participants