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/gemini-actions.php +176 -176 8.7.9 → 8.8.3 View file →
@@ -1,176 +1,176 @@
1 -<?php
2 -/**
3 - * Gemini 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 Gemini_Actions
16 - */
17 -class Gemini_Actions extends Action {
18 -
19 - /**
20 - * Constructor
21 - */
22 - public function __construct() {
23 - $this->id = 'gemini';
24 - $this->group = __( 'Gemini', '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 - if ( empty( $action_id ) ) {
33 - return false;
34 - }
35 -
36 - switch ( $action_id ) {
37 - case 'chat_completion':
38 - return $this->chat_completion( $action_data, $trigger_data );
39 - case 'test_connection':
40 - return $this->test_connection( $action_data, $trigger_data );
41 - default:
42 - return false;
43 - }
44 - }
45 -
46 - /**
47 - * Test Connection
48 - */
49 - private function test_connection( $config, $trigger_data ) {
50 - $config_values = isset( $config['config'] ) ? $config['config'] : array();
51 - $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
52 -
53 - if ( empty( $api_key ) ) {
54 - return array(
55 - 'success' => false,
56 - 'message' => 'API Key is required.',
57 - );
58 - }
59 -
60 - $url = 'https://generativelanguage.googleapis.com/v1/models?key=' . $api_key;
61 -
62 - $args = array(
63 - 'method' => 'GET',
64 - 'timeout' => 30,
65 - );
66 -
67 - $response = wp_remote_request( $url, $args );
68 -
69 - if ( is_wp_error( $response ) ) {
70 - return array(
71 - 'success' => false,
72 - 'message' => $response->get_error_message(),
73 - );
74 - }
75 -
76 - $response_code = wp_remote_retrieve_response_code( $response );
77 -
78 - if ( $response_code >= 200 && $response_code < 300 ) {
79 - return array(
80 - 'success' => true,
81 - 'message' => 'Connection successful! Your API key is valid.',
82 - );
83 - } else {
84 - $response_body = wp_remote_retrieve_body( $response );
85 - $data = json_decode( $response_body, true );
86 - $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
87 - return array(
88 - 'success' => false,
89 - 'message' => $error_message,
90 - 'code' => $response_code,
91 - );
92 - }
93 - }
94 -
95 - /**
96 - * Chat Completion
97 - */
98 - private function chat_completion( $config, $trigger_data ) {
99 - $config_values = isset( $config['config'] ) ? $config['config'] : array();
100 - $full_trigger_id = $this->id;
101 - $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
102 - $model = isset( $config_values['model'] ) ? $config_values['model'] : 'gemini-1.5-flash';
103 - $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
104 - $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
105 - $prompt = str_replace( '$response', wp_json_encode( $trigger_data ?? '' ), $prompt ); // Convert literal \n to actual newlines
106 -
107 - if ( empty( $api_key ) ) {
108 - return array(
109 - 'success' => false,
110 - 'message' => 'API Key is required.',
111 - );
112 - }
113 -
114 - if ( empty( $prompt ) ) {
115 - return array(
116 - 'success' => false,
117 - 'message' => 'Prompt is required.',
118 - );
119 - }
120 -
121 - $url = 'https://generativelanguage.googleapis.com/v1/models/' . $model . ':generateContent?key=' . $api_key;
122 -
123 - $body = array(
124 - 'contents' => array(
125 - array(
126 - 'parts' => array(
127 - array(
128 - 'text' => $prompt,
129 - ),
130 - ),
131 - ),
132 - ),
133 - 'generationConfig' => array(
134 - 'temperature' => $temperature,
135 - ),
136 - );
137 -
138 - $args = array(
139 - 'method' => 'POST',
140 - 'headers' => array(
141 - 'Content-Type' => 'application/json',
142 - ),
143 - 'body' => wp_json_encode( $body ),
144 - 'timeout' => 60,
145 - );
146 -
147 - $response = wp_remote_request( $url, $args );
148 - if ( is_wp_error( $response ) ) {
149 - return array(
150 - 'success' => false,
151 - 'message' => $response->get_error_message(),
152 - );
153 - }
154 -
155 - $response_code = wp_remote_retrieve_response_code( $response );
156 - $response_body = wp_remote_retrieve_body( $response );
157 - $data = json_decode( $response_body, true );
158 - \WPbot_Automator\Engine\Workflow_Runner::run_workflows_for_trigger( $full_trigger_id, $data );
159 - if ( $response_code >= 200 && $response_code < 300 ) {
160 - $content = isset( $data['candidates'][0]['content']['parts'][0]['text'] ) ? $data['candidates'][0]['content']['parts'][0]['text'] : '';
161 - return array(
162 - 'success' => true,
163 - 'content' => $content,
164 - 'response' => $content,
165 - 'data' => $data,
166 - );
167 - } else {
168 - $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
169 - return array(
170 - 'success' => false,
171 - 'message' => $error_message,
172 - 'code' => $response_code,
173 - );
174 - }
175 - }
176 -}
1 +<?php
2 +/**
3 + * Gemini 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 Gemini_Actions
16 + */
17 +class Gemini_Actions extends Action {
18 +
19 + /**
20 + * Constructor
21 + */
22 + public function __construct() {
23 + $this->id = 'gemini';
24 + $this->group = __( 'Gemini', '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 + if ( empty( $action_id ) ) {
33 + return false;
34 + }
35 +
36 + switch ( $action_id ) {
37 + case 'chat_completion':
38 + return $this->chat_completion( $action_data, $trigger_data );
39 + case 'test_connection':
40 + return $this->test_connection( $action_data, $trigger_data );
41 + default:
42 + return false;
43 + }
44 + }
45 +
46 + /**
47 + * Test Connection
48 + */
49 + private function test_connection( $config, $trigger_data ) {
50 + $config_values = isset( $config['config'] ) ? $config['config'] : array();
51 + $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
52 +
53 + if ( empty( $api_key ) ) {
54 + return array(
55 + 'success' => false,
56 + 'message' => 'API Key is required.',
57 + );
58 + }
59 +
60 + $url = 'https://generativelanguage.googleapis.com/v1/models?key=' . $api_key;
61 +
62 + $args = array(
63 + 'method' => 'GET',
64 + 'timeout' => 30,
65 + );
66 +
67 + $response = wp_remote_request( $url, $args );
68 +
69 + if ( is_wp_error( $response ) ) {
70 + return array(
71 + 'success' => false,
72 + 'message' => $response->get_error_message(),
73 + );
74 + }
75 +
76 + $response_code = wp_remote_retrieve_response_code( $response );
77 +
78 + if ( $response_code >= 200 && $response_code < 300 ) {
79 + return array(
80 + 'success' => true,
81 + 'message' => 'Connection successful! Your API key is valid.',
82 + );
83 + } else {
84 + $response_body = wp_remote_retrieve_body( $response );
85 + $data = json_decode( $response_body, true );
86 + $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
87 + return array(
88 + 'success' => false,
89 + 'message' => $error_message,
90 + 'code' => $response_code,
91 + );
92 + }
93 + }
94 +
95 + /**
96 + * Chat Completion
97 + */
98 + private function chat_completion( $config, $trigger_data ) {
99 + $config_values = isset( $config['config'] ) ? $config['config'] : array();
100 + $full_trigger_id = $this->id;
101 + $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
102 + $model = isset( $config_values['model'] ) ? $config_values['model'] : 'gemini-1.5-flash';
103 + $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
104 + $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
105 + $prompt = str_replace( '$response', wp_json_encode( $trigger_data ?? '' ), $prompt ); // Convert literal \n to actual newlines
106 +
107 + if ( empty( $api_key ) ) {
108 + return array(
109 + 'success' => false,
110 + 'message' => 'API Key is required.',
111 + );
112 + }
113 +
114 + if ( empty( $prompt ) ) {
115 + return array(
116 + 'success' => false,
117 + 'message' => 'Prompt is required.',
118 + );
119 + }
120 +
121 + $url = 'https://generativelanguage.googleapis.com/v1/models/' . $model . ':generateContent?key=' . $api_key;
122 +
123 + $body = array(
124 + 'contents' => array(
125 + array(
126 + 'parts' => array(
127 + array(
128 + 'text' => $prompt,
129 + ),
130 + ),
131 + ),
132 + ),
133 + 'generationConfig' => array(
134 + 'temperature' => $temperature,
135 + ),
136 + );
137 +
138 + $args = array(
139 + 'method' => 'POST',
140 + 'headers' => array(
141 + 'Content-Type' => 'application/json',
142 + ),
143 + 'body' => wp_json_encode( $body ),
144 + 'timeout' => 60,
145 + );
146 +
147 + $response = wp_remote_request( $url, $args );
148 + if ( is_wp_error( $response ) ) {
149 + return array(
150 + 'success' => false,
151 + 'message' => $response->get_error_message(),
152 + );
153 + }
154 +
155 + $response_code = wp_remote_retrieve_response_code( $response );
156 + $response_body = wp_remote_retrieve_body( $response );
157 + $data = json_decode( $response_body, true );
158 + \WPbot_Automator\Engine\Workflow_Runner::run_workflows_for_trigger( $full_trigger_id, $data );
159 + if ( $response_code >= 200 && $response_code < 300 ) {
160 + $content = isset( $data['candidates'][0]['content']['parts'][0]['text'] ) ? $data['candidates'][0]['content']['parts'][0]['text'] : '';
161 + return array(
162 + 'success' => true,
163 + 'content' => $content,
164 + 'response' => $content,
165 + 'data' => $data,
166 + );
167 + } else {
168 + $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
169 + return array(
170 + 'success' => false,
171 + 'message' => $error_message,
172 + 'code' => $response_code,
173 + );
174 + }
175 + }
176 +}