0 && is_singular() && ! in_array( get_the_ID(), $settings['display_pages'] ) ) { return; } if ( ( ! isset( $settings['display_chatbot'] ) || 'false' == $settings['display_chatbot'] ) && isset( $settings['display_basic_chatbot'] ) && 'true' == $settings['display_basic_chatbot'] ) { $chatbot = ai_chatbot_easy_integration_chatwindow(); $trustedtags = ai_chatbot_easy_integration_get_trusted_tags_array(); echo wp_kses( $chatbot, $trustedtags ); } elseif ( isset( $settings['display_chatbot'] ) && 'true' == $settings['display_chatbot'] ) { $chatbot = $settings['webchat_embed_code']; if ( strstr( $chatbot, 'window.watsonAssistantChatOptions' ) ) { echo str_replace( '=>', '=>', wp_kses( $chatbot, array( 'script' => array() ) ) ); return; } } } add_filter( 'wp_footer', 'ai_chatbot_easy_integration_append' ); /** * Register session for local chat window */ function ai_chatbot_easy_integration_register_session() { if ( ! session_id() ) { session_start(); } } add_action( 'init', 'ai_chatbot_easy_integration_register_session' ); /** * Process webhook */ function ai_chatbot_easy_integration_api_webhook_process() { $settings = ai_chatbot_easy_integration_get_settings(); // exit if not a secure chatbot callback! if ( ! isset( $_REQUEST['chatbotcall'] ) || ! isset( $settings['webhook_hash'] ) ) { return; } if ( ! function_exists( 'wp_verify_nonce' ) || ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ) ) ) { // nonce not required. } // log security issues on web hook call! if ( $settings['webhook_hash'] !== $_REQUEST['chatbotcall'] ) { $response = esc_attr( __( 'Invalid call - webhook_hash does not match chatbotcall parameter.', 'ai-chatbot-easy-integration' ) ); if ( function_exists( 'SimpleLogger' ) ) { SimpleLogger()->info( $response ); } return; } // get the parmeters that were passed by Watson Assistant! // if you sanitize the whole input you will break the code. Please review the code, sanitation takes place in the lines that follow. $body = file_get_contents( 'php://input' ); header( 'Content-Type: application/json; charset=utf-8' ); $body = json_decode( $body ); $response = esc_attr( __( 'I didn\'t understand. Could you try asking it a different way.', 'ai-chatbot-easy-integration' ) ); if ( isset( $_POST ) && isset( $settings['log_chat_messages'] ) && 'true' === $settings['log_chat_messages'] ) { // process pre webhook! if ( '' !== $body && isset( $body->payload->input->text ) ) { $message = esc_attr( $body->payload->input->text ); $smsnumber = ''; if ( isset( $body->payload->context->global->system->user_id ) ) { $userid = esc_attr( $body->payload->context->global->system->user_id ); } elseif ( isset( $body->payload->user_id ) ) { $userid = esc_attr( $body->payload->user_id ); } else { $userid = ''; } if ( isset( $body->payload->context->global->session_id ) ) { $sessionid = esc_attr( $body->payload->context->global->session_id ); } else { $sessionid = ''; } if ( '' !== $message && '' !== $userid ) { ai_chatbot_easy_integration_insert_log_message( $message, $sessionid, $smsnumber, $userid, '', 'from_user' ); } elseif ( function_exists( 'SimpleLogger' ) ) { SimpleLogger()->info( 'No response received from Watson.' ); } } // process post webhook! if ( '' !== $body && isset( $body->payload->output->generic[0]->text ) ) { $message = esc_attr( $body->payload->output->generic[0]->text ); if ( isset( $body->payload->context->global->session_id ) ) { $sessionid = esc_attr( $body->payload->context->global->session_id ); } else { $sessionid = ''; } if ( isset( $body->payload->context->global->system->user_id ) ) { $userid = esc_attr( $body->payload->context->global->system->user_id ); } elseif ( isset( $body->payload->user_id ) ) { $userid = esc_attr( $body->payload->user_id ); } else { $userid = ''; } if ( '' !== $message ) { ai_chatbot_easy_integration_insert_log_message( $message, $sessionid, '', $userid, '', 'from_chatbot' ); } elseif ( function_exists( 'SimpleLogger' ) ) { SimpleLogger()->info( 'No response received from Watson.' ); } die(); } } // process specific requests! if ( '' !== $body && isset( $body->callType ) ) { $calltype = esc_attr( $body->callType ); // insert user defined calls! $response = ai_chatbot_easy_integration_custom_calls( $calltype, $body ); if ( '' === $response ) { $response = esc_attr( __( 'I didn\'t understand. Try asking it a different way.', 'ai-chatbot-easy-integration' ) ); } } // sanitation takes place as the response is formatted! Escaping it at this point will damage the code. if ( ! strstr( $response, 'Response' ) ) { echo '{"Response":[{"message":"' . wp_kses_post( $response ) . '"}]}'; } else { echo wp_kses_post( $response ); } die(); } add_action( 'init', 'ai_chatbot_easy_integration_api_webhook_process' ); /** * Handle custom watson calls * * @param mixed $calltype the type of call to make. * @param mixed $body object containing the searchterm or phrase entered. */ function ai_chatbot_easy_integration_custom_calls( $calltype, $body ) { $response = ''; if ( '' == $response ) { $response = apply_filters( 'ai_chatbot_easy_integration_custom_calls', $response, $calltype, $body ); } return $response; } /** * Save message * * @param mixed $message the message that was received. * @param mixed $sessionid the sessionid. * @param mixed $smsnumber SMS number if applicable. * @param mixed $userid the users id. * @param mixed $agent assigned agent id. * @param mixed $from who posted the message, bot or user. * @return void */ function ai_chatbot_easy_integration_insert_log_message( $message, $sessionid, $smsnumber, $userid, $agent, $from ) { global $wpdb; if ( '' !== $sessionid ) { $session = $sessionid; } elseif ( '' !== $userid ) { $session = $userid; } else { $session = ''; } if ( '' === $session ) { return; } if ( ! ai_chatbot_easy_integration_check_log_message( $userid, $session ) ) { $wpdb->query( $wpdb->prepare( 'INSERT INTO ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs (topic, entrydate, sessionid,smsnumber,userid,agentassigned,sessionstatus) values(%s, now(), %s,%s,%s,%s,%s)', $message, $sessionid, $smsnumber, $userid, $agent, 'active' ) ); } elseif ( '' !== $userid ) { $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs SET lastaction = %s, sessionid=%s WHERE userid=%s', '', $sessionid, $userid ) ); } // update messages! ai_chatbot_easy_integration_update_messages( $session, $message, $from ); } /** * Update message values * * @param mixed $session the sessionid. * @param mixed $value the message value. * @param mixed $from who posted the message, bot or user. * @return void */ function ai_chatbot_easy_integration_update_messages( $session, $value, $from ) { global $wpdb; $value = $value . ':|:' . time() . ':|:' . $from; $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs where sessionid = %s or userid = %s', $session, $session ), ARRAY_A ); if ( count( $results ) ) { foreach ( $results as $row ) { $messages = unserialize( $row['messages'] ); } } if ( isset( $messages ) && ! is_array( $messages ) ) { $messages = array( $value ); } else { $messages[] = $value; } $message = serialize( $messages ); // update messages! $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs SET messages=%s WHERE userid=%s or sessionid = %s', $message, $session, $session ) ); } /** * Only save one log message per user * * @param mixed $userid the user id. * @param mixed $sessionid the sessionid. */ function ai_chatbot_easy_integration_check_log_message( $userid, $sessionid ) { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs where userid = %s or smsnumber = %s or sessionid = %s or userid = %s or smsnumber = %s or sessionid = %s', $userid, $userid, $userid, $sessionid, $sessionid, $sessionid ) ); if ( count( $results ) > 0 ) { return 1; } else { return 0; } } /** * Display reports **/ function ai_chatbot_easy_integration_reports() { echo '
'; esc_attr_e( 'The reports section is still being developed. Check back after each release to find new reporting features.', 'ai-chatbot-easy-integration' ); echo '
'; echo ''; $url = wp_nonce_url( '/wp-admin/admin.php?page=ai-chatbot-easy-integration-log&export_chat_log=1' ); echo ''; esc_attr_e( 'EXPORT CHAT LOG', 'ai-chatbot-easy-integration' ); echo ''; echo '
'; echo ''; esc_attr_e( 'These are the most common topics that your chatbot received.', 'ai-chatbot-easy-integration' ); echo '
'; ai_chatbot_easy_integration_report_generation( 'toptopics' ); echo ''; esc_attr_e( 'Unanswered topics are questions that your chatbot did not know how to answer.', 'ai-chatbot-easy-integration' ); /*echo ' '; esc_attr_e( 'LEARN HOW TO TRAIN YOUR CHATBOT', 'ai-chatbot-easy-integration' ); echo '';*/ echo '
'; ai_chatbot_easy_integration_report_generation( 'unansweredtopics' ); } /** * Generate report * * @param mixed $report the type of report to generate. * @return void */ function ai_chatbot_easy_integration_report_generation( $report ) { global $wpdb; if ( 'toptopics' === $report ) { $results = $wpdb->get_results( 'SELECT topic, count(topic) as number FROM ' . $wpdb->prefix . 'ai_chatbot_easy_integration_logs group by topic order by count(topic) desc limit 1', ARRAY_A ); if ( count( $results ) > 0 ) { echo '';
foreach ( $results as $row ) {
echo esc_attr( $row['topic'] ) . ' (' . esc_attr( $row['number'] ) . ')
';
}
echo '
';
foreach ( $results as $row ) {
$messages = unserialize( $row['messages'] );
if ( is_array( $messages ) ) {
foreach ( $messages as $key => $value ) {
if ( strstr( $value, 'know how to answer that?' ) ) {
$messageparts = explode( ':|:', $messages[ $key - 1 ] );
echo esc_attr( $messageparts[0] ) . '
';
}
}
}
}
echo '
';
echo ''; $url = wp_nonce_url( '/wp-admin/admin.php?page=ai-chatbot-easy-integration-log&purgelog=1' ); echo ''; esc_attr_e( 'CLEAR LOG', 'ai-chatbot-easy-integration' ); echo ''; echo ''; esc_attr_e( 'SHOW CHAT WINDOW', 'ai-chatbot-easy-integration' ); echo ''; echo ''; esc_attr_e( 'REPORTS', 'ai-chatbot-easy-integration' ); echo ''; echo '
'; echo ''; echo esc_attr( $ai_chatbot_easy_integration_notice ); echo '
'; } echo ''; if ( '' !== $row['notes'] ) { echo ''; echo esc_attr( __( 'VIEW CHAT NOTES', 'ai-chatbot-easy-integration' ) ); echo ' '; } if ( '' !== $row['messages'] ) { echo ' '; echo esc_attr( __( 'VIEW CHAT TRANSCRIPT', 'ai-chatbot-easy-integration' ) ); echo ''; } echo '
'; if ( '' !== $row['notes'] ) { echo ''; } if ( '' !== $row['messages'] ) { echo ''; } if ( '' === $keyword ) { echo ''; $log .= esc_attr( __( 'The following chat messages were received by Watson Assistant over the past ', 'ai-chatbot-easy-integration' ) ); $log .= esc_attr( $frequency ); $log .= esc_attr( __( ' hours:', 'ai-chatbot-easy-integration' ) ); $log .= '
'; foreach ( $results as $row ) { $log .= ''; $log .= esc_attr( __( 'DATE: ', 'ai-chatbot-easy-integration' ) ); $log .= esc_attr( $row['entrydate'] ); $log .= '
'; $log .= ''; $log .= esc_attr( __( 'Topic: ', 'ai-chatbot-easy-integration' ) ); $log .= esc_attr( $row['topic'] ); $log .= '
'; $log .= '