hears('{message}', function (BotMan $botman, $message) { if (strtolower($message) == 'hi' || strtolower($message) == 'hello') { $botman->startConversation(new OnboardingConversation()); } else { $botman->reply("Start the conversation by saying Hi."); } }); $botman->listen(); } } class OnboardingConversation extends Conversation { protected $name; protected $phoneNumber; protected $email; public function askName() { $this->ask('Hello! What is your name?', function (Answer $answer) { $this->name = $answer->getText(); $this->say("Nice to meet you " . $this->name); $this->askPhoneNumber(); }); } public function askPhoneNumber() { $this->ask('Tell us your phone number:', function (Answer $answer) { $phoneNumber = $answer->getText(); // Validate phone number: only numbers and exactly 10 digits if (!preg_match('/^\d{10}$/', $phoneNumber)) { $this->say("Invalid phone number format. Please enter a 10-digit phone number."); return $this->askPhoneNumber(); // Re-ask for the phone number } $this->phoneNumber = $phoneNumber; $this->say("Your phone number is " . $this->phoneNumber); $this->askEmail(); }); } public function askEmail() { $this->ask('Can you tell us your email?', function (Answer $answer) { $this->email = $answer->getText(); // Validate Email if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) { $this->say("Your email is " . $this->email); $this->confirmEmail(); } else { $this->say("Invalid email format. Please enter a valid email."); $this->askEmail(); // Re-ask for the email } }); } public function confirmEmail() { $this->ask('Confirm your Email (Y/N):', function (Answer $answer) { $confirmEmail = $answer->getText(); if (strtolower($confirmEmail) == 'y' || strtolower($confirmEmail) == 'yes') { $this->say("We got your details. Thank you!"); // Save the details to the database UserChat::create([ 'name' => $this->name, 'phone_number' => $this->phoneNumber, 'email' => $this->email, ]); } elseif (strtolower($confirmEmail) == 'n' || strtolower($confirmEmail) == 'no') { $this->say("Please confirm your email."); $this->askEmail(); // Re-ask for the email } else { $this->say("Please answer with Y or N."); $this->confirmEmail(); // Re-ask for confirmation } }); } public function run() { $this->askName(); } }