PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 5.0.1
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v5.0.1
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 8.5.4 8.5.3 All 534 releases
chatbot / qcld_df_api.php

qcld_df_api.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 5.0.1, at qcld_df_api.php

140 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 /*
6 * @package Dialogflow API V2 by QuantumCloud
7 * @Since 9.1.2
8 */
9
10
11 class Qcld_wpbot_dfv2
12 {
13 public function __construct(){
14
15 add_action('init', array($this, 'api'));
16 }
17 public function api(){
18
19 if(isset($_GET['action']) && $_GET['action']=='qcld_dfv2_api'){
20 $session_id = 'asd2342sde';
21 $language = get_option('qlcd_wp_chatbot_dialogflow_agent_language');
22 //project ID
23 $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id');
24 // Service Account Key json file
25 $JsonFileContents = get_option('qlcd_wp_chatbot_dialogflow_project_key');
26 if($project_ID==''){
27 echo json_encode(array('error'=>'Project ID is empty'));exit;
28 }
29 if($JsonFileContents==''){
30 echo json_encode(array('error'=>'Key is empty'));exit;
31 }
32 if(!isset($_POST['dfquery']) || $_POST['dfquery']==''){
33 echo json_encode(array('error'=>'Query text is not added!'));exit;
34 }
35 $query = sanitize_text_field($_POST['dfquery']);
36 if(isset($_POST['sessionid']) && $_POST['sessionid']!=''){
37 $session_id = sanitize_text_field($_POST['sessionid']);
38 }
39
40
41 if(file_exists(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php')){
42
43 require(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php');
44
45 $client = new \Google_Client();
46 $client->useApplicationDefaultCredentials();
47 $client->setScopes (['https://www.googleapis.com/auth/dialogflow']);
48 // Convert to array
49 $array = json_decode($JsonFileContents, true);
50 $client->setAuthConfig($array);
51
52 try {
53 $httpClient = $client->authorize();
54 $apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent";
55
56 $response = $httpClient->request('POST', $apiUrl, [
57 'json' => ['queryInput' => ['text' => ['text' => $query, 'languageCode' => $language]],
58 'queryParams' => ['timeZone' => '']]
59 ]);
60
61 $contents = $response->getBody()->getContents();
62 echo $contents;exit;
63
64 }catch(Exception $e) {
65 echo json_encode(array('error'=>$e->getMessage()));exit;
66 }
67
68 }else{
69 echo json_encode(array('error'=>'API client not found'));exit;
70 }
71
72 }
73
74 }
75 }
76 new Qcld_wpbot_dfv2();
77
78 add_action('wp_ajax_qcld_wp_df_api_call', 'qcld_wp_df_api_call');
79 add_action('wp_ajax_nopriv_qcld_wp_df_api_call', 'qcld_wp_df_api_call');
80 function qcld_wp_df_api_call(){
81 $nonce = sanitize_text_field($_POST['nonce']);
82 if ((! wp_verify_nonce($nonce,'wp_chatbot')) && ( ! wp_verify_nonce($nonce,'qcsecretbotnonceval123qc'))) {
83 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
84 wp_die();
85
86 }else{
87 $session_id = 'asd2342sde';
88 $language = get_option('qlcd_wp_chatbot_dialogflow_agent_language');
89 //project ID
90 $project_ID = get_option('qlcd_wp_chatbot_dialogflow_project_id');
91 // Service Account Key json file
92 $JsonFileContents = get_option('qlcd_wp_chatbot_dialogflow_project_key');
93 if($project_ID==''){
94 echo json_encode(array('error'=>'Project ID is empty'));exit;
95 }
96 if($JsonFileContents==''){
97 echo json_encode(array('error'=>'Key is empty'));exit;
98 }
99 if(!isset($_POST['dfquery']) || $_POST['dfquery']==''){
100 echo json_encode(array('error'=>'Query text is not added!'));exit;
101 }
102 $query = sanitize_text_field($_POST['dfquery']);
103 if(isset($_POST['sessionid']) && $_POST['sessionid']!=''){
104 $session_id = sanitize_text_field($_POST['sessionid']);
105 }
106
107
108 if(file_exists(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php')){
109
110 require(QCLD_wpCHATBOT_GC_DIRNAME.'/autoload.php');
111
112 $client = new \Google_Client();
113 $client->useApplicationDefaultCredentials();
114 $client->setScopes (['https://www.googleapis.com/auth/dialogflow']);
115 // Convert to array
116 $array = json_decode($JsonFileContents, true);
117 $client->setAuthConfig($array);
118
119 try {
120 $httpClient = $client->authorize();
121 $apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent";
122
123 $response = $httpClient->request('POST', $apiUrl, [
124 'json' => ['queryInput' => ['text' => ['text' => $query, 'languageCode' => $language]],
125 'queryParams' => ['timeZone' => '']]
126 ]);
127
128 $contents = $response->getBody()->getContents();
129 echo $contents;exit;
130
131 }catch(Exception $e) {
132 echo json_encode(array('error'=>$e->getMessage()));exit;
133 }
134
135 }else{
136 echo json_encode(array('error'=>'API client not found'));exit;
137 }
138 die();
139 }
140 }