250 lines
11 KiB
PHP
250 lines
11 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'
|
||
]);
|
||
|
||
// 1) Pull the current contact details from your settings/config
|
||
$phone = setting('phone'); // e.g. "+977 1-5344710"
|
||
$email = setting('email'); // e.g. "info@rohini.edu.np"
|
||
$address = setting('address'); // e.g. "House #160, Adait Marg, Kathmandu, Nepal"
|
||
$whatsapp = setting('whatsapp');
|
||
$instagram = setting('instagram');
|
||
$location = setting('location');
|
||
$youtube = setting('youtube');
|
||
$tiktok = setting('tiktok');
|
||
$userMessage = strtolower(trim($request->message));
|
||
|
||
$contactInfo = "\n\nPlease contact us for more information. Visit <a href=\"https://rohini.edu.np/\" target=\"_blank\">https://rohini.edu.np/</a> or call us on " . $phone . ".";
|
||
|
||
$allowedPrompts = [
|
||
'hi' => 'Hello! How can I assist you with your study abroad plans today?',
|
||
|
||
'what is rohini international education services?' =>
|
||
'Rohini International Education Services is a Nepal-based consultancy specializing in guiding students who want to study in New Zealand. Visit https://rohini.edu.np/ for more details.',
|
||
|
||
'where is rohini international located?' =>
|
||
'Our Kathmandu office is at ' . $address . '.',
|
||
|
||
'how can i contact support?' =>
|
||
'You can call us at ' . $phone . ', email us at ' . $email .
|
||
', or message us on WhatsApp at ' . $whatsapp . '. We are available Sunday to Friday, 09:00 AM to 05:00 PM.',
|
||
|
||
'what services do you provide?' =>
|
||
'We provide counseling, university and course selection, visa application support, documentation help, pre-departure briefings, financial and scholarship guidance, and accommodation support for students going to New Zealand.',
|
||
|
||
'do you offer test preparation?' =>
|
||
'Yes, we offer IELTS and PTE test preparation classes with experienced instructors and flexible schedules.',
|
||
|
||
'why choose new zealand for higher education?' =>
|
||
'New Zealand offers globally recognized qualifications, high-quality education, a safe environment, and post-study work opportunities.',
|
||
|
||
'what is the process to apply to study in new zealand?' =>
|
||
'We guide students through selecting a course, checking eligibility, gathering documents, applying for a visa, and preparing for departure.',
|
||
|
||
'are there work opportunities in new zealand for students?' =>
|
||
'Yes, students can work part-time during study, full-time during holidays, and apply for post-study work visas after graduation.',
|
||
|
||
'what accommodation options are available in new zealand?' =>
|
||
'Students can choose from shared flats, homestays, hostels, or on-campus housing. We help you find the right option.',
|
||
|
||
'do you support nepalese students in new zealand?' =>
|
||
'Absolutely! We provide full pre-departure briefings and continuous support to ensure students feel at home in New Zealand.',
|
||
|
||
'rohini' =>
|
||
'Rohini International Education Services is a Nepal-based consultancy located in Kathmandu, specializing in guiding students who want to study in New Zealand. Visit https://rohini.edu.np/ for full information.',
|
||
|
||
'location' =>
|
||
'Our main office is at ' . $address . '. We also have branches in Birtamode, Birgunj, Damak, Chitwan, and Pokhara.',
|
||
|
||
'contact' =>
|
||
'You can call us at ' . $phone . ', email us at ' . $email .
|
||
', or message us on WhatsApp at ' . $whatsapp . '. We’re available Sunday to Friday, 09:00 AM to 05:00 PM.',
|
||
|
||
'services' =>
|
||
'We provide counseling, university and course selection, visa application support, documentation help, pre-departure briefings, financial and scholarship guidance, and accommodation support for students going to New Zealand.',
|
||
|
||
'social media' =>
|
||
'Follow us on Instagram: ' . $instagram . ', YouTube: ' . $youtube . ', TikTok: ' . $tiktok . '.',
|
||
];
|
||
if (array_key_exists($userMessage, $allowedPrompts)) {
|
||
return response()->json([
|
||
'reply' => $allowedPrompts[$userMessage] . $contactInfo
|
||
]);
|
||
}
|
||
|
||
if (str_contains($userMessage, 'location') || str_contains($userMessage, 'located')) {
|
||
return response()->json([
|
||
'reply' => $allowedPrompts['where is rohini international located?'] . $contactInfo
|
||
]);
|
||
}
|
||
|
||
if (str_contains($userMessage, 'contact') || str_contains($userMessage, 'support')) {
|
||
return response()->json([
|
||
'reply' => $allowedPrompts['how can i contact support?'] . $contactInfo
|
||
]);
|
||
}
|
||
|
||
if (str_contains($userMessage, 'service') || str_contains($userMessage, 'services')) {
|
||
return response()->json([
|
||
'reply' => $allowedPrompts['what services do you provide?'] . $contactInfo
|
||
]);
|
||
}
|
||
|
||
foreach ($allowedPrompts as $prompt => $answer) {
|
||
similar_text($userMessage, $prompt, $percent);
|
||
if ($percent > 80) {
|
||
return response()->json([
|
||
'reply' => "Did you mean: \"$prompt\"?\n" . $answer . $contactInfo
|
||
]);
|
||
}
|
||
}
|
||
|
||
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' => [
|
||
[
|
||
'role' => 'system',
|
||
'content' => 'You are an assistant for Rohini International Education Services in Nepal. Answer ONLY using information found on https://rohini.edu.np/. If the question is not related to this organization or its services for studying in New Zealand, reply with: "Sorry, I can only answer questions related to Rohini International Education Services and studying in New Zealand."'
|
||
],
|
||
['role' => 'user', 'content' => $request->message],
|
||
],
|
||
]);
|
||
|
||
$response->throw();
|
||
|
||
return response()->json([
|
||
'reply' => ($response->json('choices.0.message.content') ?? 'No response from assistant.') . $contactInfo
|
||
]);
|
||
} catch (ConnectionException $e) {
|
||
return response()->json([
|
||
'reply' => 'Sorry, I could not connect to the AI service. Please try again later.' . $contactInfo
|
||
], 504);
|
||
} catch (RequestException $e) {
|
||
$msg = $e->response->json('error.message') ?? $e->getMessage();
|
||
return response()->json([
|
||
'reply' => 'Sorry, an error occurred. Please try again later.' . $contactInfo,
|
||
'error' => $msg
|
||
], 500);
|
||
}
|
||
}
|
||
}
|