nhance/app/Controllers/Chatbot/vehicleFormConversation.php
2025-09-10 09:28:02 +05:30

220 lines
8.5 KiB
PHP

<?php
namespace App\Controllers\Chatbot;
use App\Helpers\ChatbotHelper;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Conversations\Conversation;
class vehicleFormConversation extends Conversation
{
protected $vehicleName;
protected $vehicleYOM;
protected $vehicleEngineNo;
protected $vehicleInvoiceNo;
protected $userId;
protected $complied_information;
protected $fuelType;
protected $seater;
protected $chassisNumber;
public function run()
{
// Ensure $this->bot is properly initialized
// $user = $this->bot->getUser();
// $this->userId = $user->getId(); // Correctly store user ID
$this->askVehicleName(); // Start asking vehicle questions
}
protected function askVehicleName()
{
$this->ask('Enter Vehicle Name ?', function ($response) {
$this->vehicleName = $response->getText();
if (empty($this->vehicleName)) {
$this->say('Vehicle Name Cannot be Empty');
return $this->askVehicleName(); // Re-ask if empty
}
// $this->askVehicleYOM(); // Ask next question
$path = $this->bot->userStorage()->get('path');
if (in_array('4_wheeler', $path)) {
$this->askFuelType();
} else {
$this->askVehicleYOM(); // Ask next question
}
});
}
protected function askFuelType()
{
$this->ask('Enter Fuel Type of the Vehicle ?', function ($response) {
$this->fuelType = $response->getText();
if (empty($this->fuelType)) {
$this->say('Vehicle Fuel Type Cannot be Empty');
return $this->askFuelType(); // Re-ask if empty
}
$this->askSeating(); // Ask next question
});
}
protected function askSeating()
{
$this->ask('Enter the seat count of the Vehicle ?', function ($response) {
$this->seater = $response->getText();
if (empty($this->seater)) {
$this->say('Vehicle seat Cannot be Empty');
return $this->askSeating(); // Re-ask if empty
}
$this->askVehicleYOM(); // Ask next question
});
}
protected function askVehicleYOM()
{
$this->ask('Enter Vehicle Year of Manufacture ?', function ($response) {
$this->vehicleYOM = $response->getText();
if (empty($this->vehicleYOM)) {
$this->say('Vehicle Year of Manufacture Cannot be Empty');
return $this->askVehicleYOM(); // Re-ask if empty
}
$this->askVehicleEngineNo(); // Ask next question
});
}
protected function askVehicleEngineNo()
{
$this->ask('Enter Vehicle Engine Number ?', function ($response) {
$this->vehicleEngineNo = $response->getText();
if (empty($this->vehicleEngineNo)) {
$this->say('Vehicle Engine Number Cannot be Empty');
return $this->askVehicleEngineNo(); // Re-ask if empty
}
// Retrieve 'path' from storage
$path = $this->bot->userStorage()->get('path');
if (in_array('4_wheeler', $path)) {
$this->askChassisNumber();
} else {
$this->askVehicleInvoiceNo(); // Ask next question
}
// $this->askVehicleInvoiceNo();
});
}
protected function askChassisNumber()
{
$this->ask('Enter Chassis Number of the Vehicle ?', function ($response) {
$this->chassisNumber = $response->getText();
if (empty($this->chassisNumber)) {
$this->say('Vehicle Chassis Number Cannot be Empty');
return $this->askChassisNumber(); // Re-ask if empty
}
$this->askVehicleInvoiceNo(); // Ask next question
});
}
protected function askVehicleInvoiceNo()
{
$this->ask('Enter Vehicle Invoice Number ?', function ($response) {
$this->vehicleInvoiceNo = $response->getText();
if (empty($this->vehicleInvoiceNo)) {
$this->say('Vehicle Invoice Number Cannot be Empty');
return $this->askVehicleInvoiceNo(); // Re-ask if empty
}
$this->storeVehicleData(); // Proceed to store the data
});
}
protected function storeVehicleData()
{
// Retrieve 'path' from storage
$path = $this->bot->userStorage()->get('path');
// $this->say(json_encode($path));
// Prepare vehicle details
$vehicleDetails = [
'vehicle_name' => $this->vehicleName,
'vehicle_yom' => $this->vehicleYOM,
'vehicle_engine_no' => $this->vehicleEngineNo,
$vehicleDetails['vehicle_invoice_no'] = $this->vehicleInvoiceNo
];
// Only store 'vehicle_invoice_no' if 'new_policy' is NOT in path
if (in_array('4_wheeler', $path)) {
$vehicleDetails['vehicle_fuelType'] = $this->fuelType;
$vehicleDetails['vehicle_seater'] = $this->seater;
$vehicleDetails['vehicle_chassisNo'] = $this->chassisNumber;
}
// Save vehicle details
$this->bot->userStorage()->save(['vehicle_details' => $vehicleDetails]);
$alldata = $this->bot->userStorage()->all();
$vehicle_details = $this->bot->userStorage()->get('vehicle_details');
// Log stored data for debugging
// log_message('error', 'All Details: ' . json_encode($alldata));
// log_message('error', 'Updated Vehicle Details: ' . json_encode($vehicle_details));
$this->say('Vehicle Information Saved!');
$vehicleInfo = "🚗 **Vehicle Details:**<br>";
$vehicleInfo .= "🔹 Name: " . $vehicle_details['vehicle_name'] . "<br>";
$vehicleInfo .= "🔹 Year of Manufacture: " . $vehicle_details['vehicle_yom'] . "<br>";
$vehicleInfo .= "🔹 Engine No: " . $vehicle_details['vehicle_engine_no'] . "<br>";
if (isset($vehicle_details['vehicle_fuelType'])) {
$vehicleInfo .= "🔹 Fuel Type: " . $vehicle_details['vehicle_fuelType'] . "<br>";
$vehicleInfo .= "🔹 Seater: " . $vehicle_details['vehicle_seater'] . "<br>";
$vehicleInfo .= "🔹 Chassis Number: " . $vehicle_details['vehicle_chassisNo'] . "<br>";
}
$this->complied_information = $vehicleInfo;
// Send the message with vehicle details
$this->say($vehicleInfo);
$this->confirmVehicleDate($vehicleInfo);
// $this->bot->startConversation(new selectInsurerConversations());
}
protected function confirmVehicleDate($vehicle_info){
$question = Question::create("Confim your Details:")
->addButtons([
Button::create("✅ Confirm")->value("confirm"),
Button::create("🔁 Start Over")->value("restart"),
Button::create("❌ Cancel")->value('cancel')
]);
$this->bot->ask($question, function ($answer) use($vehicle_info) {
$path = $this->bot->userStorage()->get('path');
array_push($path,
$answer->getValue()
);
$this->bot->userStorage()->save([
'path' => $path
]);
switch ($answer->getValue()) {
case "confirm":
// $cityName = $response->getText();
$sent_staus = ChatbotHelper::sendQuotationRequestMail($vehicle_info);
if ($sent_staus){
$this->say('Thanks.Our Executive will contact you with the quotation soon.');
$this->bot->startConversation(new doYouWantToContinueConversation());
}else{
$this->say("There was a problem while requesting for a quotation please try again later.");
}
break;
case "restart":
$this->bot->startConversation(new vehicleFormConversation());
break;
case "cancel":
$this->say("Redirecting you to main menu...");
$this->bot->startConversation(new MainMenuConversation());
break;
default:
$this->say("Invalid selection. Please choose an option.");
$this->showHospitalMenu();
break;
}
});
}
}