core icon indicating copy to clipboard operation
core copied to clipboard

Parallel commands

Open megagosha opened this issue 1 year ago • 7 comments

Any updates on this https://github.com/php-telegram-bot/core/pull/1102#issuecomment-647553643?

My problem is that my bot has multiple conversation commands, and I want to explicitly notify user that older conversation is going to be canceled. GenericCommand only runs if no other command is found. So far I can't find any solution to this.

Am I missing something here?

megagosha avatar Feb 25 '24 19:02 megagosha

As far as i could see in the example command GenericmessageCommand.php of the Conversation Example, there exists the function to check if an conversation is active, on line 79 $conversation->exists()

you should be able to add this check to your conversation command and give the user the appropriate warning/message

Hitmare avatar Mar 03 '24 22:03 Hitmare

If I understand this correctly GenericMessageCommand is not executed on commands, only on messages. So if you send something like /someConv GenericCommand will be executed only if SomeConvCommand not found and GenericmessageCommand won't be executed at all.

megagosha avatar Mar 04 '24 01:03 megagosha

If I understand this correctly GenericMessageCommand is not executed on commands, only on messages.

That is true. I mentioned that file because it has already an example how you can build a check into your Conversation Command to check if a conversation is already ongoing

Hitmare avatar Mar 07 '24 14:03 Hitmare

When should I use this method? If I put it inside a command, it will only be executed after another conversation (different command) has already been cancelled.

megagosha avatar Mar 11 '24 11:03 megagosha

you could make a check within a conversation command , something like this

//within the "public function execute(): ServerResponse" function

$message = $this->getMessage();

// If a conversation is busy, execute the conversation command after handling the message.
$conversation = new Conversation(
    $message->getFrom()->getId(),
    $message->getChat()->getId()
);

// Fetch conversation command if it exists and execute it.
if ($conversation->exists()) {
    // return message to user that a conversation already exists and that he needs to cancel the other conversation first
}
//if no conversation exists it will continue the command 

you can also use $message->getText(true) to create an override of the check and continue with the conversation command instead of aborting it and send the user the message

Hitmare avatar Mar 16 '24 16:03 Hitmare

If I am reading this correctly, calling conversation constructor cancels other last active conversation https://github.com/php-telegram-bot/core/blob/57a649cfcfe35883165c19942b460ea6b2dfd606/src/Conversation.php#L119C8-L123C1

Consider this use case:

  1. /conv1 is active now.
  2. user sends /conv2
  3. If I call $conversation->exists() in conv2, conv1 is already cancelled.

$message->getText approach should work fine but it requires fetching conversations from db and comparing to current in all conversations I implement.

If GenericCommand were always executed before specific command, we could implement common logic between all commands there.

megagosha avatar Mar 17 '24 14:03 megagosha

you are right. it seems that way. but we can "just" copy the check from the file itself and do something like this

!the code was not tested!



 $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
        if (isset($conversation[0])) {
            //Pick only the first element
            $this->conversation = $conversation[0];

            if ($this->name !== $this->conversation['command']) { //check if the command name in $name matches the command name in the conversation
            //return message that another conversation is active. possible to send the user the command name with $this->conversation['command']
            }
         //continue with the command file if the check above results in a matching command name

        }


Hitmare avatar Mar 19 '24 21:03 Hitmare