PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.0
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 8.5.7 8.5.6 8.5.5 All 536 releases
chatbot / addons / automator / includes / actions / claude-actions.php

claude-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.0, at addons/automator/includes/actions/claude-actions.php

192 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Claude 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 Claude_Actions
16 */
17 class Claude_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'claude';
24 $this->group = __( 'Claude', '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.anthropic.com/v1/messages'; // We can't do a simple GET, so we'll do a minimal POST
62
63 $body = array(
64 'model' => 'claude-3-haiku-20240307',
65 'max_tokens' => 1,
66 'messages' => array(
67 array( 'role' => 'user', 'content' => 'Hi' )
68 )
69 );
70
71 $args = array(
72 'method' => 'POST',
73 'headers' => array(
74 'Content-Type' => 'application/json',
75 'x-api-key' => $api_key,
76 'anthropic-version' => '2023-06-01',
77 ),
78 'body' => wp_json_encode( $body ),
79 'timeout' => 30,
80 );
81
82 $response = wp_remote_request( $url, $args );
83
84 if ( is_wp_error( $response ) ) {
85 return array(
86 'success' => false,
87 'message' => $response->get_error_message(),
88 );
89 }
90
91 $response_code = wp_remote_retrieve_response_code( $response );
92
93 if ( $response_code >= 200 && $response_code < 300 ) {
94 return array(
95 'success' => true,
96 'message' => 'Connection successful! Your API key is valid.',
97 );
98 } else {
99 $response_body = wp_remote_retrieve_body( $response );
100 $data = json_decode( $response_body, true );
101 $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
102 return array(
103 'success' => false,
104 'message' => $error_message,
105 'code' => $response_code,
106 );
107 }
108 }
109
110 /**
111 * Chat Completion
112 */
113 private function chat_completion( $config, $trigger_data ) {
114 $config_values = isset( $config['config'] ) ? $config['config'] : array();
115
116 $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
117 $model = isset( $config_values['model'] ) ? $config_values['model'] : 'claude-3-5-sonnet-20240620';
118 $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
119 $max_tokens = isset( $config_values['max_tokens'] ) ? intval( $config_values['max_tokens'] ) : 1000;
120 $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
121
122 if ( empty( $api_key ) ) {
123 return array(
124 'success' => false,
125 'message' => 'API Key is required.',
126 );
127 }
128
129 if ( empty( $prompt ) ) {
130 return array(
131 'success' => false,
132 'message' => 'Prompt is required.',
133 );
134 }
135
136 $url = 'https://api.anthropic.com/v1/messages';
137
138 $body = array(
139 'model' => $model,
140 'messages' => array(
141 array(
142 'role' => 'user',
143 'content' => $prompt,
144 ),
145 ),
146 'max_tokens' => $max_tokens,
147 'temperature' => $temperature,
148 );
149
150 $args = array(
151 'method' => 'POST',
152 'headers' => array(
153 'Content-Type' => 'application/json',
154 'x-api-key' => $api_key,
155 'anthropic-version' => '2023-06-01',
156 ),
157 'body' => wp_json_encode( $body ),
158 'timeout' => 60,
159 );
160
161 $response = wp_remote_request( $url, $args );
162
163 if ( is_wp_error( $response ) ) {
164 return array(
165 'success' => false,
166 'message' => $response->get_error_message(),
167 );
168 }
169
170 $response_code = wp_remote_retrieve_response_code( $response );
171 $response_body = wp_remote_retrieve_body( $response );
172 $data = json_decode( $response_body, true );
173
174 if ( $response_code >= 200 && $response_code < 300 ) {
175 $content = isset( $data['content'][0]['text'] ) ? $data['content'][0]['text'] : '';
176 return array(
177 'success' => true,
178 'content' => $content,
179 'response' => $content,
180 'data' => $data,
181 );
182 } else {
183 $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
184 return array(
185 'success' => false,
186 'message' => $error_message,
187 'code' => $response_code,
188 );
189 }
190 }
191 }
192