nhance/app/Controllers/Chatbot/EcardDownloadConversation.php
2025-10-09 15:50:20 +05:30

130 lines
5.1 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 EcardDownloadConversation extends Conversation
{
protected $buttonsData = [];
public function run()
{
$this->bot->types();
// sleep(0.5);
$this->showEcardMenu();
}
protected function showEcardMenu()
{
// Log entry for debugging
log_message('error', 'showEcardMenu function called');
// Get chat session and policies
$chat_session_info = get_chatbot_session_info();
$policy_list = ChatbotHelper::getListOfPolicies($chat_session_info);
// If policies found, build an HTML list of links (and raw URLs as fallback)
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'])) {
// As a last resort, use emp_policy_id (not ideal but prevents broken links)
$randString = $policy['emp_policy_id'];
}
// Build link; make sure base_url produces correct path
$link = base_url('/download-e-card/' . $randString . '/1');
// Escape policy name to avoid HTML injection
$safeName = isset($policy['policy_name']) ? htmlspecialchars($policy['policy_name'], ENT_QUOTES, 'UTF-8') : 'Policy';
// Add to parts; include anchor and raw URL fallback
$parts[] = "🔹 <a href=\"{$link}\" target=\"_blank\">{$safeName}</a><br>";
}
$message = "Choose a policy to download (click the policy name below):<br><br>" . implode('<br><br>', $parts);
log_message('error', 'Ecard links displayed: ' . json_encode(array_column($policy_list, 'policy_name')));
$this->say($message);
} else {
log_message('error', 'No policies found for ecard download');
$this->say('No policy found.');
}
// After showing links, move to confirmation conversation (Do you want to continue?)
// This keeps the UX identical to previous flow where we asked the user if they want to continue.
$this->bot->startConversation(new doYouWantToContinueConversation());
}
protected function showEcardMenuOLD()
{
log_message('error', ('showEcardMenu function called'));
$chat_session_info = get_chatbot_session_info();
$policy_list = ChatbotHelper::getListOfPolicies($chat_session_info);
$buttons = [];
$question = 'Choose Policy to Download Ecard:';
log_message('error', ('policy_list : ' . json_encode($policy_list)));
if(is_array($policy_list) && count($policy_list))
{
foreach($policy_list as $policy)
{
$this->buttonsData[$policy['emp_policy_id'].'#'.$policy['rand_string']] = ['response_text' => "🔹 {$policy['policy_name']}"] ;
$buttons[] = Button::create("{$policy['policy_name']}")->value($policy['emp_policy_id'].'#'.$policy['rand_string']);
}
}
else
{
$question = 'No policy found';
}
$this->buttonsData['go_back'] = ['response_text' => '◀️ Go Back'] ;
$buttons = array_merge($buttons,[Button::create("◀️ Go Back")->value("go_back")]);
$question = Question::create($question)
->addButtons($buttons);
$temp = $this->buttonsData;
$this->bot->ask($question, function ($answer) use ($temp) {
log_message('error', 'showEcardMenu answer received');
log_message('error', ($answer));
$selectedOption = $answer->getText();
$this->say("You have selected {$temp[$selectedOption]['response_text']}");
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';
log_message('error', $link);
$this->say('Click here to downlad: <a href="'.$link.'" target="_blank">GMC</a><br>Click here to downlad: <a href="'.$link.'" target="_blank">GMC Parent</a>');
$this->bot->startConversation(new doYouWantToContinueConversation()); // ✅ Restart the
break;
default:
$this->say("Invalid selection. Please choose an option.");
$this->bot->startConversation(new EcardDownloadConversation()); // ✅ Restart the conversation
break;
}
});
}
}