PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.3
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.3
8.8.3 8.8.2 8.8.1 8.8.0 8.7.9 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 All 539 releases
← All changes | addons/automator/includes/actions/openai-actions.php +197 -197 8.7.9 → 8.8.3 View file →
@@ -1,197 +1,197 @@
1 -<?php
2 -/**
3 - * OpenAI Actions
4 - *
5 - * @package WPbot_Automator
6 - */
7 -
8 -namespace WPbot_Automator\Actions;
9 -
10 -if ( ! defined( 'ABSPATH' ) ) {
11 - exit;
12 -}
13 -
14 -/**
15 - * Class OpenAI_Actions
16 - */
17 -class OpenAI_Actions extends Action {
18 -
19 - /**
20 - * Constructor
21 - */
22 - public function __construct() {
23 - $this->id = 'openai';
24 - $this->group = __( 'OpenAI', 'wpbot-automator' );
25 - }
26 -
27 - /**
28 - * Execute the action
29 - */
30 - public function execute( $action_data, $trigger_data ) {
31 - $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
32 -
33 - if ( empty( $action_id ) ) {
34 - return false;
35 - }
36 -
37 - switch ( $action_id ) {
38 - case 'chat_completion':
39 - return $this->chat_completion( $action_data, $trigger_data );
40 - case 'test_connection':
41 - return $this->test_connection( $action_data, $trigger_data );
42 - default:
43 - return false;
44 - }
45 - }
46 -
47 - /**
48 - * Test Connection
49 - */
50 - private function test_connection( $config, $trigger_data ) {
51 - $config_values = isset( $config['config'] ) ? $config['config'] : array();
52 - $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
53 -
54 - if ( empty( $api_key ) ) {
55 - return array(
56 - 'success' => false,
57 - 'message' => 'API Key is required.',
58 - );
59 - }
60 -
61 - $url = 'https://api.openai.com/v1/models';
62 -
63 - $args = array(
64 - 'method' => 'GET',
65 - 'headers' => array(
66 - 'Authorization' => 'Bearer ' . $api_key,
67 - ),
68 - 'timeout' => 30,
69 - );
70 -
71 - $response = wp_remote_request( $url, $args );
72 -
73 - if ( is_wp_error( $response ) ) {
74 - return array(
75 - 'success' => false,
76 - 'message' => $response->get_error_message(),
77 - );
78 - }
79 -
80 - $response_code = wp_remote_retrieve_response_code( $response );
81 -
82 - if ( $response_code >= 200 && $response_code < 300 ) {
83 - return array(
84 - 'success' => true,
85 - 'message' => 'Connection successful! Your API key is valid.',
86 - );
87 - } else {
88 - $response_body = wp_remote_retrieve_body( $response );
89 - $data = json_decode( $response_body, true );
90 - $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
91 - return array(
92 - 'success' => false,
93 - 'message' => $error_message,
94 - 'code' => $response_code,
95 - );
96 - }
97 - }
98 -
99 - /**
100 - * Chat Completion
101 - */
102 - private function chat_completion( $config, $trigger_data ) {
103 - $config_values = isset( $config['config'] ) ? $config['config'] : array();
104 -
105 - $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
106 - $model = isset( $config_values['model'] ) ? $config_values['model'] : 'gpt-3.5-turbo';
107 - $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
108 - $max_tokens = isset( $config_values['max_tokens'] ) ? intval( $config_values['max_tokens'] ) : 1000;
109 - $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
110 -
111 - if ( empty( $api_key ) ) {
112 - return array(
113 - 'success' => false,
114 - 'message' => 'API Key is required.',
115 - );
116 - }
117 -
118 - if ( empty( $prompt ) ) {
119 - return array(
120 - 'success' => false,
121 - 'message' => 'Prompt is required.',
122 - );
123 - }
124 -
125 - $url = 'https://api.openai.com/v1/chat/completions';
126 -
127 - $body = array(
128 - 'model' => $model,
129 - 'messages' => array(
130 - array(
131 - 'role' => 'user',
132 - 'content' => $prompt,
133 - ),
134 - ),
135 - 'max_tokens' => $max_tokens,
136 - 'temperature' => $temperature,
137 - );
138 -
139 - $args = array(
140 - 'method' => 'POST',
141 - 'headers' => array(
142 - 'Content-Type' => 'application/json',
143 - 'Authorization' => 'Bearer ' . $api_key,
144 - ),
145 - 'body' => wp_json_encode( $body ),
146 - 'timeout' => 60,
147 - );
148 -
149 - $response = wp_remote_request( $url, $args );
150 -
151 - if ( is_wp_error( $response ) ) {
152 - return array(
153 - 'success' => false,
154 - 'message' => $response->get_error_message(),
155 - );
156 - }
157 -
158 - $response_code = wp_remote_retrieve_response_code( $response );
159 - $response_body = wp_remote_retrieve_body( $response );
160 - $data = json_decode( $response_body, true );
161 -
162 - if ( $response_code >= 200 && $response_code < 300 ) {
163 - $content = isset( $data['choices'][0]['message']['content'] ) ? $data['choices'][0]['message']['content'] : '';
164 -
165 - // Build base result.
166 - $result = array(
167 - 'success' => true,
168 - 'content' => $content,
169 - 'response' => $content,
170 - 'data' => $data,
171 - );
172 -
173 - // If the AI returned a JSON object, parse it and merge every key
174 - // directly into the result so downstream nodes can reference
175 - // {node-action-N.key_name} via the node_id prefix added by the runner.
176 - $trimmed = trim( $content );
177 - if ( strlen( $trimmed ) > 0 && $trimmed[0] === '{' ) {
178 - $parsed = json_decode( $trimmed, true );
179 - if ( JSON_ERROR_NONE === json_last_error() && is_array( $parsed ) ) {
180 - foreach ( $parsed as $key => $value ) {
181 - // Store scalar values directly; arrays as JSON strings.
182 - $result[ $key ] = is_array( $value ) ? wp_json_encode( $value ) : $value;
183 - }
184 - }
185 - }
186 -
187 - return $result;
188 - } else {
189 - $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
190 - return array(
191 - 'success' => false,
192 - 'message' => $error_message,
193 - 'code' => $response_code,
194 - );
195 - }
196 - }
197 -}
1 +<?php
2 +/**
3 + * OpenAI Actions
4 + *
5 + * @package WPbot_Automator
6 + */
7 +
8 +namespace WPbot_Automator\Actions;
9 +
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit;
12 +}
13 +
14 +/**
15 + * Class OpenAI_Actions
16 + */
17 +class OpenAI_Actions extends Action {
18 +
19 + /**
20 + * Constructor
21 + */
22 + public function __construct() {
23 + $this->id = 'openai';
24 + $this->group = __( 'OpenAI', 'wpbot-automator' );
25 + }
26 +
27 + /**
28 + * Execute the action
29 + */
30 + public function execute( $action_data, $trigger_data ) {
31 + $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
32 +
33 + if ( empty( $action_id ) ) {
34 + return false;
35 + }
36 +
37 + switch ( $action_id ) {
38 + case 'chat_completion':
39 + return $this->chat_completion( $action_data, $trigger_data );
40 + case 'test_connection':
41 + return $this->test_connection( $action_data, $trigger_data );
42 + default:
43 + return false;
44 + }
45 + }
46 +
47 + /**
48 + * Test Connection
49 + */
50 + private function test_connection( $config, $trigger_data ) {
51 + $config_values = isset( $config['config'] ) ? $config['config'] : array();
52 + $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
53 +
54 + if ( empty( $api_key ) ) {
55 + return array(
56 + 'success' => false,
57 + 'message' => 'API Key is required.',
58 + );
59 + }
60 +
61 + $url = 'https://api.openai.com/v1/models';
62 +
63 + $args = array(
64 + 'method' => 'GET',
65 + 'headers' => array(
66 + 'Authorization' => 'Bearer ' . $api_key,
67 + ),
68 + 'timeout' => 30,
69 + );
70 +
71 + $response = wp_remote_request( $url, $args );
72 +
73 + if ( is_wp_error( $response ) ) {
74 + return array(
75 + 'success' => false,
76 + 'message' => $response->get_error_message(),
77 + );
78 + }
79 +
80 + $response_code = wp_remote_retrieve_response_code( $response );
81 +
82 + if ( $response_code >= 200 && $response_code < 300 ) {
83 + return array(
84 + 'success' => true,
85 + 'message' => 'Connection successful! Your API key is valid.',
86 + );
87 + } else {
88 + $response_body = wp_remote_retrieve_body( $response );
89 + $data = json_decode( $response_body, true );
90 + $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
91 + return array(
92 + 'success' => false,
93 + 'message' => $error_message,
94 + 'code' => $response_code,
95 + );
96 + }
97 + }
98 +
99 + /**
100 + * Chat Completion
101 + */
102 + private function chat_completion( $config, $trigger_data ) {
103 + $config_values = isset( $config['config'] ) ? $config['config'] : array();
104 +
105 + $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
106 + $model = isset( $config_values['model'] ) ? $config_values['model'] : 'gpt-3.5-turbo';
107 + $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
108 + $max_tokens = isset( $config_values['max_tokens'] ) ? intval( $config_values['max_tokens'] ) : 1000;
109 + $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
110 +
111 + if ( empty( $api_key ) ) {
112 + return array(
113 + 'success' => false,
114 + 'message' => 'API Key is required.',
115 + );
116 + }
117 +
118 + if ( empty( $prompt ) ) {
119 + return array(
120 + 'success' => false,
121 + 'message' => 'Prompt is required.',
122 + );
123 + }
124 +
125 + $url = 'https://api.openai.com/v1/chat/completions';
126 +
127 + $body = array(
128 + 'model' => $model,
129 + 'messages' => array(
130 + array(
131 + 'role' => 'user',
132 + 'content' => $prompt,
133 + ),
134 + ),
135 + 'max_tokens' => $max_tokens,
136 + 'temperature' => $temperature,
137 + );
138 +
139 + $args = array(
140 + 'method' => 'POST',
141 + 'headers' => array(
142 + 'Content-Type' => 'application/json',
143 + 'Authorization' => 'Bearer ' . $api_key,
144 + ),
145 + 'body' => wp_json_encode( $body ),
146 + 'timeout' => 60,
147 + );
148 +
149 + $response = wp_remote_request( $url, $args );
150 +
151 + if ( is_wp_error( $response ) ) {
152 + return array(
153 + 'success' => false,
154 + 'message' => $response->get_error_message(),
155 + );
156 + }
157 +
158 + $response_code = wp_remote_retrieve_response_code( $response );
159 + $response_body = wp_remote_retrieve_body( $response );
160 + $data = json_decode( $response_body, true );
161 +
162 + if ( $response_code >= 200 && $response_code < 300 ) {
163 + $content = isset( $data['choices'][0]['message']['content'] ) ? $data['choices'][0]['message']['content'] : '';
164 +
165 + // Build base result.
166 + $result = array(
167 + 'success' => true,
168 + 'content' => $content,
169 + 'response' => $content,
170 + 'data' => $data,
171 + );
172 +
173 + // If the AI returned a JSON object, parse it and merge every key
174 + // directly into the result so downstream nodes can reference
175 + // {node-action-N.key_name} via the node_id prefix added by the runner.
176 + $trimmed = trim( $content );
177 + if ( strlen( $trimmed ) > 0 && $trimmed[0] === '{' ) {
178 + $parsed = json_decode( $trimmed, true );
179 + if ( JSON_ERROR_NONE === json_last_error() && is_array( $parsed ) ) {
180 + foreach ( $parsed as $key => $value ) {
181 + // Store scalar values directly; arrays as JSON strings.
182 + $result[ $key ] = is_array( $value ) ? wp_json_encode( $value ) : $value;
183 + }
184 + }
185 + }
186 +
187 + return $result;
188 + } else {
189 + $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
190 + return array(
191 + 'success' => false,
192 + 'message' => $error_message,
193 + 'code' => $response_code,
194 + );
195 + }
196 + }
197 +}