nhance/app/Controllers/ChatbotControllerNew.php
2025-02-04 15:51:39 +05:30

106 lines
2.9 KiB
PHP

<?php
namespace App\Controllers;
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;
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_USER_ID', $extras->employee_id);
}
$this->myLogger->logme('error', $this->session->get('CHATBOT_USER_ID'));
// print_rr(ChatbotHelper::getListOfPolicies());die();
}
public function index()
{
$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) {
$bot->reply('Hi how can i assit?');
$bot->startConversation(new MainMenuConversation());
});
// $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');
}
}