Skip to content

[FEATURE] CommandHandler should catch commands from captions too #3061

@arun-mani-j

Description

@arun-mani-j

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

Current implementation of CommandHandler doesn't work for commands that are in the caption of a message.

Why it must be done?

  • @Botfather responds to commands in captions. So, assuming that the Father is the role-model for every child (bot), the handler must do the same.
  • The entities of a caption recognize commands too.
  • Because why not 😄! A good example I can come up with is reducing the number of steps involved when a media needs to be processed.
    For example, consider a reverse image search bot. Instead of posting the picture and replying a /search to it or sending a /search and then posting the picture; users can directly post the picture with /search in caption.

A few users have also come up with this question in group:

Describe the solution you'd like

I think it would be nice if you add the feature of CommandHandler considering commands in present in captions.
And so context.args should also need to be updated to parse arguments from captions.

I guess the implementation won't be too much difficult either 🙈
The check_update can be slightly altered to consider the caption.

...
message = update.effective_message

if message.caption:
    text = message.caption
    entities = message.caption_entities
else:
    text = message.text
    entities = message.entities

if (
    entities
    and message.entities[0].type == MessageEntity.BOT_COMMAND
    and message.entities[0].offset == 0
    and text
    and message.get_bot()
):
    command = text[1 : entities[0].length]
    args = text.split()[1:]
    command_parts = command.split("@")
    command_parts.append(message.get_bot().username)
...

Describe alternatives you've considered

The alternative I know so far is to use MessageHandler with narrowed filters.

Additional context

Here's a picture of my cat!
image
BTW it is the code from Your first Bot. I used it to test if CommandHandler handles commands in caption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions