Files
rohini-web/Modules/Chatbot/app/Http/Controllers/ChatbotController.php
2025-12-28 12:16:05 +05:45

196 lines
7.8 KiB
PHP

<?php
namespace Modules\Chatbot\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
class ChatbotController extends Controller
{
public function index()
{
return view('chatbot::index');
}
public function create()
{
return view('chatbot::create');
}
public function store(Request $request)
{
//
}
public function show($id)
{
return view('chatbot::show');
}
public function edit($id)
{
return view('chatbot::edit');
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
public function queryold(Request $request)
{
$message = $request->input('message');
return response()->json(['reply' => 'You said: ' . $message]);
}
// public function query(Request $request)
// {
// $request->validate([
// 'message' => 'required|string'
// ]);
// // 1) Pull the current contact details from settings
// $phone = setting('phone'); // e.g. "5344710"
// $email = setting('email'); // e.g. "info@rohini.edu.np"
// $address = setting('address'); // e.g. "House #160, Adwait Marg, Putalisadak, Kathmandu, Nepal"
// $facebook = setting('facebook'); // if you have this
// $whatsapp = setting('whatsapp'); // if you have this
// $userMessage = trim($request->message);
// // 2) Build a system prompt that *includes* your real contact info
// $systemContents = <<<EOT
// You are an assistant for Rohini International Education Services in Nepal.
// Use *only* the following facts when answering:
// • Website: https://rohini.edu.np/
// • Phone: {$phone}
// • Email: {$email}
// • Address: {$address}
// EOT;
// // 3) Prepare the message payload
// $messages = [
// ['role' => 'system', 'content' => $systemContents],
// ['role' => 'user', 'content' => $userMessage],
// ];
// // 4) Call the AI as before
// try {
// $response = Http::timeout(60)
// ->retry(2, 100)
// ->withToken(config('services.openrouter.key'))
// ->post('https://openrouter.ai/api/v1/chat/completions', [
// 'model' => 'deepseek/deepseek-r1:free',
// 'messages' => $messages,
// ]);
// $response->throw();
// // 5) Return exactly what the AI gave, knowing the system prompt constrained it
// return response()->json([
// 'reply' => $response->json('choices.0.message.content') ?? 'No response from assistant.'
// ]);
// } catch (ConnectionException $e) {
// return response()->json([
// 'reply' => 'Sorry, I could not connect to the AI service. Please try again later.'
// ], 504);
// } catch (RequestException $e) {
// $msg = $e->response->json('error.message') ?? $e->getMessage();
// return response()->json([
// 'reply' => 'Sorry, an error occurred. Please try again later.',
// 'error' => $msg
// ], 500);
// }
// }
public function query(Request $request)
{
$request->validate([
'message' => 'required|string'
]);
// Company info
$phone = setting('phone');
$email = setting('email');
$address = setting('location');
$whatsapp = setting('whatsapp');
$instagram = setting('instagram');
$youtube = setting('youtube');
$tiktok = setting('tiktok');
$userMessage = strtolower(trim($request->message));
$companyInfo = "\n\nFor more information, visit <a href=\"https://rohini.edu.np/\" target=\"_blank\">https://rohini.edu.np/</a> or contact us: Phone: <a href=\"tel:{$phone}\">{$phone}</a>, Email: <a href=\"mailto:{$email}\">{$email}</a>, WhatsApp: <a href=\"{$whatsapp}\" target=\"_blank\">{$whatsapp}</a>.";
// Define topics instead of strict questions
$topics = [
'greeting' => [
'keywords' => ['hi','hello','hey','good morning','good afternoon','good evening'],
'response' => 'Hello! How can I assist you with your study abroad plans today?'
],
'company_info' => [
'keywords' => ['rohini', 'about us', 'who are you', 'information'],
'response' => 'Rohini International Education Services is a Nepal-based consultancy specializing in guiding students who want to study in New Zealand.'
],
'location' => [
'keywords' => ['location','located','where'],
'response' => "Our Kathmandu office is at {$address}."
],
'contact' => [
'keywords' => ['contact','support','call','email','whatsapp'],
'response' => "You can call us at {$phone}, email at {$email}, or message on WhatsApp at {$whatsapp}."
],
'services' => [
'keywords' => ['service','services','what do you offer','help','assistance','counseling','visa','course','accommodation'],
'response' => 'We provide counseling, university & course selection, visa support, documentation help, pre-departure briefings, financial & scholarship guidance, and accommodation support for students going to New Zealand.'
],
'test_preparation' => [
'keywords' => ['ielts','pte','test','exam','preparation','coaching','classes'],
'response' => 'Yes, we offer IELTS and PTE preparation with experienced instructors.'
],
'application_process' => [
'keywords' => ['process','apply','application','visa','eligibility','documents'],
'response' => 'We guide students through course selection, eligibility, document preparation, visa application, and departure preparation.'
],
'new_zealand_education' => [
'keywords' => ['new zealand','study in new zealand','higher education','university','study abroad','post study','work opportunities', 'newzealand'],
'response' => 'New Zealand offers globally recognized qualifications, high-quality education, a safe environment, and post-study work opportunities.'
],
'accommodation' => [
'keywords' => ['hostel','homestay','flat','housing','accommodation','stay'],
'response' => 'Students can choose shared flats, homestays, hostels, or on-campus housing. We assist in finding the right option.'
],
'social_media' => [
'keywords' => ['social media','instagram','youtube','tiktok','follow','socials','facebook'],
'response' => "Follow us on <a href=\"{$instagram}\" target=\"_blank\">Instagram</a>, <a href=\"{$youtube}\" target=\"_blank\">Youtube</a>, <a href=\"{$tiktok}\" target=\"_blank\">Tiktok</a>."
],
];
// Match by topic keywords
foreach ($topics as $topic) {
foreach ($topic['keywords'] as $keyword) {
if (str_contains($userMessage, strtolower($keyword))) {
return response()->json(['reply' => $topic['response'] . $companyInfo]);
}
}
}
// Fallback for unknown queries: just show company info
return response()->json([
'reply' => "I'm here to help with questions about Rohini International Education Services and studying in New Zealand." . $companyInfo
]);
}
}