nhance/app/Controllers/Chatbot/NetworkHospitalConversation.php
2025-10-09 15:00:03 +05:30

146 lines
5.5 KiB
PHP

<?php
// namespace App\Conversations;
namespace App\Controllers\Chatbot;
use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use App\Helpers\ChatbotHelper;
class NetworkHospitalConversation extends Conversation
{
public function run()
{
$this->bot->types(); // Typing indicator for the first message
// sleep(0.5); // Delay
$this->showHospitalMenu();
}
protected function showHospitalMenuOLD()
{
$chat_session_info = get_chatbot_session_info();
$policy_list = ChatbotHelper::getListOfPolicies($chat_session_info);
$buttons = [];
$question = 'Choose Policy:';
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->hears('.*', function ($bot) {
// $this->bot->types(); // Typing indicator for the first message
// sleep(0.5); // Delay
// });
$this->bot->ask($question, function ($answer) {
$path = $this->bot->userStorage()->get('path');
array_push($path,
$answer->getValue()
);
$this->bot->userStorage()->save([
'path' => $path
]);
// log_message('error', ('user_reponse : ' . $answer->getValue()));
// log_message('error', ('user_reponse : ' . json_encode(explode('#',$answer->getValue()))));
switch ($answer->getValue()) {
case is_string($answer->getValue()) && is_array(explode('#',$answer->getValue())) && count((explode('#',$answer->getValue()))) == 2:
$emp_poilicy_id = explode('#',$answer->getValue());
$emp_poilicy_id = array_shift($emp_poilicy_id);
log_message('error','EMP POL ID FOR HOSPITAL' . $emp_poilicy_id);
$hospital_link = ChatbotHelper::getHospitalLink($emp_poilicy_id);
if($hospital_link)
{
$this->say('<a href="' . $hospital_link . '" target="_blank">Click here for hospital details</a>');
}
else
{
$this->say('No Data found, please contact support team');
}
$this->bot->startConversation(new doYouWantToContinueConversation());
break;
case "go_back":
$this->bot->startConversation(new MainMenuConversation());
break;
default:
$this->say("Invalid selection. Please choose an option.");
$this->bot->startConversation(new NetworkHospitalConversation());
break;
}
});
}
protected function showHospitalMenu()
{
log_message('error', 'showHospitalMenu function called');
// Get chat session and policies
$chat_session_info = get_chatbot_session_info();
$policy_list = ChatbotHelper::getListOfPolicies($chat_session_info);
if (is_array($policy_list) && count($policy_list)) {
$parts = [];
foreach ($policy_list as $policy) {
// Resolve rand string (defensive)
$randString = '';
if (isset($policy['rand_string']) && $policy['rand_string'] !== '') {
$randString = $policy['rand_string'];
} elseif (isset($policy['rand']) && $policy['rand'] !== '') {
$randString = $policy['rand'];
} elseif (isset($policy['emp_policy_id'])) {
$randString = $policy['emp_policy_id'];
}
$emp_policy_id = isset($policy['emp_policy_id']) ? $policy['emp_policy_id'] : '';
$hospital_link = ChatbotHelper::getHospitalLink($emp_policy_id);
$safeName = isset($policy['policy_name']) ? htmlspecialchars($policy['policy_name'], ENT_QUOTES, 'UTF-8') : 'Policy';
if ($hospital_link) {
$parts[] = "🔹 <a href=\"{$hospital_link}\" target=\"_blank\">{$safeName}</a><br>";
} else {
$parts[] = "🔹 {$safeName} — No Data found, please contact support team";
}
log_message('error', 'Hospital link for policy ' . $emp_policy_id . ': ' . $hospital_link);
}
$message = "Access hospital details for your policies below:<br><br>" . implode('<br><br>', $parts);
$this->say($message);
} else {
log_message('error', 'No policies found for hospital menu');
$this->say('No policy found.');
}
// After showing links, move to confirmation conversation
$this->bot->startConversation(new doYouWantToContinueConversation());
}
}