1. Go to this page and download the library: Download mahbub/laravel-ai-chatbot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mahbub / laravel-ai-chatbot example snippets
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Mahbub\LaravelAiChatbot\Services\AiChatService;
use Mahbub\LaravelAiChatbot\Models\ChatSession;
class ChatController extends Controller
{
protected $chatService;
public function __construct(AiChatService $chatService)
{
$this->chatService = $chatService;
}
public function __invoke(Request $request)
{
// 1. Get or create a chat session for the user
$session = ChatSession::firstOrCreate([
'session_id' => $request->get('session_id') ?? Str::uuid(),
]);
// 2. Send the message and get the AI response (history saved automatically)
$aiResponse = $this->chatService->sendMessage($session, $request->get('message'));
return response()->json([
'session_id' => $session->session_id,
'response' => $aiResponse,
]);
}
}