chatbot name,email,phone stored in Database
This commit is contained in:
parent
c566bf9c27
commit
2510de390a
@ -4,7 +4,8 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use BotMan\BotMan\BotMan;
|
use BotMan\BotMan\BotMan;
|
||||||
use BotMan\BotMan\Messages\Incoming\Answer;
|
use BotMan\BotMan\Messages\Incoming\Answer;
|
||||||
|
use BotMan\BotMan\Messages\Conversations\Conversation;
|
||||||
|
use App\Models\UserChat;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class BotManController extends Controller
|
class BotManController extends Controller
|
||||||
@ -12,46 +13,88 @@ class BotManController extends Controller
|
|||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$botman = app('botman');
|
$botman = app('botman');
|
||||||
$botman->hears('{message}', function ($botman, $message) {
|
$botman->hears('{message}', function (BotMan $botman, $message) {
|
||||||
if ($message == 'hi' || $message == 'Hi' || $message == 'Hello' || $message == 'hello') {
|
if (strtolower($message) == 'hi' || strtolower($message) == 'hello') {
|
||||||
$this->askName($botman);
|
$botman->startConversation(new OnboardingConversation());
|
||||||
} else {
|
} else {
|
||||||
$botman->reply("Start the conversation saying Hi.");
|
$botman->reply("Start the conversation by saying Hi.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
$botman->listen();
|
$botman->listen();
|
||||||
}
|
}
|
||||||
public function askName($botman)
|
}
|
||||||
|
|
||||||
|
class OnboardingConversation extends Conversation
|
||||||
{
|
{
|
||||||
$botman->ask('Hello! What is your name?', function (Answer $answer, $conversation) {
|
protected $name;
|
||||||
$name = $answer->getText();
|
protected $phoneNumber;
|
||||||
$this->say("Nice to meet you " . $name);
|
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()
|
||||||
$conversation->ask('Can you tell us your phone number?', function (Answer $answer, $conversation) {
|
{
|
||||||
|
$this->ask('Tell us your phone number:', function (Answer $answer) {
|
||||||
$phoneNumber = $answer->getText();
|
$phoneNumber = $answer->getText();
|
||||||
$this->say("Your phone number is " . $phoneNumber);
|
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
$conversation->ask('Can you tell us your email?', function (Answer $answer, $conversation) {
|
$this->phoneNumber = $phoneNumber;
|
||||||
$email = $answer->getText();
|
$this->say("Your phone number is " . $this->phoneNumber);
|
||||||
$this->say("Your email is " . $email);
|
$this->askEmail();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$conversation->ask('Confirm your Email (Y/N):', function (Answer $answer, $conversation) {
|
public function askEmail()
|
||||||
|
{
|
||||||
|
$this->ask('Can you tell us your email?', function (Answer $answer) {
|
||||||
|
$this->email = $answer->getText();
|
||||||
|
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();
|
$confirmEmail = $answer->getText();
|
||||||
if ($answer == 'Y' || $answer == "y") {
|
if (strtolower($confirmEmail) == 'y') {
|
||||||
$this->say("we got your details");
|
$this->say("We got your details. Thank you!");
|
||||||
} elseif ($answer == 'N' || $answer == "n") {
|
|
||||||
$this->say("Please confirm your email");
|
// Save the details to the database
|
||||||
$conversation->ask('Write your email again.', function (Answer $answer, $conversation) {
|
UserChat::create([
|
||||||
$email = $answer->getText();
|
'name' => $this->name,
|
||||||
$this->say("Your email is " . $email);
|
'phone_number' => $this->phoneNumber,
|
||||||
});
|
'email' => $this->email,
|
||||||
|
]);
|
||||||
|
} elseif (strtolower($confirmEmail) == 'n') {
|
||||||
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,5 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
class UserChat extends Model
|
class UserChat extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
protected $fillable = ['name', 'phone_number', 'email'];
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,11 @@ return new class extends Migration
|
|||||||
*/
|
*/
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::create('userChat', function (Blueprint $table) {
|
Schema::create('user_chats', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('email')->unique();
|
$table->string('phone_number');
|
||||||
$table->string('phone');
|
$table->string('email');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -25,6 +25,6 @@ return new class extends Migration
|
|||||||
*/
|
*/
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
Schema::dropIfExists('userChat');
|
Schema::dropIfExists('user_chats');
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user