php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.89k stars 955 forks source link

Commands directed to other bots are mistakenly handled by Genericmessage #369

Open jacklul opened 7 years ago

jacklul commented 7 years ago

Before ~0.37 they were correctly handled by Genericcommand, I think since now my code for 'stalking' other bot commands in Genericcommand doesn't generate any reports.

The same in private chat and groups. Image for visualization: https://i.imgur.com/eXfV5AQ.jpg

chuv1 commented 7 years ago

I thought it is correct behavior. Multicast command for those who can handle it.... And if you have to handle unsupported command you need to generate empty response in group chats.

jacklul commented 7 years ago

My point is those messages still carry bot_command entity so they should be handled as command, not message.

noplanman commented 7 years ago

Not quite sure I understand.

On 0.35 ->type = "Message" ->text = "/c@b" When I try sending a message to a "foreign" bot within my bot, it gets executed as a text message type, not a command. That's because here the command isn't meant for my bot. So it returns false and is regarded as a normal text message.

On 0.38.1 Same thing happens here.

Only messages sent with /xyz and without @bot_name get the command type, thus forwarding it to GenericCommand.

Am I missing something here?

Would you be expecting GenericCommand to kick in for /cmd and /cmd@not_my_bot?

jacklul commented 7 years ago

Well that's interesting, either I had some own modifications or else I don't know.

Would you be expecting GenericCommand to kick in for /cmd and /cmd@not_my_bot?

Yes, since as I previously said, even if it's not meant for the bot it's still a command, not text message!

noplanman commented 7 years ago

Yes, you're right, should still be considered a command.

Looking at a nice way to implement this, but it's not that easy I think 😕 The problem is the part that I've linked to in my previous comment. If it is a command but not meant for our bot, we should return something else than the command itself, as it would otherwise be executed as if it was meant for our bot. It's also kind of out of scope of the getCommand method. We probably need to introduce an extra method or something called isThisBot.

Open to any suggestions you might have.

jacklul commented 7 years ago

I guess not a priority until we can have clean and nice way to implement this.

akalongman commented 6 years ago

@jacklul is this already fixed?

jacklul commented 6 years ago

Nope, bot still answers to messages not directed to him.

In one of my bots I'm using this as a solution:


    /**
     * @param $command
     *
     * @return ServerResponse|mixed
     * @throws TelegramException
     */
    public function executeCommand($command)
    {
        if ($message = $this->update->getMessage()) {
            $entities = $message->getEntities();

            if (count($entities) > 0) {
                $first_entity = $message->getEntities()[0];
                if ($first_entity->getType() === 'bot_command' && $message->getCommand() === null) {
                    return Request::emptyResponse();
                }
            }
        }

        return parent::executeCommand($command);
    }

Maybe we could think about doing entity = bot_command check in Message::getType() and then return Request::emptyResponse() when getCommand() is null ?