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) { $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'); } }