| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | add_action('init', array($this, 'api')); |
| 16 | 16 | } |
| 17 | 17 | public function api(){ |
| 18 | 18 | |
| 19 | - if ( isset( $_GET['action'] ) && sanitize_text_field( wp_unslash( $_GET['action'] ) ) === 'qcld_dfv2_api' ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 19 | + if(isset($_GET['action']) && $_GET['action']=='qcld_dfv2_api'){ | |
| 20 | 20 | $session_id = 'asd2342sde'; |
| 21 | 21 | $language = get_option('qlcd_wp_chatbot_dialogflow_agent_language'); |
| 22 | 22 | //project ID |
| 23 | 23 | $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id'); |
| @@ -23,19 +23,19 @@ | ||
| 23 | 23 | $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id'); |
| 24 | 24 | // Service Account Key json file |
| 25 | 25 | $JsonFileContents = get_option('qlcd_wp_chatbot_dialogflow_project_key'); |
| 26 | 26 | if($project_ID==''){ |
| 27 | - echo wp_json_encode(array('error'=>'Project ID is empty'));exit; | |
| 27 | + echo json_encode(array('error'=>'Project ID is empty'));exit; | |
| 28 | 28 | } |
| 29 | 29 | if($JsonFileContents==''){ |
| 30 | - echo wp_json_encode(array('error'=>'Key is empty'));exit; | |
| 30 | + echo json_encode(array('error'=>'Key is empty'));exit; | |
| 31 | 31 | } |
| 32 | - if(!isset($_POST['dfquery']) || wp_unslash($_POST['dfquery'])==''){ | |
| 33 | - echo wp_json_encode(array('error'=>'Query text is not added!'));exit; | |
| 32 | + if(!isset($_POST['dfquery']) || $_POST['dfquery']==''){ | |
| 33 | + echo json_encode(array('error'=>'Query text is not added!'));exit; | |
| 34 | 34 | } |
| 35 | - $query = sanitize_text_field(wp_unslash($_POST['dfquery'])); | |
| 36 | - if(isset($_POST['sessionid']) && wp_unslash($_POST['sessionid'])!=''){ | |
| 37 | - $session_id = sanitize_text_field(wp_unslash($_POST['sessionid'])); | |
| 35 | + $query = sanitize_text_field($_POST['dfquery']); | |
| 36 | + if(isset($_POST['sessionid']) && $_POST['sessionid']!=''){ | |
| 37 | + $session_id = sanitize_text_field($_POST['sessionid']); | |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | |
| 41 | 41 | if(file_exists(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php')){ |
| @@ -58,19 +58,16 @@ | ||
| 58 | 58 | 'queryParams' => ['timeZone' => '']] |
| 59 | 59 | ]); |
| 60 | 60 | |
| 61 | 61 | $contents = $response->getBody()->getContents(); |
| 62 | - | |
| 63 | - echo esc_html( $contents ); | |
| 64 | - | |
| 65 | - exit; | |
| 62 | + echo $contents;exit; | |
| 66 | 63 | |
| 67 | 64 | }catch(Exception $e) { |
| 68 | - echo wp_json_encode(array('error'=>$e->getMessage()));exit; | |
| 65 | + echo json_encode(array('error'=>$e->getMessage()));exit; | |
| 69 | 66 | } |
| 70 | 67 | |
| 71 | 68 | }else{ |
| 72 | - echo wp_json_encode(array('error'=>'API client not found'));exit; | |
| 69 | + echo json_encode(array('error'=>'API client not found'));exit; | |
| 73 | 70 | } |
| 74 | 71 | |
| 75 | 72 | } |
| 76 | 73 | |
| @@ -80,67 +77,57 @@ | ||
| 80 | 77 | |
| 81 | 78 | add_action('wp_ajax_qcld_wp_df_api_call', 'qcld_wp_df_api_call'); |
| 82 | 79 | add_action('wp_ajax_nopriv_qcld_wp_df_api_call', 'qcld_wp_df_api_call'); |
| 83 | 80 | function qcld_wp_df_api_call(){ |
| 84 | - $nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); | |
| 85 | - if ((! wp_verify_nonce($nonce,'wp_chatbot')) && ( ! wp_verify_nonce($nonce,'qcsecretbotnonceval123qc'))) { | |
| 86 | - wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'chatbot'))); | |
| 87 | - wp_die(); | |
| 81 | + $session_id = 'asd2342sde'; | |
| 82 | + $language = get_option('qlcd_wp_chatbot_dialogflow_agent_language'); | |
| 83 | + //project ID | |
| 84 | + $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id'); | |
| 85 | + // Service Account Key json file | |
| 86 | + $JsonFileContents = get_option('qlcd_wp_chatbot_dialogflow_project_key'); | |
| 87 | + if($project_ID==''){ | |
| 88 | + echo json_encode(array('error'=>'Project ID is empty'));exit; | |
| 89 | + } | |
| 90 | + if($JsonFileContents==''){ | |
| 91 | + echo json_encode(array('error'=>'Key is empty'));exit; | |
| 92 | + } | |
| 93 | + if(!isset($_POST['dfquery']) || $_POST['dfquery']==''){ | |
| 94 | + echo json_encode(array('error'=>'Query text is not added!'));exit; | |
| 95 | + } | |
| 96 | + $query = sanitize_text_field($_POST['dfquery']); | |
| 97 | + if(isset($_POST['sessionid']) && $_POST['sessionid']!=''){ | |
| 98 | + $session_id = sanitize_text_field($_POST['sessionid']); | |
| 99 | + } | |
| 100 | + | |
| 88 | 101 | |
| 89 | - }else{ | |
| 90 | - $session_id = 'asd2342sde'; | |
| 91 | - $language = get_option('qlcd_wp_chatbot_dialogflow_agent_language'); | |
| 92 | - //project ID | |
| 93 | - $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id'); | |
| 94 | - // Service Account Key json file | |
| 95 | - $JsonFileContents = get_option('qlcd_wp_chatbot_dialogflow_project_key'); | |
| 96 | - if($project_ID==''){ | |
| 97 | - echo wp_json_encode(array('error'=>'Project ID is empty'));exit; | |
| 98 | - } | |
| 99 | - if($JsonFileContents==''){ | |
| 100 | - echo wp_json_encode(array('error'=>'Key is empty'));exit; | |
| 101 | - } | |
| 102 | - if(!isset($_POST['dfquery']) || wp_unslash($_POST['dfquery'])==''){ | |
| 103 | - echo wp_json_encode(array('error'=>'Query text is not added!'));exit; | |
| 104 | - } | |
| 105 | - $query = sanitize_text_field(wp_unslash($_POST['dfquery'])); | |
| 106 | - if(isset($_POST['sessionid']) && wp_unslash($_POST['sessionid'])!=''){ | |
| 107 | - $session_id = sanitize_text_field(wp_unslash($_POST['sessionid'])); | |
| 108 | - } | |
| 109 | - | |
| 102 | + if(file_exists(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php')){ | |
| 110 | 103 | |
| 111 | - if(file_exists(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php')){ | |
| 104 | + require(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php'); | |
| 112 | 105 | |
| 113 | - require(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php'); | |
| 106 | + $client = new \Google_Client(); | |
| 107 | + $client->useApplicationDefaultCredentials(); | |
| 108 | + $client->setScopes (['https://www.googleapis.com/auth/dialogflow']); | |
| 109 | + // Convert to array | |
| 110 | + $array = json_decode($JsonFileContents, true); | |
| 111 | + $client->setAuthConfig($array); | |
| 114 | 112 | |
| 115 | - $client = new \Google_Client(); | |
| 116 | - $client->useApplicationDefaultCredentials(); | |
| 117 | - $client->setScopes (['https://www.googleapis.com/auth/dialogflow']); | |
| 118 | - // Convert to array | |
| 119 | - $array = json_decode($JsonFileContents, true); | |
| 120 | - $client->setAuthConfig($array); | |
| 113 | + try { | |
| 114 | + $httpClient = $client->authorize(); | |
| 115 | + $apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent"; | |
| 121 | 116 | |
| 122 | - try { | |
| 123 | - $httpClient = $client->authorize(); | |
| 124 | - $apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent"; | |
| 117 | + $response = $httpClient->request('POST', $apiUrl, [ | |
| 118 | + 'json' => ['queryInput' => ['text' => ['text' => $query, 'languageCode' => $language]], | |
| 119 | + 'queryParams' => ['timeZone' => '']] | |
| 120 | + ]); | |
| 121 | + | |
| 122 | + $contents = $response->getBody()->getContents(); | |
| 123 | + echo $contents;exit; | |
| 125 | 124 | |
| 126 | - $response = $httpClient->request('POST', $apiUrl, [ | |
| 127 | - 'json' => ['queryInput' => ['text' => ['text' => $query, 'languageCode' => $language]], | |
| 128 | - 'queryParams' => ['timeZone' => '']] | |
| 129 | - ]); | |
| 130 | - | |
| 131 | - $contents = $response->getBody()->getContents(); | |
| 125 | + }catch(Exception $e) { | |
| 126 | + echo json_encode(array('error'=>$e->getMessage()));exit; | |
| 127 | + } | |
| 132 | 128 | |
| 133 | - echo wp_json_encode($contents); | |
| 134 | - | |
| 135 | - exit; | |
| 136 | - | |
| 137 | - }catch(Exception $e) { | |
| 138 | - echo wp_json_encode(array('error'=>$e->getMessage()));exit; | |
| 139 | - } | |
| 140 | - | |
| 141 | - }else{ | |
| 142 | - echo wp_json_encode(array('error'=>'API client not found'));exit; | |
| 143 | - } | |
| 144 | - die(); | |
| 129 | + }else{ | |
| 130 | + echo json_encode(array('error'=>'API client not found'));exit; | |
| 145 | 131 | } |
| 132 | + die(); | |
| 146 | 133 | } |