Skip to content
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

Parallel commands #1451

Open
megagosha opened this issue Feb 25, 2024 · 7 comments
Open

Parallel commands #1451

megagosha opened this issue Feb 25, 2024 · 7 comments

Comments

@megagosha
Copy link

Any updates on this #1102 (comment)?

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?

@Hitmare
Copy link
Collaborator

Hitmare commented Mar 3, 2024

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

@megagosha
Copy link
Author

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.

@Hitmare
Copy link
Collaborator

Hitmare commented Mar 7, 2024

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

@megagosha
Copy link
Author

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.

@Hitmare
Copy link
Collaborator

Hitmare commented Mar 16, 2024

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

@megagosha
Copy link
Author

megagosha commented Mar 17, 2024

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.

@Hitmare
Copy link
Collaborator

Hitmare commented Mar 19, 2024

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

        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants