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

Backward conversations #28

Closed
hamidqp opened this issue May 21, 2021 · 2 comments
Closed

Backward conversations #28

hamidqp opened this issue May 21, 2021 · 2 comments

Comments

@hamidqp
Copy link

hamidqp commented May 21, 2021

On complex menus, if the update match the condition Nutgram call the last step (also do this for more one step back). Its also come handy on making forms like AskIceCream that user wants to change the previous answers before submit.

(yes its possible to create that condition on every step but for more steps and menus it takes so much duplicated code)

@sergix44
Copy link
Member

I didn't quite get the issue but I try to explain:
the framework doesn't know anything about the flow, it only knows which step should call in this moment. For example:

class MyConversation extends Conversation {
    
    protected ?string $step = 'askName';
    
    public function askName(Nutgram $bot)
    {
        $bot->sendMessage('What is your name?');
        $this->next('confirmName');    
    }
    
    public function confirmName(Nutgram $bot)
    {
        $name = $bot->message()->text;
        $bot->sendMessage("Your name is $name, right?");
        $this->next('replyConfirmation');
    }

    public function replyConfirmation(Nutgram $bot)
    {
        $reply = $bot->message()->text;
        
        if ($reply === 'yes') {
            $bot->sendMessage('Nice!');
            $this->end();
        } else if ($reply === 'no') {
            $this->askName($bot);
        } else {
            $bot->sendMessage('Please reply with yes or no.');
        }
    }
}

As you can see, at any time you can call back a previous step direcly from another step, just passing the current $bot instance, in this case:

  • If the user reply yes, the conversation ends.
  • If the user reply no, it call direcly the first step, which restart the conversation.
  • If the user replay other than yes or no, the confirmation step will be repeated

@hamidqp
Copy link
Author

hamidqp commented May 21, 2021

oh sorry i didn't know that every time step property from class is changing

protected function next(string $step): void
{
    $this->step = $step;
    $this->bot->stepConversation($this);
}

so i have to change it manually 😅

@hamidqp hamidqp closed this as completed May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants