nhance/app/Controllers/Chatbot/vehicleFormConversation.php
2025-02-03 10:26:49 +05:30

84 lines
2.7 KiB
PHP

<?php
namespace App\Controllers\Chatbot;
use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
class vehicleFormConversation extends Conversation
{
protected $vehicleName;
protected $vehicleYOM;
protected $vehicleEngineNo;
protected $vehicleInvoiceNo;
public function run()
{
$this->askVehicleName();
}
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
});
}
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
}
$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()
{
// Save all collected data to user storage
$this->bot->userStorage()->save([
'vehicle_name' => $this->vehicleName,
'vehicle_yom' => $this->vehicleYOM,
'vehicle_engine_no' => $this->vehicleEngineNo,
'vehicle_invoice_no' => $this->vehicleInvoiceNo
]);
$this->say('Vehicle Information Saved!');
$this->bot->startConversation(new doYouWantToContinueConversation());
}
}