2024-07-10 08:01:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use BotMan\BotMan\BotMan;
|
|
|
|
use BotMan\BotMan\Messages\Incoming\Answer;
|
2024-07-10 11:40:14 +00:00
|
|
|
use BotMan\BotMan\Messages\Conversations\Conversation;
|
|
|
|
use App\Models\UserChat;
|
2024-07-10 08:01:42 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class BotManController extends Controller
|
|
|
|
{
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$botman = app('botman');
|
2024-07-10 11:40:14 +00:00
|
|
|
$botman->hears('{message}', function (BotMan $botman, $message) {
|
|
|
|
if (strtolower($message) == 'hi' || strtolower($message) == 'hello') {
|
|
|
|
$botman->startConversation(new OnboardingConversation());
|
2024-07-10 08:01:42 +00:00
|
|
|
} else {
|
2024-07-10 11:40:14 +00:00
|
|
|
$botman->reply("Start the conversation by saying Hi.");
|
2024-07-10 08:01:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$botman->listen();
|
|
|
|
}
|
2024-07-10 11:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2024-07-12 07:06:02 +00:00
|
|
|
|
|
|
|
// Validate Email
|
2024-07-10 11:40:14 +00:00
|
|
|
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()
|
2024-07-10 08:01:42 +00:00
|
|
|
{
|
2024-07-10 11:40:14 +00:00
|
|
|
$this->ask('Confirm your Email (Y/N):', function (Answer $answer) {
|
|
|
|
$confirmEmail = $answer->getText();
|
2024-07-12 07:06:02 +00:00
|
|
|
if (strtolower($confirmEmail) == 'y' || strtolower($confirmEmail) == 'yes') {
|
2024-07-10 11:40:14 +00:00
|
|
|
$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,
|
|
|
|
]);
|
2024-07-12 07:06:02 +00:00
|
|
|
} elseif (strtolower($confirmEmail) == 'n' || strtolower($confirmEmail) == 'no') {
|
2024-07-10 11:40:14 +00:00
|
|
|
$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
|
|
|
|
}
|
2024-07-10 08:01:42 +00:00
|
|
|
});
|
|
|
|
}
|
2024-07-10 11:40:14 +00:00
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->askName();
|
|
|
|
}
|
2024-07-10 08:01:42 +00:00
|
|
|
}
|