chatbot name,email,phone stored in Database

This commit is contained in:
UronShrestha 2024-07-10 17:25:14 +05:45
parent c566bf9c27
commit 2510de390a
3 changed files with 83 additions and 39 deletions

View File

@ -4,7 +4,8 @@ namespace App\Http\Controllers;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;
use App\Models\UserChat;
use Illuminate\Http\Request;
class BotManController extends Controller
@ -12,46 +13,88 @@ class BotManController extends Controller
public function handle()
{
$botman = app('botman');
$botman->hears('{message}', function ($botman, $message) {
if ($message == 'hi' || $message == 'Hi' || $message == 'Hello' || $message == 'hello') {
$this->askName($botman);
$botman->hears('{message}', function (BotMan $botman, $message) {
if (strtolower($message) == 'hi' || strtolower($message) == 'hello') {
$botman->startConversation(new OnboardingConversation());
} else {
$botman->reply("Start the conversation saying Hi.");
$botman->reply("Start the conversation by saying Hi.");
}
});
$botman->listen();
}
public function askName($botman)
}
class OnboardingConversation extends Conversation
{
protected $name;
protected $phoneNumber;
protected $email;
public function askName()
{
$botman->ask('Hello! What is your name?', function (Answer $answer, $conversation) {
$name = $answer->getText();
$this->say("Nice to meet you " . $name);
$conversation->ask('Can you tell us your phone number?', function (Answer $answer, $conversation) {
$phoneNumber = $answer->getText();
$this->say("Your phone number is " . $phoneNumber);
$conversation->ask('Can you tell us your email?', function (Answer $answer, $conversation) {
$email = $answer->getText();
$this->say("Your email is " . $email);
$conversation->ask('Confirm your Email (Y/N):', function (Answer $answer, $conversation) {
$confirmEmail = $answer->getText();
if ($answer == 'Y' || $answer == "y") {
$this->say("we got your details");
} elseif ($answer == 'N' || $answer == "n") {
$this->say("Please confirm your email");
$conversation->ask('Write your email again.', function (Answer $answer, $conversation) {
$email = $answer->getText();
$this->say("Your email is " . $email);
$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();
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') {
$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') {
$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();
}
}

View File

@ -8,4 +8,5 @@ use Illuminate\Database\Eloquent\Model;
class UserChat extends Model
{
use HasFactory;
protected $fillable = ['name', 'phone_number', 'email'];
}

View File

@ -11,11 +11,11 @@ return new class extends Migration
*/
public function up(): void
{
Schema::create('userChat', function (Blueprint $table) {
Schema::create('user_chats', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('phone');
$table->string('phone_number');
$table->string('email');
$table->timestamps();
});
}
@ -25,6 +25,6 @@ return new class extends Migration
*/
public function down(): void
{
Schema::dropIfExists('userChat');
Schema::dropIfExists('user_chats');
}
};