prefix . 'rl_mailwarmer_conversations'; // $current_time = current_time('mysql'); $current_time = date('Y-m-d H:i:s', strtotime('-24 hours')); $future_time = date('Y-m-d H:i:s', strtotime('+24 hours')); // Fetch up to 50 conversations starting within the next 24 hours $conversations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $conversation_table WHERE status = %s AND first_message_timestamp BETWEEN %s AND %s LIMIT 1", 'new', $current_time, $future_time ), ARRAY_A ); // log_to_file("process_upcoming_conversations - Conversations: ", $conversations); foreach ($conversations as $conversation) { // log_to_file("process_upcoming_conversations - Conversation: ", $conversation); try { // Generate conversation content using AI // $ai_response = self::fetch_conversation_from_ai($conversation); $prompt = $conversation['prompt']; // log_to_file("process_upcoming_conversations - AI prompt: ", $prompt); // $ai_response = RL_MailWarmer_Campaign_Helper::clean_ai_response(RL_MailWarmer_Campaign_Helper::generate_ai_conversation($prompt)); // $ai_response = RL_MailWarmer_Campaign_Helper::generate_ai_conversation($prompt); $ai_response = get_post_meta($conversation['campaign_id'], 'last_ai_response', true); // log_to_file("process_upcoming_conversations - AI Response: ", $ai_response); if (is_wp_error($ai_response)) { return $ai_response; // Handle AI generation failure gracefully } $updated_conversation_steps = self::merge_timeline_with_ai_response($conversation['conversation_steps'], $ai_response); // log_to_file("process_upcoming_conversations - Merged steps: ", $updated_conversation_steps); // // Update conversation status to "generated" $conversation_table = $wpdb->prefix . 'rl_mailwarmer_conversation'; $status = 'new'; $result = $wpdb->update( $conversation_table, // [], ['status' => $status, 'conversation_steps' => $updated_conversation_steps], ['id' => $conversation['id']], ['%s'], ['%s'], ['%d'] ); // log_to_file("process_upcoming_conversations - Update DB Result: ", $result); $update_messages_db = self::update_messages_with_merged_output($conversation['id'], $updated_conversation_steps); log_to_file("process_upcoming_conversations - Result of update_messages_with_merged_output(): ", $update_messages_db); // Update the conversation with generated steps // log_to_file("process_upcoming_conversations - Updating conversation_steps"); // if (RL_MailWarmer_Campaign_Helper::update_conversation_steps($conversation['id'], $updated_conversation_steps)) { // // log_to_file("process_upcoming_conversations - Updated conversation_steps!"); // } // Save the generated content to the conversation // RL_MailWarmer_DB_Helper::update_conversation_steps($conversation['id'], $ai_response); } catch (Exception $e) { error_log("Failed to generate conversation ID {$conversation['id']}: " . $e->getMessage()); } } } private static function update_messages_with_merged_output($conversation_id, $merged_output) { log_to_file("update_messages_with_merged_output - merged_output: ", $merged_output); global $wpdb; $message_table = $wpdb->prefix . 'rl_mailwarmer_messages'; // $merged_output = json_decode($merged_output); // Fetch all messages for the given conversation ID $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $message_table WHERE conversation_id = %d ORDER BY `scheduled_for_timestamp` ASC", $conversation_id ), ARRAY_A ); log_to_file("update_messages_with_merged_output - Found Messages: ", $messages); if (empty($messages)) { throw new Exception("No messages found for conversation ID $conversation_id."); } // Iterate through messages and merged output foreach ($messages as $index => $message) { if (!isset($merged_output[$index])) { continue; // Skip if there's no corresponding merged output } $merged = $merged_output[$index]; // log_to_file("update_messages_with_merged_output - merged: $merged"); // Prepare updated data // $updated_data = [ // 'status' => $merged['status'], // 'scheduled_for_timestamp' => $merged['scheduled_for'], // 'from_email' => $merged['from'], // 'to_email' => json_encode($merged['to']), // 'cc_email' => json_encode($merged['cc']), // 'subject' => $merged['subject'], // 'body' => $merged['body'], // ]; // log_to_file("update_messages_with_merged_output - Updated Data: ", $updated_data); // Update the database // $wpdb->update( // $message_table, // $updated_data, // ['id' => $message['id']], // Where clause // [ // '%s', // status // '%s', // scheduled_for_timestamp // '%s', // from_email // '%s', // to_email // '%s', // cc_email // '%s', // subject // '%s', // body // ], // ['%d'] // ID // ); } return true; } /** * Fetch a conversation from ChatGPT based on conversation parameters. * * @param array $conversation The conversation data. * @return string The AI-generated conversation as JSON. * @throws Exception If the API call fails. */ private static function fetch_conversation_from_ai($conversation) { // Prepare the prompt for the AI request $prompt = [ 'profession' => $conversation['profession'], 'from' => $conversation['initiated_by'], 'to_pool' => json_decode($conversation['to_pool'], true), 'subject' => $conversation['subject'], 'num_of_replies' => $conversation['num_responses'], 'num_of_participants' => $conversation['num_participants'], 'max_days' => 3, 'can_reply' => json_decode($conversation['reply_pool'], true), 'available_to_cc' => json_decode($conversation['cc_pool'], true), 'start_date' => $conversation['first_message_timestamp'], 'end_date' => date('Y-m-d H:i:s', strtotime('+3 days', strtotime($conversation['first_message_timestamp']))) ]; // Call OpenAI API $response = RL_MailWarmer_Api_Helper::call_openai_api($prompt); if (!$response || !isset($response['choices'][0]['text'])) { throw new Exception('Invalid response from OpenAI API'); } return $response['choices'][0]['text']; } public static function merge_timeline_with_ai_response($conversation_json, $ai_response_json) { // Decode the JSON inputs into arrays $conversation = json_decode($conversation_json, true); $ai_response = json_decode($ai_response_json, true); // Ensure both arrays have the same number of elements $merged = []; $count_timeline = count($conversation); $count_ai_response = count($ai_response); if ($count_ai_response > $count_timeline) { // Trim the AI responses to match the timeline count $ai_response = array_slice($ai_response, 0, $count_timeline); } elseif ($count_ai_response < $count_timeline) { throw new Exception('The number of AI responses is less than the timeline steps.'); } // Merge corresponding elements foreach ($conversation as $index => $timeline_step) { $ai_message = $ai_response[$index]; $merged[] = array_merge($timeline_step, $ai_message); } // Return the merged result as JSON return json_encode($merged, JSON_PRETTY_PRINT); } } add_action('admin_menu', function () { add_menu_page( __('Conversations', 'rl-mailwarmer'), // Page title __('Conversations', 'rl-mailwarmer'), // Menu title 'manage_options', // Capability 'rl-mailwarmer-conversations', // Menu slug 'rl_mailwarmer_render_conversations_page', // Callback function 'dashicons-email', // Icon 8 // Position ); }); /** * Render the Conversations admin page with checkboxes and a Delete button. */ function rl_mailwarmer_render_conversations_page() { if (!current_user_can('manage_options')) { return; } global $wpdb; $table_name = $wpdb->prefix . 'rl_mailwarmer_conversations'; // Conversation table $conversations = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC"); ?>