nhance/app/Controllers/Chatbot/EcardDownloadConversation.php
2025-02-10 12:37:36 +05:30

68 lines
2.7 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
{
public function run()
{
$this->showEcardMenu();
}
protected function showEcardMenu()
{
$chat_session_info = get_chatbot_session_info();
$policy_list = ChatbotHelper::getListOfPolicies($chat_session_info);
$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) {
$selectedOption = $answer->getText();
$this->say("You have selected {$selectedOption}");
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: <a href="'.$link.'" target="_blank">Ecard</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;
}
});
}
}