nhance/app/Controllers/ChatbotControllerNew.php
2025-04-08 14:08:54 +05:30

152 lines
5.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\Chatbot\LoginToContiueConversation;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\BotMan\Cache\SymfonyCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use BotMan\Drivers\Web\WebDriver;
use App\Controllers\Chatbot\MainMenuConversation;
use App\Controllers\Chatbot\TypingMiddleware;
use App\Helpers\ChatbotHelper;
class ChatbotControllerNew extends BaseController
{
protected $myLogger;
protected $session;
protected $botman;
protected $isMemberLoggedIn;
public function __construct()
{
set_session_context('CHATBOT CON');
$this->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);
// Create a new instance of BotMan
// $botman = resolve('botman');
// Attach the TypingMiddleware
// $this->botman->middleware->received(new TypingMiddleware());
// 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_EMP_ID', $extras->employee_id);
$this->session->set('CHATBOT_ORIGIN', $extras->origin);
$this->session->set('CHATBOT_EMP_CODE', $extras->emp_code);
$this->session->set('CHATBOT_CLIENT_ID', $extras->client_id);
$this->session->set('CHATBOT_CLIENT_BRANCH_ID', $extras->client_branch_id);
if($this->session->get('CHATBOT_RANDOM_USER_ID') == '')
{
$this->session->set('CHATBOT_RANDOM_USER_ID', '-');
}
}
$log_message = [
'CHATBOT_EMP_ID' => $this->session->get('CHATBOT_EMP_ID'),
'CHATBOT_ORIGIN' => $this->session->get('CHATBOT_ORIGIN'),
'CHATBOT_EMP_CODE' => $this->session->get('CHATBOT_EMP_CODE'),
'CHATBOT_CLIENT_ID' => $this->session->get('CHATBOT_CLIENT_ID'),
'CHATBOT_CLIENT_BRANCH_ID' => $this->session->get('CHATBOT_CLIENT_BRANCH_ID'),
'CHATBOT_RANDOM_USER_ID' => $this->session->get('CHATBOT_RANDOM_USER_ID')
];
$log_message = implode(' | ', array_map(
fn($key, $value) => "{$key}: {$value}",
array_keys($log_message),
$log_message
));
$this->myLogger->logme('error', $log_message);
// print_rr(ChatbotHelper::getListOfPolicies());die();
}
public function index()
{
if($this->session->get('CHATBOT_RANDOM_USER_ID') == '-' || $this->session->get('CHATBOT_RANDOM_USER_ID') == '')
{
$user = $this->botman->getUser();
$id = $user->getId();
$this->myLogger->logme('error', ('TEST' . $id));
$this->session->set('CHATBOT_RANDOM_USER_ID', $id);
}
$this->isMemberLoggedIn = false;
$chat_session_info = get_chatbot_session_info();
if (!empty($chat_session_info['emp_id']) && !empty($chat_session_info['emp_code'] )){
$this->isMemberLoggedIn = true;
}
$this->botman->hears('.*', function ($bot) {
log_message("error","Inside Bot Type Function");
$bot->types(); // Typing indicator for the first message
sleep(0.5); // Delay
});
// Start the Main Menu when user says "hi" or "start"
$this->botman->hears('start|hi|hello', function (BotMan $bot) {
if ($this->isMemberLoggedIn){
$bot->reply('Hi how can i assist?');
$bot->startConversation(new MainMenuConversation());
}else {
$bot->startConversation(new LoginToContiueConversation());
}
});
// $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');
}
}