core = $mwai_core; add_shortcode( 'mwai_imagesbot', array( $this, 'imagesbot' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); add_filter( 'mwai_imagesbot_html', array( $this, 'chatgpt_style' ), 10, 2 ); add_filter( 'mwai_imagesbot_html', array( $this, 'spinner_style' ), 10, 2 ); } function rest_api_init() { try { register_rest_route( $this->namespace, '/imagesbot', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_imagesbot' ), 'permission_callback' => '__return_true' ) ); } catch ( Exception $e ) { var_dump( $e ); } } function spinner_style( $html, $atts ) { // If needed, we can use the $id to apply styles $id = $atts['id']; // This could be replace by the ID of a specific chatbot return " " . $html; } function chatgpt_style( $html, $atts ) { // If needed, we can use the $id to apply styles $id = $atts['id']; // This could be replace by the ID of a specific chatbot return " " . $html; } function rest_imagesbot( $request ) { try { $params = $request->get_json_params(); $prompt = $params['prompt']; //$model = $params['model']; $maxResults = $params['maxResults']; $apiKey = $params['apiKey']; $query = new Meow_MWAI_QueryImage( $prompt ); if ( $maxResults ) { $query->setMaxResults( $maxResults ); } if ( $apiKey ) { $query->setApiKey( $apiKey ); } $answer = $this->core->ai->run( $query ); return new WP_REST_Response([ 'success' => true, 'images' => $answer->results, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } function imagesbot( $atts ) { $defaults = apply_filters( 'mwai_imagesbot_atts', [ 'id' => uniqid(), 'ai_name' => "AI: ", 'user_name' => "You: ", 'sys_name' => "System: ", 'start_sentence' => "Hey there! Can you tell me what kind of images you need?", 'max_results' => 3, 'model' => 'dall-e', 'text_send' => 'Send', 'text_input_placeholder' => 'Type your message...', 'api_key' => '' ] ); $atts = shortcode_atts( $defaults, $atts, 'mwai_chat_atts' ); $id = $atts['id']; $apiUrl = get_rest_url( null, 'ai-engine/v1/imagesbot' ); $onSentClickFn = "mwai_{$id}_onSendClick"; $addReplyFn = "mwai_{$id}_addReply"; $convertToHtmlFn = "mwai_{$id}_convertToHtml"; $reuseFn = "mwai_{$id}_reuse"; $aiName = addslashes( trim($atts['ai_name']) ); $userName = addslashes( trim($atts['user_name']) ); $sysName = addslashes( trim($atts['sys_name']) ); $textSend = addslashes( trim( $atts['text_send'] ) ); $maxResults = $atts['max_results']; $textInputPlaceholder = addslashes( trim( $atts['text_input_placeholder'] ) ); ob_start(); ?>