| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/AIWEBSTATION/wp-content/plugins/wpbot-pro/ |
Upload File : |
<?php
/**
* @package Dialogflow API V2 by QuantumCloud
* @Since 9.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* QCld class
*/
class Qcld_wpbot_dfv2 {
/**
* Constrauct function
*/
public function __construct() {
add_action( 'init', array( $this, 'api' ) );
}
/**
* API function
*
* @return void
*/
public function api() {
if ( isset( $_GET['action'] ) && $_GET['action'] == 'qcld_dfv2_api' ) {
$session_id = 'asd2342sde';
$language = get_option( 'qlcd_wp_chatbot_dialogflow_agent_language' );
// project ID
$project_ID = get_option( 'qlcd_wp_chatbot_dialogflow_project_id' );
// Service Account Key json file
$JsonFileContents = get_option( 'qlcd_wp_chatbot_dialogflow_project_key' );
if ( $project_ID == '' ) {
echo json_encode( array( 'error' => 'Project ID is empty' ) );
exit;
}
if ( $JsonFileContents == '' ) {
echo json_encode( array( 'error' => 'Key is empty' ) );
exit;
}
if ( ! isset( $_POST['dfquery'] ) || $_POST['dfquery'] == '' ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing
echo json_encode( array( 'error' => 'Query text is not added!' ) );
exit;
}
$query = sanitize_text_field( $_POST['dfquery'] );// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['sessionid'] ) && $_POST['sessionid'] != '' ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing
$session_id = sanitize_text_field( $_POST['sessionid'] );// phpcs:ignore WordPress.Security.NonceVerification.Missing
}
if ( file_exists( QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php' ) ) {
require QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php';
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes( array( 'https://www.googleapis.com/auth/dialogflow' ) );
// Convert to array.
$array = json_decode( $JsonFileContents, true );
$client->setAuthConfig( $array );
try {
$httpClient = $client->authorize();
$apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent";
$response = $httpClient->request(
'POST',
$apiUrl,
array(
'json' => array(
'queryInput' => array(
'text' => array(
'text' => $query,
'languageCode' => $language,
),
),
'queryParams' => array( 'timeZone' => '' ),
),
)
);
$contents = $response->getBody()->getContents();
echo $contents;
exit;
} catch ( Exception $e ) {
echo json_encode( array( 'error' => $e->getMessage() ) );
exit;
}
} else {
echo json_encode( array( 'error' => 'API client not found' ) );
exit;
}
}
}
}
new Qcld_wpbot_dfv2();
add_action(
'rest_api_init',
function () {
register_rest_route(
'wpbot/v1',
'/dialogflow_api/',
array(
'methods' => 'POST',
'callback' => 'qc_wpbot_dfcallback',
'permission_callback' => '__return_true',
)
);
}
);
/**
* QC callback function
*
* @param [type] $request
* @return void
*/
function qc_wpbot_dfcallback( $request ) {
$postdata = $request->get_body_params();
$session_id = 'asd2342sde';
$language = get_option( 'qlcd_wp_chatbot_dialogflow_agent_language' );
// project ID
$project_ID = get_option( 'qlcd_wp_chatbot_dialogflow_project_id' );
// Service Account Key json file
$JsonFileContents = get_option( 'qlcd_wp_chatbot_dialogflow_project_key' );
if ( $project_ID == '' ) {
echo json_encode( array( 'error' => 'Project ID is empty' ) );
exit;
}
if ( $JsonFileContents == '' ) {
echo json_encode( array( 'error' => 'Key is empty' ) );
exit;
}
if ( ! isset( $postdata['dfquery'] ) || $postdata['dfquery'] == '' ) {
echo json_encode( array( 'error' => 'Query text is not added!' ) );
exit;
}
$query = sanitize_text_field( $postdata['dfquery'] );
if ( isset( $postdata['sessionid'] ) && $postdata['sessionid'] != '' ) {
$session_id = sanitize_text_field( $postdata['sessionid'] );
}
if ( file_exists( QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php' ) ) {
require QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php';
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes( array( 'https://www.googleapis.com/auth/dialogflow' ) );
// Convert to array
$array = json_decode( $JsonFileContents, true );
$client->setAuthConfig( $array );
try {
$httpClient = $client->authorize();
$apiUrl = "https://dialogflow.googleapis.com/v2/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent";
$response = $httpClient->request(
'POST',
$apiUrl,
array(
'json' => array(
'queryInput' => array(
'text' => array(
'text' => $query,
'languageCode' => $language,
),
),
'queryParams' => array( 'timeZone' => '' ),
),
)
);
$contents = $response->getBody()->getContents();
echo $contents;
exit;
} catch ( Exception $e ) {
echo json_encode( array( 'error' => $e->getMessage() ) );
exit;
}
} else {
echo json_encode( array( 'error' => 'API client not found' ) );
exit;
}
}
add_action( 'wp_ajax_qcld_wp_df_api_call', 'qcld_wp_df_api_call' );
add_action( 'wp_ajax_nopriv_qcld_wp_df_api_call', 'qcld_wp_df_api_call' );
function qcld_wp_df_api_call() {
$session_id = 'asd2342sde';
if ( isset( $_POST['language'] ) && $_POST['language'] != '' ) {
$lang = $_POST['language'];
} else {
$lang = get_wpbot_locale();
}
$language = get_option( 'qlcd_wp_chatbot_dialogflow_agent_language' );
if ( $language && is_array( $language ) && array_key_exists( $lang, $language ) ) {
$language = $language[ $lang ];
}
// project ID
$project_ID = get_option( 'qlcd_wp_chatbot_dialogflow_project_id' );
if ( $project_ID && is_array( $project_ID ) && array_key_exists( $lang, $project_ID ) ) {
$project_ID = $project_ID[ $lang ];
}
// Service Account Key json file
$JsonFileContents = get_option( 'qlcd_wp_chatbot_dialogflow_project_key' );
if ( $JsonFileContents && is_array( $JsonFileContents ) && array_key_exists( $lang, $JsonFileContents ) ) {
$JsonFileContents = $JsonFileContents[ $lang ];
}
if ( $project_ID == '' ) {
echo json_encode( array( 'error' => 'Project ID is empty' ) );
exit;
}
if ( $JsonFileContents == '' ) {
echo json_encode( array( 'error' => 'Key is empty' ) );
exit;
}
if ( ! isset( $_POST['dfquery'] ) || $_POST['dfquery'] == '' ) {
echo json_encode( array( 'error' => 'Query text is not added!' ) );
exit;
}
$query = sanitize_text_field( $_POST['dfquery'] );
if ( isset( $_POST['sessionid'] ) && $_POST['sessionid'] != '' ) {
$session_id = sanitize_text_field( $_POST['sessionid'] );
}
if ( file_exists( QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php' ) ) {
require QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php';
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes( array( 'https://www.googleapis.com/auth/dialogflow' ) );
// Convert to array
$array = json_decode( $JsonFileContents, true );
$client->setAuthConfig( $array );
try {
$httpClient = $client->authorize();
$apiUrl = "https://dialogflow.googleapis.com/v2beta1/projects/{$project_ID}/agent/sessions/{$session_id}:detectIntent";
$kbUrl = "https://dialogflow.googleapis.com/v2beta1/projects/{$project_ID}/knowledgeBases";
$bkresponse = $httpClient->request( 'GET', $kbUrl );
$kbcontents = $bkresponse->getBody()->getContents();
$datakb = json_decode( $kbcontents, true );
$kbs = array();
if ( isset( $datakb['knowledgeBases'] ) && ! empty( $datakb['knowledgeBases'] ) ) {
foreach ( $datakb['knowledgeBases'] as $kb ) {
$kbs[] = $kb['name'];
}
}
$query = array(
'json' => array(
'queryInput' => array(
'text' => array(
'text' => $query,
'languageCode' => $language,
),
),
'queryParams' => array(
'timeZone' => '',
'knowledgeBaseNames' => $kbs,
),
),
);
$response = $httpClient->request( 'POST', $apiUrl, $query );
$contents = $response->getBody()->getContents();
echo $contents;
exit;
} catch ( Exception $e ) {
echo json_encode( array( 'error' => $e->getMessage() ) );
exit;
}
} else {
echo json_encode( array( 'error' => 'API client not found' ) );
exit;
}
die();
}
function qcld_wpbot_df_cx_agent( $force, $lang ) {
$session_id = 'asd2342sde';
$language = get_option( 'qlcd_wp_chatbot_dialogflow_agent_language' );
if ( $language && is_array( $language ) && array_key_exists( $lang, $language ) ) {
$language = $language[ $lang ];
}
// project ID
$project_ID = get_option( 'qlcd_wp_chatbot_dialogflow_project_id' );
if ( $project_ID && is_array( $project_ID ) && array_key_exists( $lang, $project_ID ) ) {
$project_ID = $project_ID[ $lang ];
}
// Service Account Key json file
$JsonFileContents = get_option( 'qlcd_wp_chatbot_dialogflow_project_key' );
if ( $JsonFileContents && is_array( $JsonFileContents ) && array_key_exists( $lang, $JsonFileContents ) ) {
$JsonFileContents = $JsonFileContents[ $lang ];
}
if ( $project_ID == '' ) {
return ( array( 'error' => 'Project ID is empty' ) );
}
if ( $JsonFileContents == '' ) {
return ( array( 'error' => 'Key is empty' ) );
}
if ( isset( $_POST['sessionid'] ) && $_POST['sessionid'] != '' ) {
$session_id = sanitize_text_field( $_POST['sessionid'] );
}
if ( ! $force && get_transient( 'dialogflow_cx_agents_' . $lang ) ) {
return get_transient( 'dialogflow_cx_agents_' . $lang );
}
if ( file_exists( QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php' ) ) {
require QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php';
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes( array( 'https://www.googleapis.com/auth/dialogflow' ) );
// Convert to array
$array = json_decode( $JsonFileContents, true );
$client->setAuthConfig( $array );
try {
$httpClient = $client->authorize();
$apiUrl = "https://dialogflow.googleapis.com/v3beta1/projects/{$project_ID}/locations/global/agents";
$response = $httpClient->request( 'GET', $apiUrl );
$contents = $response->getBody()->getContents();
set_transient( 'dialogflow_cx_agents_' . $lang, json_decode( $contents, true ), 12 * HOUR_IN_SECONDS );
return json_decode( $contents, true );
} catch ( Exception $e ) {
return ( array( 'error' => $e->getMessage() ) );
}
}
}
add_action( 'wp_ajax_qcld_wp_df_api_cx', 'qcld_wp_df_api_cx' );
add_action( 'wp_ajax_nopriv_qcld_wp_df_api_cx', 'qcld_wp_df_api_cx' );
function qcld_wp_df_api_cx() {
$session_id = 'asd2342sde';
if ( isset( $_POST['language'] ) && $_POST['language'] != '' ) {
$lang = $_POST['language'];
} else {
$lang = get_wpbot_locale();
}
$name = sanitize_text_field( $_POST['name'] );
$timezone = sanitize_text_field( $_POST['timezone'] );
$defaultlangaugecode = sanitize_text_field( $_POST['defaultlanguageCode'] );
$language = get_option( 'qlcd_wp_chatbot_dialogflow_agent_language' );
if ( $language && is_array( $language ) && array_key_exists( $lang, $language ) ) {
$language = $language[ $lang ];
}
// project ID.
$project_ID = get_option( 'qlcd_wp_chatbot_dialogflow_project_id' );
if ( $project_ID && is_array( $project_ID ) && array_key_exists( $lang, $project_ID ) ) {
$project_ID = $project_ID[ $lang ];
}
// Service Account Key json file.
$JsonFileContents = get_option( 'qlcd_wp_chatbot_dialogflow_project_key' );
if ( $JsonFileContents && is_array( $JsonFileContents ) && array_key_exists( $lang, $JsonFileContents ) ) {
$JsonFileContents = $JsonFileContents[ $lang ];
}
if ( $project_ID == '' ) {
echo json_encode( array( 'error' => 'Project ID is empty' ) );
exit;
}
if ( $JsonFileContents == '' ) {
echo json_encode( array( 'error' => 'Key is empty' ) );
exit;
}
if ( ! isset( $_POST['dfquery'] ) || $_POST['dfquery'] == '' ) {
echo json_encode( array( 'error' => 'Query text is not added!' ) );
exit;
}
$query = sanitize_text_field( $_POST['dfquery'] );
if ( isset( $_POST['sessionid'] ) && $_POST['sessionid'] != '' ) {
$session_id = sanitize_text_field( $_POST['sessionid'] );
}
if ( file_exists( QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php' ) ) {
require QCLD_wpCHATBOT_GC_DIRNAME . '/autoload.php';
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes( array( 'https://www.googleapis.com/auth/dialogflow' ) );
// Convert to array.
$array = json_decode( $JsonFileContents, true );
$client->setAuthConfig( $array );
try {
$httpClient = $client->authorize();
$apiUrl = "https://dialogflow.googleapis.com/v3beta1/{$name}/sessions/{$session_id}:detectIntent";
$query = array(
'json' => array(
'queryInput' => array(
'text' => array(
'text' => $query,
),
'languageCode' => $language,
),
'queryParams' => array(
'timeZone' => $timezone,
),
),
);
$response = $httpClient->request( 'POST', $apiUrl, $query );
$contents = $response->getBody()->getContents();
echo $contents;
exit;
} catch ( Exception $e ) {
echo json_encode( array( 'error' => $e->getMessage() ) );
exit;
}
} else {
echo json_encode( array( 'error' => 'API client not found' ) );
exit;
}
die();
}