| @@ -89,8 +89,17 @@ | ||
| 89 | 89 | $this->post($this->endpoint . '/modify-content', [$this, 'modify_content']); |
| 90 | 90 | $this->post($this->endpoint . '/ai-update', [$this, 'ai_update']); |
| 91 | 91 | $this->post($this->endpoint . '/ai-update-preview', [$this, 'ai_update_preview']); |
| 92 | 92 | $this->post($this->endpoint . '/generate-tagline', [$this, 'generate_tagline']); |
| 93 | + $this->get($this->endpoint . '/chatbot-conversation', [$this, 'get_chatbot_conversation'], [ | |
| 94 | + 'chat' => [ | |
| 95 | + 'required' => true, | |
| 96 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 97 | + 'validate_callback' => function($param, $request, $key) { | |
| 98 | + return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param); | |
| 99 | + }, | |
| 100 | + ], | |
| 101 | + ]); | |
| 93 | 102 | $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [ |
| 94 | 103 | 'type' => [ |
| 95 | 104 | 'default' => 'pack', |
| 96 | 105 | 'required' => false, |
| @@ -932,8 +941,48 @@ | ||
| 932 | 941 | ); |
| 933 | 942 | } |
| 934 | 943 | |
| 935 | 944 | // Return the response as-is |
| 945 | + return $data; | |
| 946 | + } | |
| 947 | + | |
| 948 | + /** | |
| 949 | + * Fetch a chatbot conversation by ID from the external Templately chatbot API. | |
| 950 | + * | |
| 951 | + * Used to resume a conversation that began on templately.dev when the user | |
| 952 | + * is redirected into the plugin with ?process=ai&chat={uuid}. | |
| 953 | + * | |
| 954 | + * @return array|\WP_Error Pass-through of the external response { status, data } or WP_Error. | |
| 955 | + */ | |
| 956 | + public function get_chatbot_conversation() { | |
| 957 | + $chat = $this->get_param('chat'); | |
| 958 | + | |
| 959 | + if (empty($chat)) { | |
| 960 | + return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-conversation', 400); | |
| 961 | + } | |
| 962 | + | |
| 963 | + $extra_headers = [ | |
| 964 | + 'Accept' => 'application/json', | |
| 965 | + ]; | |
| 966 | + $response = Helper::make_api_get_request("v2/chatbot/conversation/{$chat}", [], $extra_headers, 30); | |
| 967 | + | |
| 968 | + if (is_wp_error($response)) { | |
| 969 | + return $this->error('request_failed', __('Failed to fetch conversation.', 'templately'), 'ai-content/chatbot-conversation', 500, ['error_detail' => $response->get_error_message()]); | |
| 970 | + } | |
| 971 | + | |
| 972 | + $response_code = wp_remote_retrieve_response_code($response); | |
| 973 | + $body = wp_remote_retrieve_body($response); | |
| 974 | + $data = json_decode($body, true); | |
| 975 | + | |
| 976 | + if ($response_code !== 200) { | |
| 977 | + $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code); | |
| 978 | + return $this->error('api_http_error', $message, 'ai-content/chatbot-conversation', $response_code); | |
| 979 | + } | |
| 980 | + | |
| 981 | + if (!is_array($data) || !isset($data['status'])) { | |
| 982 | + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-conversation', 500); | |
| 983 | + } | |
| 984 | + | |
| 936 | 985 | return $data; |
| 937 | 986 | } |
| 938 | 987 | |
| 939 | 988 | /** |