PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 6.9.3
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v6.9.3
8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / includes / integration / openrouter / qcld-bot-openrouter.php

qcld-bot-openrouter.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 6.9.3, at includes/integration/openrouter/qcld-bot-openrouter.php

235 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit; // Exit if accessed directly
3
4 if(!class_exists('qcld_wpopenrouter_addons')){
5
6
7 /**
8 * Main Class.
9 */
10 final class qcld_wpopenrouter_addons
11 {
12 private $id = 'Open AI';
13
14 /**
15 * WPBot Pro version.
16 *
17 * @var string
18 */
19 public $version = '1.0.6';
20
21 /**
22 * WPBot Pro helper.
23 *
24 * @var object
25 */
26 public $helper;
27
28 /**
29 * The single instance of the class.
30 *
31 * @var qcld_wb_Chatbot
32 * @since 1.0.0
33 */
34 protected static $_instance = null;
35
36 /**
37 * Main wpbot Instance.
38 *
39 * Ensures only one instance of wpbot is loaded or can be loaded.
40 *
41 * @return qcld_wb_Chatbot - Main instance.
42 * @since 1.0.0
43 * @static
44 */
45 public static function instance() {
46 if ( is_null( self::$_instance ) ) {
47 self::$_instance = new self();
48 }
49
50 return self::$_instance;
51 }
52
53 public $response_list;
54
55 /**
56 * Constructor
57 */
58 public function __construct()
59 {
60
61 $this->includes();
62 add_action('wp_ajax_openai_save_assistant', [$this, 'save_assistant_callback']);
63 add_action('wp_ajax_openrouter_response',[$this,'openrouter_response_callback']);
64 add_action('wp_ajax_nopriv_openrouter_response', [$this, 'openrouter_response_callback']);
65 add_action('wp_ajax_qcld_openrouter_settings_option',[$this,'qcld_openrouter_settings_option_callback']);
66
67 add_action('wp_ajax_update_settings_option', [$this, 'update_settings_option_callback']);
68
69 if (is_admin() && !empty($_GET["page"]) && (($_GET["page"] == "openai-panel_dashboard") || ($_GET["page"] == "openai-panel_file") || ($_GET["page"] == "openai-panel_help"))) {
70 add_action('admin_enqueue_scripts', array($this, 'qcld_wb_chatbot_admin_scripts'));
71 }
72 //add_action('wp_enqueue_scripts', array($this, 'qcld_wb_chatbot_openrouter_scripts'));
73 add_action('admin_enqueue_scripts', array($this, 'qcld_wb_chatbot_openrouter_admin_scripts'));
74 }
75
76
77
78 public function qcld_wb_chatbot_openrouter_admin_scripts() {
79 if ( ! current_user_can( 'manage_options' ) ) {
80 return ;
81 }
82 wp_register_script(
83 'qcld-wp-chatbot-openrouter-admin-js',
84 QCLD_wpCHATBOT_PLUGIN_URL . 'includes/integration/openrouter/assets/js/qcld-wp-openrouter-admin.js',
85 array('jquery'),
86 QCLD_wpCHATBOT_VERSION,
87 true
88 );
89
90 // Localize the script with necessary data
91 wp_localize_script('qcld-wp-chatbot-openrouter-admin-js', 'ajax_object', array(
92 'ajax_url' => admin_url('admin-ajax.php'),
93 'ajax_nonce' => wp_create_nonce('wp_chatbot'),
94 'openrouter_api_key' => get_option('qcld_openrouter_api_key'),
95 'openrouter_model' => get_option('qcld_openrouter_model'),
96 'openrouter_enabled' => get_option('qcld_openrouter_enabled'),
97 'qcld_openrouter_append_content' => get_option('qcld_openrouter_append_content'),
98 'qcld_openrouter_prepend_content' => get_option('qcld_openrouter_prepend_content')
99 ));
100
101 wp_enqueue_script('qcld-wp-chatbot-openrouter-admin-js');
102 }
103
104 /**
105 * Define wpbot Constants.
106 *
107 * @return void
108 * @since 1.0.0
109 */
110 public function includes() {
111 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/Parsedown.php" );
112 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/class-common-function.php" );
113 }
114 public function qcld_openrouter_settings_option_callback() {
115 $nonce = sanitize_text_field($_POST['nonce']);
116 if (!wp_verify_nonce($nonce, 'wp_chatbot')) {
117 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
118 wp_die();
119 } else {
120 $openrouter_api_key = sanitize_text_field($_POST['openrouter_api_key']);
121 $openrouter_model = sanitize_text_field($_POST['openrouter_model']);
122 $openrouter_enabled = sanitize_text_field($_POST['openrouter_enabled']);
123 $qcld_openrouter_page_suggestion_enabled = sanitize_text_field($_POST['qcld_openrouter_page_suggestion_enabled']);
124 $qcld_openrouter_append_content = sanitize_text_field($_POST['qcld_openrouter_append_content']) ?? '';
125 $qcld_openrouter_prepend_content = sanitize_text_field($_POST['qcld_openrouter_prepend_content']) ?? '';
126 if($openrouter_api_key != '') {
127 update_option('qcld_openrouter_api_key', $openrouter_api_key);
128 }
129 if($openrouter_model != '') {
130 update_option('qcld_openrouter_model', $openrouter_model);
131 }
132 if($openrouter_enabled != '') {
133 update_option('qcld_openrouter_enabled', $openrouter_enabled);
134 }
135 if($openrouter_enabled == '1') {
136 update_option('ai_enabled', 0);
137
138 } else {
139 update_option('ai_enabled', 1);
140 }
141 update_option('qcld_openrouter_page_suggestion_enabled', $qcld_openrouter_page_suggestion_enabled);
142 update_option( 'qcld_openai_relevant_post', $_POST['openai_post_type'] );
143
144 update_option('qcld_openrouter_append_content', $qcld_openrouter_append_content);
145 update_option('qcld_openrouter_prepend_content', $qcld_openrouter_prepend_content);
146
147 }
148 echo json_encode($openrouter_enabled);
149 wp_die();
150 }
151 public function openrouter_response_callback(){
152 $openrouter_model = get_option('qcld_openrouter_model');
153 $openrouter_api_key = get_option('qcld_openrouter_api_key');
154 $keyword = $_POST['keyword'];
155 $relevant_pagelink = Qcld_WPBot_Common_Functions::qcpd_relevant_pagelink($keyword);
156 $relevant_pagelink = array_slice($relevant_pagelink, 0, 5, true);
157
158 if( (get_option('page_suggestion_enabled') == '1') && count($relevant_pagelink) > 0 ){
159
160 $relevant_post_link = maybe_unserialize(get_option('qlcd_wp_chatbot_relevant_post_link_openai'));
161
162 if(is_array($relevant_post_link[get_wpbot_locale()] )){
163
164 $relevant_pagelinks = '<br><br><p><em>'. implode('', $relevant_post_link[get_wpbot_locale()]) .'</em><p>'. implode("</br>", $relevant_pagelink);
165 }else{
166 $relevant_pagelinks = '<br><br><p><em>'. $relevant_post_link[get_wpbot_locale()] .'</em><p>'. implode("</br>", $relevant_pagelink);
167 }
168
169
170 }else{
171 $relevant_pagelinks = '';
172 }
173 $Parsedown = new Parsedown();
174 $data = json_encode(array(
175 'model' => $openrouter_model,
176 'messages' => array(
177 array(
178 'role' => 'user',
179 'content' => $keyword
180 )
181 )
182 ));
183
184 $ch = curl_init();
185 $api_url = 'https://openrouter.ai/api/v1/chat/completions';
186
187 curl_setopt($ch, CURLOPT_URL, $api_url);
188 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
189 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
190 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
191 'Content-Type: application/json',
192 'Authorization: Bearer ' . $openrouter_api_key
193 ));
194 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
195 $result = curl_exec($ch);
196
197 if(curl_errno($ch)) {
198 $response['status'] = 'error';
199 $response['message'] = 'Curl error: ' . curl_error($ch);
200 } else {
201 $msg = json_decode($result);
202 if(isset($msg->choices[0]->message->content)) {
203 $response['status'] = 'success';
204 $response['message'] = $Parsedown->text($msg->choices[0]->message->content) . $relevant_pagelinks;
205 } else {
206 $response['status'] = 'error';
207 $response['message'] = 'Sorry, I encountered an error processing your AI request. Please check api key and try again later.';
208 }
209 }
210
211 curl_close($ch);
212 echo json_encode($response);
213 wp_die();
214 }
215 public function update_settings_option_callback(){
216 update_option('disable_wp_chatbot_site_search',1);
217 update_option('enable_wp_chatbot_post_content', '');
218 }
219 }
220
221 /**
222 * @return qcld_wpopenai_addon
223 */
224 if(!function_exists('qcld_wpopenrouter_addons')){
225 function qcld_wpopenrouter_addons() {
226 $qcld_wpopenrouter_addon = new qcld_wpopenrouter_addons();
227 return $qcld_wpopenrouter_addon->instance();
228
229 }
230 }
231
232 //fire off the plugin
233 qcld_wpopenrouter_addons();
234
235 }