Creating an AI plugin for integration into OSClass

aikis

Active member
XNullUser
Joined
Oct 9, 2021
Messages
104
Reaction score
89
Points
28
Website
autopmr.com
NullCash
260
Steps to create a plugin:
Plugin structure
Plugins in Osclass are usually located in the oc-content/plugins/ folder.
Create a new folder, for example, ai_chatbot.
Main files
Inside the ai_chatbot folder, create the following files:
index.php — the main file of the plugin.
functions.php — functions for working with the API.
form.php — form for users.
style.css — styles for the form (optional).

1. File index.php
This file registers the plugin in Osclass and includes the necessary functions.

<?php
/*
Plugin Name: AI Chatbot
Description: Интеграция ИИ-чата для ответов на вопросы пользователей.
Version: 1.0
Author: Your Name
*/

// Подключаем функции плагина
require_once 'functions.php';

// Регистрация хуков
osc_add_hook('header', 'ai_chatbot_load_assets'); // Подключение CSS/JS
osc_add_hook('footer', 'ai_chatbot_display_form'); // Отображение формы

2. functions.php file
Here we implement the logic for sending requests to my API and handling the responses.

<?php

// Подключение стилей и скриптов
function ai_chatbot_load_assets() {
echo '<link rel="stylesheet" href="' . osc_plugin_url('ai_chatbot') . 'style.css">';
}

// Отображение формы для вопросов
function ai_chatbot_display_form() {
include 'form.php';
}

// Обработка запроса к API
function ai_chatbot_get_response($question) {
$api_key = 'YOUR_API_KEY'; // Замените на ваш API-ключ
$url = 'https://api.example.com/v1/chat'; // Замените на реальный URL API

$data = [
'prompt' => $question,
'max_tokens' => 50
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
return $result['answer'] ?? 'Извините, не удалось получить ответ.';
}

3. File form.php
This is an HTML form through which users will ask questions.

<div id="ai-chatbot">
<h3>Задайте вопрос ИИ</h3>
<form method="POST" action="">
<textarea name="question" placeholder="Введите ваш вопрос..." required></textarea>
<button type="submit">Отправить</button>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$question = trim($_POST['question']);
if (!empty($question)) {
$response = ai_chatbot_get_response($question);
echo '<div class="ai-response"><strong>Ответ:</strong> ' . htmlspecialchars($response) . '</div>';
} else {
echo '<div class="ai-error">Пожалуйста, введите вопрос.</div>';
}
}
?>
</div>

4. style.css file
Let's add some styles to the form.

#ai-chatbot {
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}

#ai-chatbot h3 {
text-align: center;
margin-bottom: 15px;
}

#ai-chatbot textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}

#ai-chatbot button {
display: block;
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}

#ai-chatbot button:hover {
background-color: #0056b3;
}

.ai-response {
margin-top: 20px;
padding: 10px;
border: 1px solid #d4edda;
background-color: #f8fff8;
border-radius: 4px;
}

.ai-error {
margin-top: 20px;
padding: 10px;
border: 1px solid #f5c6cb;
background-color: #fff5f5;
border-radius: 4px;
color: #721c24;
}
 

ates

Well-known member
Diamond
Elite
XNullUser
Joined
Nov 4, 2021
Messages
168
Reaction score
311
Points
63
NullCash
269
Can't you make a zip file for this share

Also is there a demo
 

aikis

Active member
XNullUser
Joined
Oct 9, 2021
Messages
104
Reaction score
89
Points
28
Website
autopmr.com
NullCash
260
Can't you make a zip file for this share

Also is there a demo
Anyone can put everything in a folder, and then open everything in a zip file to immediately look at it and see that the code is clean.
 
Top