From 941d50441380258b2be06cf6e44eac9f80c0a394 Mon Sep 17 00:00:00 2001 From: velz Date: Sat, 1 Feb 2025 10:29:43 +0530 Subject: [PATCH] FEAT_CHATBOT --- app/Controllers/Chatbot/BotService.php | 110 ++++++++++++++++++ app/Controllers/Chatbot/ClaimHandler.php | 34 ++++++ .../Chatbot/EcardDownloadConversation.php | 65 +++++++++++ .../Chatbot/MainMenuConversation.php | 57 +++++++++ app/Controllers/Chatbot/PolicyHandler.php | 36 ++++++ app/Controllers/ChatbotControllerNew.php | 90 ++++++++++++++ app/Helpers/ChatbotHelper.php | 28 +++++ app/NetworkHospitalConversation.php | 44 +++++++ app/Views/chatbot.php | 83 +++++++++++++ app/Views/widget.php | 15 +++ 10 files changed, 562 insertions(+) create mode 100644 app/Controllers/Chatbot/BotService.php create mode 100644 app/Controllers/Chatbot/ClaimHandler.php create mode 100644 app/Controllers/Chatbot/EcardDownloadConversation.php create mode 100644 app/Controllers/Chatbot/MainMenuConversation.php create mode 100644 app/Controllers/Chatbot/PolicyHandler.php create mode 100644 app/Controllers/ChatbotControllerNew.php create mode 100644 app/Helpers/ChatbotHelper.php create mode 100644 app/NetworkHospitalConversation.php create mode 100644 app/Views/chatbot.php create mode 100644 app/Views/widget.php diff --git a/app/Controllers/Chatbot/BotService.php b/app/Controllers/Chatbot/BotService.php new file mode 100644 index 00000000..ee3b57ad --- /dev/null +++ b/app/Controllers/Chatbot/BotService.php @@ -0,0 +1,110 @@ +reply('Welcome! Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Card Download'], + ['Network Hospital'], + ['Reimbursement Claim Process'], + ['Reimbursement Claim Status'], + ['New Policy'], + ['Renew Policy'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleCardDownload(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Download Card'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleNetworkHospital(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Find Hospital'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleReimbursementClaimProcess(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Submit Claim'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleReimbursementClaimStatus(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Check Status'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleNewPolicy(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Get Quote'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } + + public static function handleRenewPolicy(BotMan $bot) + { + $bot->reply('Please choose an option:', [ + 'reply_markup' => json_encode([ + 'keyboard' => [ + ['Renew Now'], + ['Go Back'] + ], + 'resize_keyboard' => true, + 'one_time_keyboard' => true + ]) + ]); + } +} \ No newline at end of file diff --git a/app/Controllers/Chatbot/ClaimHandler.php b/app/Controllers/Chatbot/ClaimHandler.php new file mode 100644 index 00000000..b1839734 --- /dev/null +++ b/app/Controllers/Chatbot/ClaimHandler.php @@ -0,0 +1,34 @@ + "Reimbursement Claim Process: Here is a dummy response.", + 'reimbursement_claim_status' => "Reimbursement Claim Status: Here is a dummy response." + ]; + + if (isset($responses[$option])) { + $bot->reply($responses[$option]); + } else { + $bot->reply("Unknown claim option."); + } + + // Send Back Button + self::sendBackButton($bot); + } + + private static function sendBackButton(BotMan $bot) + { + $bot->reply(ButtonTemplate::create("Would you like to return?") + ->addButton(ElementButton::create("Go Back")->type('postback')->payload("main_menu")) + ); + } +} diff --git a/app/Controllers/Chatbot/EcardDownloadConversation.php b/app/Controllers/Chatbot/EcardDownloadConversation.php new file mode 100644 index 00000000..0cc82314 --- /dev/null +++ b/app/Controllers/Chatbot/EcardDownloadConversation.php @@ -0,0 +1,65 @@ +showEcardMenu(); + } + + protected function showEcardMenu() + { + $policy_list = ChatbotHelper::getListOfPolicies(); + $buttons = []; + $question = 'Choose Policy to Download Ecard:'; + if(is_array($policy_list) && count($policy_list)) + { + foreach($policy_list as $policy) + { + // $question = Question::create("Choose Policy to Download Ecard:") + // ->addButtons([ + // Button::create("🔹 $policy['policy_name']")->value("$policy['emp_policy_id']"), + // Button::create("◀️ Go Back")->value("go_back"), + // ]); + $buttons[] = Button::create("🔹 {$policy['policy_name']}")->value($policy['emp_policy_id'].'#'.$policy['rand_string']); + } + } + else + { + $question = 'No policy found'; + } + $buttons = array_merge($buttons,[Button::create("◀️ Go Back")->value("go_back")]); + $question = Question::create($question) + ->addButtons($buttons); + + $this->bot->ask($question, function ($answer) { + // print_r($answer->getValue());die(); + switch ($answer->getValue()) { + case "go_back": + $this->bot->startConversation(new MainMenuConversation()); + break; + case is_string($answer->getValue()) && is_array(explode('#',$answer->getValue())) && count((explode('#',$answer->getValue()))) == 2: + + $link = base_url().'/download-e-card/' . explode('#',$answer->getValue())[1].'/1'; + $this->say('Click here to downlad: Ecard'); + + $this->bot->startConversation(new EcardDownloadConversation()); // ✅ Restart the + + break; + + default: + $this->say("Invalid selection. Please choose an option."); + $this->bot->startConversation(new EcardDownloadConversation()); // ✅ Restart the conversation + break; + } + }); + } +} diff --git a/app/Controllers/Chatbot/MainMenuConversation.php b/app/Controllers/Chatbot/MainMenuConversation.php new file mode 100644 index 00000000..01b1c576 --- /dev/null +++ b/app/Controllers/Chatbot/MainMenuConversation.php @@ -0,0 +1,57 @@ +showMainMenu(); + } + + protected function showMainMenu() + { + $question = Question::create("Please choose") + ->addButtons([ + Button::create("📄 Ecard Download")->value("ecard_download"), + Button::create("🏥 Network Hospital")->value("network_hospital"), + Button::create("💰 Reimbursement Claim Process")->value("reimbursement_claim"), + Button::create("📑 Reimbursement Claim Status")->value("reimbursement_status"), + Button::create("🆕 New Policy")->value("new_policy"), + Button::create("🔄 Renew Policy")->value("renew_policy"), + ]); + + $this->bot->ask($question, function ($answer) { + switch ($answer->getValue()) { + case "ecard_download": + $this->say("📄 Ecard Download Menu:"); + $this->bot->startConversation(new EcardDownloadConversation()); + break; + case "network_hospital": + $this->say("🏥 Network Hospital Menu:"); + $this->bot->startConversation(new NetworkHospitalConversation()); + break; + case "reimbursement_claim": + $this->say("Reimbursement Claim Process selected. (Dummy Response)"); + break; + case "reimbursement_status": + $this->say("Reimbursement Claim Status selected. (Dummy Response)"); + break; + case "new_policy": + $this->say("New Policy selected. (Dummy Response)"); + break; + case "renew_policy": + $this->say("Renew Policy selected. (Dummy Response)"); + break; + default: + $this->say("Invalid selection. Please choose an option."); + $this->showMainMenu(); + break; + } + }); + } +} diff --git a/app/Controllers/Chatbot/PolicyHandler.php b/app/Controllers/Chatbot/PolicyHandler.php new file mode 100644 index 00000000..9491ef9d --- /dev/null +++ b/app/Controllers/Chatbot/PolicyHandler.php @@ -0,0 +1,36 @@ + "Ecard Download: Here is a dummy response.", + 'network_hospital' => "Network Hospital: Here is a dummy response.", + 'new_policy' => "New Policy: Here is a dummy response.", + 'renew_policy' => "Renew Policy: Here is a dummy response." + ]; + + if (isset($responses[$option])) { + $bot->reply($responses[$option]); + } else { + $bot->reply("Unknown policy option."); + } + + // Send Back Button + self::sendBackButton($bot); + } + + private static function sendBackButton(BotMan $bot) + { + $bot->reply(ButtonTemplate::create("Would you like to return?") + ->addButton(ElementButton::create("Go Back")->type('postback')->payload("main_menu")) + ); + } +} diff --git a/app/Controllers/ChatbotControllerNew.php b/app/Controllers/ChatbotControllerNew.php new file mode 100644 index 00000000..fc6bd75a --- /dev/null +++ b/app/Controllers/ChatbotControllerNew.php @@ -0,0 +1,90 @@ +myLogger = \Config\Services::mylogger(); + $this->session = \Config\Services::session(); + + $this->myLogger->logme('error', 'CALLED'); + + // Load BotMan driver + DriverManager::loadDriver(WebDriver::class); + + // Setup BotMan with cache + $cache = new SymfonyCache(new FilesystemAdapter()); + $this->botman = BotManFactory::create([], $cache); + + // Retrieve user ID from session or request + $extras = request()->getGet('conf'); + $extras = json_decode($extras); + $router = service('router'); + $method = $router->methodName(); + $this->myLogger->logme('error', $method); + + if (isset($extras) && $method == 'widget') { + $extras = $extras->parameters; + $this->session->set('CHATBOT_USER_ID', $extras->employee_id); + } + + $this->myLogger->logme('error', $this->session->get('CHATBOT_USER_ID')); + + // print_rr(ChatbotHelper::getListOfPolicies());die(); + } + + public function index() + { + // Start the Main Menu when user says "hi" or "start" + $this->botman->hears('start|hi|hello', function (BotMan $bot) { + $bot->reply('How how can i assit?'); + $bot->startConversation(new MainMenuConversation()); + }); + + // $this->botman->hears('main_menu', function (BotMan $bot) { + // BotService::sendMainOptions($bot); + // }); + + // // Register Option Handlers + // $this->registerHandlers(); + + // Listen for Messages + $this->botman->listen(); + } + + private function registerHandlers() + { + // Handling Policy and Claims + $this->botman->hears('group:policy:{option}', [\App\Controllers\Chatbot\PolicyHandler::class, 'handle']); + $this->botman->hears('group:claim:{option}', [\App\Libraries\Chatbot\ClaimHandler::class, 'handle']); + + + } + + public function chatbot() + { + $this->loadLayout('chatbot.php'); + } + + public function widget() + { + echo view('widget.php'); + } +} diff --git a/app/Helpers/ChatbotHelper.php b/app/Helpers/ChatbotHelper.php new file mode 100644 index 00000000..0b547cd0 --- /dev/null +++ b/app/Helpers/ChatbotHelper.php @@ -0,0 +1,28 @@ +getEmpFamilybyEmpCode(emp_code: $emp_code,client_id: $client_id,emp_status: ['draft'],policy_status:['draft'],client_branch_id:[ $client_branch_id ],relationship:[$relationship]); + } + + +} + +?> diff --git a/app/NetworkHospitalConversation.php b/app/NetworkHospitalConversation.php new file mode 100644 index 00000000..70e71340 --- /dev/null +++ b/app/NetworkHospitalConversation.php @@ -0,0 +1,44 @@ +showHospitalMenu(); + } + + protected function showHospitalMenu() + { + $question = Question::create("Choose your Network Hospital option:") + ->addButtons([ + Button::create("🔹 Search by City")->value("search_city"), + Button::create("🔹 Search by Pincode")->value("search_pincode"), + Button::create("◀️ Go Back")->value("go_back"), + ]); + + $this->bot->ask($question, function ($answer) { + switch ($answer->getValue()) { + case "search_city": + $this->say("Searching by City... (Dummy Response)"); + break; + case "search_pincode": + $this->say("Searching by Pincode... (Dummy Response)"); + break; + case "go_back": + $this->bot->startConversation(new MainMenuConversation()); + break; + default: + $this->say("Invalid selection. Please choose an option."); + $this->showHospitalMenu(); + break; + } + }); + } +} diff --git a/app/Views/chatbot.php b/app/Views/chatbot.php new file mode 100644 index 00000000..77adf697 --- /dev/null +++ b/app/Views/chatbot.php @@ -0,0 +1,83 @@ + + + + + + Insurance ChatBot + + + + +

Welcome to Insurance ChatBot

+ + + + + + + + + + diff --git a/app/Views/widget.php b/app/Views/widget.php new file mode 100644 index 00000000..1b14b195 --- /dev/null +++ b/app/Views/widget.php @@ -0,0 +1,15 @@ + + + + BotMan Widget + + + + + + + + + + + \ No newline at end of file